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

            Line data    Source code
       1              : /* valaswitchlabel.vala
       2              :  *
       3              :  * Copyright (C) 2006-2010  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              : using GLib;
      24              : 
      25              : /**
      26              :  * Represents a switch label in the source code.
      27              :  */
      28          701 : public class Vala.SwitchLabel : CodeNode {
      29              :         /**
      30              :          * Specifies the label expression.
      31              :          */
      32              :         public Expression expression {
      33        15303 :                 get { return _expression; }
      34          595 :                 private set {
      35         2035 :                         _expression = value;
      36          595 :                         _expression.parent_node = this;
      37              :                 }
      38              :         }
      39              : 
      40              :         public weak SwitchSection section {
      41         1245 :                 get { return (SwitchSection) parent_node; }
      42              :         }
      43              : 
      44          669 :         private Expression _expression;
      45              : 
      46              :         /**
      47              :          * Creates a new switch case label.
      48              :          *
      49              :          * @param expr   label expression
      50              :          * @param source reference to source code
      51              :          * @return       newly created switch case label
      52              :          */
      53         1785 :         public SwitchLabel (Expression expr, SourceReference? source = null) {
      54          595 :                 expression = expr;
      55          595 :                 source_reference = source;
      56              :         }
      57              : 
      58              :         /**
      59              :          * Creates a new switch default label.
      60              :          *
      61              :          * @param source reference to source code
      62              :          * @return       newly created switch default label
      63              :          */
      64          148 :         public SwitchLabel.with_default (SourceReference? source = null) {
      65           74 :                 source_reference = source;
      66              :         }
      67              : 
      68          672 :         public override void accept (CodeVisitor visitor) {
      69          672 :                 visitor.visit_switch_label (this);
      70              :         }
      71              : 
      72          672 :         public override void accept_children (CodeVisitor visitor) {
      73          672 :                 if (expression != null) {
      74          596 :                         expression.accept (visitor);
      75              : 
      76          596 :                         visitor.visit_end_full_expression (expression);
      77              :                 }
      78              :         }
      79              : 
      80          663 :         public override bool check (CodeContext context) {
      81          663 :                 if (checked) {
      82            0 :                         return !error;
      83              :                 }
      84              : 
      85          663 :                 checked = true;
      86              : 
      87         1252 :                 if (expression != null) {
      88          591 :                         var switch_statement = (SwitchStatement) section.parent_node;
      89              : 
      90              :                         // enum-type inference
      91          591 :                         var condition_target_type = switch_statement.expression.target_type;
      92          849 :                         if (expression.symbol_reference == null && condition_target_type != null && condition_target_type.type_symbol is Enum) {
      93          516 :                                 var enum_type = (Enum) condition_target_type.type_symbol;
      94         7104 :                                 foreach (var val in enum_type.get_values ()) {
      95         3431 :                                         if (expression.to_string () == val.name) {
      96            8 :                                                 expression.target_type = condition_target_type.copy ();
      97            8 :                                                 expression.symbol_reference = val;
      98            8 :                                                 break;
      99              :                                         }
     100              :                                 }
     101              :                         }
     102              : 
     103          591 :                         if (!expression.check (context)) {
     104            1 :                                 error = true;
     105            1 :                                 return false;
     106              :                         }
     107              : 
     108          590 :                         if (!expression.is_constant ()) {
     109            0 :                                 error = true;
     110            0 :                                 Report.error (expression.source_reference, "Expression must be constant");
     111            0 :                                 return false;
     112              :                         }
     113          590 :                         if (!expression.value_type.compatible (switch_statement.expression.value_type)) {
     114            1 :                                 error = true;
     115            1 :                                 Report.error (expression.source_reference, "Cannot convert from `%s' to `%s'", expression.value_type.to_string (), switch_statement.expression.value_type.to_string ());
     116            1 :                                 return false;
     117              :                         }
     118              :                 }
     119              : 
     120          663 :                 return true;
     121              :         }
     122              : 
     123          654 :         public override void emit (CodeGenerator codegen) {
     124          654 :                 codegen.visit_switch_label (this);
     125              :         }
     126              : }
        

Generated by: LCOV version 2.0-1