girparser: Better support for arrays in return type.
[vala-lang.git] / vala / valamapliteral.vala
blobe9eca0b5db9b0ecba0d2395d309ec74913d8b7ea
1 /* valamapliteral.vala
3 * Copyright (C) 2009-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>
23 public class Vala.MapLiteral : Literal {
24 private List<Expression> keys = new ArrayList<Expression> ();
25 private List<Expression> values = new ArrayList<Expression> ();
27 public DataType map_key_type { get; private set; }
28 public DataType map_value_type { get; private set; }
30 public MapLiteral (SourceReference? source_reference = null) {
31 this.source_reference = source_reference;
34 public override void accept_children (CodeVisitor visitor) {
35 for (int i = 0; i < keys.size; i++) {
36 keys[i].accept (visitor);
37 values[i].accept (visitor);
41 public override void accept (CodeVisitor visitor) {
42 visitor.visit_map_literal (this);
44 visitor.visit_expression (this);
47 public void add_key (Expression expr) {
48 keys.add (expr);
49 expr.parent_node = this;
52 public void add_value (Expression expr) {
53 values.add (expr);
54 expr.parent_node = this;
57 public List<Expression> get_keys () {
58 return keys;
61 public List<Expression> get_values () {
62 return values;
65 public override bool is_pure () {
66 return false;
69 public override void replace_expression (Expression old_node, Expression new_node) {
70 for (int i = 0; i < keys.size; i++) {
71 if (keys[i] == old_node) {
72 keys[i] = new_node;
74 if (values[i] == old_node) {
75 values[i] = new_node;
80 public override bool check (CodeContext context) {
81 if (checked) {
82 return !error;
85 checked = true;
87 var map_type = new ObjectType ((Class) context.root.scope.lookup ("Dova").scope.lookup ("Map"));
88 map_type.value_owned = true;
90 bool fixed_element_type = false;
91 if (target_type != null && target_type.data_type == map_type.data_type && target_type.get_type_arguments ().size == 2) {
92 map_key_type = target_type.get_type_arguments ().get (0).copy ();
93 map_key_type.value_owned = false;
94 map_value_type = target_type.get_type_arguments ().get (1).copy ();
95 map_value_type.value_owned = false;
96 fixed_element_type = true;
99 for (int i = 0; i < keys.size; i++) {
100 if (fixed_element_type) {
101 keys[i].target_type = map_key_type;
102 values[i].target_type = map_value_type;
104 if (!keys[i].check (context)) {
105 return false;
107 if (!values[i].check (context)) {
108 return false;
110 if (map_key_type == null) {
111 map_key_type = keys[i].value_type.copy ();
112 map_key_type.value_owned = false;
113 map_value_type = values[i].value_type.copy ();
114 map_value_type.value_owned = false;
118 if (map_key_type == null) {
119 error = true;
120 Report.error (source_reference, "cannot infer key type for map literal");
121 return false;
124 if (map_value_type == null) {
125 error = true;
126 Report.error (source_reference, "cannot infer value type for map literal");
127 return false;
130 map_key_type = map_key_type.copy ();
131 map_key_type.value_owned = true;
132 map_value_type = map_value_type.copy ();
133 map_value_type.value_owned = true;
134 map_type.add_type_argument (map_key_type);
135 map_type.add_type_argument (map_value_type);
136 value_type = map_type;
138 return !error;
141 public override void emit (CodeGenerator codegen) {
142 for (int i = 0; i < keys.size; i++) {
143 keys[i].emit (codegen);
144 values[i].emit (codegen);
147 codegen.visit_map_literal (this);
149 codegen.visit_expression (this);