/* valanamespace.vala
*
- * Copyright (C) 2006-2009 Jürg Billeter
+ * Copyright (C) 2006-2010 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
*
* @param ns a namespace
*/
- public void add_namespace (Namespace ns) {
+ public override void add_namespace (Namespace ns) {
+ if (ns.owner == null) {
+ ns.source_reference.file.add_node (ns);
+ }
+
if (scope.lookup (ns.name) is Namespace) {
// merge if namespace already exists
var old_ns = (Namespace) scope.lookup (ns.name);
*
* @param cl a class
*/
- public void add_class (Class cl) {
+ public override void add_class (Class cl) {
// namespaces do not support private memebers
if (cl.access == SymbolAccessibility.PRIVATE) {
cl.access = SymbolAccessibility.INTERNAL;
}
+ if (cl.owner == null) {
+ cl.source_reference.file.add_node (cl);
+ }
+
classes.add (cl);
scope.add (cl.name, cl);
}
*
* @param iface an interface
*/
- public void add_interface (Interface iface) {
+ public override void add_interface (Interface iface) {
// namespaces do not support private memebers
if (iface.access == SymbolAccessibility.PRIVATE) {
iface.access = SymbolAccessibility.INTERNAL;
}
+ if (iface.owner == null) {
+ iface.source_reference.file.add_node (iface);
+ }
+
interfaces.add (iface);
scope.add (iface.name, iface);
+
}
/**
*
* @param st a struct
*/
- public void add_struct (Struct st) {
+ public override void add_struct (Struct st) {
// namespaces do not support private memebers
if (st.access == SymbolAccessibility.PRIVATE) {
st.access = SymbolAccessibility.INTERNAL;
}
+ if (st.owner == null) {
+ st.source_reference.file.add_node (st);
+ }
+
structs.add (st);
scope.add (st.name, st);
}
*
* @param en an enum
*/
- public void add_enum (Enum en) {
+ public override void add_enum (Enum en) {
// namespaces do not support private memebers
if (en.access == SymbolAccessibility.PRIVATE) {
en.access = SymbolAccessibility.INTERNAL;
}
+ if (en.owner == null) {
+ en.source_reference.file.add_node (en);
+ }
+
enums.add (en);
scope.add (en.name, en);
}
*
* @param edomain an error domain
*/
- public void add_error_domain (ErrorDomain edomain) {
+ public override void add_error_domain (ErrorDomain edomain) {
// namespaces do not support private memebers
if (edomain.access == SymbolAccessibility.PRIVATE) {
edomain.access = SymbolAccessibility.INTERNAL;
}
+ if (edomain.owner == null) {
+ edomain.source_reference.file.add_node (edomain);
+ }
+
error_domains.add (edomain);
scope.add (edomain.name, edomain);
}
*
* @param d a delegate
*/
- public void add_delegate (Delegate d) {
+ public override void add_delegate (Delegate d) {
// namespaces do not support private memebers
if (d.access == SymbolAccessibility.PRIVATE) {
d.access = SymbolAccessibility.INTERNAL;
}
+ if (d.owner == null) {
+ d.source_reference.file.add_node (d);
+ }
+
delegates.add (d);
scope.add (d.name, d);
}
*
* @param constant a constant
*/
- public void add_constant (Constant constant) {
+ public override void add_constant (Constant constant) {
// namespaces do not support private memebers
if (constant.access == SymbolAccessibility.PRIVATE) {
constant.access = SymbolAccessibility.INTERNAL;
}
+ if (constant.owner == null) {
+ constant.source_reference.file.add_node (constant);
+ }
+
constants.add (constant);
scope.add (constant.name, constant);
}
*
* @param f a field
*/
- public void add_field (Field f) {
+ public override void add_field (Field f) {
+ if (f.binding == MemberBinding.INSTANCE) {
+ // default to static member binding
+ f.binding = MemberBinding.STATIC;
+ }
+
// namespaces do not support private memebers
if (f.access == SymbolAccessibility.PRIVATE) {
f.access = SymbolAccessibility.INTERNAL;
return;
}
+ if (f.owner == null) {
+ f.source_reference.file.add_node (f);
+ }
+
fields.add (f);
scope.add (f.name, f);
}
*
* @param m a method
*/
- public void add_method (Method m) {
+ public override void add_method (Method m) {
+ if (m.binding == MemberBinding.INSTANCE) {
+ // default to static member binding
+ m.binding = MemberBinding.STATIC;
+ }
+
// namespaces do not support private memebers
if (m.access == SymbolAccessibility.PRIVATE) {
m.access = SymbolAccessibility.INTERNAL;
m.result_var.is_result = true;
}
+ if (m.owner == null) {
+ m.source_reference.file.add_node (m);
+ }
+
methods.add (m);
scope.add (m.name, m);
}
}
}
- Method parse_main_block () throws ParseError {
+ void parse_main_block (Symbol parent) throws ParseError {
var begin = get_location ();
var method = new Method ("main", new VoidType (), get_src (begin));
Report.warning (method.source_reference, "main blocks are experimental");
}
- return method;
+ parent.add_method (method);
}
- Symbol parse_declaration (bool root = false) throws ParseError {
+ void parse_declaration (Symbol parent, bool root = false) throws ParseError {
comment = scanner.pop_comment ();
var attrs = parse_attributes ();
case TokenType.CONSTRUCT:
if (context.profile == Profile.GOBJECT) {
rollback (begin);
- return parse_constructor_declaration (attrs);
+ parse_constructor_declaration (parent, attrs);
+ return;
}
break;
case TokenType.TILDE:
rollback (begin);
- return parse_destructor_declaration (attrs);
+ parse_destructor_declaration (parent, attrs);
+ return;
case TokenType.OPEN_BRACE:
case TokenType.SEMICOLON:
case TokenType.IF:
throw new ParseError.SYNTAX (get_error ("statements outside blocks allowed only in root namespace"));
}
rollback (begin);
- return parse_main_block ();
+ parse_main_block (parent);
+ return;
default:
if (root) {
bool is_expr = is_expression ();
if (is_expr) {
rollback (begin);
- return parse_main_block ();
+ parse_main_block (parent);
+ return;
}
}
case TokenType.COLON:
rollback (begin);
switch (last_keyword) {
- case TokenType.CLASS: return parse_class_declaration (attrs);
- case TokenType.ENUM: return parse_enum_declaration (attrs);
- case TokenType.ERRORDOMAIN: return parse_errordomain_declaration (attrs);
- case TokenType.INTERFACE: return parse_interface_declaration (attrs);
- case TokenType.NAMESPACE: return parse_namespace_declaration (attrs);
- case TokenType.STRUCT: return parse_struct_declaration (attrs);
- default: break;
+ case TokenType.CLASS:
+ parse_class_declaration (parent, attrs);
+ return;
+ case TokenType.ENUM:
+ parse_enum_declaration (parent, attrs);
+ return;
+ case TokenType.ERRORDOMAIN:
+ parse_errordomain_declaration (parent, attrs);
+ return;
+ case TokenType.INTERFACE:
+ parse_interface_declaration (parent, attrs);
+ return;
+ case TokenType.NAMESPACE:
+ parse_namespace_declaration (parent, attrs);
+ return;
+ case TokenType.STRUCT:
+ parse_struct_declaration (parent, attrs);
+ return;
+ default:
+ break;
}
break;
case TokenType.OPEN_PARENS:
rollback (begin);
- return parse_creation_method_declaration (attrs);
+ parse_creation_method_declaration (parent, attrs);
+ return;
default:
skip_type (); // might contain type parameter list
switch (current ()) {
case TokenType.OPEN_PARENS:
rollback (begin);
switch (last_keyword) {
- case TokenType.DELEGATE: return parse_delegate_declaration (attrs);
- case TokenType.SIGNAL: return parse_signal_declaration (attrs);
- default: return parse_method_declaration (attrs);
+ case TokenType.DELEGATE:
+ parse_delegate_declaration (parent, attrs);
+ return;
+ case TokenType.SIGNAL:
+ parse_signal_declaration (parent, attrs);
+ return;
+ default:
+ parse_method_declaration (parent, attrs);
+ return;
}
case TokenType.ASSIGN:
case TokenType.SEMICOLON:
rollback (begin);
switch (last_keyword) {
- case TokenType.CONST: return parse_constant_declaration (attrs);
- default: return parse_field_declaration (attrs);
+ case TokenType.CONST:
+ parse_constant_declaration (parent, attrs);
+ return;
+ default:
+ parse_field_declaration (parent, attrs);
+ return;
}
case TokenType.OPEN_BRACE:
case TokenType.THROWS:
rollback (begin);
- return parse_property_declaration (attrs);
+ parse_property_declaration (parent, attrs);
+ return;
default:
break;
}
}
while (current () != TokenType.CLOSE_BRACE && current () != TokenType.EOF) {
try {
- if (parent is Namespace) {
- parse_namespace_member ((Namespace) parent, (parent == context.root));
- } else if (parent is Class) {
- parse_class_member ((Class) parent);
- } else if (parent is Struct) {
- parse_struct_member ((Struct) parent);
- } else if (parent is Interface) {
- parse_interface_member ((Interface) parent);
- }
+ parse_declaration (parent, (parent == context.root));
} catch (ParseError e) {
int r;
do {
return RecoveryState.EOF;
}
- Namespace parse_namespace_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_namespace_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
expect (TokenType.NAMESPACE);
var sym = parse_symbol_name ();
}
}
- Namespace result = ns;
- while (sym.inner != null) {
+ Symbol result = ns;
+ while (sym != null) {
sym = sym.inner;
- ns = new Namespace (sym.name, result.source_reference);
- ns.add_namespace ((Namespace) result);
- result = ns;
- }
- return result;
- }
-
- void parse_namespace_member (Namespace ns, bool root = false) throws ParseError {
- var sym = parse_declaration (root);
-
- if (sym is Namespace) {
- ns.add_namespace ((Namespace) sym);
- } else if (sym is Class) {
- ns.add_class ((Class) sym);
- } else if (sym is Interface) {
- ns.add_interface ((Interface) sym);
- } else if (sym is Struct) {
- ns.add_struct ((Struct) sym);
- } else if (sym is Enum) {
- ns.add_enum ((Enum) sym);
- } else if (sym is ErrorDomain) {
- ns.add_error_domain ((ErrorDomain) sym);
- } else if (sym is Delegate) {
- ns.add_delegate ((Delegate) sym);
- } else if (sym is Method) {
- var method = (Method) sym;
- if (method.binding == MemberBinding.INSTANCE) {
- // default to static member binding
- method.binding = MemberBinding.STATIC;
- }
- ns.add_method (method);
- } else if (sym is Field) {
- var field = (Field) sym;
- if (field.binding == MemberBinding.INSTANCE) {
- // default to static member binding
- field.binding = MemberBinding.STATIC;
- }
- ns.add_field (field);
- } else if (sym is Constant) {
- ns.add_constant ((Constant) sym);
- } else {
- Report.error (sym.source_reference, "unexpected declaration in namespace");
+
+ Symbol next = (sym != null ? new Namespace (sym.name, ns.source_reference) : parent);
+ next.add_namespace ((Namespace) result);
+ result = next;
}
- scanner.source_file.add_node (sym);
}
void parse_using_directives (Namespace ns) throws ParseError {
}
}
- Symbol parse_class_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_class_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_type_declaration_modifiers ();
}
Symbol result = cl;
- while (sym.inner != null) {
+ while (sym != null) {
sym = sym.inner;
- var ns = new Namespace (sym.name, cl.source_reference);
+
+ Symbol next = (sym != null ? new Namespace (sym.name, cl.source_reference) : parent);
if (result is Namespace) {
- ns.add_namespace ((Namespace) result);
- } else {
- ns.add_class ((Class) result);
- scanner.source_file.add_node (result);
- }
- result = ns;
- }
- return result;
- }
-
- void parse_class_member (Class cl) throws ParseError {
- var sym = parse_declaration ();
- if (sym is Class) {
- cl.add_class ((Class) sym);
- } else if (sym is Struct) {
- cl.add_struct ((Struct) sym);
- } else if (sym is Enum) {
- cl.add_enum ((Enum) sym);
- } else if (sym is Delegate) {
- cl.add_delegate ((Delegate) sym);
- } else if (sym is Method) {
- cl.add_method ((Method) sym);
- } else if (sym is Signal) {
- cl.add_signal ((Signal) sym);
- } else if (sym is Field) {
- cl.add_field ((Field) sym);
- } else if (sym is Constant) {
- cl.add_constant ((Constant) sym);
- } else if (sym is Property) {
- cl.add_property ((Property) sym);
- } else if (sym is Constructor) {
- var c = (Constructor) sym;
- if (c.binding == MemberBinding.INSTANCE) {
- if (cl.constructor != null) {
- Report.error (c.source_reference, "class already contains a constructor");
- }
- cl.constructor = c;
- } else if (c.binding == MemberBinding.CLASS) {
- if (cl.class_constructor != null) {
- Report.error (c.source_reference, "class already contains a class constructor");
- }
- cl.class_constructor = c;
- } else {
- if (cl.static_constructor != null) {
- Report.error (c.source_reference, "class already contains a static constructor");
- }
- cl.static_constructor = c;
- }
- } else if (sym is Destructor) {
- var d = (Destructor) sym;
- if (d.binding == MemberBinding.STATIC) {
- if (cl.static_destructor != null) {
- Report.error (d.source_reference, "class already contains a static destructor");
- }
- cl.static_destructor = (Destructor) d;
- } else if (d.binding == MemberBinding.CLASS) {
- if (cl.class_destructor != null) {
- Report.error (d.source_reference, "class already contains a class destructor");
- }
- cl.class_destructor = (Destructor) d;
+ next.add_namespace ((Namespace) result);
} else {
- if (cl.destructor != null) {
- Report.error (d.source_reference, "class already contains a destructor");
- }
- cl.destructor = (Destructor) d;
+ next.add_class ((Class) result);
}
- } else {
- Report.error (sym.source_reference, "unexpected declaration in class");
+ result = next;
}
}
- Constant parse_constant_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_constant_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
c.hides = true;
}
set_attributes (c, attrs);
- return c;
+
+ parent.add_constant (c);
}
- Field parse_field_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_field_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
f.initializer = parse_expression ();
}
expect (TokenType.SEMICOLON);
- return f;
+
+ parent.add_field (f);
}
InitializerList parse_initializer () throws ParseError {
return map;
}
- Method parse_method_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_method_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
} else if (scanner.source_file.external_package) {
method.external = true;
}
- return method;
+
+ parent.add_method (method);
}
- Property parse_property_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_property_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
}
}
- return prop;
+ parent.add_property (prop);
}
- Signal parse_signal_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_signal_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
sig.body = parse_block ();
}
- return sig;
+ parent.add_signal (sig);
}
- Constructor parse_constructor_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_constructor_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var flags = parse_member_declaration_modifiers ();
expect (TokenType.CONSTRUCT);
c.binding = MemberBinding.CLASS;
}
c.body = parse_block ();
- return c;
+
+ parent.add_constructor (c);
}
- Destructor parse_destructor_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_destructor_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var flags = parse_member_declaration_modifiers ();
expect (TokenType.TILDE);
d.binding = MemberBinding.CLASS;
}
d.body = parse_block ();
- return d;
+
+ parent.add_destructor (d);
}
- Symbol parse_struct_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_struct_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_type_declaration_modifiers ();
parse_declarations (st);
Symbol result = st;
- while (sym.inner != null) {
+ while (sym != null) {
sym = sym.inner;
- var ns = new Namespace (sym.name, st.source_reference);
+ Symbol next = (sym != null ? new Namespace (sym.name, st.source_reference) : parent);
if (result is Namespace) {
- ns.add_namespace ((Namespace) result);
+ next.add_namespace ((Namespace) result);
} else {
- ns.add_struct ((Struct) result);
- scanner.source_file.add_node (result);
+ next.add_struct ((Struct) result);
}
- result = ns;
+ result = next;
}
- return result;
}
- void parse_struct_member (Struct st) throws ParseError {
- var sym = parse_declaration ();
- if (sym is Method) {
- st.add_method ((Method) sym);
- } else if (sym is Field) {
- st.add_field ((Field) sym);
- } else if (sym is Constant) {
- st.add_constant ((Constant) sym);
- } else if (sym is Property) {
- st.add_property ((Property) sym);
- } else {
- Report.error (sym.source_reference, "unexpected declaration in struct");
- }
- }
-
- Symbol parse_interface_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_interface_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_type_declaration_modifiers ();
parse_declarations (iface);
Symbol result = iface;
- while (sym.inner != null) {
+ while (sym != null) {
sym = sym.inner;
- var ns = new Namespace (sym.name, iface.source_reference);
+
+ Symbol next = (sym != null ? new Namespace (sym.name, iface.source_reference) : parent);
if (result is Namespace) {
- ns.add_namespace ((Namespace) result);
+ next.add_namespace ((Namespace) result);
} else {
- ns.add_interface ((Interface) result);
- scanner.source_file.add_node (result);
- }
- result = ns;
- }
- return result;
- }
-
- void parse_interface_member (Interface iface) throws ParseError {
- var sym = parse_declaration ();
- if (sym is Class) {
- iface.add_class ((Class) sym);
- } else if (sym is Struct) {
- iface.add_struct ((Struct) sym);
- } else if (sym is Enum) {
- iface.add_enum ((Enum) sym);
- } else if (sym is Delegate) {
- iface.add_delegate ((Delegate) sym);
- } else if (sym is Method) {
- iface.add_method ((Method) sym);
- } else if (sym is Signal) {
- iface.add_signal ((Signal) sym);
- } else if (sym is Field) {
- iface.add_field ((Field) sym);
- } else if (sym is Constant) {
- iface.add_constant ((Constant) sym);
- } else if (sym is Property) {
- iface.add_property ((Property) sym);
- } else {
- Report.error (sym.source_reference, "unexpected declaration in interface");
+ next.add_interface ((Interface) result);
+ }
+ result = next;
}
}
- Symbol parse_enum_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_enum_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_type_declaration_modifiers ();
if (accept (TokenType.SEMICOLON)) {
// enum methods
while (current () != TokenType.CLOSE_BRACE) {
- var member_sym = parse_declaration ();
- if (member_sym is Method) {
- en.add_method ((Method) member_sym);
- } else if (member_sym is Constant) {
- en.add_constant ((Constant) member_sym);
- } else {
- Report.error (member_sym.source_reference, "unexpected declaration in enum");
- }
+ parse_declaration (en);
}
}
expect (TokenType.CLOSE_BRACE);
Symbol result = en;
- while (sym.inner != null) {
+ while (sym != null) {
sym = sym.inner;
- var ns = new Namespace (sym.name, en.source_reference);
+
+ Symbol next = (sym != null ? new Namespace (sym.name, en.source_reference) : parent);
if (result is Namespace) {
- ns.add_namespace ((Namespace) result);
+ next.add_namespace ((Namespace) result);
} else {
- ns.add_enum ((Enum) result);
- scanner.source_file.add_node (result);
+ next.add_enum ((Enum) result);
}
- result = ns;
+ result = next;
}
- return result;
}
- Symbol parse_errordomain_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_errordomain_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_type_declaration_modifiers ();
if (accept (TokenType.SEMICOLON)) {
// errordomain methods
while (current () != TokenType.CLOSE_BRACE) {
- var member_sym = parse_declaration ();
- if (member_sym is Method) {
- ed.add_method ((Method) member_sym);
- } else {
- Report.error (member_sym.source_reference, "unexpected declaration in errordomain");
- }
+ parse_declaration (ed);
}
}
expect (TokenType.CLOSE_BRACE);
Symbol result = ed;
- while (sym.inner != null) {
+ while (sym != null) {
sym = sym.inner;
- var ns = new Namespace (sym.name, ed.source_reference);
+
+ Symbol next = (sym != null ? new Namespace (sym.name, ed.source_reference) : parent);
if (result is Namespace) {
- ns.add_namespace ((Namespace) result);
+ next.add_namespace ((Namespace) result);
} else {
- ns.add_error_domain ((ErrorDomain) result);
- scanner.source_file.add_node (result);
+ next.add_error_domain ((ErrorDomain) result);
}
- result = ns;
+ result = next;
}
- return result;
}
SymbolAccessibility parse_access_modifier (SymbolAccessibility default_access = SymbolAccessibility.PRIVATE) {
return param;
}
- CreationMethod parse_creation_method_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_creation_method_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
} else if (scanner.source_file.external_package) {
method.external = true;
}
- return method;
+
+ parent.add_method (method);
}
- Symbol parse_delegate_declaration (List<Attribute>? attrs) throws ParseError {
+ void parse_delegate_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
var begin = get_location ();
var access = parse_access_modifier ();
var flags = parse_member_declaration_modifiers ();
expect (TokenType.SEMICOLON);
Symbol result = d;
- while (sym.inner != null) {
+ while (sym != null) {
sym = sym.inner;
- var ns = new Namespace (sym.name, d.source_reference);
+
+ Symbol next = (sym != null ? new Namespace (sym.name, d.source_reference) : parent);
if (result is Namespace) {
- ns.add_namespace ((Namespace) result);
+ next.add_namespace ((Namespace) result);
} else {
- ns.add_delegate ((Delegate) result);
- scanner.source_file.add_node (result);
+ next.add_delegate ((Delegate) result);
}
- result = ns;
+ result = next;
}
- return result;
}
List<TypeParameter> parse_type_parameter_list () throws ParseError {