valaarraycreationexpression.vala 6.08 KB
Newer Older
1 2
/* valaarraycreationexpression.vala
 *
3 4
 * Copyright (C) 2006-2009  Jürg Billeter
 * 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
 */

using GLib;
26
using Gee;
27 28 29 30 31 32 33 34

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

	private DataType _element_type;
67
	private InitializerList? _initializer_list;
68

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

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

		foreach (Expression e in sizes) {
			e.accept (visitor);
97
		}
98

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

104
	public override void accept (CodeVisitor visitor) {
105
		visitor.visit_array_creation_expression (this);
106 107

		visitor.visit_expression (this);
108
	}
109 110 111 112

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

114
	public override void replace_type (DataType old_type, DataType new_type) {
115 116 117 118
		if (element_type == old_type) {
			element_type = new_type;
		}
	}
119 120

	private int create_sizes_from_initializer_list (SemanticAnalyzer analyzer, InitializerList il, int rank, Gee.List<Literal> sl) {
121 122 123 124 125 126
		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);
			init.check (analyzer);
			sl.add (init);
		}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

		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;
				}
				int size = create_sizes_from_initializer_list (analyzer, (InitializerList) e, rank - 1, sl);
				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;
	}

	public override bool check (SemanticAnalyzer analyzer) {
		if (checked) {
			return !error;
		}

		checked = true;

167
		Gee.List<Expression> sizes = get_sizes ();
168 169 170
		var initlist = initializer_list;

		if (element_type != null) {
171
			element_type.check (analyzer);
172 173
		}

174
		foreach (Expression e in sizes) {
175
			e.check (analyzer);
176 177 178 179 180 181
		}

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

182
			initlist.check (analyzer);
183 184 185 186 187 188 189

			var ret = create_sizes_from_initializer_list (analyzer, initlist, rank, calc_sizes);
			if (ret == -1) {
				error = true;
			}
		}

190
		if (sizes.size > 0) {
191
			/* check for errors in the size list */
192
			foreach (Expression e in sizes) {
193 194 195
				if (e.value_type == null) {
					/* return on previous error */
					return false;
196
				} else if (!(e.value_type is IntegerType || e.value_type is EnumValueType)) {
197 198 199 200 201 202 203 204 205 206 207 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
					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;
	}
236
}