LCOV - code coverage report
Current view: top level - vala - valacastexpression.vala (source / functions) Coverage Total Hit
Test: vala 0.57.0.298-a8cae1 Lines: 90.1 % 131 118
Test Date: 2024-04-25 11:34:36 Functions: - 0 0

            Line data    Source code
       1              : /* valacastexpression.vala
       2              :  *
       3              :  * Copyright (C) 2006-2011  Jürg Billeter
       4              :  *
       5              :  * This library is free software; you can redistribute it and/or
       6              :  * modify it under the terms of the GNU Lesser General Public
       7              :  * License as published by the Free Software Foundation; either
       8              :  * version 2.1 of the License, or (at your option) any later version.
       9              : 
      10              :  * This library is distributed in the hope that it will be useful,
      11              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13              :  * Lesser General Public License for more details.
      14              : 
      15              :  * You should have received a copy of the GNU Lesser General Public
      16              :  * License along with this library; if not, write to the Free Software
      17              :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
      18              :  *
      19              :  * Author:
      20              :  *      Jürg Billeter <j@bitron.ch>
      21              :  */
      22              : 
      23              : 
      24              : /**
      25              :  * Represents a type cast in the source code.
      26              :  */
      27       192054 : public class Vala.CastExpression : Expression {
      28              :         /**
      29              :          * The expression to be cast.
      30              :          */
      31              :         public Expression inner {
      32      3199609 :                 get {
      33      3199609 :                         return _inner;
      34              :                 }
      35       190528 :                 private set {
      36       442246 :                         _inner = value;
      37       190528 :                         _inner.parent_node = this;
      38              :                 }
      39              :         }
      40              : 
      41              :         /**
      42              :          * The target type.
      43              :          */
      44              :         public DataType type_reference {
      45      2003341 :                 get { return _data_type; }
      46       251718 :                 private set {
      47       503436 :                         _data_type = value;
      48       251718 :                         _data_type.parent_node = this;
      49              :                 }
      50              :         }
      51              : 
      52              :         /**
      53              :          * Checked casts return NULL instead of raising an error.
      54              :          */
      55       179214 :         public bool is_silent_cast { get; private set; }
      56              : 
      57       196278 :         public bool is_non_null_cast { get; private set; }
      58              : 
      59       190517 :         private Expression _inner;
      60              : 
      61       190517 :         private DataType _data_type;
      62              : 
      63              :         /**
      64              :          * Creates a new cast expression.
      65              :          *
      66              :          * @param inner           expression to be cast
      67              :          * @param type_reference  target type
      68              :          * @return                newly created cast expression
      69              :          */
      70       427878 :         public CastExpression (Expression inner, DataType type_reference, SourceReference? source_reference = null) {
      71       142626 :                 this.type_reference = type_reference;
      72       142626 :                 this.source_reference = source_reference;
      73       142626 :                 this.is_silent_cast = false;
      74       142626 :                 this.is_non_null_cast = false;
      75       142626 :                 this.inner = inner;
      76              :         }
      77              : 
      78         1410 :         public CastExpression.silent (Expression inner, DataType type_reference, SourceReference? source_reference = null) {
      79          470 :                 this.type_reference = type_reference;
      80          470 :                 this.source_reference = source_reference;
      81          470 :                 this.is_silent_cast = true;
      82          470 :                 this.is_non_null_cast = false;
      83          470 :                 this.inner = inner;
      84              :         }
      85              : 
      86       142263 :         public CastExpression.non_null (Expression inner, SourceReference? source_reference = null) {
      87        47421 :                 this.inner = inner;
      88        47421 :                 this.is_non_null_cast = true;
      89        47421 :                 this.source_reference = source_reference;
      90              :         }
      91              : 
      92       736210 :         public override void accept (CodeVisitor visitor) {
      93       736210 :                 visitor.visit_cast_expression (this);
      94              : 
      95       736210 :                 visitor.visit_expression (this);
      96              :         }
      97              : 
      98       736210 :         public override void accept_children (CodeVisitor visitor) {
      99       736210 :                 inner.accept (visitor);
     100       736210 :                 if (!is_non_null_cast) {
     101       546790 :                         type_reference.accept (visitor);
     102              :                 }
     103              :         }
     104              : 
     105            0 :         public override string to_string () {
     106            0 :                 if (is_non_null_cast) {
     107            0 :                         return "(!) %s".printf (inner.to_string ());
     108            0 :                 } else if (is_silent_cast) {
     109            0 :                         return "%s as %s".printf (inner.to_string (), type_reference.to_string ());
     110              :                 } else {
     111            0 :                         return "(%s) %s".printf (type_reference.to_string (), inner.to_string ());
     112              :                 }
     113              :         }
     114              : 
     115           11 :         public override void replace_expression (Expression old_node, Expression new_node) {
     116           11 :                 if (inner == old_node) {
     117           11 :                         inner = new_node;
     118              :                 }
     119              :         }
     120              : 
     121            0 :         public override bool is_pure () {
     122            0 :                 return inner.is_pure ();
     123              :         }
     124              : 
     125            6 :         public override bool is_accessible (Symbol sym) {
     126            6 :                 return inner.is_accessible (sym);
     127              :         }
     128              : 
     129        64735 :         public override void replace_type (DataType old_type, DataType new_type) {
     130        64735 :                 if (type_reference == old_type) {
     131        64735 :                         type_reference = new_type;
     132              :                 }
     133              :         }
     134              : 
     135         3922 :         public override bool is_non_null () {
     136         3922 :                 return is_non_null_cast || (!is_silent_cast && inner.is_non_null ());
     137              :         }
     138              : 
     139       401324 :         public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
     140       401324 :                 inner.get_error_types (collection, source_reference);
     141              :         }
     142              : 
     143       227758 :         public override bool check (CodeContext context) {
     144       227758 :                 if (checked) {
     145        51264 :                         return !error;
     146              :                 }
     147              : 
     148       176494 :                 checked = true;
     149              : 
     150       176494 :                 if (!inner.check (context)) {
     151            0 :                         error = true;
     152            0 :                         return false;
     153              :                 }
     154              : 
     155       176494 :                 if (inner.value_type == null) {
     156            0 :                         Report.error (source_reference, "Invalid cast expression");
     157            0 :                         error = true;
     158            0 :                         return false;
     159              :                 }
     160              : 
     161       176494 :                 if (is_non_null_cast) {
     162              :                         // (!) non-null cast
     163        43887 :                         type_reference = inner.value_type.copy ();
     164        43887 :                         type_reference.nullable = false;
     165              :                 }
     166              : 
     167       176494 :                 type_reference.check (context);
     168              : 
     169              :                 // FIXME: check whether cast is allowed
     170              : 
     171       176494 :                 if (type_reference is VoidType) {
     172            1 :                         Report.error (source_reference, "Casting to `void' is not allowed");
     173            1 :                         error = true;
     174            1 :                         return false;
     175              :                 }
     176              : 
     177              :                 // Allow casting to array or pointer type
     178       176493 :                 if (!(type_reference is ArrayType || type_reference is PointerType)) {
     179       106982 :                         if (!type_reference.is_real_struct_type () && inner.value_type.is_real_struct_type ()
     180           55 :                             && (context.profile != Profile.GOBJECT || !(is_gvariant (context, inner.value_type) || is_gvalue (context, inner.value_type)))) {
     181            2 :                                 error = true;
     182            2 :                                 Report.error (source_reference, "Casting of struct `%s' to `%s' is not allowed", inner.value_type.to_qualified_string (), type_reference.to_qualified_string ());
     183              :                         }
     184              :                 }
     185              : 
     186       176493 :                 if (type_reference is DelegateType && inner.value_type is MethodType) {
     187           78 :                         if (target_type != null) {
     188           44 :                                 inner.value_type.value_owned = target_type.value_owned;
     189              :                         } else {
     190           34 :                                 inner.value_type.value_owned = true;
     191              :                         }
     192              :                 }
     193              : 
     194              :                 // Implicit transformation of stack-allocated value to heap-allocated boxed-type
     195       176532 :                 if (!(is_silent_cast || is_non_null_cast)
     196       132136 :                     && (type_reference is ValueType && type_reference.nullable)
     197           39 :                     && inner.value_type.is_non_null_simple_type ()) {
     198           22 :                         var local = new LocalVariable (type_reference, get_temp_name (), null, inner.source_reference);
     199           22 :                         var decl = new DeclarationStatement (local, source_reference);
     200              : 
     201           22 :                         insert_statement (context.analyzer.insert_block, decl);
     202              : 
     203           22 :                         var temp_access = SemanticAnalyzer.create_temp_access (local, target_type);
     204           22 :                         temp_access.formal_target_type = formal_target_type;
     205              : 
     206              :                         // don't set initializer earlier as this changes parent_node and parent_statement
     207           22 :                         local.initializer = inner;
     208           22 :                         decl.check (context);
     209              : 
     210           22 :                         context.analyzer.replaced_nodes.add (this);
     211           22 :                         parent_node.replace_expression (this, temp_access);
     212           22 :                         return temp_access.check (context);
     213              :                 }
     214              : 
     215       176471 :                 value_type = type_reference;
     216       176471 :                 value_type.value_owned = inner.value_type.value_owned;
     217       176471 :                 value_type.floating_reference = inner.value_type.floating_reference;
     218              : 
     219       176471 :                 if (is_silent_cast) {
     220          470 :                         value_type.nullable = true;
     221              :                 }
     222              : 
     223       352914 :                 if (context.profile == Profile.GOBJECT
     224       176512 :                     && is_gvariant (context, inner.value_type) && !is_gvariant (context, value_type)) {
     225              :                         // GVariant unboxing returns owned value
     226           68 :                         value_type.value_owned = true;
     227           68 :                         if (value_type.get_type_signature () == null) {
     228            1 :                                 error = true;
     229            1 :                                 Report.error (source_reference, "Casting of `GLib.Variant' to `%s' is not supported", value_type.to_qualified_string ());
     230              :                         }
     231              :                 }
     232              : 
     233       352914 :                 if (context.profile == Profile.GOBJECT
     234       176499 :                     && is_gvalue (context, inner.value_type) && !is_gvalue (context, value_type)) {
     235              :                         // GValue unboxing returns unowned value
     236           56 :                         value_type.value_owned = false;
     237           56 :                         if (value_type.nullable && value_type.type_symbol != null && !value_type.type_symbol.is_reference_type ()) {
     238            1 :                                 error = true;
     239            1 :                                 Report.error (source_reference, "Casting of `GLib.Value' to `%s' is not supported", value_type.to_qualified_string ());
     240              :                         }
     241              :                 }
     242              : 
     243       176471 :                 inner.target_type = inner.value_type.copy ();
     244              : 
     245       176471 :                 return !error;
     246              :         }
     247              : 
     248       176567 :         bool is_gvariant (CodeContext context, DataType type) {
     249       176567 :                 return type.type_symbol != null && type.type_symbol.is_subtype_of (context.analyzer.gvariant_type.type_symbol);
     250              :         }
     251              : 
     252       176554 :         bool is_gvalue (CodeContext context, DataType type) {
     253       176554 :                 return type.type_symbol != null && type.type_symbol.is_subtype_of (context.analyzer.gvalue_type.type_symbol);
     254              :         }
     255              : 
     256         2918 :         public override void emit (CodeGenerator codegen) {
     257         2918 :                 inner.emit (codegen);
     258              : 
     259         2918 :                 codegen.visit_cast_expression (this);
     260              : 
     261         2918 :                 codegen.visit_expression (this);
     262              :         }
     263              : 
     264       362373 :         public override void get_defined_variables (Collection<Variable> collection) {
     265       362373 :                 inner.get_defined_variables (collection);
     266              :         }
     267              : 
     268       120807 :         public override void get_used_variables (Collection<Variable> collection) {
     269       120807 :                 inner.get_used_variables (collection);
     270              :         }
     271              : 
     272            6 :         public override bool is_constant () {
     273            6 :                 return inner.is_constant ();
     274              :         }
     275              : }
        

Generated by: LCOV version 2.0-1