00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef CFRONT_ENVIRONMENT_HPP
00035 #define CFRONT_ENVIRONMENT_HPP
00036
00037 #include <string>
00038 #include <map>
00039 #include <list>
00040 #include <algorithm>
00041
00042 #include <antlr/AST.hpp>
00043 #include "NameDescription.hpp"
00044 #include "Type.hpp"
00045
00046 #ifdef PARSER_DEBUG
00047 #include <sstream>
00048 #endif
00049
00050 namespace cfront {
00051
00056 typedef std::map<std::string, NameDescription> OrdinaryNames;
00057
00061 typedef std::map<std::string, NameDescription> TagNames;
00062
00066 class Environment {
00067 public:
00068 Environment(Environment* outer = NULL)
00069 : outer_(outer)
00070 {
00071 }
00072
00073 Environment* getOuterEnv() const
00074 {
00075 return outer_;
00076 }
00077
00088 bool addOrdinaryName(const std::string& name, Type* type,
00089 NameDescription::StorageClassSpecTag storageSpec,
00090 NameDescription::DeclarationLocation loc,
00091 NameDescription** nd)
00092 {
00093 OrdinaryNames::iterator i = ordinaryNames_.find(name);
00094 if (i != ordinaryNames_.end()) {
00095 NameDescription& tnd = i->second;
00096 Type* t = tnd.getType();
00097 if (t == NULL) {
00098
00099 tnd.setType(type);
00100 *nd = &tnd;
00101 return true;
00102 } else {
00103
00104 }
00105 return false;
00106 }
00107
00108 OrdinaryNames::value_type v =
00109 OrdinaryNames::value_type(name,
00110 NameDescription(type, storageSpec, loc));
00111 std::pair<OrdinaryNames::iterator, bool> p = ordinaryNames_.insert(v);
00112 *nd = &(p.first->second);
00113 return true;
00114 }
00115
00124 Environment* lookupOrdinaryName(const std::string& name,
00125 NameDescription** nd)
00126 {
00127 OrdinaryNames::iterator i = ordinaryNames_.find(name);
00128 if (i != ordinaryNames_.end()) {
00129 *nd = &(i->second);
00130 return this;
00131 } else {
00132 if (outer_ == NULL) {
00133 return NULL;
00134 } else {
00135 return outer_->lookupOrdinaryName(name, nd);
00136 }
00137 }
00138 }
00139
00143 bool addTagName(const std::string& name, Type* type,
00144 NameDescription::DeclarationLocation loc)
00145 {
00146 TagNames::iterator i = tagNames_.find(name);
00147 if (i != tagNames_.end()) {
00148 NameDescription& nd = i->second;
00149 Type* t = nd.getType();
00150 if (t == NULL) {
00151
00152 nd.setType(type);
00153 return true;
00154 } else {
00155 return false;
00156 }
00157 }
00158 tagNames_[name] = NameDescription(type, NameDescription::SS_NONE, loc);
00159 if (outer_ == NULL) {
00160 return true;
00161 } else {
00162
00163 return outer_->addTagName(name, type, loc);
00164 }
00165 }
00166
00175 Environment* lookupTagName(const std::string& name,
00176 NameDescription** nd)
00177 {
00178 TagNames::iterator i = tagNames_.find(name);
00179 if (i != tagNames_.end()) {
00180 *nd = &(i->second);
00181 return this;
00182 } else {
00183 if (outer_ == NULL) {
00184 return NULL;
00185 } else {
00186 return outer_->lookupTagName(name, nd);
00187 }
00188 }
00189 }
00190
00191 #ifdef PARSER_DEBUG
00192 std::string toString() const
00193 {
00194 std::stringstream ss;
00195 ss << "OrdinaryNames: ";
00196 for (OrdinaryNames::const_iterator i = ordinaryNames_.begin();
00197 i != ordinaryNames_.end(); ++i) {
00198 ss << "[" << i->first << ", " << i->second.getType()->toString() << "]";
00199 }
00200 if (outer_ != NULL) {
00201
00202 }
00203
00204 return ss.str();
00205 }
00206 #endif
00207
00208 private:
00209 Environment* outer_;
00210
00211 OrdinaryNames ordinaryNames_;
00212 TagNames tagNames_;
00213
00214 };
00215
00216
00217 }
00218
00219
00220 #endif
00221
00222
00223
00224
00225
00226
00227
00228