1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/* $Id$
*
* DeclarativeRegion: One pocket to store symbols. Can be chained together.
*
* Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
* This program is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License, either version 2 of
* the License, or (at your option) any later version. See COPYING.
*/
#include <cassert>
#include <iostream>
#include "frontend/misc/DeclarativeRegion.hpp"
#include "frontend/reporting/DuplicateName.hpp"
#include "frontend/reporting/ErrorRegistry.hpp"
#define DEBUG 0
#define HEAVY_DEBUG 0
namespace ast {
DeclarativeRegion::DeclarativeRegion(
DeclarativeRegion *p
) : parent(p)
{
}
DeclarativeRegion::~DeclarativeRegion()
{
for (std::list<Symbol*>::const_iterator i =
this->declarations.begin();
i != this->declarations.end(); i++) {
if ((*i)->region) {
delete (*i)->region;
}
delete *i;
}
}
void
DeclarativeRegion::registerSymbol(Symbol *sym)
{
//check for homographs.
const Symbol *dup = this->containsHomograph(*sym);
if (dup != NULL) {
DuplicateName *dn = new DuplicateName(
sym->declaration,
dup->declaration,
*sym->name);
ErrorRegistry::addError(dn);
return;
}
this->declarations.push_back(sym);
}
const Symbol*
DeclarativeRegion::containsHomograph(const Symbol &sym) const
{
for (std::list<Symbol*>::const_iterator i =
this->declarations.begin(); i != this->declarations.end();
i++) {
if ((*i)->isHomograph(sym)){
return *i;
}
}
return NULL;
}
bool
DeclarativeRegion::isEnclosingSymbol(const Symbol &sym) const
{
if (sym.region == NULL) {
return false;
}
if (sym.region == this) {
return true;
}
if (this->parent != NULL) {
return this->parent->isEnclosingSymbol(sym);
}
return false;
}
std::list<Symbol*>
DeclarativeRegion::lookup(const std::string &name) const
{
std::list<Symbol*> result;
#if DEBUG
std::cerr << "STARTING LOOKUP for " << name << std::endl;
#endif
this->lookupRec(name, result);
return result;
}
std::list<Symbol*>
DeclarativeRegion::lookupLocal(const std::string &name) const
{
std::list<Symbol*> result = std::list<Symbol*>();
for (std::list<Symbol*>::const_iterator i =
this->declarations.begin();
i != this->declarations.end(); i++) {
if ((*(*i)->name) == name) {
result.push_back(*i);
}
}
return result;
}
void
DeclarativeRegion::lookupRec(
const std::string &name,
std::list<Symbol*> &candidates
) const
{
// lookup in local region
for (std::list<Symbol*>::const_iterator i =
this->declarations.begin();
i != this->declarations.end(); i++) {
#if HEAVY_DEBUG /* causes extreme debug output */
std::cerr << " c>" << (*(*i)->name) << std::endl;
#endif /* HEAVY_DEBUG */
if ((*(*i)->name) == name) {
// check visibility
assert(*i);
if (this->isVisible(**i, candidates)) {
candidates.push_back(*i);
}
}
}
//lookup in imported symbols
for (std::list<Symbol*>::const_iterator i =
this->imports.begin();
i != this->imports.end(); i++) {
#if HEAVY_DEBUG /* causes extreme debug output */
std::cerr << " i>" << (*(*i)->name) << std::endl;
#endif /* HEAVY_DEBUG */
if ((*(*i)->name) == name) {
// check visibility
assert(*i);
if (this->isVisible(**i, candidates)) {
candidates.push_back(*i);
}
}
}
// traverse to parent Declarative Region.
if (this->parent) {
#if 0 /* not useful w.o. above output */
std::cerr << " going up." << std::endl;
#endif /* 0 */
this->parent->lookupRec(name, candidates);
}
}
bool
DeclarativeRegion::isVisible(
const Symbol &sym,
const std::list<Symbol*> &check
) const
{
for (std::list<Symbol*>::const_iterator i = check.begin();
i != check.end();
i++) {
assert(*i);
if (sym.isHomograph(**i)) {
return false;
}
}
return true;
}
}; /* namespace ast */
|