From: Jürg Billeter Date: Thu, 18 May 2006 18:12:23 +0000 (+0000) Subject: add method parameters, public instance field access, invocation arguments X-Git-Tag: VALA_0_0_1~52 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=94ab84e3ffbceae8a815309bfcf68759def37c10;p=thirdparty%2Fvala.git add method parameters, public instance field access, invocation arguments 2006-05-18 Jürg Billeter * vala/valacodegenerator.vala: add method parameters, public instance field access, invocation arguments * vala/valamethod.vala: add get_parameters method * ccode/valaccodeformalparameter.vala * ccode/valaccodefunction.vala: use CCodeFormalParameter * ccode/valaccodememberaccess.vala * valac/scanner.l: support ASSIGN_BITWISE_OR, BITWISE_OR * valac/parser.y: support bitwise or * valac/context.h: support bitwise or * valac/generator.c: support bitwise or svn path=/trunk/; revision=27 --- diff --git a/vala/ccode/valaccodeformalparameter.vala b/vala/ccode/valaccodeformalparameter.vala new file mode 100644 index 000000000..655728a38 --- /dev/null +++ b/vala/ccode/valaccodeformalparameter.vala @@ -0,0 +1,36 @@ +/* valaccodeformalparameter.vala + * + * Copyright (C) 2006 Jürg Billeter + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Jürg Billeter + */ + +using GLib; + +namespace Vala { + public class CCodeFormalParameter : CCodeNode { + public string name { get; construct; } + public string type_name { get; construct; } + + public override void write (CCodeWriter writer) { + writer.write_string (type_name); + writer.write_string (" "); + writer.write_string (name); + } + } +} diff --git a/vala/ccode/valaccodefunction.vala b/vala/ccode/valaccodefunction.vala index 242d29cff..afe5e8890 100644 --- a/vala/ccode/valaccodefunction.vala +++ b/vala/ccode/valaccodefunction.vala @@ -27,11 +27,11 @@ namespace Vala { public string name { get; construct; } public CCodeModifiers modifiers; public string return_type { get; construct; } - List parameters; + List parameters; public CCodeBlock block; - public void add_parameter (string type, string name) { - parameters.append ("%s %s".printf (type, name)); + public void add_parameter (CCodeFormalParameter param) { + parameters.append (param); } public override void write (CCodeWriter writer) { @@ -43,9 +43,17 @@ namespace Vala { writer.write_string (" "); writer.write_string (name); writer.write_string (" ("); - foreach (string parameter in parameters) { - writer.write_string (parameter); + + bool first = true; + foreach (CCodeFormalParameter param in parameters) { + if (!first) { + writer.write_string (", "); + } else { + first = false; + } + param.write (writer); } + writer.write_string (")"); if (block == null) { writer.write_string (";"); diff --git a/vala/ccode/valaccodememberaccess.vala b/vala/ccode/valaccodememberaccess.vala new file mode 100644 index 000000000..e24de21bb --- /dev/null +++ b/vala/ccode/valaccodememberaccess.vala @@ -0,0 +1,41 @@ +/* valaccodememberaccess.vala + * + * Copyright (C) 2006 Jürg Billeter + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Jürg Billeter + */ + +using GLib; + +namespace Vala { + public class CCodeMemberAccess : CCodeExpression { + public CCodeExpression inner { get; construct; } + public string member_name { get; construct; } + public bool is_pointer { get; construct; } + + public override void write (CCodeWriter writer) { + inner.write (writer); + if (is_pointer) { + writer.write_string ("->"); + } else { + writer.write_string ("."); + } + writer.write_string (member_name); + } + } +} diff --git a/vala/vala/valacodegenerator.vala b/vala/vala/valacodegenerator.vala index 1806b767d..f5f6cb4b8 100644 --- a/vala/vala/valacodegenerator.vala +++ b/vala/vala/valacodegenerator.vala @@ -39,6 +39,7 @@ namespace Vala { CCodeBlock block; TypeReference reference; // dummy for dependency resolution + Symbol dummy_symbol; // dummy for dependency resolution public void emit (CodeContext context) { context.accept (this); @@ -128,6 +129,20 @@ namespace Vala { function.modifiers |= CCodeModifiers.STATIC; source_type_member_declaration.append (cmethod_decl); } + + if (m.instance) { + var st = (Struct) m.symbol.parent_symbol.node; + var this_type = new TypeReference (); + this_type.type = st; + var cparam = new CCodeFormalParameter (type_name = this_type.get_cname (), name = "self"); + cmethod_decl.add_parameter (cparam); + function.add_parameter (cparam); + } + + foreach (FormalParameter param in m.get_parameters ()) { + cmethod_decl.add_parameter ((CCodeFormalParameter) param.ccodenode); + function.add_parameter ((CCodeFormalParameter) param.ccodenode); + } if (m.body != null) { function.block = m.body.ccodenode; @@ -139,6 +154,9 @@ namespace Vala { source_type_member_definition.append (function); } + public override void visit_formal_parameter (FormalParameter p) { + p.ccodenode = new CCodeFormalParameter (type_name = p.type_reference.get_cname (), name = p.name); + } public override void visit_end_block (Block b) { var cblock = new CCodeBlock (); @@ -250,6 +268,9 @@ namespace Vala { if (expr.symbol_reference.node is Method) { var m = (Method) expr.symbol_reference.node; expr.ccodenode = new CCodeIdentifier (name = m.get_cname ()); + } else if (expr.symbol_reference.node is Field) { + var f = (Field) expr.symbol_reference.node; + expr.ccodenode = new CCodeMemberAccess (inner = new CCodeIdentifier (name = "self"), member_name = f.name, is_pointer = true); } else { expr.ccodenode = new CCodeIdentifier (name = expr.name); } @@ -271,6 +292,19 @@ namespace Vala { public override void visit_invocation_expression (InvocationExpression expr) { var ccall = new CCodeFunctionCall (call = (CCodeExpression) expr.call.ccodenode); + var m = (Method) expr.call.symbol_reference.node; + if (m.instance) { + CCodeExpression instance; + if (expr.call is SimpleName) { + instance = new CCodeIdentifier (name = "self"); + } else if (expr.call is MemberAccess) { + instance = ((MemberAccess) expr.call).inner.ccodenode; + } else { + stderr.printf ("internal error: unsupported method invocation\n"); + } + ccall.add_argument (instance); + } + foreach (Expression arg in expr.argument_list) { ccall.add_argument ((CCodeExpression) arg.ccodenode); } diff --git a/vala/vala/valamethod.vala b/vala/vala/valamethod.vala index 7910d48da..44b7e5eeb 100644 --- a/vala/vala/valamethod.vala +++ b/vala/vala/valamethod.vala @@ -51,6 +51,10 @@ namespace Vala { parameters.append (param); } + public ref List get_parameters () { + return parameters.copy (); + } + public override void accept (CodeVisitor visitor) { visitor.visit_begin_method (this); diff --git a/vala/valac/context.h b/vala/valac/context.h index fe4ca08c5..36f1baf1b 100644 --- a/vala/valac/context.h +++ b/vala/valac/context.h @@ -134,6 +134,7 @@ enum _ValaOpType { VALA_OP_TYPE_AND, VALA_OP_TYPE_BITWISE_AND, VALA_OP_TYPE_OR, + VALA_OP_TYPE_BITWISE_OR, }; struct _ValaContext { diff --git a/vala/valac/generator.c b/vala/valac/generator.c index 810a2b1da..b97c9bcdf 100644 --- a/vala/valac/generator.c +++ b/vala/valac/generator.c @@ -316,6 +316,9 @@ vala_code_generator_process_operation_expression (ValaCodeGenerator *generator, case VALA_OP_TYPE_OR: cop = "||"; break; + case VALA_OP_TYPE_BITWISE_OR: + cop = "|"; + break; } fprintf (generator->c_file, " %s ", cop); vala_code_generator_process_expression (generator, expr->op.right); diff --git a/vala/valac/parser.y b/vala/valac/parser.y index 16c1e96da..83f826a1f 100644 --- a/vala/valac/parser.y +++ b/vala/valac/parser.y @@ -156,6 +156,8 @@ ValaLocation *get_location (int lineno, int colno) %token SEMICOLON ";" %token HASH "#" +%token ASSIGN_BITWISE_OR "|=" + %token OP_INC "++" %token OP_DEC "--" %token OP_EQ "==" @@ -167,6 +169,7 @@ ValaLocation *get_location (int lineno, int colno) %token OP_NEG "!" %token OP_OR "||" %token OP_AND "&&" +%token BITWISE_OR "|" %token BITWISE_AND "&" %token ASSIGN "=" @@ -268,6 +271,7 @@ ValaLocation *get_location (int lineno, int colno) %type equality_expression %type relational_expression %type and_expression +%type inclusive_or_expression %type conditional_and_expression %type conditional_or_expression %type post_increment_expression @@ -1192,9 +1196,22 @@ and_expression } ; -conditional_and_expression +inclusive_or_expression : and_expression - | conditional_and_expression OP_AND and_expression + | inclusive_or_expression BITWISE_OR and_expression + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_OPERATION; + $$->location = current_location (@1); + $$->op.left = $1; + $$->op.type = VALA_OP_TYPE_BITWISE_OR; + $$->op.right = $3; + } + ; + +conditional_and_expression + : inclusive_or_expression + | conditional_and_expression OP_AND inclusive_or_expression { $$ = g_new0 (ValaExpression, 1); $$->type = VALA_EXPRESSION_TYPE_OPERATION; @@ -1681,7 +1698,7 @@ statement_expression ; assignment - : primary_expression ASSIGN expression + : primary_expression assignment_operator expression { $$ = g_new0 (ValaExpression, 1); $$->type = VALA_EXPRESSION_TYPE_ASSIGNMENT; @@ -1691,6 +1708,11 @@ assignment } ; +assignment_operator + : ASSIGN + | ASSIGN_BITWISE_OR + ; + selection_statement : if_statement { diff --git a/vala/valac/scanner.l b/vala/valac/scanner.l index b11cbcfce..595daa261 100644 --- a/vala/valac/scanner.l +++ b/vala/valac/scanner.l @@ -64,6 +64,8 @@ literal ({literal_integer}|{literal_character}|{literal_string}) ";" { uploc; return SEMICOLON; } "#" { uploc; return HASH; } +"|=" { uploc; return ASSIGN_BITWISE_OR; } + "++" { uploc; return OP_INC; } "--" { uploc; return OP_DEC; } "==" { uploc; return OP_EQ; } @@ -76,6 +78,7 @@ literal ({literal_integer}|{literal_character}|{literal_string}) "&&" { uploc; return OP_AND; } "&" { uploc; return BITWISE_AND; } "||" { uploc; return OP_OR; } +"|" { uploc; return BITWISE_OR; } "=" { uploc; return ASSIGN; } "+" { uploc; return PLUS; }