1/* valaexpression.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 23using GLib; 24 25/** 26 * Base class for all code nodes that might be used as an expression. 27 */ 28public abstract class Vala.Expression : CodeNode { 29 /** 30 * The static type of the value of this expression. 31 * 32 * The semantic analyzer computes this value. 33 */ 34 public DataType value_type { get; set; } 35 36 public DataType? formal_value_type { get; set; } 37 38 /* 39 * The static type this expression is expected to have. 40 * 41 * The semantic analyzer computes this value, lambda expressions use it. 42 */ 43 public DataType target_type { get; set; } 44 45 public DataType? formal_target_type { get; set; } 46 47 /** 48 * The symbol this expression refers to. 49 */ 50 public weak Symbol symbol_reference { get; set; } 51 52 /** 53 * Specifies that this expression is used as lvalue, i.e. the 54 * left hand side of an assignment. 55 */ 56 public bool lvalue { get; set; } 57 58 public TargetValue? target_value { get; set; } 59 60 /** 61 * Returns whether this expression is constant, i.e. whether this 62 * expression only consists of literals and other constants. 63 */ 64 public virtual bool is_constant () { 65 return false; 66 } 67 68 /** 69 * Returns whether this expression is pure, i.e. whether this expression 70 * is free of side-effects. 71 */ 72 public abstract bool is_pure (); 73 74 /** 75 * Returns whether this expression is guaranteed to be non-null. 76 */ 77 public virtual bool is_non_null () { 78 return false; 79 } 80 81 /** 82 * Check whether symbol_references in this expression are at least 83 * as accessible as the specified symbol. 84 */ 85 public virtual bool is_accessible (Symbol sym) { 86 return true; 87 } 88 89 public Statement? parent_statement { 90 get { 91 unowned Expression? expr = parent_node as Expression; 92 unowned Statement? stmt = parent_node as Statement; 93 unowned LocalVariable? local = parent_node as LocalVariable; 94 unowned MemberInitializer? initializer = parent_node as MemberInitializer; 95 if (stmt != null) { 96 return (Statement) parent_node; 97 } else if (expr != null) { 98 return expr.parent_statement; 99 } else if (local != null) { 100 return (Statement) local.parent_node; 101 } else if (initializer != null) { 102 return ((Expression)initializer.parent_node).parent_statement; 103 } else { 104 return null; 105 } 106 } 107 } 108 109 public void insert_statement (Block block, Statement stmt) { 110 block.insert_before (parent_statement, stmt); 111 } 112} 113