2012-10-21 20:42:19 +02:00
|
|
|
/*
|
2021-01-02 23:04:06 +01:00
|
|
|
* Copyright (c) 2012-2021 Stephen Williams (steve@icarus.com)
|
2013-02-15 02:53:47 +01:00
|
|
|
* Copyright CERN 2013 Stephen Williams (steve@icarus.com)
|
2012-10-21 20:42:19 +02:00
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form under the terms of the GNU
|
|
|
|
|
* General Public License as published by the Free Software
|
|
|
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-12 04:31:35 +02:00
|
|
|
# include <cstdarg>
|
2012-10-21 20:42:19 +02:00
|
|
|
# include "pform.h"
|
|
|
|
|
# include "PPackage.h"
|
|
|
|
|
# include "parse_misc.h"
|
2013-02-15 02:53:47 +01:00
|
|
|
# include "parse_api.h"
|
|
|
|
|
# include <map>
|
|
|
|
|
# include <sstream>
|
2012-10-21 20:42:19 +02:00
|
|
|
# include "ivl_assert.h"
|
|
|
|
|
|
2013-02-15 02:53:47 +01:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
/*
|
2021-01-03 19:45:08 +01:00
|
|
|
* This is a list of packages in the order that they were defined.
|
2013-02-15 02:53:47 +01:00
|
|
|
*/
|
2021-01-03 19:45:08 +01:00
|
|
|
vector<PPackage*> pform_packages;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This allows us to easily check for name collisions.
|
|
|
|
|
*/
|
|
|
|
|
static map<perm_string,PPackage*> packages_by_name;
|
2013-02-15 02:53:47 +01:00
|
|
|
|
2012-10-21 20:42:19 +02:00
|
|
|
static PPackage*pform_cur_package = 0;
|
|
|
|
|
|
2016-03-19 18:27:27 +01:00
|
|
|
void pform_start_package_declaration(const struct vlltype&loc, const char*name,
|
|
|
|
|
LexicalScope::lifetime_t lifetime)
|
2012-10-21 20:42:19 +02:00
|
|
|
{
|
|
|
|
|
ivl_assert(loc, pform_cur_package == 0);
|
|
|
|
|
|
|
|
|
|
perm_string use_name = lex_strings.make(name);
|
2016-03-19 18:27:27 +01:00
|
|
|
PPackage*pkg_scope = pform_push_package_scope(loc, use_name, lifetime);
|
2013-02-15 02:53:47 +01:00
|
|
|
FILE_NAME(pkg_scope, loc);
|
2012-10-21 20:42:19 +02:00
|
|
|
pform_cur_package = pkg_scope;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pform_end_package_declaration(const struct vlltype&loc)
|
|
|
|
|
{
|
|
|
|
|
ivl_assert(loc, pform_cur_package);
|
2013-02-15 02:53:47 +01:00
|
|
|
perm_string use_name = pform_cur_package->pscope_name();
|
|
|
|
|
|
2021-01-03 19:45:08 +01:00
|
|
|
map<perm_string,PPackage*>::const_iterator test = packages_by_name.find(use_name);
|
|
|
|
|
if (test != packages_by_name.end()) {
|
2013-02-15 02:53:47 +01:00
|
|
|
ostringstream msg;
|
|
|
|
|
msg << "Package " << use_name << " was already declared here: "
|
|
|
|
|
<< test->second->get_fileline() << ends;
|
|
|
|
|
VLerror(msg.str().c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-03 19:45:08 +01:00
|
|
|
packages_by_name[use_name] = pform_cur_package;
|
|
|
|
|
pform_packages.push_back(pform_cur_package);
|
2012-10-21 20:42:19 +02:00
|
|
|
pform_cur_package = 0;
|
|
|
|
|
pform_pop_scope();
|
|
|
|
|
}
|
2013-02-15 02:53:47 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do the import early, during processing. This requires that the
|
|
|
|
|
* package is declared in pform ahead of time (it is) and that we can
|
|
|
|
|
* simply transfer definitions to the current scope (we can).
|
|
|
|
|
*/
|
2019-09-27 00:35:57 +02:00
|
|
|
void pform_package_import(const struct vlltype&loc, PPackage*pkg, const char*ident)
|
2013-02-15 02:53:47 +01:00
|
|
|
{
|
|
|
|
|
LexicalScope*scope = pform_peek_scope();
|
|
|
|
|
|
2013-02-15 03:25:10 +01:00
|
|
|
if (ident) {
|
|
|
|
|
perm_string use_ident = lex_strings.make(ident);
|
|
|
|
|
|
2019-09-27 00:35:57 +02:00
|
|
|
// Check that the requested symbol is available.
|
|
|
|
|
map<perm_string,PNamedItem*>::const_iterator cur_sym
|
|
|
|
|
= pkg->local_symbols.find(use_ident);
|
|
|
|
|
if (cur_sym == pkg->local_symbols.end()) {
|
|
|
|
|
cerr << loc.get_fileline() << ": error: "
|
|
|
|
|
"'" << use_ident << "' is not exported by '"
|
|
|
|
|
<< pkg->pscope_name() << "'." << endl;
|
|
|
|
|
error_count += 1;
|
2013-04-01 00:46:36 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 00:35:57 +02:00
|
|
|
// Check for conflict with local symbol.
|
|
|
|
|
cur_sym = scope->local_symbols.find(use_ident);
|
|
|
|
|
if (cur_sym != scope->local_symbols.end()) {
|
|
|
|
|
cerr << loc.get_fileline() << ": error: "
|
|
|
|
|
"'" << use_ident << "' has already been declared "
|
|
|
|
|
"in this scope." << endl;
|
|
|
|
|
cerr << cur_sym->second->get_fileline() << ": : "
|
|
|
|
|
"It was declared here as "
|
|
|
|
|
<< cur_sym->second->symbol_type() << "." << endl;
|
|
|
|
|
error_count += 1;
|
2013-04-01 00:46:36 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 00:35:57 +02:00
|
|
|
// Check for conflict with previous import.
|
|
|
|
|
map<perm_string,PPackage*>::const_iterator cur_pkg
|
|
|
|
|
= scope->explicit_imports.find(use_ident);
|
|
|
|
|
if (cur_pkg != scope->explicit_imports.end()) {
|
|
|
|
|
if (cur_pkg->second != pkg) {
|
|
|
|
|
cerr << loc.get_fileline() << ": error: "
|
|
|
|
|
"'" << use_ident << "' has already been "
|
|
|
|
|
"imported into this scope from package '"
|
|
|
|
|
<< cur_pkg->second->pscope_name() << "'." << endl;
|
|
|
|
|
error_count += 1;
|
|
|
|
|
}
|
2013-02-15 03:25:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2013-02-15 02:53:47 +01:00
|
|
|
|
2019-09-27 00:35:57 +02:00
|
|
|
scope->explicit_imports[use_ident] = pkg;
|
2013-02-15 03:25:10 +01:00
|
|
|
|
|
|
|
|
} else {
|
2021-07-31 19:36:18 +02:00
|
|
|
list<PPackage*>::const_iterator cur_pkg
|
|
|
|
|
= find(scope->potential_imports.begin(),
|
|
|
|
|
scope->potential_imports.end(),
|
|
|
|
|
pkg);
|
2019-09-27 00:35:57 +02:00
|
|
|
if (cur_pkg == scope->potential_imports.end())
|
2021-07-31 19:36:18 +02:00
|
|
|
scope->potential_imports.push_back(pkg);
|
2013-02-15 02:53:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-17 23:42:07 +01:00
|
|
|
|
|
|
|
|
PExpr* pform_package_ident(const struct vlltype&loc,
|
2013-04-08 01:38:48 +02:00
|
|
|
PPackage*pkg, pform_name_t*ident_name)
|
2013-04-01 00:46:36 +02:00
|
|
|
{
|
2013-04-08 01:38:48 +02:00
|
|
|
assert(ident_name);
|
|
|
|
|
PEIdent*tmp = new PEIdent(pkg, *ident_name);
|
2013-04-01 00:46:36 +02:00
|
|
|
FILE_NAME(tmp, loc);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-23 15:05:53 +02:00
|
|
|
typedef_t* pform_test_type_identifier(PPackage*pkg, const char*txt)
|
2013-04-01 00:46:36 +02:00
|
|
|
{
|
|
|
|
|
perm_string use_name = lex_strings.make(txt);
|
2022-04-23 15:05:53 +02:00
|
|
|
LexicalScope::typedef_map_t::const_iterator cur;
|
|
|
|
|
|
|
|
|
|
cur = pkg->typedefs.find(use_name);
|
2013-04-01 00:46:36 +02:00
|
|
|
if (cur != pkg->typedefs.end())
|
|
|
|
|
return cur->second;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2013-04-19 03:18:40 +02:00
|
|
|
* The lexor uses this function to know if the identifier names the
|
|
|
|
|
* package. It will call this a PACKAGE_IDENTIFIER token in that case,
|
|
|
|
|
* instead of a generic IDENTIFIER.
|
2013-04-01 00:46:36 +02:00
|
|
|
*/
|
|
|
|
|
PPackage* pform_test_package_identifier(const char*pkg_name)
|
2013-02-17 23:42:07 +01:00
|
|
|
{
|
|
|
|
|
perm_string use_name = lex_strings.make(pkg_name);
|
2021-01-03 19:45:08 +01:00
|
|
|
map<perm_string,PPackage*>::const_iterator pcur = packages_by_name.find(use_name);
|
|
|
|
|
if (pcur == packages_by_name.end())
|
2013-02-17 23:42:07 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
assert(pcur->second);
|
2013-04-01 00:46:36 +02:00
|
|
|
return pcur->second;
|
2013-02-17 23:42:07 +01:00
|
|
|
}
|