Bump version suffix
[vala-lang.git] / vala / valareturnstatement.vala
blob39aff8259570f419fa5cc82ed6ccee2181c2600f
1 /* valareturnstatement.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
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.
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.
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
24 /**
25 * Represents a return statement in the source code.
27 public class Vala.ReturnStatement : CodeNode, Statement {
28 /**
29 * The optional expression to return.
31 public Expression? return_expression {
32 get { return _return_expression; }
33 set {
34 _return_expression = value;
35 if (_return_expression != null) {
36 _return_expression.parent_node = this;
41 private Expression _return_expression;
43 /**
44 * Creates a new return statement.
46 * @param return_expression the return expression
47 * @param source_reference reference to source code
48 * @return newly created return statement
50 public ReturnStatement (Expression? return_expression = null, SourceReference? source_reference = null) {
51 this.source_reference = source_reference;
52 this.return_expression = return_expression;
55 public override void accept (CodeVisitor visitor) {
56 visitor.visit_return_statement (this);
59 public override void accept_children (CodeVisitor visitor) {
60 if (return_expression != null) {
61 return_expression.accept (visitor);
63 visitor.visit_end_full_expression (return_expression);
67 public override void replace_expression (Expression old_node, Expression new_node) {
68 if (return_expression == old_node) {
69 return_expression = new_node;
73 public override bool check (CodeContext context) {
74 if (checked) {
75 return !error;
78 checked = true;
80 if (return_expression != null) {
81 return_expression.target_type = context.analyzer.current_return_type;
84 if (return_expression != null && !return_expression.check (context)) {
85 // ignore inner error
86 error = true;
87 return false;
90 if (context.analyzer.current_return_type == null) {
91 error = true;
92 Report.error (source_reference, "Return not allowed in this context");
93 return false;
96 if (context.profile == Profile.DOVA) {
97 // no return expressions in Dova profile
98 return !error;
101 if (return_expression == null) {
102 if (!(context.analyzer.current_return_type is VoidType)) {
103 error = true;
104 Report.error (source_reference, "Return without value in non-void function");
106 return !error;
109 if (context.analyzer.current_return_type is VoidType) {
110 Report.error (source_reference, "Return with value in void function");
111 return false;
114 if (return_expression.value_type == null) {
115 error = true;
116 Report.error (source_reference, "Invalid expression in return value");
117 return false;
120 if (!return_expression.value_type.compatible (context.analyzer.current_return_type)) {
121 error = true;
122 Report.error (source_reference, "Return: Cannot convert from `%s' to `%s'".printf (return_expression.value_type.to_string (), context.analyzer.current_return_type.to_string ()));
123 return false;
126 if (return_expression.value_type.is_disposable () &&
127 !context.analyzer.current_return_type.value_owned) {
128 error = true;
129 Report.error (source_reference, "Return value transfers ownership but method return type hasn't been declared to transfer ownership");
130 return false;
133 var local = return_expression.symbol_reference as LocalVariable;
134 if (local != null && local.variable_type.is_disposable () &&
135 !context.analyzer.current_return_type.value_owned) {
136 error = true;
137 Report.error (source_reference, "Local variable with strong reference used as return value and method return type has not been declared to transfer ownership");
138 return false;
141 if (return_expression is NullLiteral
142 && !context.analyzer.current_return_type.nullable) {
143 Report.warning (source_reference, "`null' incompatible with return type `%s`".printf (context.analyzer.current_return_type.to_string ()));
146 add_error_types (return_expression.get_error_types ());
148 return !error;
151 public override void emit (CodeGenerator codegen) {
152 if (return_expression != null) {
153 return_expression.emit (codegen);
155 codegen.visit_end_full_expression (return_expression);
158 codegen.visit_return_statement (this);
161 public override void get_defined_variables (Collection<LocalVariable> collection) {
162 if (return_expression != null) {
163 return_expression.get_defined_variables (collection);
167 public override void get_used_variables (Collection<LocalVariable> collection) {
168 if (return_expression != null) {
169 return_expression.get_used_variables (collection);