1 /* initializerbuilder.vala
3 * Copyright (C) 2011 Florian Brosch
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 * Florian Brosch <flo.brosch@gmail.com>
24 using Valadoc
.Content
;
26 private class Valadoc
.Api
.InitializerBuilder
: Vala
.CodeVisitor
{
27 private Vala
.HashMap
<Vala
.Symbol
, Symbol
> symbol_map
;
28 private SignatureBuilder signature
;
30 private Symbol?
resolve (Vala
.Symbol symbol
) {
31 return symbol_map
.get (symbol
);
34 private void write_node (Vala
.Symbol vsymbol
) {
35 signature
.append_symbol (resolve (vsymbol
));
38 private void write_type (Vala
.DataType vsymbol
) {
39 if (vsymbol
.data_type
!= null) {
40 write_node (vsymbol
.data_type
);
42 signature
.append_literal ("null");
45 var type_args
= vsymbol
.get_type_arguments ();
46 if (type_args
.size
> 0) {
47 signature
.append ("<");
49 foreach (Vala
.DataType type_arg
in type_args
) {
51 signature
.append (",");
55 if (!type_arg
.value_owned
) {
56 signature
.append_keyword ("weak");
58 signature
.append (type_arg
.to_qualified_string (null));
60 signature
.append (">");
63 if (vsymbol
.nullable
) {
64 signature
.append ("?");
71 public override void visit_array_creation_expression (Vala
.ArrayCreationExpression expr
) {
72 signature
.append_keyword ("new");
73 write_type (expr
.element_type
);
74 signature
.append ("[", false);
77 foreach (Vala
.Expression size
in expr
.get_sizes ()) {
79 signature
.append (", ", false);
85 signature
.append ("]", false);
87 if (expr
.initializer_list
!= null) {
88 signature
.append (" ", false);
89 expr
.initializer_list
.accept (this
);
93 public InitializerBuilder (SignatureBuilder signature
, Vala
.HashMap
<Vala
.Symbol
, Symbol
> symbol_map
) {
94 this
.symbol_map
= symbol_map
;
95 this
.signature
= signature
;
101 public override void visit_binary_expression (Vala
.BinaryExpression expr
) {
102 expr
.left
.accept (this
);
104 switch (expr
.operator
) {
105 case Vala
.BinaryOperator
.PLUS
:
106 signature
.append ("+ ");
109 case Vala
.BinaryOperator
.MINUS
:
110 signature
.append ("- ");
113 case Vala
.BinaryOperator
.MUL
:
114 signature
.append ("* ");
117 case Vala
.BinaryOperator
.DIV
:
118 signature
.append ("/ ");
121 case Vala
.BinaryOperator
.MOD
:
122 signature
.append ("% ");
125 case Vala
.BinaryOperator
.SHIFT_LEFT
:
126 signature
.append ("<< ");
129 case Vala
.BinaryOperator
.SHIFT_RIGHT
:
130 signature
.append (">> ");
133 case Vala
.BinaryOperator
.LESS_THAN
:
134 signature
.append ("< ");
137 case Vala
.BinaryOperator
.GREATER_THAN
:
138 signature
.append ("> ");
141 case Vala
.BinaryOperator
.LESS_THAN_OR_EQUAL
:
142 signature
.append ("<= ");
145 case Vala
.BinaryOperator
.GREATER_THAN_OR_EQUAL
:
146 signature
.append (">= ");
149 case Vala
.BinaryOperator
.EQUALITY
:
150 signature
.append ("== ");
153 case Vala
.BinaryOperator
.INEQUALITY
:
154 signature
.append ("!= ");
157 case Vala
.BinaryOperator
.BITWISE_AND
:
158 signature
.append ("& ");
161 case Vala
.BinaryOperator
.BITWISE_OR
:
162 signature
.append ("| ");
165 case Vala
.BinaryOperator
.BITWISE_XOR
:
166 signature
.append ("^ ");
169 case Vala
.BinaryOperator
.AND
:
170 signature
.append ("&& ");
173 case Vala
.BinaryOperator
.OR
:
174 signature
.append ("|| ");
177 case Vala
.BinaryOperator
.IN
:
178 signature
.append_keyword ("in");
179 signature
.append (" ");
182 case Vala
.BinaryOperator
.COALESCE
:
183 signature
.append ("?? ");
187 assert_not_reached ();
190 expr
.right
.accept (this
);
196 public override void visit_unary_expression (Vala
.UnaryExpression expr
) {
197 switch (expr
.operator
) {
198 case Vala
.UnaryOperator
.PLUS
:
199 signature
.append ("+");
202 case Vala
.UnaryOperator
.MINUS
:
203 signature
.append ("-");
206 case Vala
.UnaryOperator
.LOGICAL_NEGATION
:
207 signature
.append ("!");
210 case Vala
.UnaryOperator
.BITWISE_COMPLEMENT
:
211 signature
.append ("~");
214 case Vala
.UnaryOperator
.INCREMENT
:
215 signature
.append ("++");
218 case Vala
.UnaryOperator
.DECREMENT
:
219 signature
.append ("--");
222 case Vala
.UnaryOperator
.REF
:
223 signature
.append_keyword ("ref");
226 case Vala
.UnaryOperator
.OUT
:
227 signature
.append_keyword ("out");
231 assert_not_reached ();
233 expr
.inner
.accept (this
);
239 public override void visit_assignment (Vala
.Assignment a
) {
240 a
.left
.accept (this
);
242 switch (a
.operator
) {
243 case Vala
.AssignmentOperator
.SIMPLE
:
244 signature
.append ("=");
247 case Vala
.AssignmentOperator
.BITWISE_OR
:
248 signature
.append ("|");
251 case Vala
.AssignmentOperator
.BITWISE_AND
:
252 signature
.append ("&");
255 case Vala
.AssignmentOperator
.BITWISE_XOR
:
256 signature
.append ("^");
259 case Vala
.AssignmentOperator
.ADD
:
260 signature
.append ("+");
263 case Vala
.AssignmentOperator
.SUB
:
264 signature
.append ("-");
267 case Vala
.AssignmentOperator
.MUL
:
268 signature
.append ("*");
271 case Vala
.AssignmentOperator
.DIV
:
272 signature
.append ("/");
275 case Vala
.AssignmentOperator
.PERCENT
:
276 signature
.append ("%");
279 case Vala
.AssignmentOperator
.SHIFT_LEFT
:
280 signature
.append ("<<");
283 case Vala
.AssignmentOperator
.SHIFT_RIGHT
:
284 signature
.append (">>");
288 assert_not_reached ();
291 a
.right
.accept (this
);
297 public override void visit_cast_expression (Vala
.CastExpression expr
) {
298 if (expr
.is_non_null_cast
) {
299 signature
.append ("(!)");
300 expr
.inner
.accept (this
);
304 if (!expr
.is_silent_cast
) {
305 signature
.append ("(", false);
306 write_type (expr
.type_reference
);
307 signature
.append (")", false);
310 expr
.inner
.accept (this
);
312 if (expr
.is_silent_cast
) {
313 signature
.append_keyword ("as");
314 write_type (expr
.type_reference
);
321 public override void visit_initializer_list (Vala
.InitializerList list
) {
322 signature
.append ("{", false);
325 foreach (Vala
.Expression initializer
in list
.get_initializers ()) {
327 signature
.append (", ", false);
330 initializer
.accept (this
);
333 signature
.append ("}", false);
339 public override void visit_member_access (Vala
.MemberAccess expr
) {
340 if (expr
.symbol_reference
!= null) {
341 expr
.symbol_reference
.accept (this
);
343 signature
.append (expr
.member_name
);
350 public override void visit_element_access (Vala
.ElementAccess expr
) {
351 expr
.container
.accept (this
);
352 signature
.append ("[", false);
355 foreach (Vala
.Expression index
in expr
.get_indices ()) {
357 signature
.append (", ", false);
364 signature
.append ("]", false);
370 public override void visit_pointer_indirection (Vala
.PointerIndirection expr
) {
371 signature
.append ("*", false);
372 expr
.inner
.accept (this
);
378 public override void visit_addressof_expression (Vala
.AddressofExpression expr
) {
379 signature
.append ("&", false);
380 expr
.inner
.accept (this
);
386 public override void visit_reference_transfer_expression (Vala
.ReferenceTransferExpression expr
) {
387 signature
.append ("(", false).append_keyword ("owned", false).append (")", false);
388 expr
.inner
.accept (this
);
394 public override void visit_type_check (Vala
.TypeCheck expr
) {
395 expr
.expression
.accept (this
);
396 signature
.append_keyword ("is");
397 write_type (expr
.type_reference
);
403 public override void visit_method_call (Vala
.MethodCall expr
) {
405 expr
.call
.symbol_reference
.accept (this
);
408 signature
.append (" (", false);
410 foreach (Vala
.Expression literal
in expr
.get_argument_list ()) {
412 signature
.append (", ", false);
415 literal
.accept (this
);
418 signature
.append (")", false);
424 public override void visit_slice_expression (Vala
.SliceExpression expr
) {
425 expr
.container
.accept (this
);
426 signature
.append ("[", false);
427 expr
.start
.accept (this
);
428 signature
.append (":", false);
429 expr
.stop
.accept (this
);
430 signature
.append ("]", false);
436 public override void visit_base_access (Vala
.BaseAccess expr
) {
437 signature
.append_keyword ("base", false);
443 public override void visit_postfix_expression (Vala
.PostfixExpression expr
) {
444 expr
.inner
.accept (this
);
445 if (expr
.increment
) {
446 signature
.append ("++", false);
448 signature
.append ("--", false);
455 public override void visit_object_creation_expression (Vala
.ObjectCreationExpression expr
) {
456 if (!expr
.struct_creation
) {
457 signature
.append_keyword ("new");
460 signature
.append_symbol (resolve (expr
.symbol_reference
));
462 signature
.append (" (", false);
464 //TODO: rm conditional space
466 foreach (Vala
.Expression arg
in expr
.get_argument_list ()) {
468 signature
.append (", ", false);
474 signature
.append (")", false);
480 public override void visit_sizeof_expression (Vala
.SizeofExpression expr
) {
481 signature
.append_keyword ("sizeof", false).append (" (", false);
482 write_type (expr
.type_reference
);
483 signature
.append (")", false);
489 public override void visit_typeof_expression (Vala
.TypeofExpression expr
) {
490 signature
.append_keyword ("typeof", false).append (" (", false);
491 write_type (expr
.type_reference
);
492 signature
.append (")", false);
498 public override void visit_lambda_expression (Vala
.LambdaExpression expr
) {
499 signature
.append ("(", false);
502 foreach (Vala
.Parameter param
in expr
.get_parameters ()) {
504 signature
.append (", ", false);
506 signature
.append (param
.name
, false);
511 signature
.append (") => {", false);
512 signature
.append_highlighted (" [...] ", false);
513 signature
.append ("}", false);
521 public override void visit_boolean_literal (Vala
.BooleanLiteral lit
) {
522 signature
.append_literal (lit
.to_string (), false);
528 public override void visit_character_literal (Vala
.CharacterLiteral lit
) {
529 signature
.append_literal (lit
.to_string (), false);
535 public override void visit_integer_literal (Vala
.IntegerLiteral lit
) {
536 signature
.append_literal (lit
.to_string (), false);
542 public override void visit_real_literal (Vala
.RealLiteral lit
) {
543 signature
.append_literal (lit
.to_string (), false);
549 public override void visit_regex_literal (Vala
.RegexLiteral lit
) {
550 signature
.append_literal (lit
.to_string (), false);
556 public override void visit_string_literal (Vala
.StringLiteral lit
) {
557 signature
.append_literal (lit
.to_string (), false);
563 public override void visit_null_literal (Vala
.NullLiteral lit
) {
564 signature
.append_literal (lit
.to_string (), false);
572 public override void visit_field (Vala
.Field field
) {
579 public override void visit_constant (Vala
.Constant constant
) {
580 write_node (constant
);
586 public override void visit_enum_value (Vala
.EnumValue ev
) {
593 public override void visit_error_code (Vala
.ErrorCode ec
) {
600 public override void visit_delegate (Vala
.Delegate d
) {
607 public override void visit_method (Vala
.Method m
) {
614 public override void visit_creation_method (Vala
.CreationMethod m
) {
621 public override void visit_signal (Vala
.Signal sig
) {
628 public override void visit_class (Vala
.Class c
) {
635 public override void visit_struct (Vala
.Struct s
) {
642 public override void visit_interface (Vala
.Interface i
) {
649 public override void visit_enum (Vala
.Enum en
) {
656 public override void visit_error_domain (Vala
.ErrorDomain ed
) {
663 public override void visit_property (Vala
.Property prop
) {