]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Support (!) non-null casts
authorJürg Billeter <j@bitron.ch>
Wed, 21 Oct 2009 19:40:36 +0000 (21:40 +0200)
committerJürg Billeter <j@bitron.ch>
Wed, 21 Oct 2009 19:42:05 +0000 (21:42 +0200)
codegen/valaccodebasemodule.vala
vala/valacastexpression.vala
vala/valacodewriter.vala
vala/valaparser.vala

index 2b2edde7a6996a0812a60d721a012bb74e6b8c3d..9966bff0c5644bcbde0f10a8c310a7d99d06e49f 100644 (file)
@@ -3955,6 +3955,12 @@ internal class Vala.CCodeBaseModule : CCodeModule {
        }
 
        public override void visit_cast_expression (CastExpression expr) {
+               if (expr.is_non_null_cast) {
+                       // TODO add NULL runtime check
+                       expr.ccodenode = expr.inner.ccodenode;
+                       return;
+               }
+
                if (expr.inner.value_type != null && gvalue_type != null && expr.inner.value_type.data_type == gvalue_type
                    && expr.type_reference.get_type_id () != null) {
                        // explicit conversion from GValue
index f8e76c5f39d464b95a7a28ca117fa6323ad8c2d4..c0415b7f4ba7f560cc4fa02f61fc6c90b288aa3d 100644 (file)
@@ -54,6 +54,8 @@ public class Vala.CastExpression : Expression {
         */
        public bool is_silent_cast { get; set; }
 
+       public bool is_non_null_cast { get; set; }
+
        private Expression _inner;
 
        private DataType _data_type;
@@ -71,10 +73,18 @@ public class Vala.CastExpression : Expression {
                this.is_silent_cast = is_silent_cast;
                this.inner = inner;
        }
-       
+
+       public CastExpression.non_null (Expression inner, SourceReference source_reference) {
+               this.inner = inner;
+               this.is_non_null_cast = true;
+               this.source_reference = source_reference;
+       }
+
        public override void accept (CodeVisitor visitor) {
                inner.accept (visitor);
-               type_reference.accept (visitor);
+               if (!is_non_null_cast) {
+                       type_reference.accept (visitor);
+               }
 
                visitor.visit_cast_expression (this);
 
@@ -109,6 +119,16 @@ public class Vala.CastExpression : Expression {
                        return false;
                }
 
+               if (is_non_null_cast) {
+                       // (!) non-null cast
+                       value_type = inner.value_type.copy ();
+                       value_type.nullable = false;
+
+                       inner.target_type = inner.value_type.copy ();
+
+                       return !error;
+               }
+
                type_reference.check (analyzer);
 
                // FIXME: check whether cast is allowed
index 22f59193522d677e4b2a927b646bda3f5f5ebab3..1f28ea7410c0c6d9c27e05ce7167253a95fb4c9e 100644 (file)
@@ -1446,6 +1446,12 @@ public class Vala.CodeWriter : CodeVisitor {
        }
 
        public override void visit_cast_expression (CastExpression expr) {
+               if (expr.is_non_null_cast) {
+                       write_string ("(!) ");
+                       expr.inner.accept (this);
+                       return;
+               }
+
                if (!expr.is_silent_cast) {
                        write_string ("(");
                        write_type (expr.type_reference);
index 43687f7dc3b72a53634ce65259b1b920d5702721..cb544b2bb6cfa3d49299ad4e51353351c5e44e33 100644 (file)
@@ -944,6 +944,14 @@ public class Vala.Parser : CodeVisitor {
                                        }
                                }
                                break;
+                       case TokenType.OP_NEG:
+                               next ();
+                               if (accept (TokenType.CLOSE_PARENS)) {
+                                       // (!) non-null cast
+                                       var inner = parse_unary_expression ();
+                                       return new CastExpression.non_null (inner, get_src (begin));
+                               }
+                               break;
                        default:
                                break;
                        }