cppPreprocessor.cxx 67 KB

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