From 7d614d7c15f56917161a3889d9953b02f991e6b9 Mon Sep 17 00:00:00 2001 From: Raffaele Sandrini Date: Sun, 4 Mar 2007 15:22:33 +0000 Subject: [PATCH] add support for construct formal creation method parameters add testcase 2007-03-04 Raffaele Sandrini * vala/valasemanticanalyzer.vala, vala/parser.y, vala/valaformalparameter.vala: add support for construct formal creation method parameters * tests/test-029.vala, tests/test-029.out: add testcase for construct formal parameters * tests/Makefile.am: update svn path=/trunk/; revision=216 --- vala/ChangeLog | 9 ++++++ vala/tests/Makefile.am | 3 ++ vala/tests/test-029.out | 1 + vala/tests/test-029.vala | 24 ++++++++++++++ vala/vala/parser.y | 50 ++++++++++++++++++----------- vala/vala/valaformalparameter.vala | 9 +++++- vala/vala/valasemanticanalyzer.vala | 26 +++++++++++++++ 7 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 vala/tests/test-029.out create mode 100644 vala/tests/test-029.vala diff --git a/vala/ChangeLog b/vala/ChangeLog index fe23be700..1a089b7e0 100644 --- a/vala/ChangeLog +++ b/vala/ChangeLog @@ -1,3 +1,12 @@ +2007-03-04 Raffaele Sandrini + + * vala/valasemanticanalyzer.vala, vala/parser.y, + vala/valaformalparameter.vala: add support for construct formal + creation method parameters + * tests/test-029.vala, tests/test-029.out: add testcase for construct + formal parameters + * tests/Makefile.am: update + 2007-03-04 Raffaele Sandrini * vala/valaattributeprocessor.vala, vala/valacharacterliteral.vala, diff --git a/vala/tests/Makefile.am b/vala/tests/Makefile.am index b1f77e5e1..be001c236 100644 --- a/vala/tests/Makefile.am +++ b/vala/tests/Makefile.am @@ -31,6 +31,7 @@ TESTS = \ test-026.vala \ test-027.vala \ test-028.vala \ + test-029.vala \ $(NULL) EXTRA_DIST = \ @@ -63,6 +64,7 @@ EXTRA_DIST = \ test-026.vala \ test-027.vala \ test-028.vala \ + test-029.vala \ test-001.out \ test-002.out \ test-003.out \ @@ -91,4 +93,5 @@ EXTRA_DIST = \ test-026.out \ test-027.out \ test-028.out \ + test-029.out \ $(NULL) diff --git a/vala/tests/test-029.out b/vala/tests/test-029.out new file mode 100644 index 000000000..4dcfda1c9 --- /dev/null +++ b/vala/tests/test-029.out @@ -0,0 +1 @@ +Construct Formal Parameter Test: 1 2 3 4 5 diff --git a/vala/tests/test-029.vala b/vala/tests/test-029.vala new file mode 100644 index 000000000..e913e82ac --- /dev/null +++ b/vala/tests/test-029.vala @@ -0,0 +1,24 @@ +using GLib; + +class Maman.Foo { + public int p1 { get; set; } + public int p2 { get; set; } + + public Foo (int i, construct int p2) { + p1 = 2 * i; + } + + public static int main (string[] args) { + stdout.printf ("Construct Formal Parameter Test: 1"); + + var foo = new Foo (2, 3); + + stdout.printf (" 2"); + stdout.printf (" %d", foo.p2); + stdout.printf (" %d", foo.p1); + + stdout.printf (" 5\n", foo.p2); + + return 0; + } +} diff --git a/vala/vala/parser.y b/vala/vala/parser.y index 2c1a1c765..974286d6a 100644 --- a/vala/vala/parser.y +++ b/vala/vala/parser.y @@ -324,6 +324,7 @@ static void yyerror (YYLTYPE *locp, ValaParser *parser, const char *msg); %type method_body %type opt_formal_parameter_list %type formal_parameter_list +%type opt_construct %type fixed_parameters %type fixed_parameter %type signal_declaration @@ -2365,34 +2366,47 @@ fixed_parameters } ; +opt_construct + : /* empty */ + { + $$ = FALSE; + } + | CONSTRUCT + { + $$ = TRUE; + } + ; + fixed_parameter - : opt_attributes type IDENTIFIER + : opt_attributes opt_construct type IDENTIFIER { - if (vala_type_reference_get_is_ref ($2) && vala_type_reference_get_is_out ($2)) { - vala_type_reference_set_takes_ownership ($2, TRUE); - vala_type_reference_set_is_ref ($2, FALSE); + if (vala_type_reference_get_is_ref ($3) && vala_type_reference_get_is_out ($3)) { + vala_type_reference_set_takes_ownership ($3, TRUE); + vala_type_reference_set_is_ref ($3, FALSE); } - ValaSourceReference *src = src(@2); - $$ = vala_formal_parameter_new ($3, $2, src); + ValaSourceReference *src = src(@3); + $$ = vala_formal_parameter_new ($4, $3, src); g_object_unref (src); - g_object_unref ($2); - g_free ($3); + vala_formal_parameter_set_construct_parameter ($$, $2); + g_object_unref ($3); + g_free ($4); } - | opt_attributes type IDENTIFIER ASSIGN expression + | opt_attributes opt_construct type IDENTIFIER ASSIGN expression { - if (vala_type_reference_get_is_ref ($2) && vala_type_reference_get_is_out ($2)) { - vala_type_reference_set_takes_ownership ($2, TRUE); - vala_type_reference_set_is_ref ($2, FALSE); + if (vala_type_reference_get_is_ref ($3) && vala_type_reference_get_is_out ($3)) { + vala_type_reference_set_takes_ownership ($3, TRUE); + vala_type_reference_set_is_ref ($3, FALSE); } - ValaSourceReference *src = src(@2); - $$ = vala_formal_parameter_new ($3, $2, src); + ValaSourceReference *src = src(@3); + $$ = vala_formal_parameter_new ($4, $3, src); g_object_unref (src); - vala_formal_parameter_set_default_expression ($$, $5); - g_object_unref ($2); - g_free ($3); - g_object_unref ($5); + vala_formal_parameter_set_default_expression ($$, $6); + vala_formal_parameter_set_construct_parameter ($$, $2); + g_object_unref ($3); + g_free ($4); + g_object_unref ($6); } ; diff --git a/vala/vala/valaformalparameter.vala b/vala/vala/valaformalparameter.vala index 9bdf96ef3..c699a185c 100644 --- a/vala/vala/valaformalparameter.vala +++ b/vala/vala/valaformalparameter.vala @@ -1,6 +1,6 @@ /* valaformalparameter.vala * - * Copyright (C) 2006 Jürg Billeter + * Copyright (C) 2006-2007 Jürg Billeter, Raffaele Sandrini * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,6 +18,7 @@ * * Author: * Jürg Billeter + * Raffaele Sandrini */ using GLib; @@ -54,6 +55,12 @@ public class Vala.FormalParameter : CodeNode, Invokable { */ public bool no_array_length { get; set; } + /** + * Specifies whether this parameter holds a value to be assigned to a + * construct property. This is only allowed in CreationMethod headers. + */ + public bool construct_parameter { get; set; } + /** * Creates a new formal parameter. * diff --git a/vala/vala/valasemanticanalyzer.vala b/vala/vala/valasemanticanalyzer.vala index 07a2cccfa..956d6f209 100644 --- a/vala/vala/valasemanticanalyzer.vala +++ b/vala/vala/valasemanticanalyzer.vala @@ -291,6 +291,32 @@ public class Vala.SemanticAnalyzer : CodeVisitor { current_source_file.add_symbol_dependency (p.type_reference.data_type.symbol, SourceFileDependencyType.SOURCE); } } + + /* special treatment for construct formal parameters used in creation methods */ + if (p.construct_parameter) { + if (!(p.symbol.parent_symbol.node is CreationMethod)) { + p.error = true; + Report.error (p.source_reference, "construct parameters are only allowed in type creation methods"); + return; + } + + var method_body = ((CreationMethod)p.symbol.parent_symbol.node).body; + var left = new MemberAccess.simple (p.name); + var right = new MemberAccess.simple (p.name); + + /* try to lookup the requeted property */ + var prop_sym = current_class.symbol.lookup (p.name); + if (prop_sym == null || !(prop_sym.node is Property)) { + p.error = true; + Report.error (p.source_reference, "class `%s' does not contain a property named `%s'".printf (current_class.symbol.get_full_name (), p.name)); + return; + } + left.symbol_reference = prop_sym; + + right.symbol_reference = p.symbol; + + method_body.add_statement (new ExpressionStatement (new Assignment (left, right))); + } } public override void visit_end_property (Property! prop) { -- 2.47.3