]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
add method parameters, public instance field access, invocation arguments
authorJürg Billeter <j@bitron.ch>
Thu, 18 May 2006 18:12:23 +0000 (18:12 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Thu, 18 May 2006 18:12:23 +0000 (18:12 +0000)
2006-05-18  Jürg Billeter  <j@bitron.ch>

* 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

vala/ccode/valaccodeformalparameter.vala [new file with mode: 0644]
vala/ccode/valaccodefunction.vala
vala/ccode/valaccodememberaccess.vala [new file with mode: 0644]
vala/vala/valacodegenerator.vala
vala/vala/valamethod.vala
vala/valac/context.h
vala/valac/generator.c
vala/valac/parser.y
vala/valac/scanner.l

diff --git a/vala/ccode/valaccodeformalparameter.vala b/vala/ccode/valaccodeformalparameter.vala
new file mode 100644 (file)
index 0000000..655728a
--- /dev/null
@@ -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 <j@bitron.ch>
+ */
+
+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);
+               }
+       }
+}
index 242d29cffffa6151179ee03457ce0c1d93cc5470..afe5e88905df22b97e92515dbc79a4331dd3f15c 100644 (file)
@@ -27,11 +27,11 @@ namespace Vala {
                public string name { get; construct; }
                public CCodeModifiers modifiers;
                public string return_type { get; construct; }
-               List<string> parameters;
+               List<CCodeFormalParameter> 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 (file)
index 0000000..e24de21
--- /dev/null
@@ -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 <j@bitron.ch>
+ */
+
+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);
+               }
+       }
+}
index 1806b767d5504bd0c70c176ab7795c47953f7b51..f5f6cb4b8da9554dde417ffb71f3bbbb7dde551c 100644 (file)
@@ -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);
                        }
index 7910d48da61c180ae2dfe184d536c062fbadefe2..44b7e5eebc51dc35fc11288839ed53417b3f8ac8 100644 (file)
@@ -51,6 +51,10 @@ namespace Vala {
                        parameters.append (param);
                }
                
+               public ref List<FormalParameter> get_parameters () {
+                       return parameters.copy ();
+               }
+               
                public override void accept (CodeVisitor visitor) {
                        visitor.visit_begin_method (this);
                        
index fe4ca08c5e367eebf8c39e9d46dce3dcde8685a2..36f1baf1b449e5b03b4022ad24ae5a2ab1ca24b7 100644 (file)
@@ -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 {
index 810a2b1da435dedcdc95acaf6228ffafb9a3b2e1..b97c9bcdfe0e34eecefcadd10663c08d04100215 100644 (file)
@@ -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);
index 16c1e96daa89ece2f0b58de3829368639d05eae1..83f826a1f430c6a7ed7565e052d7b14975331b67 100644 (file)
@@ -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 <expression> equality_expression
 %type <expression> relational_expression
 %type <expression> and_expression
+%type <expression> inclusive_or_expression
 %type <expression> conditional_and_expression
 %type <expression> conditional_or_expression
 %type <expression> 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
          {
index b11cbcfce6f0371e91357d5ed7252a08992c9a77..595daa261fbb6dc432d8885fd573692c9035d2fb 100644 (file)
@@ -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; }