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
|
/* $Id$
* LookupTypes: find out the type of an AST node.
*
* Copyright (C) 2008-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.
*/
#ifndef __LOOKUP_TYPES_HPP_INCLUDED
#define __LOOKUP_TYPES_HPP_INCLUDED
#include "frontend/visitor/NullVisitor.hpp"
#include "frontend/ast/AttributeDeclaration.hpp"
#include "frontend/ast/UnconstrainedArrayType.hpp"
#include <cassert>
namespace ast {
/** class to determine the type of a symbol, inline implementation.
*/
class LookupTypes : public NullVisitor {
public:
//! c'tor
/** @param doBaseLookups stop lookup at normal type or traverse to
* base of the type?
* @param doElementLookups also cross the boundary from Arrays to
* array elements?
*/
LookupTypes(
bool doBaseLookups,
bool doElementLookups
) : declaration(NULL),
baseLookup(doBaseLookups),
elementLookup(doElementLookups) {}
/** determined declaration, NULL if no type is known */
const TypeDeclaration *declaration;
private:
/** The following SymbolDeclarations are "not for us".
*/
virtual void visit(Library &node) {}
virtual void process(SeqStat &node) {}
virtual void process(ConcurrentStat &node) {}
virtual void process(Callable &node) {}
virtual void process(LibUnit &node) {}
/** Visit an UnconstrainedArrayType node.
* @param node UnconstrainedArrayType node that get's visited.
*/
virtual void visit(UnconstrainedArrayType &node) {
if (this->elementLookup) {
assert(node.elementType != NULL);
node.elementType->accept(*this);
} else {
this->process(node);
}
}
/** visit a TypeDeclaration
* @param node the TypeDeclaration itself.
*/
virtual void process(TypeDeclaration &node) {
this->declaration = &node;
}
//! visit a RecordTypeElement
/** @param node RecordTypeElement */
virtual void visit(RecordTypeElement& node) {
assert(node.subtype);
if (this->baseLookup) {
node.subtype->accept(*this);
} else {
this->declaration = node.subtype;
}
}
//! process a ValDeclaration
/** @param node ValDeclaration */
virtual void process(ValDeclaration &node) {
assert(node.subtypeIndic);
if (this->baseLookup) {
node.subtypeIndic->accept(*this);
} else {
this->declaration = node.subtypeIndic;
}
}
/** visit a PhysicalTypeUnit.
* SimpleNames on the right hand side can mean a physical
* literal w.o. number.
* @param node PhysicalTypeUnit.
*/
virtual void visit(PhysicalTypeUnit &node) {
// already at base type
this->declaration = node.parent;
assert(this->declaration != NULL);
}
/** visit a SubtypeIndication
* @param node SubtypeIndication which is the type itself.
*/
virtual void visit(SubtypeIndication &node) {
if (this->baseLookup) {
// ugly const cast, nevermind though, since
// the final const will be preserved.
TypeDeclaration *t =
const_cast<TypeDeclaration*>(
node.declaration);
t->accept(*this);
} else {
this->declaration = &node;
}
}
/** visit a FunctionDeclaration
* @param node FunctionDeclaration, take result type.
*/
virtual void visit(FunctionDeclaration &node) {
assert(node.returnType);
if (this->baseLookup) {
node.returnType->accept(*this);
} else {
this->declaration = node.returnType;
}
}
virtual void visit(AttributeDeclaration &node) {
assert(node.type != NULL);
if (this->baseLookup) {
TypeDeclaration *t =
const_cast<TypeDeclaration*>(node.type);
t->accept(*this);
} else {
this->declaration = node.type;
}
}
//! process a SymbolDeclaration
/** @param node SymbolDeclaration */
virtual void process(SymbolDeclaration &node) {
// must not happen, coming from a non-type SymbolDecl.
assert(false);
}
/** lookup the base type, or is a SubtypeIndication enough? */
bool baseLookup;
/** lookup spans to element types of Array types as well? */
bool elementLookup;
};
}; /* namespace ast */
#endif /* __LOOKUP_TYPES_HPP_INCLUDED */
|