valaarraycreationexpression.vala 6.54 KB
Newer Older
1 2
/* valaarraycreationexpression.vala
 *
3
 * Copyright (C) 2006-2010  Jürg Billeter
4
 * Copyright (C) 2006-2008  Raffaele Sandrini
5 6 7 8
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10 11 12 13 14 15 16 17 18 19 20

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
21
 * 	Raffaele Sandrini <raffaele@sandrini.ch>
22
 * 	Jürg Billeter <j@bitron.ch>
23 24 25 26 27
 */

using GLib;

/**
Didier 'Ptitjes's avatar
Didier 'Ptitjes committed
28
 * Represents an array creation expression e.g. {{{ new int[] {1,2,3} }}}.
29 30 31 32 33
 */
public class Vala.ArrayCreationExpression : Expression {
	/**
	 * The type of the elements of the array.
	 */
34 35 36 37 38 39 40
	public DataType element_type {
		get { return _element_type; }
		set {
			_element_type = value;
			_element_type.parent_node = this;
		}
	}
41 42 43 44
	
	/**
	 * The rank of the array.
	 */
45
	public int rank { get; set; }
46 47 48 49
	
	/**
	 * The size for each dimension ascending from left to right.
	 */
50
	private List<Expression> sizes = new ArrayList<Expression> ();
51 52 53 54
	
	/**
	 * The root array initializer list.
	 */
55 56 57 58 59 60 61 62 63
	public InitializerList? initializer_list {
		get { return _initializer_list; }
		set {
			_initializer_list = value;
			if (_initializer_list != null) {
				_initializer_list.parent_node = this;
			}
		}
	}
64 65

	private DataType _element_type;
66
	private InitializerList? _initializer_list;
67

68 69 70
	/**
	 * Add a size expression.
	 */
71
	public void append_size (Expression size) {
72
		sizes.add (size);
73 74 75
		if (size != null) {
			size.parent_node = this;
		}
76 77 78 79 80
	}
	
	/**
	 * Get the sizes for all dimensions ascending from left to right.
	 */
81
	public List<Expression> get_sizes () {
82
		return sizes;
83 84
	}
	
85 86
	public ArrayCreationExpression (DataType element_type, int rank, InitializerList? initializer_list, SourceReference source_reference) {
		this.element_type = element_type;
87 88 89
		this.rank = rank;
		this.initializer_list = initializer_list;
		this.source_reference = source_reference;
90
	}
91

92
	public override void accept_children (CodeVisitor visitor) {
93 94 95
		if (element_type != null) {
			element_type.accept (visitor);
		}
96 97 98

		foreach (Expression e in sizes) {
			e.accept (visitor);
99
		}
100

101 102 103
		if (initializer_list != null) {
			initializer_list.accept (visitor);
		}
104 105
	}

106
	public override void accept (CodeVisitor visitor) {
107
		visitor.visit_array_creation_expression (this);
108 109

		visitor.visit_expression (this);
110
	}
111 112 113 114

	public override bool is_pure () {
		return false;
	}
115

116 117 118 119 120 121 122 123 124
	public override void replace_expression (Expression old_node, Expression new_node) {
		for (int i = 0; i < sizes.size; i++) {
			if (sizes[i] == old_node) {
				sizes[i] = new_node;
				return;
			}
		}
	}

125
	public override void replace_type (DataType old_type, DataType new_type) {
126 127 128 129
		if (element_type == old_type) {
			element_type = new_type;
		}
	}
130

131
	private int create_sizes_from_initializer_list (CodeContext context, InitializerList il, int rank, List<Literal> sl) {
132 133 134
		if (sl.size == (this.rank - rank)) {
			// only add size if this is the first initializer list of the current dimension
			var init = new IntegerLiteral (il.size.to_string (), il.source_reference);
135
			init.check (context);
136 137
			sl.add (init);
		}
138 139 140 141 142 143 144 145 146 147

		int subsize = -1;
		foreach (Expression e in il.get_initializers ()) {
			if (e is InitializerList) {
				if (rank == 1) {
					il.error = true;
					e.error = true;
					Report.error (e.source_reference, "Expected array element, got array initializer list");
					return -1;
				}
148
				int size = create_sizes_from_initializer_list (context, (InitializerList) e, rank - 1, sl);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
				if (size == -1) {
					return -1;
				}
				if (subsize >= 0 && subsize != size) {
					il.error = true;
					Report.error (il.source_reference, "Expected initializer list of size %d, got size %d".printf (subsize, size));
					return -1;
				} else {
					subsize = size;
				}
			} else {
				if (rank != 1) {
					il.error = true;
					e.error = true;
					Report.error (e.source_reference, "Expected array initializer list, got array element");
					return -1;
				}
			}
		}
		return il.size;
	}

171
	public override bool check (CodeContext context) {
172 173 174 175 176 177
		if (checked) {
			return !error;
		}

		checked = true;

178
		List<Expression> sizes = get_sizes ();
179 180 181
		var initlist = initializer_list;

		if (element_type != null) {
182
			element_type.check (context);
183 184
		}

185
		foreach (Expression e in sizes) {
186
			e.check (context);
187 188 189 190 191 192
		}

		var calc_sizes = new ArrayList<Literal> ();
		if (initlist != null) {
			initlist.target_type = new ArrayType (element_type, rank, source_reference);

193
			initlist.check (context);
194

195
			var ret = create_sizes_from_initializer_list (context, initlist, rank, calc_sizes);
196 197 198 199 200
			if (ret == -1) {
				error = true;
			}
		}

201
		if (sizes.size > 0) {
202
			/* check for errors in the size list */
203
			foreach (Expression e in sizes) {
204 205 206
				if (e.value_type == null) {
					/* return on previous error */
					return false;
207
				} else if (!(e.value_type is IntegerType || e.value_type is EnumValueType)) {
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
					error = true;
					Report.error (e.source_reference, "Expression of integer type expected");
				}
			}
		} else {
			if (initlist == null) {
				error = true;
				/* this is an internal error because it is already handeld by the parser */
				Report.error (source_reference, "internal error: initializer list expected");
			} else {
				foreach (Expression size in calc_sizes) {
					append_size (size);
				}
			}
		}

		if (error) {
			return false;
		}

		/* check for wrong elements inside the initializer */
		if (initializer_list != null && initializer_list.value_type == null) {
			return false;
		}

		/* try to construct the type of the array */
		if (element_type == null) {
			error = true;
			Report.error (source_reference, "Cannot determine the element type of the created array");
			return false;
		}

		element_type.value_owned = true;

		value_type = new ArrayType (element_type, rank, source_reference);
		value_type.value_owned = true;

		return !error;
	}
247 248 249 250 251 252 253 254 255 256 257 258 259 260

	public override void emit (CodeGenerator codegen) {
		foreach (Expression e in sizes) {
			e.emit (codegen);
		}

		if (initializer_list != null) {
			initializer_list.emit (codegen);
		}

		codegen.visit_array_creation_expression (this);

		codegen.visit_expression (this);
	}
261
}