cppPreprocessor.cxx 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  1. // Filename: cppPreprocessor.cxx
  2. // Created by: drose (22Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "cppPreprocessor.h"
  15. #include "cppExpressionParser.h"
  16. #include "cppExpression.h"
  17. #include "cppScope.h"
  18. #include "cppIdentifier.h"
  19. #include "cppTemplateScope.h"
  20. #include "cppTemplateParameterList.h"
  21. #include "cppSimpleType.h"
  22. #include "cppGlobals.h"
  23. #include "cppCommentBlock.h"
  24. #include "cppBison.h"
  25. #include "indent.h"
  26. #include "pstrtod.h"
  27. #include <assert.h>
  28. #include <ctype.h>
  29. // We manage our own visibility counter, in addition to that managed
  30. // by cppBison.y. We do this just so we can define manifests with the
  31. // correct visibility when they are declared. (Asking the parser for
  32. // the current visibility is prone to error, since the parser might be
  33. // several tokens behind the preprocessor.)
  34. static CPPVisibility preprocessor_vis = V_public;
  35. static int
  36. hex_val(int c) {
  37. switch (c) {
  38. case '0':
  39. case '1':
  40. case '2':
  41. case '3':
  42. case '4':
  43. case '5':
  44. case '6':
  45. case '7':
  46. case '8':
  47. case '9':
  48. return (c - '0');
  49. default:
  50. return (tolower(c) - 'a' + 10);
  51. }
  52. }
  53. static string
  54. trim_blanks(const string &str) {
  55. size_t first, last;
  56. if(str.empty())
  57. return str;
  58. first = 0;
  59. while (first < str.length() && isspace(str[first])) {
  60. first++;
  61. }
  62. last = str.length() - 1;
  63. while (last > first && isspace(str[last])) {
  64. last--;
  65. }
  66. return str.substr(first, last - first + 1);
  67. }
  68. ////////////////////////////////////////////////////////////////////
  69. // Function: CPPPreprocessor::InputFile::Constructor
  70. // Access: Public
  71. // Description:
  72. ////////////////////////////////////////////////////////////////////
  73. CPPPreprocessor::InputFile::
  74. InputFile() {
  75. _in = NULL;
  76. _ignore_manifest = NULL;
  77. _line_number = 1;
  78. _col_number = 1;
  79. _lock_position = false;
  80. }
  81. ////////////////////////////////////////////////////////////////////
  82. // Function: CPPPreprocessor::InputFile::Destructor
  83. // Access: Public
  84. // Description:
  85. ////////////////////////////////////////////////////////////////////
  86. CPPPreprocessor::InputFile::
  87. ~InputFile() {
  88. if (_in != NULL) {
  89. // For some reason--compiler bug in gcc 3.2?--explicitly deleting
  90. // the stream pointer does not call the appropriate global delete
  91. // function; instead apparently calling the system delete
  92. // function. So we call the delete function by hand instead.
  93. #if !defined(USE_MEMORY_NOWRAPPERS) && defined(REDEFINE_GLOBAL_OPERATOR_NEW)
  94. _in->~istream();
  95. (*global_operator_delete)(_in);
  96. #else
  97. delete _in;
  98. #endif
  99. }
  100. }
  101. ////////////////////////////////////////////////////////////////////
  102. // Function: CPPPreprocessor::InputFile::open
  103. // Access: Public
  104. // Description:
  105. ////////////////////////////////////////////////////////////////////
  106. bool CPPPreprocessor::InputFile::
  107. open(const CPPFile &file) {
  108. assert(_in == NULL);
  109. _file = file;
  110. pifstream *in = new pifstream;
  111. _in = in;
  112. return _file._filename.open_read(*in);
  113. }
  114. ////////////////////////////////////////////////////////////////////
  115. // Function: CPPPreprocessor::InputFile::connect_input
  116. // Access: Public
  117. // Description:
  118. ////////////////////////////////////////////////////////////////////
  119. bool CPPPreprocessor::InputFile::
  120. connect_input(const string &input) {
  121. assert(_in == NULL);
  122. _input = input;
  123. _in = new istringstream(_input);
  124. return !_in->fail();
  125. }
  126. ////////////////////////////////////////////////////////////////////
  127. // Function: CPPPreprocessor::InputFile::get
  128. // Access: Public
  129. // Description:
  130. ////////////////////////////////////////////////////////////////////
  131. int CPPPreprocessor::InputFile::
  132. get() {
  133. assert(_in != NULL);
  134. int c = _in->get();
  135. // Quietly skip over embedded carriage-return characters. We
  136. // shouldn't see any of these unless there was some DOS-to-Unix file
  137. // conversion problem.
  138. while (c == '\r') {
  139. c = _in->get();
  140. }
  141. switch (c) {
  142. case EOF:
  143. break;
  144. case '\n':
  145. if (!_lock_position) {
  146. _line_number++;
  147. _col_number = 1;
  148. }
  149. break;
  150. default:
  151. if (!_lock_position) {
  152. _col_number++;
  153. }
  154. }
  155. return c;
  156. }
  157. ////////////////////////////////////////////////////////////////////
  158. // Function: CPPPreprocessor::Constructor
  159. // Access: Public
  160. // Description:
  161. ////////////////////////////////////////////////////////////////////
  162. CPPPreprocessor::
  163. CPPPreprocessor() {
  164. _noangles = false;
  165. _state = S_eof;
  166. _paren_nesting = 0;
  167. _angle_bracket_found = false;
  168. _unget = '\0';
  169. _last_c = '\0';
  170. _start_of_line = true;
  171. _last_cpp_comment = false;
  172. _save_comments = true;
  173. _resolve_identifiers = true;
  174. _warning_count = 0;
  175. _error_count = 0;
  176. _error_abort = false;
  177. #ifdef CPP_VERBOSE_LEX
  178. _token_index = 0;
  179. #endif
  180. _verbose = 1;
  181. }
  182. ////////////////////////////////////////////////////////////////////
  183. // Function: CPPPreprocessor::set_verbose
  184. // Access: Public
  185. // Description: Sets the verbosity level of the parser. At 0, no
  186. // warnings will be reported; at 1 or higher, expect to
  187. // get spammed.
  188. ////////////////////////////////////////////////////////////////////
  189. void CPPPreprocessor::
  190. set_verbose(int verbose) {
  191. _verbose = verbose;
  192. }
  193. ////////////////////////////////////////////////////////////////////
  194. // Function: CPPPreprocessor::get_verbose
  195. // Access: Public
  196. // Description: Returns the verbosity level of the parser.
  197. ////////////////////////////////////////////////////////////////////
  198. int CPPPreprocessor::
  199. get_verbose() const {
  200. return _verbose;
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. // Function: CPPPreprocessor::copy_filepos
  204. // Access: Public
  205. // Description:
  206. ////////////////////////////////////////////////////////////////////
  207. void CPPPreprocessor::
  208. copy_filepos(const CPPPreprocessor &other) {
  209. assert(!_files.empty());
  210. _files.back()._file = other.get_file();
  211. _files.back()._line_number = other.get_line_number();
  212. _files.back()._col_number = other.get_col_number();
  213. }
  214. ////////////////////////////////////////////////////////////////////
  215. // Function: CPPPreprocessor::get_file
  216. // Access: Public
  217. // Description:
  218. ////////////////////////////////////////////////////////////////////
  219. CPPFile CPPPreprocessor::
  220. get_file() const {
  221. if (_files.empty()) {
  222. return CPPFile("");
  223. }
  224. return _files.back()._file;
  225. }
  226. ////////////////////////////////////////////////////////////////////
  227. // Function: CPPPreprocessor::get_line_number
  228. // Access: Public
  229. // Description:
  230. ////////////////////////////////////////////////////////////////////
  231. int CPPPreprocessor::
  232. get_line_number() const {
  233. if (_files.empty()) {
  234. return 0;
  235. }
  236. return _files.back()._line_number;
  237. }
  238. ////////////////////////////////////////////////////////////////////
  239. // Function: CPPPreprocessor::get_col_number
  240. // Access: Public
  241. // Description:
  242. ////////////////////////////////////////////////////////////////////
  243. int CPPPreprocessor::
  244. get_col_number() const {
  245. if (_files.empty()) {
  246. return 0;
  247. }
  248. return _files.back()._col_number;
  249. }
  250. ////////////////////////////////////////////////////////////////////
  251. // Function: CPPPreprocessor::get_next_token
  252. // Access: Public
  253. // Description:
  254. ////////////////////////////////////////////////////////////////////
  255. CPPToken CPPPreprocessor::
  256. get_next_token() {
  257. #ifdef CPP_VERBOSE_LEX
  258. CPPToken tok = get_next_token0();
  259. indent(cerr, _files.size() * 2)
  260. << _token_index++ << ". " << tok << "\n";
  261. return tok;
  262. }
  263. CPPToken CPPPreprocessor::
  264. get_next_token0() {
  265. #endif
  266. // We make a nested call to internal_get_next_token(), so we can
  267. // combine sequences of identifiers and scoping symbols into a
  268. // single identifier, for yacc's convenience.
  269. CPPToken token(0);
  270. if (!_saved_tokens.empty()) {
  271. token = _saved_tokens.back();
  272. _saved_tokens.pop_back();
  273. } else {
  274. token = internal_get_next_token();
  275. }
  276. int first_line = token._lloc.first_line;
  277. int first_col = token._lloc.first_column;
  278. CPPFile first_file = token._lloc.file;
  279. if (_resolve_identifiers &&
  280. (token._token == SIMPLE_IDENTIFIER || token._token == SCOPE)) {
  281. // We will be returning a scoped identifier, or a scoping. Keep
  282. // pulling off tokens until we reach the end of the
  283. // scope/identifier sequence.
  284. string name;
  285. // If we started the ball with an identifier, use it and get the
  286. // next token. Otherwise, we started with :: (global scope), and
  287. // we indicate this with an empty string at the beginning of the
  288. // scoping sequence.
  289. if (token._token == SIMPLE_IDENTIFIER) {
  290. name = token._lval.str;
  291. token = internal_get_next_token();
  292. }
  293. CPPIdentifier *ident = new CPPIdentifier(name, token._lloc.file);
  294. YYSTYPE result;
  295. result.u.identifier = ident;
  296. if (token._token == '<') {
  297. // If the next token is an angle bracket and the current
  298. // identifier wants template instantiation, assume the angle
  299. // bracket begins the instantiation and call yacc recursively to
  300. // parse the template parameters.
  301. CPPDeclaration *decl = ident->find_template(current_scope, global_scope);
  302. if (decl != NULL) {
  303. ident->_names.back().set_templ
  304. (nested_parse_template_instantiation(decl->get_template_scope()));
  305. token = internal_get_next_token();
  306. }
  307. }
  308. while (token._token == SCOPE) {
  309. name += "::";
  310. token = internal_get_next_token();
  311. string token_prefix;
  312. if (token._token == '~') {
  313. // A scoping operator followed by a tilde can only be the
  314. // start of a scoped destructor name. Make the tilde be part
  315. // of the name.
  316. name += "~";
  317. token_prefix = "~";
  318. token = internal_get_next_token();
  319. }
  320. if (token._token != SIMPLE_IDENTIFIER) {
  321. // The last useful token was a SCOPE, thus this is a scoping
  322. // token.
  323. if (token._token == KW_OPERATOR) {
  324. // Unless the last token we came across was the "operator"
  325. // keyword. We make a special case for this, because it's
  326. // occasionally scoped in normal use.
  327. token._lval = result;
  328. return token;
  329. }
  330. _saved_tokens.push_back(token);
  331. return CPPToken(SCOPING, first_line, first_col, first_file,
  332. name, result);
  333. }
  334. name += token._lval.str;
  335. ident->_names.push_back(token_prefix + token._lval.str);
  336. token = internal_get_next_token();
  337. if (token._token == '<') {
  338. // If the next token is an angle bracket and the current
  339. // indentifier wants template instantiation, assume the angle
  340. // bracket begins the instantiation and call yacc recursively to
  341. // parse the template parameters.
  342. CPPDeclaration *decl =
  343. ident->find_template(current_scope, global_scope);
  344. if (decl != NULL) {
  345. ident->_names.back().set_templ
  346. (nested_parse_template_instantiation(decl->get_template_scope()));
  347. token = internal_get_next_token();
  348. }
  349. }
  350. }
  351. // The last useful token was a SIMPLE_IDENTIFIER, thus this is a
  352. // normal scoped identifier.
  353. _saved_tokens.push_back(token);
  354. int token_type = IDENTIFIER;
  355. CPPDeclaration *decl = ident->find_symbol(current_scope, global_scope);
  356. if (decl != NULL && decl->as_type() != NULL) {
  357. token_type = TYPENAME_IDENTIFIER;
  358. }
  359. return CPPToken(token_type, first_line, first_col, first_file,
  360. name, result);
  361. }
  362. // This is the normal case: just pass through whatever token we got.
  363. return token;
  364. }
  365. ////////////////////////////////////////////////////////////////////
  366. // Function: CPPPreprocessor::warning
  367. // Access: Public
  368. // Description:
  369. ////////////////////////////////////////////////////////////////////
  370. void CPPPreprocessor::
  371. warning(const string &message, int line, int col, CPPFile file) {
  372. if (_verbose >= 2) {
  373. if (line == 0) {
  374. line = get_line_number();
  375. col = get_col_number();
  376. }
  377. if (file.empty()) {
  378. file = get_file();
  379. }
  380. int indent_level = 0;
  381. if (_verbose >= 3) {
  382. indent_level = _files.size() * 2;
  383. }
  384. indent(cerr, indent_level)
  385. << "*** Warning in " << file
  386. << " near line " << line << ", column " << col << ":\n";
  387. indent(cerr, indent_level)
  388. << message << "\n";
  389. }
  390. _warning_count++;
  391. }
  392. ////////////////////////////////////////////////////////////////////
  393. // Function: CPPPreprocessor::error
  394. // Access: Public
  395. // Description:
  396. ////////////////////////////////////////////////////////////////////
  397. void CPPPreprocessor::
  398. error(const string &message, int line, int col, CPPFile file) {
  399. if (_state == S_nested || _state == S_end_nested) {
  400. // Don't report or log errors in the nested state. These will be
  401. // reported when the nesting level collapses.
  402. return;
  403. }
  404. if (_verbose >= 1) {
  405. if (line == 0) {
  406. line = get_line_number();
  407. col = get_col_number();
  408. }
  409. if (file.empty()) {
  410. file = get_file();
  411. }
  412. int indent_level = 0;
  413. if (_verbose >= 3) {
  414. indent_level = _files.size() * 2;
  415. }
  416. indent(cerr, indent_level)
  417. << "*** Error in " << file
  418. << " near line " << line << ", column " << col << ":\n";
  419. indent(cerr, indent_level)
  420. << message << "\n";
  421. if (_error_abort) {
  422. cerr << "Aborting.\n";
  423. abort();
  424. }
  425. }
  426. _error_count++;
  427. }
  428. ////////////////////////////////////////////////////////////////////
  429. // Function: CPPPreprocessor::get_warning_count
  430. // Access: Public
  431. // Description:
  432. ////////////////////////////////////////////////////////////////////
  433. int CPPPreprocessor::
  434. get_warning_count() const {
  435. return _warning_count;
  436. }
  437. ////////////////////////////////////////////////////////////////////
  438. // Function: CPPPreprocessor::get_error_count
  439. // Access: Public
  440. // Description:
  441. ////////////////////////////////////////////////////////////////////
  442. int CPPPreprocessor::
  443. get_error_count() const {
  444. return _error_count;
  445. }
  446. ////////////////////////////////////////////////////////////////////
  447. // Function: CPPPreprocessor::get_comment_before
  448. // Access: Public
  449. // Description: Returns the CPPCommentBlock immediately preceding the
  450. // indicated line, if any. If there is no such comment,
  451. // returns NULL.
  452. ////////////////////////////////////////////////////////////////////
  453. CPPCommentBlock *CPPPreprocessor::
  454. get_comment_before(int line, CPPFile file) {
  455. CPPComments::reverse_iterator ci;
  456. ci = _comments.rbegin();
  457. int wrong_file_count = 0;
  458. while (ci != _comments.rend()) {
  459. CPPCommentBlock *comment = (*ci);
  460. if (comment->_file == file) {
  461. wrong_file_count = 0;
  462. if (comment->_last_line == line || comment->_last_line == line - 1) {
  463. return comment;
  464. }
  465. if (comment->_last_line < line) {
  466. return (CPPCommentBlock *)NULL;
  467. }
  468. } else {
  469. wrong_file_count++;
  470. if (wrong_file_count > 10) {
  471. return (CPPCommentBlock *)NULL;
  472. }
  473. }
  474. ++ci;
  475. }
  476. return (CPPCommentBlock *)NULL;
  477. }
  478. ////////////////////////////////////////////////////////////////////
  479. // Function: CPPPreprocessor::get_comment_on
  480. // Access: Public
  481. // Description: Returns the CPPCommentBlock that starts on the
  482. // indicated line, if any. If there is no such
  483. // comment, returns NULL.
  484. ////////////////////////////////////////////////////////////////////
  485. CPPCommentBlock *CPPPreprocessor::
  486. get_comment_on(int line, CPPFile file) {
  487. CPPComments::reverse_iterator ci;
  488. ci = _comments.rbegin();
  489. while (ci != _comments.rend()) {
  490. CPPCommentBlock *comment = (*ci);
  491. if (comment->_file == file) {
  492. if (comment->_line_number == line) {
  493. return comment;
  494. } else if (comment->_line_number < line) {
  495. return (CPPCommentBlock *)NULL;
  496. }
  497. }
  498. ++ci;
  499. }
  500. return (CPPCommentBlock *)NULL;
  501. }
  502. ////////////////////////////////////////////////////////////////////
  503. // Function: CPPPreprocessor::init_cpp
  504. // Access: Protected
  505. // Description:
  506. ////////////////////////////////////////////////////////////////////
  507. bool CPPPreprocessor::
  508. init_cpp(const CPPFile &file) {
  509. _state = S_normal;
  510. _saved_tokens.push_back(CPPToken(START_CPP));
  511. _last_c = '\0';
  512. return push_file(file);
  513. }
  514. ////////////////////////////////////////////////////////////////////
  515. // Function: CPPPreprocessor::init_const_expr
  516. // Access: Protected
  517. // Description:
  518. ////////////////////////////////////////////////////////////////////
  519. bool CPPPreprocessor::
  520. init_const_expr(const string &expr) {
  521. _state = S_normal;
  522. _saved_tokens.push_back(CPPToken(START_CONST_EXPR));
  523. return push_string(expr, false);
  524. }
  525. ////////////////////////////////////////////////////////////////////
  526. // Function: CPPPreprocessor::init_type
  527. // Access: Protected
  528. // Description:
  529. ////////////////////////////////////////////////////////////////////
  530. bool CPPPreprocessor::
  531. init_type(const string &type) {
  532. _state = S_normal;
  533. _saved_tokens.push_back(CPPToken(START_TYPE));
  534. return push_string(type, false);
  535. }
  536. ////////////////////////////////////////////////////////////////////
  537. // Function: CPPPreprocessor::push_file
  538. // Access: Protected
  539. // Description:
  540. ////////////////////////////////////////////////////////////////////
  541. bool CPPPreprocessor::
  542. push_file(const CPPFile &file) {
  543. if (_verbose >= 3) {
  544. indent(cerr, _files.size() * 2)
  545. << "Reading " << file << "\n";
  546. }
  547. _files.push_back(InputFile());
  548. InputFile &infile = _files.back();
  549. if (infile.open(file)) {
  550. // Record the fact that we opened the file for the benefit of user
  551. // code.
  552. _parsed_files.insert(file);
  553. infile._prev_last_c = _last_c;
  554. _last_c = '\0';
  555. _start_of_line = true;
  556. return true;
  557. }
  558. _files.pop_back();
  559. return false;
  560. }
  561. ////////////////////////////////////////////////////////////////////
  562. // Function: CPPPreprocessor::push_string
  563. // Access: Protected
  564. // Description:
  565. ////////////////////////////////////////////////////////////////////
  566. bool CPPPreprocessor::
  567. push_string(const string &input, bool lock_position) {
  568. #ifdef CPP_VERBOSE_LEX
  569. indent(cerr, _files.size() * 2)
  570. << "Pushing to string \"" << input
  571. << "\"\nlock_position = " << lock_position << "\n";
  572. #endif
  573. CPPFile first_file = get_file();
  574. int first_line = get_line_number();
  575. int first_col = get_col_number();
  576. _files.push_back(InputFile());
  577. InputFile &infile = _files.back();
  578. if (infile.connect_input(input)) {
  579. if (lock_position) {
  580. infile._file = first_file;
  581. infile._line_number = first_line;
  582. infile._col_number = first_col;
  583. infile._lock_position = true;
  584. }
  585. infile._prev_last_c = _last_c;
  586. _last_c = '\0';
  587. return true;
  588. }
  589. #ifdef CPP_VERBOSE_LEX
  590. indent(cerr, _files.size() * 2)
  591. << "Unable to read string\n";
  592. #endif
  593. _files.pop_back();
  594. return false;
  595. }
  596. ////////////////////////////////////////////////////////////////////
  597. // Function: CPPPreprocessor::expand_manifests
  598. // Access: Protected
  599. // Description: Given a string, expand all manifests within the
  600. // string and return the new string.
  601. ////////////////////////////////////////////////////////////////////
  602. string CPPPreprocessor::
  603. expand_manifests(const string &input_expr, bool expand_undefined) {
  604. // Get a copy of the expression string we can modify.
  605. string expr = input_expr;
  606. // Repeatedly scan the expr for any manifest names or defined()
  607. // function.
  608. // We'll need to save the set of manifests we've already expanded,
  609. // to guard against recursive references.
  610. set<const CPPManifest *> already_expanded;
  611. bool manifest_found;
  612. do {
  613. manifest_found = false;
  614. size_t p = 0;
  615. while (p < expr.size()) {
  616. if (isalpha(expr[p]) || expr[p] == '_') {
  617. size_t q = p;
  618. while (p < expr.size() && (isalnum(expr[p]) || expr[p] == '_')) {
  619. p++;
  620. }
  621. string ident = expr.substr(q, p - q);
  622. // Here's an identifier. Is it "defined"?
  623. if (ident == "defined") {
  624. expand_defined_function(expr, q, p);
  625. } else {
  626. // Is it a manifest?
  627. Manifests::const_iterator mi = _manifests.find(ident);
  628. if (mi != _manifests.end()) {
  629. const CPPManifest *manifest = (*mi).second;
  630. if (already_expanded.insert(manifest).second) {
  631. expand_manifest_inline(expr, q, p, (*mi).second);
  632. manifest_found = true;
  633. }
  634. } else if (expand_undefined && ident != "true" && ident != "false") {
  635. // It is not found. Expand it to 0.
  636. expr = expr.substr(0, q) + "0" + expr.substr(p);
  637. p = q + 1;
  638. }
  639. }
  640. } else {
  641. p++;
  642. }
  643. }
  644. // If we expanded any manifests at all that time, then go back
  645. // through the string and look again--we might have a manifest
  646. // that expands to another manifest.
  647. } while (manifest_found);
  648. return expr;
  649. }
  650. ////////////////////////////////////////////////////////////////////
  651. // Function: CPPPreprocessor::parse_expr
  652. // Access: Protected
  653. // Description: Given a string, expand all manifests within the
  654. // string and evaluate it as an expression. Returns
  655. // NULL if the string is not a valid expression.
  656. //
  657. // This is an internal support function for
  658. // CPPPreprocessor; however, there is a public variant
  659. // of this function defined for CPPParser.
  660. ////////////////////////////////////////////////////////////////////
  661. CPPExpression *CPPPreprocessor::
  662. parse_expr(const string &input_expr, CPPScope *current_scope,
  663. CPPScope *global_scope) {
  664. string expr = expand_manifests(input_expr, true);
  665. CPPExpressionParser ep(current_scope, global_scope);
  666. ep._verbose = 0;
  667. if (ep.parse_expr(expr, *this)) {
  668. return ep._expr;
  669. } else {
  670. return (CPPExpression *)NULL;
  671. }
  672. }
  673. ////////////////////////////////////////////////////////////////////
  674. // Function: CPPPreprocessor::internal_get_next_token
  675. // Access: Private
  676. // Description:
  677. ////////////////////////////////////////////////////////////////////
  678. CPPToken CPPPreprocessor::
  679. internal_get_next_token() {
  680. if (_state == S_eof || _state == S_end_nested) {
  681. return CPPToken::eof();
  682. }
  683. int c = _last_c;
  684. _last_c = '\0';
  685. if (c == '\0' || c == EOF) {
  686. c = get();
  687. }
  688. // Skip any whitespace, comments, and preprocessor directives before
  689. // the token.
  690. c = skip_whitespace(c);
  691. while (c == '#' && _start_of_line && !should_ignore_preprocessor()) {
  692. c = skip_whitespace(process_directive(c));
  693. }
  694. if (c == '\'') {
  695. return get_quoted_char(c);
  696. } else if (c == '"') {
  697. return get_quoted_string(c);
  698. } else if (isalpha(c) || c == '_') {
  699. return get_identifier(c);
  700. } else if (isdigit(c)) {
  701. return get_number(c);
  702. }
  703. _last_c = c;
  704. if (c == EOF) {
  705. _state = S_eof;
  706. return CPPToken::eof();
  707. }
  708. CPPFile first_file = get_file();
  709. int first_line = get_line_number();
  710. int first_col = get_col_number();
  711. // Check for a number beginning with a decimal point.
  712. int next_c = get();
  713. if (c == '.' && isdigit(next_c)) {
  714. return get_number(c, next_c);
  715. }
  716. // Check for two- or three-character tokens.
  717. _last_c = get();
  718. switch (c) {
  719. case '+':
  720. if (next_c == '+') return CPPToken(PLUSPLUS, first_line, first_col, first_file);
  721. if (next_c == '=') return CPPToken(PLUSEQUAL, first_line, first_col, first_file);
  722. break;
  723. case '-':
  724. if (next_c == '-') return CPPToken(MINUSMINUS, first_line, first_col, first_file);
  725. if (next_c == '=') return CPPToken(MINUSEQUAL, first_line, first_col, first_file);
  726. if (next_c == '>' && _last_c == '*') {
  727. _last_c = get();
  728. return CPPToken(POINTSAT_STAR, first_line, first_col, first_file);
  729. }
  730. if (next_c == '>') return CPPToken(POINTSAT, first_line, first_col, first_file);
  731. break;
  732. case '<':
  733. if (next_c == '<' && _last_c == '=') {
  734. _last_c = get();
  735. return CPPToken(LSHIFTEQUAL, first_line, first_col, first_file);
  736. }
  737. if (next_c == '<') return CPPToken(LSHIFT, first_line, first_col, first_file);
  738. if (next_c == '=') return CPPToken(LECOMPARE, first_line, first_col, first_file);
  739. if (next_c == ':') return CPPToken('[', first_line, first_col, first_file);
  740. if (next_c == '%') return CPPToken('{', first_line, first_col, first_file);
  741. break;
  742. case '>':
  743. if (next_c == '>' && _last_c == '=') {
  744. _last_c = get();
  745. return CPPToken(RSHIFTEQUAL, first_line, first_col, first_file);
  746. }
  747. if (next_c == '>') return CPPToken(RSHIFT, first_line, first_col, first_file);
  748. if (next_c == '=') return CPPToken(GECOMPARE, first_line, first_col, first_file);
  749. break;
  750. case '|':
  751. if (next_c == '|') return CPPToken(OROR, first_line, first_col, first_file);
  752. if (next_c == '=') return CPPToken(OREQUAL, first_line, first_col, first_file);
  753. break;
  754. case '&':
  755. if (next_c == '&') return CPPToken(ANDAND, first_line, first_col, first_file);
  756. if (next_c == '=') return CPPToken(ANDEQUAL, first_line, first_col, first_file);
  757. break;
  758. case '^':
  759. if (next_c == '=') return CPPToken(XOREQUAL, first_line, first_col, first_file);
  760. break;
  761. case '=':
  762. if (next_c == '=') return CPPToken(EQCOMPARE, first_line, first_col, first_file);
  763. break;
  764. case '!':
  765. if (next_c == '=') return CPPToken(NECOMPARE, first_line, first_col, first_file);
  766. break;
  767. case '.':
  768. if (next_c == '*') return CPPToken(DOT_STAR, first_line, first_col, first_file);
  769. if (next_c == '.' && _last_c == '.') {
  770. _last_c = get();
  771. return CPPToken(ELLIPSIS, first_line, first_col, first_file);
  772. }
  773. break;
  774. case ':':
  775. if (next_c == ':') return CPPToken(SCOPE, first_line, first_col, first_file);
  776. if (next_c == '>') return CPPToken(']', first_line, first_col, first_file);
  777. break;
  778. case '*':
  779. if (next_c == '=') return CPPToken(TIMESEQUAL, first_line, first_col, first_file);
  780. break;
  781. case '/':
  782. if (next_c == '=') return CPPToken(DIVIDEEQUAL, first_line, first_col, first_file);
  783. break;
  784. case '%':
  785. if (next_c == '=') return CPPToken(MODEQUAL, first_line, first_col, first_file);
  786. if (next_c == '>') return CPPToken('}', first_line, first_col, first_file);
  787. break;
  788. }
  789. // It wasn't any of the two- or three-character tokens, so put back
  790. // the lookahead character and return the one-character token.
  791. unget(_last_c);
  792. _last_c = next_c;
  793. if (_state == S_nested) {
  794. // If we're running a nested lexer, keep track of the paren
  795. // levels. When we encounter a comma or closing angle bracket at
  796. // the bottom level, we stop.
  797. switch (c) {
  798. case '(':
  799. case '[':
  800. _paren_nesting++;
  801. break;
  802. case ')':
  803. case ']':
  804. _paren_nesting--;
  805. break;
  806. case ',':
  807. if (_paren_nesting <= 0) {
  808. _state = S_end_nested;
  809. return CPPToken::eof();
  810. }
  811. break;
  812. case '>':
  813. if (_paren_nesting <= 0) {
  814. _angle_bracket_found = true;
  815. _state = S_end_nested;
  816. return CPPToken::eof();
  817. }
  818. }
  819. }
  820. // We skip whitespace here again, so that we can read any comments
  821. // after this point before we parse this line.
  822. _last_c = skip_whitespace(_last_c);
  823. return CPPToken(c, first_line, first_col, first_file);
  824. }
  825. ////////////////////////////////////////////////////////////////////
  826. // Function: CPPPreprocessor::skip_whitespace
  827. // Access: Private
  828. // Description:
  829. ////////////////////////////////////////////////////////////////////
  830. int CPPPreprocessor::
  831. skip_whitespace(int c) {
  832. while (c != EOF) {
  833. c = skip_comment(c);
  834. if (c == '\\') {
  835. // This does not usually occur in the middle of unquoted C++
  836. // code, except before a newline character.
  837. c = get();
  838. if (c != '\n') {
  839. unget(c);
  840. return '\\';
  841. }
  842. }
  843. if (!isspace(c)) {
  844. return c;
  845. }
  846. c = get();
  847. }
  848. return c;
  849. }
  850. ////////////////////////////////////////////////////////////////////
  851. // Function: CPPPreprocessor::skip_comment
  852. // Access: Private
  853. // Description:
  854. ////////////////////////////////////////////////////////////////////
  855. int CPPPreprocessor::
  856. skip_comment(int c) {
  857. while (c == '/') {
  858. int next_c = get();
  859. if (next_c == '*') {
  860. _last_cpp_comment = false;
  861. c = skip_c_comment(get());
  862. } else if (next_c == '/') {
  863. c = skip_cpp_comment(get());
  864. break;
  865. } else {
  866. _last_cpp_comment = false;
  867. unget(next_c);
  868. return c;
  869. }
  870. }
  871. if (!isspace(c)) {
  872. _last_cpp_comment = false;
  873. }
  874. return c;
  875. }
  876. ////////////////////////////////////////////////////////////////////
  877. // Function: CPPPreprocessor::skip_c_comment
  878. // Access: Private
  879. // Description:
  880. ////////////////////////////////////////////////////////////////////
  881. int CPPPreprocessor::
  882. skip_c_comment(int c) {
  883. if (_save_comments) {
  884. CPPCommentBlock *comment = new CPPCommentBlock;
  885. _comments.push_back(comment);
  886. comment->_file = get_file();
  887. comment->_line_number = get_line_number();
  888. comment->_last_line = get_line_number();
  889. comment->_col_number = get_col_number() - 2;
  890. comment->_c_style = true;
  891. comment->_comment = "/*";
  892. while (c != EOF) {
  893. if (c == '*') {
  894. comment->_comment += c;
  895. c = get();
  896. if (c == '/') {
  897. comment->_comment += c;
  898. comment->_last_line = get_line_number();
  899. return get();
  900. }
  901. } else {
  902. comment->_comment += c;
  903. c = get();
  904. }
  905. }
  906. warning("Comment is unterminated",
  907. comment->_line_number, comment->_col_number,
  908. comment->_file);
  909. } else {
  910. CPPFile first_file = get_file();
  911. int first_line_number = get_line_number();
  912. int first_col_number = get_col_number() - 2;
  913. while (c != EOF) {
  914. if (c == '*') {
  915. c = get();
  916. if (c == '/') {
  917. return get();
  918. }
  919. } else {
  920. c = get();
  921. }
  922. }
  923. warning("Comment is unterminated",
  924. first_line_number, first_col_number,
  925. first_file);
  926. }
  927. return c;
  928. }
  929. ////////////////////////////////////////////////////////////////////
  930. // Function: CPPPreprocessor::skip_cpp_comment
  931. // Access: Private
  932. // Description:
  933. ////////////////////////////////////////////////////////////////////
  934. int CPPPreprocessor::
  935. skip_cpp_comment(int c) {
  936. if (_save_comments) {
  937. CPPCommentBlock *comment;
  938. int line_number = get_line_number();
  939. if (c == '\n') {
  940. // We have to subtract one from the line number as we just
  941. // fetched a newline.
  942. --line_number;
  943. }
  944. if (_last_cpp_comment && !_comments.empty() &&
  945. _comments.back()->_last_line >= line_number - 1) {
  946. // If the last non-whitespace character read was also part of a
  947. // C++ comment, then this is just a continuation of that comment
  948. // block. However, if there was a line without comment in between,
  949. // it starts a new block anyway.
  950. comment = _comments.back();
  951. assert(!comment->_c_style);
  952. comment->_comment += "//";
  953. } else {
  954. // Otherwise, this begins a new comment block.
  955. comment = new CPPCommentBlock;
  956. comment->_file = get_file();
  957. comment->_line_number = line_number;
  958. comment->_last_line = line_number;
  959. comment->_col_number = get_col_number() - 2;
  960. comment->_c_style = false;
  961. comment->_comment = "//";
  962. _comments.push_back(comment);
  963. }
  964. while (c != EOF && c != '\n') {
  965. comment->_comment += c;
  966. c = get();
  967. }
  968. comment->_comment += '\n';
  969. comment->_last_line = line_number;
  970. _last_cpp_comment = true;
  971. } else {
  972. while (c != EOF && c != '\n') {
  973. c = get();
  974. }
  975. }
  976. return c;
  977. }
  978. ////////////////////////////////////////////////////////////////////
  979. // Function: CPPPreprocessor::process_directive
  980. // Access: Private
  981. // Description:
  982. ////////////////////////////////////////////////////////////////////
  983. int CPPPreprocessor::
  984. process_directive(int c) {
  985. CPPFile first_file = get_file();
  986. int first_line = get_line_number();
  987. int first_col = get_col_number();
  988. string command, args;
  989. c = get_preprocessor_command(c, command);
  990. c = get_preprocessor_args(c, args);
  991. #ifdef CPP_VERBOSE_LEX
  992. indent(cerr, _files.size() * 2)
  993. << "#" << command << " " << args << "\n";
  994. #endif
  995. if (command == "define") {
  996. handle_define_directive(args, first_line, first_col, first_file);
  997. } else if (command == "undef") {
  998. handle_undef_directive(args, first_line, first_col, first_file);
  999. } else if (command == "ifdef") {
  1000. handle_ifdef_directive(args, first_line, first_col, first_file);
  1001. } else if (command == "ifndef") {
  1002. handle_ifndef_directive(args, first_line, first_col, first_file);
  1003. } else if (command == "if") {
  1004. handle_if_directive(args, first_line, first_col, first_file);
  1005. } else if (command == "else" || command == "elif") {
  1006. // Presumably this follows some #if or #ifdef. We don't bother to
  1007. // check this, however.
  1008. skip_false_if_block(false);
  1009. } else if (command == "endif") {
  1010. // Presumably this follows some #if or #ifdef. We don't bother to
  1011. // check this, however.
  1012. } else if (command == "include") {
  1013. handle_include_directive(args, first_line, first_col, first_file);
  1014. } else if (command == "pragma") {
  1015. handle_pragma_directive(args, first_line, first_col, first_file);
  1016. } else if (command == "ident") {
  1017. // Quietly ignore idents.
  1018. } else if (command == "error") {
  1019. handle_error_directive(args, first_line, first_col, first_file);
  1020. } else {
  1021. warning("Ignoring unknown directive #" + command,
  1022. first_line, first_col, first_file);
  1023. }
  1024. _start_of_line = true;
  1025. return '\n';
  1026. }
  1027. ////////////////////////////////////////////////////////////////////
  1028. // Function: CPPPreprocessor::get_preprocessor_command
  1029. // Access: Private
  1030. // Description:
  1031. ////////////////////////////////////////////////////////////////////
  1032. int CPPPreprocessor::
  1033. get_preprocessor_command(int c, string &command) {
  1034. // Skip the hash mark.
  1035. assert(c == '#');
  1036. c = get();
  1037. // Also skip any whitespace following the hash mark--but don't skip
  1038. // past a newline.
  1039. while (c != EOF && (c == ' ' || c == '\t')) {
  1040. c = get();
  1041. }
  1042. // The next sequence of characters is the command.
  1043. while (c != EOF && (isalnum(c) || c == '_')) {
  1044. command += c;
  1045. c = get();
  1046. }
  1047. return c;
  1048. }
  1049. ////////////////////////////////////////////////////////////////////
  1050. // Function: CPPPreprocessor::get_preprocessor_args
  1051. // Access: Private
  1052. // Description:
  1053. ////////////////////////////////////////////////////////////////////
  1054. int CPPPreprocessor::
  1055. get_preprocessor_args(int c, string &args) {
  1056. // Following the command, the rest of the line, as well as any text
  1057. // on successive lines, is part of the arguments to the command.
  1058. while (c != EOF && c != '\n') {
  1059. if (c == '\\') {
  1060. int next_c = get();
  1061. if (next_c == '\n') {
  1062. // Here we have an escaped newline: a continuation.
  1063. args += '\n';
  1064. } else {
  1065. // Just a backslash followed by some non-backslash, keep both.
  1066. args += c;
  1067. if (next_c != EOF) {
  1068. args += next_c;
  1069. }
  1070. }
  1071. } else {
  1072. args += c;
  1073. }
  1074. c = skip_comment(get());
  1075. }
  1076. // Remove any leading and trailing whitespace from the args.
  1077. args = trim_blanks(args);
  1078. return c;
  1079. }
  1080. ////////////////////////////////////////////////////////////////////
  1081. // Function: CPPPreprocessor::handle_define_directive
  1082. // Access: Private
  1083. // Description:
  1084. ////////////////////////////////////////////////////////////////////
  1085. void CPPPreprocessor::
  1086. handle_define_directive(const string &args, int first_line,
  1087. int first_col, const CPPFile &first_file) {
  1088. if (args.empty()) {
  1089. warning("Ignoring empty #define directive",
  1090. first_line, first_col, first_file);
  1091. } else {
  1092. CPPManifest *manifest = new CPPManifest(args, first_file);
  1093. manifest->_vis = preprocessor_vis;
  1094. if (!manifest->_has_parameters) {
  1095. string expr_string = manifest->expand();
  1096. if (!expr_string.empty()) {
  1097. manifest->_expr = parse_expr(expr_string, global_scope, global_scope);
  1098. }
  1099. }
  1100. // ok one memory leak here..
  1101. Manifests::iterator mi = _manifests.find(manifest->_name);
  1102. if(mi != _manifests.end())
  1103. {
  1104. // i do not see a goodway to compare the old and new hmmmm
  1105. //cerr << "Warning Overwriting Constant " << manifest->_name << "\n";
  1106. delete mi->second;
  1107. }
  1108. _manifests[manifest->_name] = manifest;
  1109. }
  1110. }
  1111. ////////////////////////////////////////////////////////////////////
  1112. // Function: CPPPreprocessor::handle_undef_directive
  1113. // Access: Private
  1114. // Description:
  1115. ////////////////////////////////////////////////////////////////////
  1116. void CPPPreprocessor::
  1117. handle_undef_directive(const string &args, int first_line,
  1118. int first_col, const CPPFile &first_file) {
  1119. if (args.empty()) {
  1120. warning("Ignoring empty #undef directive",
  1121. first_line, first_col, first_file);
  1122. } else {
  1123. Manifests::iterator mi = _manifests.find(args);
  1124. if (mi != _manifests.end()) {
  1125. _manifests.erase(mi);
  1126. }
  1127. }
  1128. }
  1129. ////////////////////////////////////////////////////////////////////
  1130. // Function: CPPPreprocessor::handle_ifdef_directive
  1131. // Access: Private
  1132. // Description:
  1133. ////////////////////////////////////////////////////////////////////
  1134. void CPPPreprocessor::
  1135. handle_ifdef_directive(const string &args, int, int, const CPPFile &) {
  1136. Manifests::const_iterator mi = _manifests.find(args);
  1137. if (mi != _manifests.end()) {
  1138. // The macro is defined. We continue.
  1139. return;
  1140. }
  1141. // The macro is undefined. Skip stuff.
  1142. skip_false_if_block(true);
  1143. }
  1144. ////////////////////////////////////////////////////////////////////
  1145. // Function: CPPPreprocessor::handle_ifndef_directive
  1146. // Access: Private
  1147. // Description:
  1148. ////////////////////////////////////////////////////////////////////
  1149. void CPPPreprocessor::
  1150. handle_ifndef_directive(const string &args, int, int, const CPPFile &) {
  1151. Manifests::const_iterator mi = _manifests.find(args);
  1152. if (mi == _manifests.end()) {
  1153. // The macro is undefined. We continue.
  1154. return;
  1155. }
  1156. // The macro is defined. Skip stuff.
  1157. skip_false_if_block(true);
  1158. }
  1159. ////////////////////////////////////////////////////////////////////
  1160. // Function: CPPPreprocessor::handle_if_directive
  1161. // Access: Private
  1162. // Description:
  1163. ////////////////////////////////////////////////////////////////////
  1164. void CPPPreprocessor::
  1165. handle_if_directive(const string &args, int first_line,
  1166. int first_col, const CPPFile &first_file) {
  1167. CPPExpression *expr = parse_expr(args, global_scope, global_scope);
  1168. int expression_result = 0;
  1169. if (expr != (CPPExpression *)NULL) {
  1170. CPPExpression::Result result = expr->evaluate();
  1171. if (result._type == CPPExpression::RT_error) {
  1172. warning("Ignoring invalid expression " + args,
  1173. first_line, first_col, first_file);
  1174. } else {
  1175. expression_result = result.as_integer();
  1176. }
  1177. } else {
  1178. warning("Ignoring invalid expression " + args,
  1179. first_line, first_col, first_file);
  1180. }
  1181. if (expression_result) {
  1182. // The expression result is true. We continue.
  1183. return;
  1184. }
  1185. // The expression result is false. Skip stuff.
  1186. skip_false_if_block(true);
  1187. }
  1188. ////////////////////////////////////////////////////////////////////
  1189. // Function: CPPPreprocessor::handle_include_directive
  1190. // Access: Private
  1191. // Description:
  1192. ////////////////////////////////////////////////////////////////////
  1193. void CPPPreprocessor::
  1194. handle_include_directive(const string &args, int first_line,
  1195. int first_col, const CPPFile &first_file) {
  1196. bool okflag = false;
  1197. Filename filename;
  1198. Filename filename_as_referenced;
  1199. bool angle_quotes = false;
  1200. string expr = args;
  1201. // The filename to include might actually be hidden within a
  1202. // manifest definition. Wow. FreeType depends on this.
  1203. // Just to play things safe, since our manifest-expansion logic
  1204. // might not filter out quotes and angle brackets properly, we'll
  1205. // only expand manifests if we don't begin with a quote or bracket.
  1206. if (!expr.empty() && (expr[0] != '"' && expr[0] != '<')) {
  1207. expr = expand_manifests(expr, false);
  1208. }
  1209. if (!expr.empty()) {
  1210. if (expr[0] == '"' && expr[expr.size() - 1] == '"') {
  1211. filename = expr.substr(1, expr.size() - 2);
  1212. okflag = true;
  1213. if (_files.size() == 1) {
  1214. // If we're currently processing a top-level file, record the
  1215. // include directive. We don't need to record includes from
  1216. // included files.
  1217. _quote_includes.insert(filename);
  1218. }
  1219. } else if (expr[0] == '<' && expr[expr.size() - 1] == '>') {
  1220. filename = expr.substr(1, expr.size() - 2);
  1221. if (!_noangles) {
  1222. // If _noangles is true, we don't make a distinction between
  1223. // angle brackets and quote marks--all #include statements are
  1224. // treated the same, as if they used quote marks.
  1225. angle_quotes = true;
  1226. }
  1227. okflag = true;
  1228. if (_files.size() == 1) {
  1229. // If we're currently processing a top-level file, record the
  1230. // include directive. We don't need to record includes from
  1231. // included files.
  1232. _angle_includes.insert(filename);
  1233. }
  1234. }
  1235. }
  1236. filename.set_text();
  1237. filename_as_referenced = filename;
  1238. // Now look for the filename. If we didn't use angle quotes, look
  1239. // first in the current directory.
  1240. bool found_file = false;
  1241. CPPFile::Source source = CPPFile::S_none;
  1242. if (okflag) {
  1243. found_file = false;
  1244. // Search the current directory.
  1245. if (!angle_quotes && !found_file && filename.exists()) {
  1246. found_file = true;
  1247. source = CPPFile::S_local;
  1248. }
  1249. // Search the same directory as the includer.
  1250. if (!angle_quotes && !found_file) {
  1251. Filename match(get_file()._filename.get_dirname(), filename);
  1252. if (match.exists()) {
  1253. filename = match;
  1254. found_file = true;
  1255. source = CPPFile::S_alternate;
  1256. }
  1257. }
  1258. // Now search the angle-include-path
  1259. if (angle_quotes && !found_file && filename.resolve_filename(_angle_include_path)) {
  1260. found_file = true;
  1261. source = CPPFile::S_system;
  1262. }
  1263. // Now search the quote-include-path
  1264. if (!angle_quotes && !found_file) {
  1265. for (int dir=0; dir<_quote_include_path.get_num_directories(); dir++) {
  1266. Filename match(_quote_include_path.get_directory(dir), filename);
  1267. if (match.exists()) {
  1268. filename = match;
  1269. found_file = true;
  1270. source = _quote_include_kind[dir];
  1271. }
  1272. }
  1273. }
  1274. if (!found_file) {
  1275. warning("Cannot find " + filename.get_fullpath(),
  1276. first_line, first_col, first_file);
  1277. } else {
  1278. _last_c = '\0';
  1279. CPPFile file(filename, filename_as_referenced, source);
  1280. // Don't include it if we included it before and it had #pragma once.
  1281. ParsedFiles::const_iterator it = _parsed_files.find(file);
  1282. if (it->_pragma_once) {
  1283. return;
  1284. }
  1285. if (!push_file(file)) {
  1286. warning("Unable to read " + filename.get_fullpath(),
  1287. first_line, first_col, first_file);
  1288. }
  1289. }
  1290. } else {
  1291. warning("Ignoring invalid #include directive",
  1292. first_line, first_col, first_file);
  1293. }
  1294. }
  1295. ////////////////////////////////////////////////////////////////////
  1296. // Function: CPPPreprocessor::handle_pragma_directive
  1297. // Access: Private
  1298. // Description:
  1299. ////////////////////////////////////////////////////////////////////
  1300. void CPPPreprocessor::
  1301. handle_pragma_directive(const string &args, int first_line,
  1302. int first_col, const CPPFile &first_file) {
  1303. if (args == "once") {
  1304. ParsedFiles::iterator it = _parsed_files.find(first_file);
  1305. assert(it != _parsed_files.end());
  1306. it->_pragma_once = true;
  1307. }
  1308. }
  1309. ////////////////////////////////////////////////////////////////////
  1310. // Function: CPPPreprocessor::handle_error_directive
  1311. // Access: Private
  1312. // Description:
  1313. ////////////////////////////////////////////////////////////////////
  1314. void CPPPreprocessor::
  1315. handle_error_directive(const string &args, int first_line,
  1316. int first_col, const CPPFile &first_file) {
  1317. error(args, first_line, first_col, first_file);
  1318. }
  1319. ////////////////////////////////////////////////////////////////////
  1320. // Function: CPPPreprocessor::skip_false_if_block
  1321. // Access: Private
  1322. // Description: We come here when we fail an #if or an #ifdef test,
  1323. // or when we reach the #else clause to something we
  1324. // didn't fail. This function skips all text up until
  1325. // the matching #endif.
  1326. ////////////////////////////////////////////////////////////////////
  1327. void CPPPreprocessor::
  1328. skip_false_if_block(bool consider_elifs) {
  1329. int level = 0;
  1330. _save_comments = false;
  1331. int c = skip_comment(get());
  1332. while (c != EOF) {
  1333. if (c == '#' && _start_of_line) {
  1334. CPPFile first_file = get_file();
  1335. int first_line = get_line_number();
  1336. int first_col = get_col_number();
  1337. // Is this it?
  1338. string command, args;
  1339. c = get_preprocessor_command(c, command);
  1340. c = get_preprocessor_args(c, args);
  1341. if (command == "if" || command == "ifdef" || command == "ifndef") {
  1342. // Hmm, a nested if block. Even more to skip.
  1343. level++;
  1344. } else if (command == "else") {
  1345. if (level == 0 && consider_elifs) {
  1346. // This will do!
  1347. _save_comments = true;
  1348. return;
  1349. }
  1350. } else if (command == "elif") {
  1351. if (level == 0 && consider_elifs) {
  1352. // If we pass this test, we're in.
  1353. _save_comments = true;
  1354. handle_if_directive(args, first_line, first_col, first_file);
  1355. return;
  1356. }
  1357. } else if (command == "endif") {
  1358. // Skip any args.
  1359. if (level == 0) {
  1360. // Here's the end!
  1361. _save_comments = true;
  1362. return;
  1363. }
  1364. level--;
  1365. }
  1366. } else {
  1367. c = skip_comment(get());
  1368. }
  1369. }
  1370. _save_comments = true;
  1371. }
  1372. ////////////////////////////////////////////////////////////////////
  1373. // Function: CPPPreprocessor::get_quoted_char
  1374. // Access: Private
  1375. // Description:
  1376. ////////////////////////////////////////////////////////////////////
  1377. CPPToken CPPPreprocessor::
  1378. get_quoted_char(int c) {
  1379. CPPFile first_file = get_file();
  1380. int first_line = get_line_number();
  1381. int first_col = get_col_number();
  1382. string str = scan_quoted(c);
  1383. YYSTYPE result;
  1384. if (!str.empty()) {
  1385. result.u.integer = (int)str[0];
  1386. } else {
  1387. result.u.integer = 0;
  1388. }
  1389. return CPPToken(CHAR_TOK, first_line, first_col, first_file, str, result);
  1390. }
  1391. ////////////////////////////////////////////////////////////////////
  1392. // Function: CPPPreprocessor::get_quoted_string
  1393. // Access: Private
  1394. // Description:
  1395. ////////////////////////////////////////////////////////////////////
  1396. CPPToken CPPPreprocessor::
  1397. get_quoted_string(int c) {
  1398. CPPFile first_file = get_file();
  1399. int first_line = get_line_number();
  1400. int first_col = get_col_number();
  1401. string str = scan_quoted(c);
  1402. return CPPToken(STRING, first_line, first_col, first_file, str);
  1403. }
  1404. ////////////////////////////////////////////////////////////////////
  1405. // Function: CPPPreprocessor::get_identifier
  1406. // Access: Private
  1407. // Description:
  1408. ////////////////////////////////////////////////////////////////////
  1409. CPPToken CPPPreprocessor::
  1410. get_identifier(int c) {
  1411. CPPFile first_file = get_file();
  1412. int first_line = get_line_number();
  1413. int first_col = get_col_number();
  1414. string name(1, (char)c);
  1415. c = get();
  1416. while (c != EOF && (isalnum(c) || c == '_')) {
  1417. name += c;
  1418. c = get();
  1419. }
  1420. if (c == '\'' || c == '"') {
  1421. // This is actually a wide-character or wide-string literal or
  1422. // some such: a string with an alphanumeric prefix. We don't
  1423. // necessarily try to parse it correctly; for most purposes, we
  1424. // don't care.
  1425. CPPToken token(0);
  1426. if (c == '\'') {
  1427. token = get_quoted_char(c);
  1428. } else {
  1429. token = get_quoted_string(c);
  1430. }
  1431. token._lloc.first_column = first_col;
  1432. return token;
  1433. }
  1434. _last_c = c;
  1435. // Is it a manifest?
  1436. Manifests::const_iterator mi = _manifests.find(name);
  1437. if (mi != _manifests.end() && !should_ignore_manifest((*mi).second)) {
  1438. return expand_manifest((*mi).second);
  1439. }
  1440. // Check for keywords.
  1441. int kw = check_keyword(name);
  1442. // Update our internal visibility flag.
  1443. switch (kw) {
  1444. case KW_BEGIN_PUBLISH:
  1445. preprocessor_vis = V_published;
  1446. break;
  1447. case KW_END_PUBLISH:
  1448. preprocessor_vis = V_public;
  1449. break;
  1450. }
  1451. if (kw != 0) {
  1452. YYSTYPE result;
  1453. result.u.identifier = (CPPIdentifier *)NULL;
  1454. return CPPToken(kw, first_line, first_col, first_file, name, result);
  1455. }
  1456. return CPPToken(SIMPLE_IDENTIFIER, first_line, first_col, first_file,
  1457. name);
  1458. }
  1459. ////////////////////////////////////////////////////////////////////
  1460. // Function: CPPPreprocessor::expand_manifest
  1461. // Access: Private
  1462. // Description:
  1463. ////////////////////////////////////////////////////////////////////
  1464. CPPToken CPPPreprocessor::
  1465. expand_manifest(const CPPManifest *manifest) {
  1466. vector_string args;
  1467. if (manifest->_has_parameters) {
  1468. // Hmm, we're expecting arguments.
  1469. extract_manifest_args(manifest->_name, manifest->_num_parameters,
  1470. manifest->_variadic_param, args);
  1471. }
  1472. string expanded = " " + manifest->expand(args) + " ";
  1473. push_string(expanded, true);
  1474. if (!manifest->_has_parameters) {
  1475. // If the manifest does not use arguments, then disallow recursive
  1476. // expansion.
  1477. _files.back()._ignore_manifest = manifest;
  1478. }
  1479. #ifdef CPP_VERBOSE_LEX
  1480. indent(cerr, _files.size() * 2)
  1481. << "Expanding " << manifest->_name << " to " << expanded << "\n";
  1482. #endif
  1483. return internal_get_next_token();
  1484. }
  1485. ////////////////////////////////////////////////////////////////////
  1486. // Function: CPPPreprocessor::extract_manifest_args
  1487. // Access: Private
  1488. // Description:
  1489. ////////////////////////////////////////////////////////////////////
  1490. void CPPPreprocessor::
  1491. extract_manifest_args(const string &name, int num_args, int va_arg,
  1492. vector_string &args) {
  1493. CPPFile first_file = get_file();
  1494. int first_line = get_line_number();
  1495. int first_col = get_col_number();
  1496. // Skip whitespace till paren.
  1497. int c = _last_c;
  1498. _last_c = '\0';
  1499. while (c != EOF && isspace(c)) {
  1500. c = get();
  1501. }
  1502. if (c != '(') {
  1503. // No paren, so we have only one arg.
  1504. string arg;
  1505. while (c != EOF && (isalnum(c) || c == '_')) {
  1506. arg += c;
  1507. c = get();
  1508. }
  1509. args.push_back(arg);
  1510. } else {
  1511. // Skip paren.
  1512. c = skip_whitespace(get());
  1513. int paren_level = 1;
  1514. string arg;
  1515. while (c != EOF) {
  1516. if (c == ',' && paren_level == 1) {
  1517. args.push_back(arg);
  1518. arg = "";
  1519. c = get();
  1520. } else if (c == '"' || c == '\'') {
  1521. // Quoted string or character.
  1522. int quote_mark = c;
  1523. arg += c;
  1524. c = get();
  1525. while (c != EOF && c != quote_mark && c != '\n') {
  1526. if (c == '\\') {
  1527. arg += c;
  1528. c = get();
  1529. }
  1530. if (c != EOF) {
  1531. arg += c;
  1532. c = get();
  1533. }
  1534. }
  1535. arg += c;
  1536. c = get();
  1537. } else if (c == '(') {
  1538. arg += '(';
  1539. ++paren_level;
  1540. c = get();
  1541. } else if (c == ')') {
  1542. --paren_level;
  1543. if (paren_level == 0) {
  1544. break;
  1545. }
  1546. arg += ')';
  1547. c = get();
  1548. } else if (isspace(c)) {
  1549. // Skip extra whitespace.
  1550. c = skip_whitespace(c);
  1551. if (!arg.empty()) {
  1552. arg += ' ';
  1553. }
  1554. } else if (c == '\\') {
  1555. // It could be a slash before a newline.
  1556. // If so, that's whitespace as well.
  1557. c = get();
  1558. if (c != '\n') {
  1559. arg += '\\';
  1560. } else if (!arg.empty()) {
  1561. arg += ' ';
  1562. c = skip_whitespace(get());
  1563. }
  1564. } else {
  1565. arg += c;
  1566. c = get();
  1567. }
  1568. }
  1569. if (num_args != 0 || !arg.empty()) {
  1570. args.push_back(arg);
  1571. }
  1572. }
  1573. if ((int)args.size() < num_args) {
  1574. warning("Not enough arguments for manifest " + name,
  1575. first_line, first_col, first_file);
  1576. } else if (va_arg < 0 && (int)args.size() > num_args) {
  1577. warning("Too many arguments for manifest " + name,
  1578. first_line, first_col, first_file);
  1579. }
  1580. }
  1581. ////////////////////////////////////////////////////////////////////
  1582. // Function: CPPPreprocessor::expand_defined_function
  1583. // Access: Private
  1584. // Description: Expands the defined(manifest) function to either
  1585. // 1 or 0, depending on whether the manifest exists.
  1586. ////////////////////////////////////////////////////////////////////
  1587. void CPPPreprocessor::
  1588. expand_defined_function(string &expr, size_t q, size_t &p) {
  1589. string result;
  1590. vector_string args;
  1591. extract_manifest_args_inline("defined", 1, -1, args, expr, p);
  1592. if (args.size() >= 1) {
  1593. const string &manifest_name = args[0];
  1594. Manifests::const_iterator mi = _manifests.find(manifest_name);
  1595. if (mi != _manifests.end()) {
  1596. // The macro is defined; the result is "1".
  1597. result = "1";
  1598. } else {
  1599. // The macro is undefined; the result is "0".
  1600. result = "0";
  1601. }
  1602. }
  1603. expr = expr.substr(0, q) + result + expr.substr(p);
  1604. p = q + result.size();
  1605. }
  1606. ////////////////////////////////////////////////////////////////////
  1607. // Function: CPPPreprocessor::expand_manifest_inline
  1608. // Access: Private
  1609. // Description:
  1610. ////////////////////////////////////////////////////////////////////
  1611. void CPPPreprocessor::
  1612. expand_manifest_inline(string &expr, size_t q, size_t &p,
  1613. const CPPManifest *manifest) {
  1614. vector_string args;
  1615. if (manifest->_has_parameters) {
  1616. extract_manifest_args_inline(manifest->_name, manifest->_num_parameters,
  1617. manifest->_variadic_param, args, expr, p);
  1618. }
  1619. string result = manifest->expand(args);
  1620. expr = expr.substr(0, q) + result + expr.substr(p);
  1621. p = q + result.size();
  1622. }
  1623. ////////////////////////////////////////////////////////////////////
  1624. // Function: CPPPreprocessor::extract_manifest_args_inline
  1625. // Access: Private
  1626. // Description:
  1627. ////////////////////////////////////////////////////////////////////
  1628. void CPPPreprocessor::
  1629. extract_manifest_args_inline(const string &name, int num_args,
  1630. int va_arg, vector_string &args,
  1631. const string &expr, size_t &p) {
  1632. // Skip whitespace till paren.
  1633. while (p < expr.size() && isspace(expr[p])) {
  1634. p++;
  1635. }
  1636. if (p >= expr.size() || expr[p] != '(') {
  1637. // No paren, so we have only one arg.
  1638. size_t q = p;
  1639. while (p < expr.size() && (isalnum(expr[p]) || expr[p] == '_')) {
  1640. p++;
  1641. }
  1642. args.push_back(expr.substr(q, p - q));
  1643. } else if (expr[p] == '"' || expr[p] == '\'') {
  1644. // Quoted string or character.
  1645. int quote_mark = expr[p];
  1646. p++;
  1647. while (p < expr.size() && expr[p] != quote_mark && expr[p] != '\n') {
  1648. if (expr[p] == '\\') {
  1649. p++;
  1650. }
  1651. if (p < expr.size()) {
  1652. p++;
  1653. }
  1654. }
  1655. p++;
  1656. } else {
  1657. // Skip paren.
  1658. p++;
  1659. size_t q = p;
  1660. while (p < expr.size() && expr[p] != ')') {
  1661. if (expr[p] == ',') {
  1662. args.push_back(expr.substr(q, p - q));
  1663. q = p+1;
  1664. } else if (expr[p] == '(') {
  1665. // Nested parens.
  1666. int paren_level = 1;
  1667. while (p+1 < expr.size() && paren_level > 0) {
  1668. p++;
  1669. if (expr[p] == '(') {
  1670. paren_level++;
  1671. } else if (expr[p] == ')') {
  1672. paren_level--;
  1673. }
  1674. }
  1675. }
  1676. p++;
  1677. }
  1678. args.push_back(expr.substr(q, p - q));
  1679. if (p < expr.size() && expr[p] == ')') {
  1680. p++;
  1681. }
  1682. }
  1683. if ((int)args.size() < num_args) {
  1684. warning("Not enough arguments for manifest " + name);
  1685. } else if (va_arg < 0 && (int)args.size() > num_args) {
  1686. warning("Too many arguments for manifest " + name);
  1687. }
  1688. }
  1689. ////////////////////////////////////////////////////////////////////
  1690. // Function: CPPPreprocessor::get_number
  1691. // Access: Private
  1692. // Description:
  1693. ////////////////////////////////////////////////////////////////////
  1694. CPPToken CPPPreprocessor::
  1695. get_number(int c, int c2) {
  1696. CPPFile first_file = get_file();
  1697. int first_line = get_line_number();
  1698. int first_col = get_col_number();
  1699. string num(1, (char)c);
  1700. bool leading_zero = (c == '0');
  1701. bool decimal_point = (c == '.');
  1702. if (c2 == 0) {
  1703. c = get();
  1704. } else {
  1705. c = c2;
  1706. }
  1707. if (leading_zero && c == 'x') {
  1708. // Here we have a hex number.
  1709. num += c;
  1710. c = get();
  1711. while (c != EOF && (isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f'))) {
  1712. num += c;
  1713. c = get();
  1714. }
  1715. while (c == 'L' || c == 'U') {
  1716. // We allow (and ignore) an 'L' and/or 'U' following the number.
  1717. c = get();
  1718. }
  1719. _last_c = c;
  1720. YYSTYPE result;
  1721. result.u.integer = strtol(num.c_str(), (char **)NULL, 16);
  1722. return CPPToken(INTEGER, first_line, first_col, first_file, num, result);
  1723. }
  1724. while (c != EOF && isdigit(c)) {
  1725. num += c;
  1726. c = get();
  1727. }
  1728. if (c == '.' && !decimal_point) {
  1729. // Now we have a floating-point number.
  1730. decimal_point = true;
  1731. num += c;
  1732. c = get();
  1733. while (c != EOF && isdigit(c)) {
  1734. num += c;
  1735. c = get();
  1736. }
  1737. }
  1738. if (decimal_point) {
  1739. if (tolower(c) == 'e') {
  1740. // An exponent is allowed.
  1741. num += c;
  1742. c = get();
  1743. if (c == '-' || c == '+') {
  1744. num += c;
  1745. c = get();
  1746. }
  1747. while (c != EOF && isdigit(c)) {
  1748. num += c;
  1749. c = get();
  1750. }
  1751. }
  1752. if (c == 'f') {
  1753. // We allow (and ignore) an 'f' following the number.
  1754. c = get();
  1755. }
  1756. _last_c = c;
  1757. YYSTYPE result;
  1758. result.u.real = pstrtod(num.c_str(), (char **)NULL);
  1759. return CPPToken(REAL, first_line, first_col, first_file, num, result);
  1760. }
  1761. // This is a decimal or octal integer number.
  1762. while (c == 'L' || c == 'U') {
  1763. // We allow (and ignore) an 'L' and/or 'U' following the number.
  1764. c = get();
  1765. }
  1766. _last_c = c;
  1767. YYSTYPE result;
  1768. if (leading_zero) {
  1769. // A leading zero implies an octal number. strtol() is supposed
  1770. // to be able to make this distinction by itself, but we'll do it
  1771. // explicitly just to be sure.
  1772. result.u.integer = strtol(num.c_str(), (char **)NULL, 8);
  1773. } else {
  1774. // A decimal (base 10) integer.
  1775. result.u.integer = strtol(num.c_str(), (char **)NULL, 10);
  1776. }
  1777. return CPPToken(INTEGER, first_line, first_col, first_file, num, result);
  1778. }
  1779. ////////////////////////////////////////////////////////////////////
  1780. // Function: CPPPreprocessor::check_keyword
  1781. // Access: Private, Static
  1782. // Description:
  1783. ////////////////////////////////////////////////////////////////////
  1784. int CPPPreprocessor::
  1785. check_keyword(const string &name) {
  1786. if (name == "__begin_publish") return KW_BEGIN_PUBLISH;
  1787. if (name == "__blocking") return KW_BLOCKING;
  1788. if (name == "bool") return KW_BOOL;
  1789. if (name == "catch") return KW_CATCH;
  1790. if (name == "char") return KW_CHAR;
  1791. if (name == "char16_t") return KW_CHAR16_T;
  1792. if (name == "char32_t") return KW_CHAR32_T;
  1793. if (name == "class") return KW_CLASS;
  1794. if (name == "const") return KW_CONST;
  1795. if (name == "delete") return KW_DELETE;
  1796. if (name == "double") return KW_DOUBLE;
  1797. if (name == "dynamic_cast") return KW_DYNAMIC_CAST;
  1798. if (name == "else") return KW_ELSE;
  1799. if (name == "__end_publish") return KW_END_PUBLISH;
  1800. if (name == "enum") return KW_ENUM;
  1801. if (name == "extern") return KW_EXTERN;
  1802. if (name == "__extension") return KW_EXTENSION;
  1803. if (name == "explicit") return KW_EXPLICIT;
  1804. if (name == "__published") return KW_PUBLISHED;
  1805. if (name == "false") return KW_FALSE;
  1806. if (name == "float") return KW_FLOAT;
  1807. if (name == "friend") return KW_FRIEND;
  1808. if (name == "for") return KW_FOR;
  1809. if (name == "goto") return KW_GOTO;
  1810. if (name == "if") return KW_IF;
  1811. if (name == "inline") return KW_INLINE;
  1812. if (name == "int") return KW_INT;
  1813. if (name == "long") return KW_LONG;
  1814. if (name == "__make_property") return KW_MAKE_PROPERTY;
  1815. if (name == "__make_seq") return KW_MAKE_SEQ;
  1816. if (name == "mutable") return KW_MUTABLE;
  1817. if (name == "namespace") return KW_NAMESPACE;
  1818. if (name == "noexcept") return KW_NOEXCEPT;
  1819. if (name == "new") return KW_NEW;
  1820. if (name == "operator") return KW_OPERATOR;
  1821. if (name == "private") return KW_PRIVATE;
  1822. if (name == "protected") return KW_PROTECTED;
  1823. if (name == "public") return KW_PUBLIC;
  1824. if (name == "register") return KW_REGISTER;
  1825. if (name == "return") return KW_RETURN;
  1826. if (name == "short") return KW_SHORT;
  1827. if (name == "signed") return KW_SIGNED;
  1828. if (name == "sizeof") return KW_SIZEOF;
  1829. if (name == "static") return KW_STATIC;
  1830. if (name == "static_cast") return KW_STATIC_CAST;
  1831. if (name == "struct") return KW_STRUCT;
  1832. if (name == "template") return KW_TEMPLATE;
  1833. if (name == "throw") return KW_THROW;
  1834. if (name == "true") return KW_TRUE;
  1835. if (name == "try") return KW_TRY;
  1836. if (name == "typedef") return KW_TYPEDEF;
  1837. if (name == "typename") return KW_TYPENAME;
  1838. if (name == "union") return KW_UNION;
  1839. if (name == "unsigned") return KW_UNSIGNED;
  1840. if (name == "using") return KW_USING;
  1841. if (name == "virtual") return KW_VIRTUAL;
  1842. if (name == "void") return KW_VOID;
  1843. if (name == "volatile") return KW_VOLATILE;
  1844. if (name == "wchar_t") return KW_WCHAR_T;
  1845. if (name == "while") return KW_WHILE;
  1846. // These are alternative ways to refer to built-in operators.
  1847. if (name == "and") return ANDAND;
  1848. if (name == "and_eq") return ANDEQUAL;
  1849. if (name == "bitand") return '&';
  1850. if (name == "bitor") return '|';
  1851. if (name == "compl") return '~';
  1852. if (name == "not") return '!';
  1853. if (name == "not_eq") return NECOMPARE;
  1854. if (name == "or") return OROR;
  1855. if (name == "or_eq") return OREQUAL;
  1856. if (name == "xor") return '^';
  1857. if (name == "xor_eq") return XOREQUAL;
  1858. if (!cpp_longlong_keyword.empty() && name == cpp_longlong_keyword) {
  1859. return KW_LONGLONG;
  1860. }
  1861. return 0;
  1862. }
  1863. ////////////////////////////////////////////////////////////////////
  1864. // Function: CPPPreprocessor::scan_escape_sequence
  1865. // Access: Private
  1866. // Description:
  1867. ////////////////////////////////////////////////////////////////////
  1868. int CPPPreprocessor::
  1869. scan_escape_sequence(int c) {
  1870. if (c != '\\') {
  1871. return c;
  1872. }
  1873. c = get();
  1874. switch (c) {
  1875. case 'a':
  1876. return '\a';
  1877. case 'b':
  1878. return '\b';
  1879. case 'f':
  1880. return '\f';
  1881. case 'n':
  1882. return '\n';
  1883. case 'r':
  1884. return '\r';
  1885. case 't':
  1886. return '\t';
  1887. case 'v':
  1888. return '\v';
  1889. case 'e':
  1890. // \e is non-standard, buT GCC supports it.
  1891. return '\x1B';
  1892. case 'x':
  1893. // hex character.
  1894. c = get();
  1895. if (isxdigit(c)) {
  1896. int val = hex_val(c);
  1897. c = get();
  1898. if (isxdigit(c)) {
  1899. val = (val << 4) | hex_val(c);
  1900. } else {
  1901. unget(c);
  1902. }
  1903. return val;
  1904. }
  1905. break;
  1906. case '0':
  1907. case '1':
  1908. case '2':
  1909. case '3':
  1910. case '4':
  1911. case '5':
  1912. case '6':
  1913. case '7':
  1914. // Octal character.
  1915. {
  1916. int val = (c - '0');
  1917. c = get();
  1918. if (c >= '0' && c <= '7') {
  1919. val = (val << 3) | (c - '0');
  1920. c = get();
  1921. if (c >= '0' && c <= '7') {
  1922. val = (val << 3) | (c - '0');
  1923. } else {
  1924. unget(c);
  1925. }
  1926. } else {
  1927. unget(c);
  1928. }
  1929. return val;
  1930. }
  1931. }
  1932. // Simply output the following character.
  1933. return c;
  1934. }
  1935. ////////////////////////////////////////////////////////////////////
  1936. // Function: CPPPreprocessor::scan_quoted
  1937. // Access: Private
  1938. // Description:
  1939. ////////////////////////////////////////////////////////////////////
  1940. string CPPPreprocessor::
  1941. scan_quoted(int c) {
  1942. int quote_mark = c;
  1943. string str;
  1944. c = get();
  1945. while (c != EOF && c != '\n' && c != quote_mark) {
  1946. if (c == '\\') {
  1947. // Backslash means a special character follows.
  1948. c = scan_escape_sequence(c);
  1949. }
  1950. str += c;
  1951. c = get();
  1952. }
  1953. if (c != quote_mark) {
  1954. warning("Unclosed string");
  1955. }
  1956. return str;
  1957. }
  1958. ////////////////////////////////////////////////////////////////////
  1959. // Function: CPPPreprocessor::should_ignore_manifest
  1960. // Access: Public
  1961. // Description: Returns true if the manifest is one that is being
  1962. // ignored right now (presumably because we are
  1963. // presently expanding it).
  1964. ////////////////////////////////////////////////////////////////////
  1965. bool CPPPreprocessor::
  1966. should_ignore_manifest(const CPPManifest *manifest) const {
  1967. Files::const_iterator fi;
  1968. for (fi = _files.begin(); fi != _files.end(); ++fi) {
  1969. if ((*fi)._ignore_manifest == manifest) {
  1970. return true;
  1971. }
  1972. }
  1973. return false;
  1974. }
  1975. ////////////////////////////////////////////////////////////////////
  1976. // Function: CPPPreprocessor::should_ignore_preprocessor
  1977. // Access: Public
  1978. // Description: Returns true if we should ignore any preprocessor
  1979. // directives (e.g. we're presently expanding a
  1980. // manifest).
  1981. ////////////////////////////////////////////////////////////////////
  1982. bool CPPPreprocessor::
  1983. should_ignore_preprocessor() const {
  1984. Files::const_iterator fi;
  1985. for (fi = _files.begin(); fi != _files.end(); ++fi) {
  1986. if ((*fi)._ignore_manifest != NULL) {
  1987. return true;
  1988. }
  1989. }
  1990. return false;
  1991. }
  1992. ////////////////////////////////////////////////////////////////////
  1993. // Function: CPPPreprocessor::get
  1994. // Access: Private
  1995. // Description:
  1996. ////////////////////////////////////////////////////////////////////
  1997. int CPPPreprocessor::
  1998. get() {
  1999. if (_unget != '\0') {
  2000. int c = _unget;
  2001. _unget = '\0';
  2002. return c;
  2003. }
  2004. if (_files.empty()) {
  2005. return EOF;
  2006. }
  2007. int c = _files.back().get();
  2008. while (c == EOF && !_files.empty()) {
  2009. #ifdef CPP_VERBOSE_LEX
  2010. indent(cerr, _files.size() * 2)
  2011. << "End of input stream, restoring to previous input\n";
  2012. #endif
  2013. int last_c = _files.back()._prev_last_c;
  2014. _files.pop_back();
  2015. if (last_c != '\0') {
  2016. c = last_c;
  2017. } else if (!_files.empty()) {
  2018. c = _files.back().get();
  2019. }
  2020. }
  2021. if (c == '\n') {
  2022. _start_of_line = true;
  2023. } else if (!isspace(c) && c != '#') {
  2024. _start_of_line = false;
  2025. }
  2026. return c;
  2027. }
  2028. ////////////////////////////////////////////////////////////////////
  2029. // Function: CPPPreprocessor::unget
  2030. // Access: Private
  2031. // Description:
  2032. ////////////////////////////////////////////////////////////////////
  2033. void CPPPreprocessor::
  2034. unget(int c) {
  2035. assert(_unget == '\0');
  2036. _unget = c;
  2037. }
  2038. ////////////////////////////////////////////////////////////////////
  2039. // Function: CPPPreprocessor::nested_parse_template_instantiation
  2040. // Access: Private
  2041. // Description: Recursively invokes yacc to parse the stuff within
  2042. // angle brackets that's the template instantiation part
  2043. // of an identifier. This involves setting and
  2044. // restoring some state flags so we can return EOF when
  2045. // we reach the closing bracket.
  2046. ////////////////////////////////////////////////////////////////////
  2047. CPPTemplateParameterList *CPPPreprocessor::
  2048. nested_parse_template_instantiation(CPPTemplateScope *scope) {
  2049. #ifdef CPP_VERBOSE_LEX
  2050. indent(cerr, _files.size() * 2)
  2051. << "Beginning nested parse\n";
  2052. #endif
  2053. assert(scope != NULL);
  2054. State old_state = _state;
  2055. int old_nesting = _paren_nesting;
  2056. const CPPTemplateParameterList &formal_params = scope->_parameters;
  2057. CPPTemplateParameterList::Parameters::const_iterator pi;
  2058. _angle_bracket_found = false;
  2059. CPPToken token = internal_get_next_token();
  2060. if (token._token == '>') {
  2061. _angle_bracket_found = true;
  2062. } else {
  2063. _saved_tokens.push_back(token);
  2064. }
  2065. CPPTemplateParameterList *actual_params = new CPPTemplateParameterList;
  2066. for (pi = formal_params._parameters.begin();
  2067. pi != formal_params._parameters.end() && !_angle_bracket_found;
  2068. ++pi) {
  2069. _state = S_nested;
  2070. _paren_nesting = 0;
  2071. CPPFile first_file = get_file();
  2072. int first_line = get_line_number();
  2073. int first_col = get_col_number();
  2074. CPPDeclaration *decl = (*pi);
  2075. if (decl->as_type()) {
  2076. _saved_tokens.push_back(CPPToken(START_TYPE));
  2077. CPPType *type = ::parse_type(this, current_scope, global_scope);
  2078. if (type == NULL) {
  2079. warning("Invalid type", first_line, first_col, first_file);
  2080. skip_to_end_nested();
  2081. type = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  2082. }
  2083. actual_params->_parameters.push_back(type);
  2084. } else {
  2085. _saved_tokens.push_back(CPPToken(START_CONST_EXPR));
  2086. CPPExpression *expr = parse_const_expr(this, current_scope, global_scope);
  2087. if (expr == NULL) {
  2088. warning("Invalid expression", first_line, first_col, first_file);
  2089. skip_to_end_nested();
  2090. expr = new CPPExpression(0);
  2091. }
  2092. actual_params->_parameters.push_back(expr);
  2093. }
  2094. }
  2095. if (!_angle_bracket_found) {
  2096. warning("Ignoring extra parameters in template instantiation");
  2097. skip_to_angle_bracket();
  2098. }
  2099. _state = old_state;
  2100. _paren_nesting = old_nesting;
  2101. _angle_bracket_found = false;
  2102. #ifdef CPP_VERBOSE_LEX
  2103. indent(cerr, _files.size() * 2)
  2104. << "Ending nested parse\n";
  2105. #endif
  2106. return actual_params;
  2107. }
  2108. ////////////////////////////////////////////////////////////////////
  2109. // Function: CPPPreprocessor::skip_to_end_nested
  2110. // Access: Private
  2111. // Description: This is an error-recovery function, called after
  2112. // returning from a nested parse. If the state is not
  2113. // S_end_nested, there was an error in parsing the
  2114. // nested tokens, and not all of the nested tokens may
  2115. // have been consumed. This function will consume the
  2116. // rest of the nested tokens.
  2117. ////////////////////////////////////////////////////////////////////
  2118. void CPPPreprocessor::
  2119. skip_to_end_nested() {
  2120. #ifdef CPP_VERBOSE_LEX
  2121. indent(cerr, _files.size() * 2)
  2122. << "Skipping tokens:\n";
  2123. #endif
  2124. // Eat any eof tokens on the pushback stack.
  2125. while (!_saved_tokens.empty() && _saved_tokens.back().is_eof()) {
  2126. _saved_tokens.pop_back();
  2127. }
  2128. while (_state != S_end_nested && _state != S_eof) {
  2129. get_next_token();
  2130. }
  2131. #ifdef CPP_VERBOSE_LEX
  2132. indent(cerr, _files.size() * 2)
  2133. << "Done skipping tokens.\n";
  2134. #endif
  2135. }
  2136. ////////////////////////////////////////////////////////////////////
  2137. // Function: CPPPreprocessor::skip_to_angle_bracket
  2138. // Access: Private
  2139. // Description: This is an error-recovery function, called after
  2140. // returning from a nested parse. If we haven't yet
  2141. // consumed the closing angle bracket on the template
  2142. // instantiation, keep consuming tokens until we do.
  2143. ////////////////////////////////////////////////////////////////////
  2144. void CPPPreprocessor::
  2145. skip_to_angle_bracket() {
  2146. #ifdef CPP_VERBOSE_LEX
  2147. indent(cerr, _files.size() * 2)
  2148. << "Skipping tokens:\n";
  2149. #endif
  2150. while (!_angle_bracket_found && _state != S_eof) {
  2151. _state = S_nested;
  2152. while (_state != S_end_nested && _state != S_eof) {
  2153. get_next_token();
  2154. }
  2155. }
  2156. // Eat any eof tokens on the pushback stack.
  2157. while (!_saved_tokens.empty() && _saved_tokens.back().is_eof()) {
  2158. _saved_tokens.pop_back();
  2159. }
  2160. #ifdef CPP_VERBOSE_LEX
  2161. indent(cerr, _files.size() * 2)
  2162. << "Done skipping tokens.\n";
  2163. #endif
  2164. }