From f41df8679fb3e8f258d2e4a91becb05ee30c9e68 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrg=20Billeter?= Date: Thu, 18 May 2006 12:42:42 +0000 Subject: [PATCH] support namespace attributes process namespace and class attributes use MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2006-05-18 Jürg Billeter * 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 | 12 +++++++++ vala/bindings/GLib.vala | 4 ++- vala/vala/parser.y | 1 + vala/vala/valaattributeprocessor.vala | 8 ++++++ vala/vala/valacodegenerator.vala | 21 ++++++++++----- vala/vala/valamethod.vala | 22 ++++++++++++++-- vala/vala/valanamespace.vala | 48 ++++++++++++++++++++++++++++++++++- vala/vala/valastruct.vala | 2 +- vala/valac/parser.y | 4 +-- 9 files changed, 109 insertions(+), 13 deletions(-) diff --git a/vala/ChangeLog b/vala/ChangeLog index c4873d4..0d357ba 100644 --- a/vala/ChangeLog +++ b/vala/ChangeLog @@ -1,5 +1,17 @@ 2006-05-18 Jürg Billeter + * 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 + * update reference and property annotations * switch string struct to utf-8 * valac/context.c: set source file in root namespace diff --git a/vala/bindings/GLib.vala b/vala/bindings/GLib.vala index 7ee6cc5..5af0c03 100644 --- a/vala/bindings/GLib.vala +++ b/vala/bindings/GLib.vala @@ -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); diff --git a/vala/vala/parser.y b/vala/vala/parser.y index 48b008f..ed10b46 100644 --- a/vala/vala/parser.y +++ b/vala/vala/parser.y @@ -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 { diff --git a/vala/vala/valaattributeprocessor.vala b/vala/vala/valaattributeprocessor.vala index c74ed8e..6b3339a 100644 --- a/vala/vala/valaattributeprocessor.vala +++ b/vala/vala/valaattributeprocessor.vala @@ -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 (); + } } } diff --git a/vala/vala/valacodegenerator.vala b/vala/vala/valacodegenerator.vala index dcc9838..4b0e03c 100644 --- a/vala/vala/valacodegenerator.vala +++ b/vala/vala/valacodegenerator.vala @@ -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) { diff --git a/vala/vala/valamethod.vala b/vala/vala/valamethod.vala index 9d41e24..db47639 100644 --- a/vala/vala/valamethod.vala +++ b/vala/vala/valamethod.vala @@ -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 parameters; + List 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; } } } diff --git a/vala/vala/valanamespace.vala b/vala/vala/valanamespace.vala index a49fc74..b8a831f 100644 --- a/vala/vala/valanamespace.vala +++ b/vala/vala/valanamespace.vala @@ -32,6 +32,7 @@ namespace Vala { List enums; List 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); + } + } + } } } diff --git a/vala/vala/valastruct.vala b/vala/vala/valastruct.vala index e60d90b..bb8b6bd 100644 --- a/vala/vala/valastruct.vala +++ b/vala/vala/valastruct.vala @@ -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; } diff --git a/vala/valac/parser.y b/vala/valac/parser.y index 9f22187..16c1e96 100644 --- a/vala/valac/parser.y +++ b/vala/valac/parser.y @@ -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); -- 2.7.4