From 8fa79ceb30bb95d07557c00883949f20a0489e8c Mon Sep 17 00:00:00 2001 From: Stephen Williams Date: Sun, 17 Feb 2013 16:22:24 -0800 Subject: [PATCH] Properly implement import :: This was temporarily implemented to just copy definitions to the local scope, but the better method is to create a PEIdent that has the package attached to it. --- PScope.h | 5 +++++ parse.y | 12 ++++++------ pform.cc | 19 +++++++++++++++++++ pform.h | 6 ++++++ pform_package.cc | 4 ++-- t-dll.cc | 8 ++++---- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/PScope.h b/PScope.h index 8fe77cae8..fe2d3a711 100644 --- a/PScope.h +++ b/PScope.h @@ -28,6 +28,7 @@ class PEvent; class PExpr; class PFunction; +class PPackage; class AProcess; class PProcess; class PClass; @@ -92,6 +93,10 @@ class LexicalScope { // Named events in the scope. mapevents; + // Symbols that are imported. Bind the imported name to the + // package from which the name is imported. + std::mapimports; + // Nets and variables (wires) in the scope mapwires; PWire* wires_find(perm_string name); diff --git a/parse.y b/parse.y index 0b40b1e9b..c75cd90bb 100644 --- a/parse.y +++ b/parse.y @@ -2955,12 +2955,12 @@ expr_primary /* The hierarchy_identifier rule matches simple identifiers as well as indexed arrays and part selects */ - | hierarchy_identifier - { PEIdent*tmp = new PEIdent(*$1); - FILE_NAME(tmp, @1); - $$ = tmp; - delete $1; - } + | hierarchy_identifier + { PEIdent*tmp = pform_new_ident(*$1); + FILE_NAME(tmp, @1); + $$ = tmp; + delete $1; + } | IDENTIFIER K_SCOPE_RES IDENTIFIER { $$ = pform_package_ident(@3, $1, $3); } diff --git a/pform.cc b/pform.cc index 57379d8ca..bdb55e3f3 100644 --- a/pform.cc +++ b/pform.cc @@ -419,6 +419,25 @@ PBlock* pform_push_block_scope(char*name, PBlock::BL_TYPE bt) return block; } +/* + * Create a new identifier. Check if this is an imported name. + */ +PEIdent* pform_new_ident(const pform_name_t&name) +{ + if (name.size() != 1) + return new PEIdent(name); + + LexicalScope*scope = pform_peek_scope(); + map::const_iterator pkg = scope->imports.find(name.back().name); + if (pkg == scope->imports.end()) + return new PEIdent(name); + + // XXXX For now, do not support indexed imported names. + assert(name.back().index.size() == 0); + + return new PEIdent(pkg->second, name.back().name); +} + PGenerate* pform_parent_generate(void) { return pform_cur_generate; diff --git a/pform.h b/pform.h index b7d5ac702..0ea568bd2 100644 --- a/pform.h +++ b/pform.h @@ -210,6 +210,12 @@ extern void pform_package_import(const struct vlltype&loc, extern PExpr* pform_package_ident(const struct vlltype&loc, const char*pkg_name, const char*ident); +/* + * This creates an identifier aware of names that may have been + * imported from other packages. + */ +extern PEIdent* pform_new_ident(const pform_name_t&name); + /* * Enter/exit name scopes. The push_scope function pushes the scope * name string onto the scope hierarchy. The pop pulls it off and diff --git a/pform_package.cc b/pform_package.cc index 60d93b3dd..24cf52da2 100644 --- a/pform_package.cc +++ b/pform_package.cc @@ -98,7 +98,7 @@ void pform_package_import(const struct vlltype&, const char*pkg_name, const char return; } - scope->parameters[cur->first] = cur->second; + scope->imports[cur->first] = pkg; } else { @@ -107,7 +107,7 @@ void pform_package_import(const struct vlltype&, const char*pkg_name, const char for (map::const_iterator cur = pkg->parameters.begin() ; cur != pkg->parameters.end() ; ++cur) { - scope->parameters[cur->first] = cur->second; + scope->imports[cur->first] = pkg; } } } diff --git a/t-dll.cc b/t-dll.cc index 5d6bdeed4..662880b17 100644 --- a/t-dll.cc +++ b/t-dll.cc @@ -611,14 +611,14 @@ bool dll_target::start_design(const Design*des) assert(idx == des_.disciplines.size()); list package_scopes = des->find_package_scopes(); - for (list::const_iterator scop = package_scopes.begin(); - scop != package_scopes.end(); ++ scop ) { + for (list::const_iterator scop = package_scopes.begin() + ; scop != package_scopes.end(); ++ scop ) { add_root(des_, *scop); } list root_scopes = des->find_root_scopes(); - for (list::const_iterator scop = root_scopes.begin(); - scop != root_scopes.end(); ++ scop ) { + for (list::const_iterator scop = root_scopes.begin() + ; scop != root_scopes.end(); ++ scop ) { add_root(des_, *scop); }