Fix crash when using out parameters in delegates, fixes bug 563705
[vala-lang.git] / vala / valareferencetransferexpression.vala
blobdcf2e5369e7306f1aff9836de788bf72ca551573
1 /* valareferencetransferexpression.vala
3 * Copyright (C) 2007-2008 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>
23 using Gee;
25 /**
26 * Represents a reference transfer expression in the source code, e.g. `#foo'.
28 public class Vala.ReferenceTransferExpression : Expression {
29 /**
30 * The variable whose reference is to be transferred.
32 public Expression inner {
33 get {
34 return _inner;
36 set {
37 _inner = value;
38 _inner.parent_node = this;
42 private Expression _inner;
44 /**
45 * Creates a new reference transfer expression.
47 * @param inner variable whose reference is to be transferred
48 * @return newly created reference transfer expression
50 public ReferenceTransferExpression (Expression inner, SourceReference? source_reference = null) {
51 this.inner = inner;
52 this.source_reference = source_reference;
55 public override void accept (CodeVisitor visitor) {
56 visitor.visit_reference_transfer_expression (this);
58 visitor.visit_expression (this);
61 public override void accept_children (CodeVisitor visitor) {
62 inner.accept (visitor);
65 public override void replace_expression (Expression old_node, Expression new_node) {
66 if (inner == old_node) {
67 inner = new_node;
71 public override bool is_pure () {
72 return false;
75 public override bool check (SemanticAnalyzer analyzer) {
76 if (checked) {
77 return !error;
80 checked = true;
82 inner.lvalue = true;
84 inner.check (analyzer);
86 if (inner.error) {
87 /* if there was an error in the inner expression, skip type check */
88 error = true;
89 return false;
92 if (!(inner is MemberAccess || inner is ElementAccess)) {
93 error = true;
94 Report.error (source_reference, "Reference transfer not supported for this expression");
95 return false;
98 if (!inner.value_type.is_disposable ()
99 && !(inner.value_type is PointerType)) {
100 error = true;
101 Report.error (source_reference, "No reference to be transferred");
102 return false;
105 value_type = inner.value_type.copy ();
106 value_type.value_owned = true;
108 return !error;
111 public override void get_defined_variables (Collection<LocalVariable> collection) {
112 inner.get_defined_variables (collection);
115 public override void get_used_variables (Collection<LocalVariable> collection) {
116 inner.get_used_variables (collection);