1 /* valalistliteral.vala
3 * Copyright (C) 2009 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
20 * Jürg Billeter <j@bitron.ch>
23 public class Vala
.ListLiteral
: Literal
{
24 private List
<Expression
> expression_list
= new ArrayList
<Expression
> ();
26 public DataType element_type
{ get; private set; }
28 public ListLiteral (SourceReference? source_reference
= null) {
29 this
.source_reference
= source_reference
;
32 public override void accept_children (CodeVisitor visitor
) {
33 foreach (Expression expr
in expression_list
) {
34 expr
.accept (visitor
);
38 public override void accept (CodeVisitor visitor
) {
39 visitor
.visit_list_literal (this
);
42 public void add_expression (Expression expr
) {
43 expression_list
.add (expr
);
44 expr
.parent_node
= this
;
47 public List
<Expression
> get_expressions () {
48 return expression_list
;
51 public override bool is_pure () {
55 public override void replace_expression (Expression old_node
, Expression new_node
) {
56 for (int i
= 0; i
< expression_list
.size
; i
++) {
57 if (expression_list
[i
] == old_node
) {
58 expression_list
[i
] = new_node
;
63 public override bool check (SemanticAnalyzer analyzer
) {
70 // list literals are also allowed for constant arrays,
71 // however, they are currently handled by InitializerList
72 // therefore transform this expression if necessary
73 var array_type
= target_type as ArrayType
;
74 if (array_type
!= null && array_type
.inline_allocated
) {
75 var initializer
= new
InitializerList (source_reference
);
76 initializer
.target_type
= target_type
;
77 foreach (var expr
in expression_list
) {
78 initializer
.append (expr
);
81 analyzer
.replaced_nodes
.add (this
);
82 parent_node
.replace_expression (this
, initializer
);
83 return initializer
.check (analyzer
);
86 var list_type
= new
ObjectType ((Class
) analyzer
.context
.root
.scope
.lookup ("Dova").scope
.lookup ("List"));
87 list_type
.value_owned
= true;
89 bool fixed_element_type
= false;
90 if (target_type
!= null && target_type
.data_type
== list_type
.data_type
&& target_type
.get_type_arguments ().size
== 1) {
91 element_type
= target_type
.get_type_arguments ().get (0).copy ();
92 fixed_element_type
= true;
95 foreach (var expr
in expression_list
) {
96 if (fixed_element_type
) {
97 expr
.target_type
= element_type
;
99 if (!expr
.check (analyzer
)) {
102 if (element_type
== null) {
103 element_type
= expr
.value_type
.copy ();
107 element_type
.value_owned
= true;
108 list_type
.add_type_argument (element_type
);
109 value_type
= list_type
;