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
20 * Jürg Billeter <j@bitron.ch>
23 public class Vala
.SetLiteral
: Literal
{
24 private List
<Expression
> expression_list
= new ArrayList
<Expression
> ();
26 public DataType element_type
{ get; private set; }
28 public SetLiteral (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_set_literal (this
);
41 visitor
.visit_expression (this
);
44 public void add_expression (Expression expr
) {
45 expression_list
.add (expr
);
46 expr
.parent_node
= this
;
49 public List
<Expression
> get_expressions () {
50 return expression_list
;
53 public override bool is_pure () {
57 public override void replace_expression (Expression old_node
, Expression new_node
) {
58 for (int i
= 0; i
< expression_list
.size
; i
++) {
59 if (expression_list
[i
] == old_node
) {
60 expression_list
[i
] = new_node
;
65 public override bool check (CodeContext context
) {
72 var set_type
= new
ObjectType ((Class
) context
.root
.scope
.lookup ("Dova").scope
.lookup ("Set"));
73 set_type
.value_owned
= true;
75 bool fixed_element_type
= false;
76 if (target_type
!= null && target_type
.data_type
== set_type
.data_type
&& target_type
.get_type_arguments ().size
== 1) {
77 element_type
= target_type
.get_type_arguments ().get (0).copy ();
78 element_type
.value_owned
= false;
79 fixed_element_type
= true;
82 for (int i
= 0; i
< expression_list
.size
; i
++) {
83 var expr
= expression_list
[i
];
85 if (fixed_element_type
) {
86 expr
.target_type
= element_type
;
88 if (!expr
.check (context
)) {
92 // expression might have been replaced in the list
93 expr
= expression_list
[i
];
95 if (element_type
== null) {
96 element_type
= expr
.value_type
.copy ();
97 element_type
.value_owned
= false;
101 if (element_type
== null) {
103 Report
.error (source_reference
, "cannot infer element type for set literal");
107 element_type
= element_type
.copy ();
108 element_type
.value_owned
= true;
109 set_type
.add_type_argument (element_type
);
110 value_type
= set_type
;
115 public override void emit (CodeGenerator codegen
) {
116 foreach (Expression expr
in expression_list
) {
120 codegen
.visit_set_literal (this
);
122 codegen
.visit_expression (this
);