Commit efb72750 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

move namespaces from SourceFile to CodeContext

2007-07-17  Juerg Billeter  <j@bitron.ch>

	* vala/parser.y, vala/valaattributeprocessor.vala,
	  vala/valacodecontext.vala, vala/valadatatype.vala,
	  vala/valainterfacewriter.vala, vala/valamemorymanager.vala,
	  vala/valanamespace.vala, vala/valasemanticanalyzer.vala,
	  vala/valasourcefile.vala, vala/valasymbolbuilder.vala,
	  vala/valasymbolresolver.vala, gobject/valacodegenerator.vala,
	  gobject/valacodegeneratorsourcefile.vala, vapigen/valagidlparser.vala,
	  vapigen/valavapigen.vala: move namespaces from SourceFile to
	  CodeContext

svn path=/trunk/; revision=360
parent 7fe2912a
2007-07-17 Jürg Billeter <j@bitron.ch>
* vala/parser.y, vala/valaattributeprocessor.vala,
vala/valacodecontext.vala, vala/valadatatype.vala,
vala/valainterfacewriter.vala, vala/valamemorymanager.vala,
vala/valanamespace.vala, vala/valasemanticanalyzer.vala,
vala/valasourcefile.vala, vala/valasymbolbuilder.vala,
vala/valasymbolresolver.vala, gobject/valacodegenerator.vala,
gobject/valacodegeneratorsourcefile.vala, vapigen/valagidlparser.vala,
vapigen/valavapigen.vala: move namespaces from SourceFile to
CodeContext
2007-07-17 Jürg Billeter <j@bitron.ch>
* vapigen/valagidlparser.vala: generate more ellipsis parameters
......
......@@ -262,10 +262,6 @@ public class Vala.CodeGenerator : CodeVisitor {
}
}
public override void visit_namespace (Namespace! ns) {
ns.accept_children (this);
}
public override void visit_enum (Enum! en) {
cenum = new CCodeEnum (en.get_cname ());
......
......@@ -81,19 +81,16 @@ public class Vala.CodeGenerator {
}
if (source_file.is_cycle_head) {
foreach (SourceFile cycle_file in source_file.cycle.files) {
var namespaces = cycle_file.get_namespaces ();
foreach (Namespace ns in namespaces) {
var structs = ns.get_structs ();
foreach (Struct st in structs) {
foreach (CodeNode node in cycle_file.get_nodes ()) {
if (node is Struct) {
var st = (Struct) node;
header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (st.get_cname ()), new CCodeVariableDeclarator (st.get_cname ())));
}
var classes = ns.get_classes ();
foreach (Class cl in classes) {
} else if (node is Class) {
var cl = (Class) node;
header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (cl.get_cname ()), new CCodeVariableDeclarator (cl.get_cname ())));
header_type_declaration.append (new CCodeTypeDefinition ("struct _%sClass".printf (cl.get_cname ()), new CCodeVariableDeclarator ("%sClass".printf (cl.get_cname ()))));
}
var ifaces = ns.get_interfaces ();
foreach (Interface iface in ifaces) {
} else if (node is Interface) {
var iface = (Interface) node;
header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (iface.get_cname ()), new CCodeVariableDeclarator (iface.get_cname ())));
header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (iface.get_type_cname ()), new CCodeVariableDeclarator (iface.get_type_cname ())));
}
......
......@@ -2035,7 +2035,7 @@ namespace_declaration
namespace_body
{
$$ = current_namespace;
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
}
;
......@@ -2094,7 +2094,7 @@ outer_declarations
outer_declaration
: namespace_declaration
{
vala_source_file_add_namespace (current_source_file, $1);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), $1);
g_object_unref ($1);
}
| namespace_member_declaration
......@@ -2116,12 +2116,13 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_class (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
if (current_namespace_implicit) {
/* current namespace has been declared implicitly */
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
current_namespace_implicit = FALSE;
}
}
......@@ -2130,12 +2131,13 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_struct (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
if (current_namespace_implicit) {
/* current namespace has been declared implicitly */
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
current_namespace_implicit = FALSE;
}
}
......@@ -2144,12 +2146,13 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_interface (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
if (current_namespace_implicit) {
/* current namespace has been declared implicitly */
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
current_namespace_implicit = FALSE;
}
}
......@@ -2158,12 +2161,13 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_enum (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
if (current_namespace_implicit) {
/* current namespace has been declared implicitly */
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
current_namespace_implicit = FALSE;
}
}
......@@ -2172,12 +2176,13 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_flags (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
if (current_namespace_implicit) {
/* current namespace has been declared implicitly */
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
current_namespace_implicit = FALSE;
}
}
......@@ -2186,12 +2191,13 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_callback (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
if (current_namespace_implicit) {
/* current namespace has been declared implicitly */
current_namespace = vala_source_file_get_global_namespace (current_source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
current_namespace_implicit = FALSE;
}
}
......@@ -2200,6 +2206,7 @@ namespace_member_declaration
/* skip declarations with errors */
if ($1 != NULL) {
vala_namespace_add_constant (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
}
......@@ -2212,6 +2219,7 @@ namespace_member_declaration
vala_field_set_instance ($1, FALSE);
vala_namespace_add_field (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
}
......@@ -2224,6 +2232,7 @@ namespace_member_declaration
vala_method_set_instance ($1, FALSE);
vala_namespace_add_method (current_namespace, $1);
vala_source_file_add_node (current_source_file, VALA_CODE_NODE ($1));
g_object_unref ($1);
}
}
......@@ -2244,7 +2253,7 @@ class_declaration
g_object_unref (ns_src);
current_namespace_implicit = TRUE;
vala_source_file_add_namespace (current_source_file, current_namespace);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), current_namespace);
g_object_unref (current_namespace);
name = $7;
......@@ -2959,7 +2968,7 @@ struct_header
g_object_unref (ns_src);
current_namespace_implicit = TRUE;
vala_source_file_add_namespace (current_source_file, current_namespace);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), current_namespace);
g_object_unref (current_namespace);
name = $6;
......@@ -3032,7 +3041,7 @@ interface_declaration
g_object_unref (ns_src);
current_namespace_implicit = TRUE;
vala_source_file_add_namespace (current_source_file, current_namespace);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), current_namespace);
g_object_unref (current_namespace);
name = $7;
......@@ -3117,7 +3126,6 @@ interface_member_declaration
enum_declaration
: comment opt_attributes opt_access_modifier ENUM identifier opt_name_specifier
{
GList *l;
ValaSourceReference *src;
char *name = $5;
......@@ -3129,7 +3137,7 @@ enum_declaration
g_object_unref (ns_src);
current_namespace_implicit = TRUE;
vala_source_file_add_namespace (current_source_file, current_namespace);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), current_namespace);
g_object_unref (current_namespace);
name = $6;
......@@ -3221,7 +3229,7 @@ flags_declaration
g_object_unref (ns_src);
current_namespace_implicit = TRUE;
vala_source_file_add_namespace (current_source_file, current_namespace);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), current_namespace);
g_object_unref (current_namespace);
name = $6;
......@@ -3298,7 +3306,7 @@ callback_declaration
g_object_unref (ns_src);
current_namespace_implicit = TRUE;
vala_source_file_add_namespace (current_source_file, current_namespace);
vala_code_context_add_namespace (vala_source_file_get_context (current_source_file), current_namespace);
g_object_unref (current_namespace);
name = $8;
......@@ -3565,7 +3573,7 @@ void
vala_parser_parse_file (ValaParser *parser, ValaSourceFile *source_file)
{
current_source_file = source_file;
current_namespace = vala_source_file_get_global_namespace (source_file);
current_namespace = vala_code_context_get_global_namespace (vala_source_file_get_context (current_source_file));
yyin = fopen (vala_source_file_get_filename (current_source_file), "r");
if (yyin == NULL) {
printf ("Couldn't open source file: %s.\n", vala_source_file_get_filename (current_source_file));
......
......@@ -42,8 +42,6 @@ public class Vala.AttributeProcessor : CodeVisitor {
public override void visit_namespace (Namespace! ns) {
ns.process_attributes ();
ns.accept_children (this);
}
public override void visit_class (Class! cl) {
......
......@@ -86,6 +86,9 @@ public class Vala.CodeContext {
List<SourceFile> source_files;
private Symbol! root = new Symbol ();
private Namespace global_namespace = new Namespace (null);
private List<Namespace> namespaces;
List<SourceFileCycle> cycles;
......@@ -100,6 +103,33 @@ public class Vala.CodeContext {
return root;
}
/**
* Adds the specified namespace.
*
* @param ns a namespace
*/
public void add_namespace (Namespace! ns) {
namespaces.append (ns);
}
/**
* Returns the implicitly declared root namespace.
*
* @return root namespace
*/
public Namespace! get_global_namespace () {
return global_namespace;
}
/**
* Returns a copy of the list of namespaces.
*
* @return namespace list
*/
public List<weak Namespace> get_namespaces () {
return namespaces.copy ();
}
/**
* Returns a copy of the list of source files.
*
......@@ -152,6 +182,12 @@ public class Vala.CodeContext {
* @param visitor the visitor to be called when traversing
*/
public void accept (CodeVisitor! visitor) {
global_namespace.accept (visitor);
foreach (Namespace ns in namespaces) {
ns.accept (visitor);
}
foreach (SourceFile file in source_files) {
file.accept (visitor);
}
......
......@@ -224,6 +224,11 @@ public abstract class Vala.DataType : CodeNode {
foreach (string filename in @namespace.get_cheader_filenames ()) {
add_cheader_filename (filename);
}
if (cheader_filenames == null && source_reference != null && !source_reference.file.pkg) {
// don't add default include directives for VAPI files
cheader_filenames.append (source_reference.file.get_cinclude_filename ());
}
}
return cheader_filenames.copy ();
}
......
......@@ -48,22 +48,17 @@ public class Vala.InterfaceWriter : CodeVisitor {
this.context = context;
stream = FileStream.open (filename, "w");
/* we're only interested in non-pkg source files */
foreach (SourceFile file in context.get_source_files ()) {
if (!file.pkg) {
file.accept (this);
}
}
context.accept (this);
stream = null;
}
public override void visit_source_file (SourceFile! source_file) {
source_file.accept_children (this);
}
public override void visit_namespace (Namespace! ns) {
if (ns.source_reference != null && ns.source_reference.file.pkg) {
return;
}
if (ns.name == null) {
ns.accept_children (this);
return;
......@@ -87,6 +82,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_class (Class! cl) {
if (cl.source_reference != null && cl.source_reference.file.pkg) {
return;
}
if (cl.access == MemberAccessibility.PRIVATE) {
return;
}
......@@ -137,6 +136,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_struct (Struct! st) {
if (st.source_reference != null && st.source_reference.file.pkg) {
return;
}
if (st.access == MemberAccessibility.PRIVATE) {
return;
}
......@@ -158,6 +161,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_interface (Interface! iface) {
if (iface.source_reference != null && iface.source_reference.file.pkg) {
return;
}
if (iface.access == MemberAccessibility.PRIVATE) {
return;
}
......@@ -191,6 +198,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_enum (Enum! en) {
if (en.source_reference != null && en.source_reference.file.pkg) {
return;
}
if (en.access == MemberAccessibility.PRIVATE) {
return;
}
......@@ -217,6 +228,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_flags (Flags! fl) {
if (fl.source_reference != null && fl.source_reference.file.pkg) {
return;
}
if (fl.access == MemberAccessibility.PRIVATE) {
return;
}
......@@ -243,6 +258,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_constant (Constant! c) {
if (c.source_reference != null && c.source_reference.file.pkg) {
return;
}
write_indent ();
write_string ("public const ");
write_string (c.type_reference.data_type.symbol.get_full_name ());
......@@ -254,6 +273,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_field (Field! f) {
if (f.source_reference != null && f.source_reference.file.pkg) {
return;
}
if (f.access == MemberAccessibility.PRIVATE) {
return;
}
......@@ -341,6 +364,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
}
public override void visit_callback (Callback! cb) {
if (cb.source_reference != null && cb.source_reference.file.pkg) {
return;
}
if (cb.access == MemberAccessibility.PRIVATE) {
return;
}
......
......@@ -65,10 +65,6 @@ public class Vala.MemoryManager : CodeVisitor {
source_file.accept_children (this);
}
public override void visit_namespace (Namespace! ns) {
ns.accept_children (this);
}
public override void visit_class (Class! cl) {
cl.accept_children (this);
}
......
......@@ -328,7 +328,7 @@ public class Vala.Namespace : CodeNode {
*/
public List<weak string> get_cheader_filenames () {
if (cheader_filenames == null) {
if (!source_reference.file.pkg) {
if (source_reference != null && !source_reference.file.pkg) {
// don't add default include directives for VAPI files
cheader_filenames.append (source_reference.file.get_cinclude_filename ());
}
......
......@@ -117,14 +117,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
current_using_directives = null;
}
public override void visit_namespace (Namespace! ns) {
current_symbol = ns.symbol;
ns.accept_children (this);
current_symbol = current_symbol.parent_symbol;
}
public override void visit_class (Class! cl) {
current_symbol = cl.symbol;
current_class = cl;
......
......@@ -70,8 +70,7 @@ public class Vala.SourceFile {
private List<NamespaceReference> using_directives;
private Namespace global_namespace;
private List<Namespace> namespaces;
private List<CodeNode> nodes;
private string cheader_filename = null;
private string csource_filename = null;
......@@ -98,10 +97,6 @@ public class Vala.SourceFile {
pkg = _pkg;
}
construct {
global_namespace = new Namespace (null, new SourceReference (this));
}
/**
* Adds a new using directive with the specified namespace.
*
......@@ -121,30 +116,30 @@ public class Vala.SourceFile {
}
/**
* Adds the specified namespace to this source file.
* Adds the specified code node to this source file.
*
* @param ns a namespace
* @param node a code node
*/
public void add_namespace (Namespace! ns) {
namespaces.append (ns);
public void add_node (CodeNode! node) {
nodes.append (node);
}
/**
* Returns the implicitly declared root namespace of this source file.
* Removes the specified code node from this source file.
*
* @return root namespace
* @param node a code node
*/
public Namespace! get_global_namespace () {
return global_namespace;
public void remove_node (CodeNode! node) {
nodes.remove (node);
}
/**
* Returns a copy of the list of namespaces.
* Returns a copy of the list of code nodes.
*
* @return namespace list
* @return code node list
*/
public List<weak Namespace> get_namespaces () {
return namespaces.copy ();
public List<weak CodeNode> get_nodes () {
return nodes.copy ();
}
public void accept (CodeVisitor! visitor) {
......@@ -156,10 +151,8 @@ public class Vala.SourceFile {
ns_ref.accept (visitor);
}
global_namespace.accept (visitor);
foreach (Namespace ns in namespaces) {
ns.accept (visitor);
foreach (CodeNode node in nodes) {
node.accept (visitor);
}
}
......
......@@ -30,7 +30,6 @@ public class Vala.SymbolBuilder : CodeVisitor {
Symbol root;
Symbol current_type;
Symbol current_symbol;
SourceFile current_source_file;
/**
* Build the symbol tree for the specified code context.
......@@ -42,12 +41,6 @@ public class Vala.SymbolBuilder : CodeVisitor {
context.accept (this);
}
public override void visit_source_file (SourceFile! file) {
current_source_file = file;
file.accept_children (this);
}
public override void visit_namespace (Namespace! ns) {
if (ns.name == null) {
ns.symbol = root;
......@@ -140,6 +133,9 @@ public class Vala.SymbolBuilder : CodeVisitor {
if (cl.symbol == null) {
/* remove merged class */
cl.@namespace.remove_class (cl);
if (cl.source_reference != null) {
cl.source_reference.file.remove_node (cl);
}
}
}
......@@ -332,7 +328,7 @@ public class Vala.SymbolBuilder : CodeVisitor {
acc.symbol = new Symbol (acc);
acc.symbol.parent_symbol = current_symbol;
if (current_source_file.pkg) {
if (acc.source_reference.file.pkg) {
return;
}
......
......@@ -52,22 +52,13 @@ public class Vala.SymbolResolver : CodeVisitor {
public override void visit_source_file (SourceFile! file) {
current_using_directives = file.get_using_directives ();
current_scope = root_symbol;
file.accept_children (this);
current_using_directives = null;
}
public override void visit_namespace (Namespace! ns) {
current_scope = ns.symbol;
ns.accept_children (this);
// don't use current_scope.parent_symbol as that would be null
// if the current namespace is SourceFile.global_namespace
current_scope = root_symbol;
}
public override void visit_class (Class! cl) {
current_scope = cl.symbol;
......
......@@ -27,6 +27,8 @@ using GLib;
* Code visitor parsing all GIDL files.
*/
public class Vala.GIdlParser : CodeVisitor {
private CodeContext context;
private SourceReference current_source_reference;
private DataType current_data_type;
......@@ -40,6 +42,7 @@ public class Vala.GIdlParser : CodeVisitor {
* @param context a code context
*/
public void parse (CodeContext! context) {
this.context = context;
context.accept (this);
}
......@@ -80,7 +83,7 @@ public class Vala.GIdlParser : CodeVisitor {
foreach (IdlModule module in modules) {
var ns = parse_module (module);
source_file.add_namespace (ns);
context.add_namespace (ns);
}
} catch (MarkupError e) {
stdout.printf ("error parsing GIDL file: %s\n", e.message);
......
......@@ -32,7 +32,6 @@ class Vala.VAPIGen {
static string library;
[NoArrayLength ()]
static string[] packages;
static bool disable_memory_management;
CodeContext context;
const OptionEntry[] options = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment