support namespace attributes process namespace and class attributes use
authorJürg Billeter <j@bitron.ch>
Thu, 18 May 2006 12:42:42 +0000 (12:42 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Thu, 18 May 2006 12:42:42 +0000 (12:42 +0000)
2006-05-18  Jürg Billeter  <j@bitron.ch>

* vala/parser.y: support namespace attributes
* vala/valaattributeprocessor.vala: process namespace and class
  attributes
* vala/valacodegenerator.vala: use correct symbol names
* vala/valamethod.vala: add cname
* vala/valanamespace.vala: add cprefix, support cprefix attribute
* vala/valastruct.vala: prefix cname with namespace cprefix
* bindings/GLib.vala: add tolower to unichar
* valac/parser.y: rename namespace attribute cname to lower_case_cprefix

svn path=/trunk/; revision=24

vala/ChangeLog
vala/bindings/GLib.vala
vala/vala/parser.y
vala/vala/valaattributeprocessor.vala
vala/vala/valacodegenerator.vala
vala/vala/valamethod.vala
vala/vala/valanamespace.vala
vala/vala/valastruct.vala
vala/valac/parser.y

index c4873d4..0d357ba 100644 (file)
@@ -1,5 +1,17 @@
 2006-05-18  Jürg Billeter  <j@bitron.ch>
 
+       * vala/parser.y: support namespace attributes
+       * vala/valaattributeprocessor.vala: process namespace and class
+         attributes
+       * vala/valacodegenerator.vala: use correct symbol names
+       * vala/valamethod.vala: add cname
+       * vala/valanamespace.vala: add cprefix, support cprefix attribute
+       * vala/valastruct.vala: prefix cname with namespace cprefix
+       * bindings/GLib.vala: add tolower to unichar
+       * valac/parser.y: rename namespace attribute cname to lower_case_cprefix
+
+2006-05-18  Jürg Billeter  <j@bitron.ch>
+
        * update reference and property annotations
        * switch string struct to utf-8
        * valac/context.c: set source file in root namespace
index 7ee6cc5..5af0c03 100644 (file)
@@ -92,6 +92,8 @@ public struct uint64 {
 public struct unichar {
        [CCode (cname = "g_unichar_isupper")]
        public bool isupper ();
+       [CCode (cname = "g_unichar_tolower")]
+       public unichar tolower ();
 }
 
 [ReferenceType ()]
@@ -152,7 +154,7 @@ public struct string {
 }
 
 [Import ()]
-[CCode (cname = "g", cprefix = "G", include_filename = "glib.h")]
+[CCode (cprefix = "G", lower_case_cprefix = "g_", include_filename = "glib.h")]
 namespace GLib {
        public struct Path {
                public static ref string get_basename (string file_name);
index 48b008f..ed10b46 100644 (file)
@@ -826,6 +826,7 @@ namespace_declaration
        : comment opt_attributes NAMESPACE IDENTIFIER
          {
                current_namespace = vala_namespace_new ($4, src_com (@4, $1));
+               VALA_CODE_NODE(current_namespace)->attributes = $2;
          }
          namespace_body
          {
index c74ed8e..6b3339a 100644 (file)
@@ -27,9 +27,17 @@ namespace Vala {
                public void process (CodeContext context) {
                        context.accept (this);
                }
+
+               public override void visit_begin_namespace (Namespace ns) {
+                       ns.process_attributes ();
+               }
        
                public override void visit_begin_struct (Struct st) {
                        st.process_attributes ();
                }
+       
+               public override void visit_begin_class (Class cl) {
+                       cl.process_attributes ();
+               }
        }
 }
index dcc9838..4b0e03c 100644 (file)
@@ -118,8 +118,8 @@ namespace Vala {
                }
                
                public override void visit_end_method (Method m) {
-                       var cmethod_decl = new CCodeFunction (name = m.name, return_type = "void");
-                       function = new CCodeFunction (name = m.name, return_type = "void");
+                       var cmethod_decl = new CCodeFunction (name = m.get_cname (), return_type = "void");
+                       function = new CCodeFunction (name = m.get_cname (), return_type = "void");
                        
                        if (m.access == MemberAccessibility.PUBLIC) {
                                header_type_member_declaration.append (cmethod_decl);
@@ -245,10 +245,14 @@ namespace Vala {
                public override void visit_literal_expression (LiteralExpression expr) {
                        expr.ccodenode = expr.literal.ccodenode;
                }
-
+               
                public override void visit_simple_name (SimpleName expr) {
-                       /* local variable */
-                       expr.ccodenode = new CCodeIdentifier (name = expr.name);
+                       if (expr.symbol_reference.node is Method) {
+                               var m = (Method) expr.symbol_reference.node;
+                               expr.ccodenode = new CCodeIdentifier (name = m.get_cname ());
+                       } else {
+                               expr.ccodenode = new CCodeIdentifier (name = expr.name);
+                       }
                }
 
                public override void visit_parenthesized_expression (ParenthesizedExpression expr) {
@@ -256,7 +260,12 @@ namespace Vala {
                }
 
                public override void visit_member_access (MemberAccess expr) {
-                       expr.ccodenode = new CCodeIdentifier (name = expr.member_name);
+                       if (expr.symbol_reference.node is Method) {
+                               var m = (Method) expr.symbol_reference.node;
+                               expr.ccodenode = new CCodeIdentifier (name = m.get_cname ());
+                       } else {
+                               expr.ccodenode = new CCodeIdentifier (name = expr.member_name);
+                       }
                }
 
                public override void visit_invocation_expression (InvocationExpression expr) {
index 9d41e24..db47639 100644 (file)
@@ -28,7 +28,7 @@ namespace Vala {
                public TypeReference return_type { get; construct; }
                public SourceReference source_reference { get; construct; }
                public weak CodeNode parent_type;
-               ref Statement _body;
+               Statement _body;
                public Statement body {
                        get {
                                return _body;
@@ -39,7 +39,10 @@ namespace Vala {
                }
                public MemberAccessibility access;
                public bool instance = true;
-               ref List<ref FormalParameter> parameters;
+               List<FormalParameter> parameters;
+               public string cname;
+               
+               Symbol dummy_symbol; // dummy type reference for broken dependency handling
                
                public static ref Method new (string name, TypeReference return_type, SourceReference source) {
                        return (new Method (name = name, return_type = return_type, source_reference = source));
@@ -66,6 +69,21 @@ namespace Vala {
                }
 
                public string get_cname () {
+                       if (cname == null) {
+                               var parent = symbol.parent_symbol.node;
+                               if (parent is Struct) {
+                                       cname = "%s_%s".printf (((Struct) parent).get_lower_case_cname (null), name);
+                               } else if (parent is Namespace) {
+                                       cname = "%s%s".printf (((Namespace) parent).get_lower_case_cprefix (), name);
+                               } else {
+                                       stderr.printf ("internal error: method is neither in struct nor in namespace\n");
+                               }
+                       }
+                       return cname;
+               }
+               
+               public void set_cname (string cname) {
+                       this.cname = cname;
                }
        }
 }
index a49fc74..b8a831f 100644 (file)
@@ -32,6 +32,7 @@ namespace Vala {
                List<Enum> enums;
                List<Field> fields;
                
+               public string cprefix;
                public string lower_case_cprefix;
                
                public static ref Namespace new (string name, SourceReference source) {
@@ -119,7 +120,7 @@ namespace Vala {
                                        }
                                }
                                
-                               result.append_unichar (c);
+                               result.append_unichar (c.tolower ());
                                
                                first = false;
                                i = i.next_char ();
@@ -128,6 +129,21 @@ namespace Vala {
                        return result.str;
                }
                
+               public string get_cprefix () {
+                       if (cprefix == null) {
+                               if (name == null) {
+                                       cprefix = "";
+                               } else {
+                                       cprefix = name;
+                               }
+                       }
+                       return cprefix;
+               }
+               
+               public void set_cprefix (string cprefix) {
+                       this.cprefix = cprefix;
+               }
+               
                public string get_lower_case_cprefix () {
                        if (lower_case_cprefix == null) {
                                if (name == null) {
@@ -142,5 +158,35 @@ namespace Vala {
                public void set_lower_case_cprefix (string cprefix) {
                        this.lower_case_cprefix = cprefix;
                }
+               
+               void process_ccode_attribute (Attribute a) {
+                       foreach (NamedArgument arg in a.args) {
+                               if (arg.name.collate ("cprefix") == 0) {
+                                       /* this will already be checked during semantic analysis */
+                                       if (arg.argument is LiteralExpression) {
+                                               var lit = ((LiteralExpression) arg.argument).literal;
+                                               if (lit is StringLiteral) {
+                                                       set_cprefix (((StringLiteral) lit).eval ());
+                                               }
+                                       }
+                               } else if (arg.name.collate ("lower_case_cprefix") == 0) {
+                                       /* this will already be checked during semantic analysis */
+                                       if (arg.argument is LiteralExpression) {
+                                               var lit = ((LiteralExpression) arg.argument).literal;
+                                               if (lit is StringLiteral) {
+                                                       set_lower_case_cprefix (((StringLiteral) lit).eval ());
+                                               }
+                                       }
+                               }
+                       }
+               }
+               
+               public void process_attributes () {
+                       foreach (Attribute a in attributes) {
+                               if (a.name.collate ("CCode") == 0) {
+                                       process_ccode_attribute (a);
+                               }
+                       }
+               }
        }
 }
index e60d90b..bb8b6bd 100644 (file)
@@ -88,7 +88,7 @@ namespace Vala {
                
                public override string get_cname () {
                        if (cname == null) {
-                               cname = name;
+                               cname = "%s%s".printf (@namespace.get_cprefix (), name);
                        }
                        return cname;
                }
index 9f22187..16c1e96 100644 (file)
@@ -376,8 +376,8 @@ namespace_declaration
                                for (al = anno->argument_list; al != NULL; al = al->next) {
                                        ValaNamedArgument *arg = al->data;
                                        
-                                       if (strcmp (arg->name, "cname") == 0) {
-                                               current_namespace->lower_case_cname = g_strdup_printf ("%s_", eval_string (arg->expression->str));
+                                       if (strcmp (arg->name, "lower_case_cprefix") == 0) {
+                                               current_namespace->lower_case_cname = g_strdup (eval_string (arg->expression->str));
                                                current_namespace->upper_case_cname = g_ascii_strup (current_namespace->lower_case_cname, -1);
                                        } else if (strcmp (arg->name, "cprefix") == 0) {
                                                current_namespace->cprefix = eval_string (arg->expression->str);