Go to the documentation of this file.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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include <iostream>
00064 #include "CPPParser.hpp"
00065 #include "CPPSymbol.hh"
00066
00067 using namespace std;
00068
00069 void CPPParser::init()
00070 {
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 symbols = new CPPDictionary(4001, 200, 800000);
00085
00086 symbols->saveScope();
00087 externalScope = symbols->getCurrentScopeIndex();
00088
00089
00090 _td = false;
00091 _sc = scInvalid;
00092 _tq = tqInvalid;
00093 _ts = tsInvalid;
00094 _ds = dsInvalid;
00095
00096 functionDefinition = 0;
00097 assign_stmt_RHS_found = 0;
00098 in_parameter_list = false;
00099 in_return = false;
00100 }
00101
00102 CPPParser::~CPPParser()
00103 {
00104 delete symbols;
00105 }
00106
00107
00108
00109
00110 int CPPParser::isTypeName(const std::string& s)
00111 {
00112 CPPSymbol *cs = (CPPSymbol *) symbols->lookup(s);
00113
00114 if (cs==NULL)
00115 {
00116
00117 return 0;
00118 }
00119
00120 if (cs->getType()==CPPSymbol::otTypedef||
00121 cs->getType()==CPPSymbol::otEnum||
00122 cs->getType()==CPPSymbol::otClass||
00123 cs->getType()==CPPSymbol::otStruct||
00124 cs->getType()==CPPSymbol::otUnion)
00125 {
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 return 1;
00139 }
00140
00141 return 0;
00142 }
00143
00144 void CPPParser::beginDeclaration() { }
00145
00146 void CPPParser::endDeclaration() { }
00147
00148 void CPPParser::beginFunctionDefinition()
00149 {
00150 functionDefinition = 1;
00151 }
00152
00153 void CPPParser::endFunctionDefinition()
00154 {
00155
00156 symbols->dumpScope(stdout);
00157
00158 symbols->removeScope();
00159 symbols->restoreScope();
00160
00161 functionDefinition = 0;
00162 }
00163
00164 void CPPParser::beginParameterDeclaration()
00165 { }
00166
00167 void CPPParser::beginFieldDeclaration()
00168 { }
00169
00170 void CPPParser::declarationSpecifier(bool td, StorageClass sc, TypeQualifier tq,
00171 TypeSpecifier ts, DeclSpecifier ds)
00172 {
00173
00174
00175 _td = td;
00176 _sc = sc;
00177 _tq = tq;
00178 _ts = ts;
00179 _ds = ds;
00180
00181 }
00182
00183
00184
00185
00186
00187 void CPPParser::declaratorID(const std::string& id, QualifiedItem qi)
00188 {
00189 CPPSymbol *c;
00190 if (qi==qiType || _td)
00191 {
00192 if (!(qi==qiType && _td))
00193 {
00194 std::cerr << LT(1) -> getLine()
00195 << "support.cpp declaratorID warning qi " << qi << ", _td " << _td
00196 << " inconsistent for " << id << std::endl;
00197 }
00198
00199 c = new CPPSymbol(id, CPPSymbol::otTypedef);
00200 symbols->defineInScope(id, c, externalScope);
00201 }
00202 else if (qi==qiFun)
00203 {
00204 c = new CPPSymbol(id, CPPSymbol::otFunction);
00205 symbols->define(id, c);
00206 }
00207 else
00208 {
00209 if (qi!=qiVar)
00210 {
00211 std::cerr << LT(1) -> getLine()
00212 << "support.cpp declaratorID warning qi " << qi << "not qiVar _td " << qiVar
00213 << " inconsistent for " << id << std::endl;
00214 }
00215
00216 c = new CPPSymbol(id, CPPSymbol::otVariable);
00217 symbols->define(id, c);
00218 }
00219 }
00220
00221 void CPPParser::enterNamespace(std::string const& name) { }
00222 void CPPParser::exitNamespace() { }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 void CPPParser::declaratorArray(int size) { }
00237
00238 void CPPParser::declaratorParameterList(int def)
00239 {
00240 symbols->saveScope();
00241
00242 }
00243
00244 void CPPParser::declaratorEndParameterList(int def)
00245 {
00246 if (!def)
00247 {
00248 symbols->dumpScope(stdout);
00249 symbols->removeScope();
00250 symbols->restoreScope();
00251
00252 }
00253 }
00254
00255 void CPPParser::functionParameterList()
00256 {
00257 symbols->saveScope();
00258
00259
00260 functionDefinition = 2;
00261 }
00262
00263 void CPPParser::functionEndParameterList(int def)
00264 {
00265
00266 if ( !def)
00267 {
00268 symbols->dumpScope(stdout);
00269 symbols->removeScope();
00270 symbols->restoreScope();
00271
00272 }
00273 else
00274 {
00275
00276 functionDefinition = 3;
00277 }
00278
00279 }
00280
00281 void CPPParser::enterNewLocalScope()
00282 {
00283 symbols->saveScope();
00284
00285 }
00286
00287 void CPPParser::exitLocalScope()
00288 {
00289 symbols->dumpScope(stdout);
00290 symbols->removeScope();
00291 symbols->restoreScope();
00292
00293 }
00294
00295 void CPPParser::enterExternalScope()
00296 {
00297
00298 functionDefinition = 0;
00299
00300 }
00301
00302 void CPPParser::exitExternalScope()
00303 {
00304 symbols->dumpScope(stdout);
00305 symbols->removeScope();
00306 symbols->restoreScope();
00307
00308
00309
00310
00311 }
00312
00313 void CPPParser::classForwardDeclaration(TypeSpecifier ts, DeclSpecifier ds, const std::string& tag)
00314 {
00315 CPPSymbol *c = NULL;
00316
00317
00318
00319
00320
00321 if ( symbols->lookup(tag)!=NULL )
00322 return;
00323
00324 switch (ts)
00325 {
00326 case tsSTRUCT :
00327 c = new CPPSymbol(tag, CPPSymbol::otStruct);
00328 break;
00329 case tsUNION :
00330 c = new CPPSymbol(tag, CPPSymbol::otUnion);
00331 break;
00332 case tsCLASS :
00333 c = new CPPSymbol(tag, CPPSymbol::otClass);
00334 break;
00335 }
00336
00337 symbols->defineInScope(tag, c, externalScope);
00338 }
00339
00340 void CPPParser::beginClassDefinition(TypeSpecifier ts,const std::string& tag)
00341 {
00342 CPPSymbol *c;
00343
00344
00345
00346
00347
00348 if (symbols->lookup(tag) != NULL)
00349 {
00350 symbols->saveScope();
00351
00352 return;
00353 }
00354
00355 switch (ts)
00356 {
00357 case tsSTRUCT:
00358 c = new CPPSymbol(tag, CPPSymbol::otStruct);
00359 break;
00360 case tsUNION:
00361 c = new CPPSymbol(tag, CPPSymbol::otUnion);
00362 break;
00363 case tsCLASS:
00364 c = new CPPSymbol(tag, CPPSymbol::otClass);
00365 break;
00366 default:
00367 throw std::runtime_error("unexpected TypeSpecifier in CPPParser::beginClassDefinition");
00368 }
00369 symbols->defineInScope(tag, c, externalScope);
00370 qualifierPrefix += tag;
00371 qualifierPrefix += "::";
00372
00373
00374
00375
00376
00377 symbols->saveScope();
00378 }
00379
00380 void CPPParser::endClassDefinition()
00381 {
00382 symbols->dumpScope(stdout);
00383 symbols->removeScope();
00384 symbols->restoreScope();
00385
00386
00387 size_t index = qualifierPrefix.rfind("::");
00388 if (index == std::string::npos)
00389 return;
00390 qualifierPrefix.resize(index);
00391 }
00392
00393 void CPPParser::enumElement(const std::string& e, bool has_value, int value)
00394 { }
00395
00396 void CPPParser::beginEnumDefinition(const std::string& e)
00397 {
00398
00399
00400
00401
00402 CPPSymbol *c = new CPPSymbol(e, CPPSymbol::otEnum);
00403 symbols->defineInScope(e, c, externalScope);
00404 }
00405
00406 void CPPParser::endEnumDefinition() { }
00407
00408 void CPPParser::foundSimpleType(const std::list<std::string>& full_type) { }
00409
00410 void CPPParser::end_of_stmt() { }
00411
00412