cppPreprocessor.cxx 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  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. _parsing_template_params = 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 (_parsing_template_params && _paren_nesting <= 0) {
  744. // Don't parse > as greater-than when parsing a template list, as per C++11.
  745. // However, nested >> must be preserved, such as in A<(2>>1)>
  746. break;
  747. }
  748. if (next_c == '>' && _last_c == '=') {
  749. _last_c = get();
  750. return CPPToken(RSHIFTEQUAL, first_line, first_col, first_file);
  751. }
  752. if (next_c == '>') return CPPToken(RSHIFT, first_line, first_col, first_file);
  753. if (next_c == '=') return CPPToken(GECOMPARE, first_line, first_col, first_file);
  754. break;
  755. case '|':
  756. if (next_c == '|') return CPPToken(OROR, first_line, first_col, first_file);
  757. if (next_c == '=') return CPPToken(OREQUAL, first_line, first_col, first_file);
  758. break;
  759. case '&':
  760. if (next_c == '&') return CPPToken(ANDAND, first_line, first_col, first_file);
  761. if (next_c == '=') return CPPToken(ANDEQUAL, first_line, first_col, first_file);
  762. break;
  763. case '^':
  764. if (next_c == '=') return CPPToken(XOREQUAL, first_line, first_col, first_file);
  765. break;
  766. case '=':
  767. if (next_c == '=') return CPPToken(EQCOMPARE, first_line, first_col, first_file);
  768. break;
  769. case '!':
  770. if (next_c == '=') return CPPToken(NECOMPARE, first_line, first_col, first_file);
  771. break;
  772. case '.':
  773. if (next_c == '*') return CPPToken(DOT_STAR, first_line, first_col, first_file);
  774. if (next_c == '.' && _last_c == '.') {
  775. _last_c = get();
  776. return CPPToken(ELLIPSIS, first_line, first_col, first_file);
  777. }
  778. break;
  779. case ':':
  780. if (next_c == ':') return CPPToken(SCOPE, first_line, first_col, first_file);
  781. if (next_c == '>') return CPPToken(']', first_line, first_col, first_file);
  782. break;
  783. case '*':
  784. if (next_c == '=') return CPPToken(TIMESEQUAL, first_line, first_col, first_file);
  785. break;
  786. case '/':
  787. if (next_c == '=') return CPPToken(DIVIDEEQUAL, first_line, first_col, first_file);
  788. break;
  789. case '%':
  790. if (next_c == '=') return CPPToken(MODEQUAL, first_line, first_col, first_file);
  791. if (next_c == '>') return CPPToken('}', first_line, first_col, first_file);
  792. break;
  793. }
  794. // It wasn't any of the two- or three-character tokens, so put back
  795. // the lookahead character and return the one-character token.
  796. unget(_last_c);
  797. _last_c = next_c;
  798. if (_state == S_nested) {
  799. // If we're running a nested lexer, keep track of the paren
  800. // levels. When we encounter a comma or closing angle bracket at
  801. // the bottom level, we stop.
  802. switch (c) {
  803. case '(':
  804. case '[':
  805. _paren_nesting++;
  806. break;
  807. case ')':
  808. case ']':
  809. _paren_nesting--;
  810. break;
  811. case ',':
  812. if (_paren_nesting <= 0) {
  813. _state = S_end_nested;
  814. return CPPToken::eof();
  815. }
  816. break;
  817. case '>':
  818. if (_paren_nesting <= 0) {
  819. _parsing_template_params = false;
  820. _state = S_end_nested;
  821. return CPPToken::eof();
  822. }
  823. }
  824. }
  825. // We skip whitespace here again, so that we can read any comments
  826. // after this point before we parse this line.
  827. _last_c = skip_whitespace(_last_c);
  828. return CPPToken(c, first_line, first_col, first_file);
  829. }
  830. ////////////////////////////////////////////////////////////////////
  831. // Function: CPPPreprocessor::skip_whitespace
  832. // Access: Private
  833. // Description:
  834. ////////////////////////////////////////////////////////////////////
  835. int CPPPreprocessor::
  836. skip_whitespace(int c) {
  837. while (c != EOF) {
  838. c = skip_comment(c);
  839. if (c == '\\') {
  840. // This does not usually occur in the middle of unquoted C++
  841. // code, except before a newline character.
  842. c = get();
  843. if (c != '\n') {
  844. unget(c);
  845. return '\\';
  846. }
  847. }
  848. if (!isspace(c)) {
  849. return c;
  850. }
  851. c = get();
  852. }
  853. return c;
  854. }
  855. ////////////////////////////////////////////////////////////////////
  856. // Function: CPPPreprocessor::skip_comment
  857. // Access: Private
  858. // Description:
  859. ////////////////////////////////////////////////////////////////////
  860. int CPPPreprocessor::
  861. skip_comment(int c) {
  862. while (c == '/') {
  863. int next_c = get();
  864. if (next_c == '*') {
  865. _last_cpp_comment = false;
  866. c = skip_c_comment(get());
  867. } else if (next_c == '/') {
  868. c = skip_cpp_comment(get());
  869. break;
  870. } else {
  871. _last_cpp_comment = false;
  872. unget(next_c);
  873. return c;
  874. }
  875. }
  876. if (!isspace(c)) {
  877. _last_cpp_comment = false;
  878. }
  879. return c;
  880. }
  881. ////////////////////////////////////////////////////////////////////
  882. // Function: CPPPreprocessor::skip_c_comment
  883. // Access: Private
  884. // Description:
  885. ////////////////////////////////////////////////////////////////////
  886. int CPPPreprocessor::
  887. skip_c_comment(int c) {
  888. if (_save_comments) {
  889. CPPCommentBlock *comment = new CPPCommentBlock;
  890. _comments.push_back(comment);
  891. comment->_file = get_file();
  892. comment->_line_number = get_line_number();
  893. comment->_last_line = get_line_number();
  894. comment->_col_number = get_col_number() - 2;
  895. comment->_c_style = true;
  896. comment->_comment = "/*";
  897. while (c != EOF) {
  898. if (c == '*') {
  899. comment->_comment += c;
  900. c = get();
  901. if (c == '/') {
  902. comment->_comment += c;
  903. comment->_last_line = get_line_number();
  904. return get();
  905. }
  906. } else {
  907. comment->_comment += c;
  908. c = get();
  909. }
  910. }
  911. warning("Comment is unterminated",
  912. comment->_line_number, comment->_col_number,
  913. comment->_file);
  914. } else {
  915. CPPFile first_file = get_file();
  916. int first_line_number = get_line_number();
  917. int first_col_number = get_col_number() - 2;
  918. while (c != EOF) {
  919. if (c == '*') {
  920. c = get();
  921. if (c == '/') {
  922. return get();
  923. }
  924. } else {
  925. c = get();
  926. }
  927. }
  928. warning("Comment is unterminated",
  929. first_line_number, first_col_number,
  930. first_file);
  931. }
  932. return c;
  933. }
  934. ////////////////////////////////////////////////////////////////////
  935. // Function: CPPPreprocessor::skip_cpp_comment
  936. // Access: Private
  937. // Description:
  938. ////////////////////////////////////////////////////////////////////
  939. int CPPPreprocessor::
  940. skip_cpp_comment(int c) {
  941. if (_save_comments) {
  942. CPPCommentBlock *comment;
  943. int line_number = get_line_number();
  944. if (c == '\n') {
  945. // We have to subtract one from the line number as we just
  946. // fetched a newline.
  947. --line_number;
  948. }
  949. if (_last_cpp_comment && !_comments.empty() &&
  950. _comments.back()->_last_line >= line_number - 1) {
  951. // If the last non-whitespace character read was also part of a
  952. // C++ comment, then this is just a continuation of that comment
  953. // block. However, if there was a line without comment in between,
  954. // it starts a new block anyway.
  955. comment = _comments.back();
  956. assert(!comment->_c_style);
  957. comment->_comment += "//";
  958. } else {
  959. // Otherwise, this begins a new comment block.
  960. comment = new CPPCommentBlock;
  961. comment->_file = get_file();
  962. comment->_line_number = line_number;
  963. comment->_last_line = line_number;
  964. comment->_col_number = get_col_number() - 2;
  965. comment->_c_style = false;
  966. comment->_comment = "//";
  967. _comments.push_back(comment);
  968. }
  969. while (c != EOF && c != '\n') {
  970. comment->_comment += c;
  971. c = get();
  972. }
  973. comment->_comment += '\n';
  974. comment->_last_line = line_number;
  975. _last_cpp_comment = true;
  976. } else {
  977. while (c != EOF && c != '\n') {
  978. c = get();
  979. }
  980. }
  981. return c;
  982. }
  983. ////////////////////////////////////////////////////////////////////
  984. // Function: CPPPreprocessor::process_directive
  985. // Access: Private
  986. // Description:
  987. ////////////////////////////////////////////////////////////////////
  988. int CPPPreprocessor::
  989. process_directive(int c) {
  990. CPPFile first_file = get_file();
  991. int first_line = get_line_number();
  992. int first_col = get_col_number();
  993. string command, args;
  994. c = get_preprocessor_command(c, command);
  995. c = get_preprocessor_args(c, args);
  996. #ifdef CPP_VERBOSE_LEX
  997. indent(cerr, _files.size() * 2)
  998. << "#" << command << " " << args << "\n";
  999. #endif
  1000. if (command == "define") {
  1001. handle_define_directive(args, first_line, first_col, first_file);
  1002. } else if (command == "undef") {
  1003. handle_undef_directive(args, first_line, first_col, first_file);
  1004. } else if (command == "ifdef") {
  1005. handle_ifdef_directive(args, first_line, first_col, first_file);
  1006. } else if (command == "ifndef") {
  1007. handle_ifndef_directive(args, first_line, first_col, first_file);
  1008. } else if (command == "if") {
  1009. handle_if_directive(args, first_line, first_col, first_file);
  1010. } else if (command == "else" || command == "elif") {
  1011. // Presumably this follows some #if or #ifdef. We don't bother to
  1012. // check this, however.
  1013. skip_false_if_block(false);
  1014. } else if (command == "endif") {
  1015. // Presumably this follows some #if or #ifdef. We don't bother to
  1016. // check this, however.
  1017. } else if (command == "include") {
  1018. handle_include_directive(args, first_line, first_col, first_file);
  1019. } else if (command == "pragma") {
  1020. handle_pragma_directive(args, first_line, first_col, first_file);
  1021. } else if (command == "ident") {
  1022. // Quietly ignore idents.
  1023. } else if (command == "error") {
  1024. handle_error_directive(args, first_line, first_col, first_file);
  1025. } else {
  1026. warning("Ignoring unknown directive #" + command,
  1027. first_line, first_col, first_file);
  1028. }
  1029. _start_of_line = true;
  1030. return '\n';
  1031. }
  1032. ////////////////////////////////////////////////////////////////////
  1033. // Function: CPPPreprocessor::get_preprocessor_command
  1034. // Access: Private
  1035. // Description:
  1036. ////////////////////////////////////////////////////////////////////
  1037. int CPPPreprocessor::
  1038. get_preprocessor_command(int c, string &command) {
  1039. // Skip the hash mark.
  1040. assert(c == '#');
  1041. c = get();
  1042. // Also skip any whitespace following the hash mark--but don't skip
  1043. // past a newline.
  1044. while (c != EOF && (c == ' ' || c == '\t')) {
  1045. c = get();
  1046. }
  1047. // The next sequence of characters is the command.
  1048. while (c != EOF && (isalnum(c) || c == '_')) {
  1049. command += c;
  1050. c = get();
  1051. }
  1052. return c;
  1053. }
  1054. ////////////////////////////////////////////////////////////////////
  1055. // Function: CPPPreprocessor::get_preprocessor_args
  1056. // Access: Private
  1057. // Description:
  1058. ////////////////////////////////////////////////////////////////////
  1059. int CPPPreprocessor::
  1060. get_preprocessor_args(int c, string &args) {
  1061. // Following the command, the rest of the line, as well as any text
  1062. // on successive lines, is part of the arguments to the command.
  1063. while (c != EOF && c != '\n') {
  1064. if (c == '\\') {
  1065. int next_c = get();
  1066. if (next_c == '\n') {
  1067. // Here we have an escaped newline: a continuation.
  1068. args += '\n';
  1069. } else {
  1070. // Just a backslash followed by some non-backslash, keep both.
  1071. args += c;
  1072. if (next_c != EOF) {
  1073. args += next_c;
  1074. }
  1075. }
  1076. } else {
  1077. args += c;
  1078. }
  1079. c = skip_comment(get());
  1080. }
  1081. // Remove any leading and trailing whitespace from the args.
  1082. args = trim_blanks(args);
  1083. return c;
  1084. }
  1085. ////////////////////////////////////////////////////////////////////
  1086. // Function: CPPPreprocessor::handle_define_directive
  1087. // Access: Private
  1088. // Description:
  1089. ////////////////////////////////////////////////////////////////////
  1090. void CPPPreprocessor::
  1091. handle_define_directive(const string &args, int first_line,
  1092. int first_col, const CPPFile &first_file) {
  1093. if (args.empty()) {
  1094. warning("Ignoring empty #define directive",
  1095. first_line, first_col, first_file);
  1096. } else {
  1097. CPPManifest *manifest = new CPPManifest(args, first_file);
  1098. manifest->_vis = preprocessor_vis;
  1099. if (!manifest->_has_parameters) {
  1100. string expr_string = manifest->expand();
  1101. if (!expr_string.empty()) {
  1102. manifest->_expr = parse_expr(expr_string, global_scope, global_scope);
  1103. }
  1104. }
  1105. // ok one memory leak here..
  1106. Manifests::iterator mi = _manifests.find(manifest->_name);
  1107. if(mi != _manifests.end())
  1108. {
  1109. // i do not see a goodway to compare the old and new hmmmm
  1110. //cerr << "Warning Overwriting Constant " << manifest->_name << "\n";
  1111. delete mi->second;
  1112. }
  1113. _manifests[manifest->_name] = manifest;
  1114. }
  1115. }
  1116. ////////////////////////////////////////////////////////////////////
  1117. // Function: CPPPreprocessor::handle_undef_directive
  1118. // Access: Private
  1119. // Description:
  1120. ////////////////////////////////////////////////////////////////////
  1121. void CPPPreprocessor::
  1122. handle_undef_directive(const string &args, int first_line,
  1123. int first_col, const CPPFile &first_file) {
  1124. if (args.empty()) {
  1125. warning("Ignoring empty #undef directive",
  1126. first_line, first_col, first_file);
  1127. } else {
  1128. Manifests::iterator mi = _manifests.find(args);
  1129. if (mi != _manifests.end()) {
  1130. _manifests.erase(mi);
  1131. }
  1132. }
  1133. }
  1134. ////////////////////////////////////////////////////////////////////
  1135. // Function: CPPPreprocessor::handle_ifdef_directive
  1136. // Access: Private
  1137. // Description:
  1138. ////////////////////////////////////////////////////////////////////
  1139. void CPPPreprocessor::
  1140. handle_ifdef_directive(const string &args, int, int, const CPPFile &) {
  1141. Manifests::const_iterator mi = _manifests.find(args);
  1142. if (mi != _manifests.end()) {
  1143. // The macro is defined. We continue.
  1144. return;
  1145. }
  1146. // The macro is undefined. Skip stuff.
  1147. skip_false_if_block(true);
  1148. }
  1149. ////////////////////////////////////////////////////////////////////
  1150. // Function: CPPPreprocessor::handle_ifndef_directive
  1151. // Access: Private
  1152. // Description:
  1153. ////////////////////////////////////////////////////////////////////
  1154. void CPPPreprocessor::
  1155. handle_ifndef_directive(const string &args, int, int, const CPPFile &) {
  1156. Manifests::const_iterator mi = _manifests.find(args);
  1157. if (mi == _manifests.end()) {
  1158. // The macro is undefined. We continue.
  1159. return;
  1160. }
  1161. // The macro is defined. Skip stuff.
  1162. skip_false_if_block(true);
  1163. }
  1164. ////////////////////////////////////////////////////////////////////
  1165. // Function: CPPPreprocessor::handle_if_directive
  1166. // Access: Private
  1167. // Description:
  1168. ////////////////////////////////////////////////////////////////////
  1169. void CPPPreprocessor::
  1170. handle_if_directive(const string &args, int first_line,
  1171. int first_col, const CPPFile &first_file) {
  1172. CPPExpression *expr = parse_expr(args, global_scope, global_scope);
  1173. int expression_result = 0;
  1174. if (expr != (CPPExpression *)NULL) {
  1175. CPPExpression::Result result = expr->evaluate();
  1176. if (result._type == CPPExpression::RT_error) {
  1177. warning("Ignoring invalid expression " + args,
  1178. first_line, first_col, first_file);
  1179. } else {
  1180. expression_result = result.as_integer();
  1181. }
  1182. } else {
  1183. warning("Ignoring invalid expression " + args,
  1184. first_line, first_col, first_file);
  1185. }
  1186. if (expression_result) {
  1187. // The expression result is true. We continue.
  1188. return;
  1189. }
  1190. // The expression result is false. Skip stuff.
  1191. skip_false_if_block(true);
  1192. }
  1193. ////////////////////////////////////////////////////////////////////
  1194. // Function: CPPPreprocessor::handle_include_directive
  1195. // Access: Private
  1196. // Description:
  1197. ////////////////////////////////////////////////////////////////////
  1198. void CPPPreprocessor::
  1199. handle_include_directive(const string &args, int first_line,
  1200. int first_col, const CPPFile &first_file) {
  1201. bool okflag = false;
  1202. Filename filename;
  1203. Filename filename_as_referenced;
  1204. bool angle_quotes = false;
  1205. string expr = args;
  1206. // The filename to include might actually be hidden within a
  1207. // manifest definition. Wow. FreeType depends on this.
  1208. // Just to play things safe, since our manifest-expansion logic
  1209. // might not filter out quotes and angle brackets properly, we'll
  1210. // only expand manifests if we don't begin with a quote or bracket.
  1211. if (!expr.empty() && (expr[0] != '"' && expr[0] != '<')) {
  1212. expr = expand_manifests(expr, false);
  1213. }
  1214. if (!expr.empty()) {
  1215. if (expr[0] == '"' && expr[expr.size() - 1] == '"') {
  1216. filename = expr.substr(1, expr.size() - 2);
  1217. okflag = true;
  1218. if (_files.size() == 1) {
  1219. // If we're currently processing a top-level file, record the
  1220. // include directive. We don't need to record includes from
  1221. // included files.
  1222. _quote_includes.insert(filename);
  1223. }
  1224. } else if (expr[0] == '<' && expr[expr.size() - 1] == '>') {
  1225. filename = expr.substr(1, expr.size() - 2);
  1226. if (!_noangles) {
  1227. // If _noangles is true, we don't make a distinction between
  1228. // angle brackets and quote marks--all #include statements are
  1229. // treated the same, as if they used quote marks.
  1230. angle_quotes = true;
  1231. }
  1232. okflag = true;
  1233. if (_files.size() == 1) {
  1234. // If we're currently processing a top-level file, record the
  1235. // include directive. We don't need to record includes from
  1236. // included files.
  1237. _angle_includes.insert(filename);
  1238. }
  1239. }
  1240. }
  1241. filename.set_text();
  1242. filename_as_referenced = filename;
  1243. // Now look for the filename. If we didn't use angle quotes, look
  1244. // first in the current directory.
  1245. bool found_file = false;
  1246. CPPFile::Source source = CPPFile::S_none;
  1247. if (okflag) {
  1248. found_file = false;
  1249. // Search the current directory.
  1250. if (!angle_quotes && !found_file && filename.exists()) {
  1251. found_file = true;
  1252. source = CPPFile::S_local;
  1253. }
  1254. // Search the same directory as the includer.
  1255. if (!angle_quotes && !found_file) {
  1256. Filename match(get_file()._filename.get_dirname(), filename);
  1257. if (match.exists()) {
  1258. filename = match;
  1259. found_file = true;
  1260. source = CPPFile::S_alternate;
  1261. }
  1262. }
  1263. // Now search the angle-include-path
  1264. if (angle_quotes && !found_file && filename.resolve_filename(_angle_include_path)) {
  1265. found_file = true;
  1266. source = CPPFile::S_system;
  1267. }
  1268. // Now search the quote-include-path
  1269. if (!angle_quotes && !found_file) {
  1270. for (int dir=0; dir<_quote_include_path.get_num_directories(); dir++) {
  1271. Filename match(_quote_include_path.get_directory(dir), filename);
  1272. if (match.exists()) {
  1273. filename = match;
  1274. found_file = true;
  1275. source = _quote_include_kind[dir];
  1276. }
  1277. }
  1278. }
  1279. if (!found_file) {
  1280. warning("Cannot find " + filename.get_fullpath(),
  1281. first_line, first_col, first_file);
  1282. } else {
  1283. _last_c = '\0';
  1284. CPPFile file(filename, filename_as_referenced, source);
  1285. // Don't include it if we included it before and it had #pragma once.
  1286. ParsedFiles::const_iterator it = _parsed_files.find(file);
  1287. if (it != _parsed_files.end() && it->_pragma_once) {
  1288. return;
  1289. }
  1290. if (!push_file(file)) {
  1291. warning("Unable to read " + filename.get_fullpath(),
  1292. first_line, first_col, first_file);
  1293. }
  1294. }
  1295. } else {
  1296. warning("Ignoring invalid #include directive",
  1297. first_line, first_col, first_file);
  1298. }
  1299. }
  1300. ////////////////////////////////////////////////////////////////////
  1301. // Function: CPPPreprocessor::handle_pragma_directive
  1302. // Access: Private
  1303. // Description:
  1304. ////////////////////////////////////////////////////////////////////
  1305. void CPPPreprocessor::
  1306. handle_pragma_directive(const string &args, int first_line,
  1307. int first_col, const CPPFile &first_file) {
  1308. if (args == "once") {
  1309. ParsedFiles::iterator it = _parsed_files.find(first_file);
  1310. assert(it != _parsed_files.end());
  1311. it->_pragma_once = true;
  1312. }
  1313. }
  1314. ////////////////////////////////////////////////////////////////////
  1315. // Function: CPPPreprocessor::handle_error_directive
  1316. // Access: Private
  1317. // Description:
  1318. ////////////////////////////////////////////////////////////////////
  1319. void CPPPreprocessor::
  1320. handle_error_directive(const string &args, int first_line,
  1321. int first_col, const CPPFile &first_file) {
  1322. error(args, first_line, first_col, first_file);
  1323. }
  1324. ////////////////////////////////////////////////////////////////////
  1325. // Function: CPPPreprocessor::skip_false_if_block
  1326. // Access: Private
  1327. // Description: We come here when we fail an #if or an #ifdef test,
  1328. // or when we reach the #else clause to something we
  1329. // didn't fail. This function skips all text up until
  1330. // the matching #endif.
  1331. ////////////////////////////////////////////////////////////////////
  1332. void CPPPreprocessor::
  1333. skip_false_if_block(bool consider_elifs) {
  1334. int level = 0;
  1335. _save_comments = false;
  1336. int c = skip_comment(get());
  1337. while (c != EOF) {
  1338. if (c == '#' && _start_of_line) {
  1339. CPPFile first_file = get_file();
  1340. int first_line = get_line_number();
  1341. int first_col = get_col_number();
  1342. // Is this it?
  1343. string command, args;
  1344. c = get_preprocessor_command(c, command);
  1345. c = get_preprocessor_args(c, args);
  1346. if (command == "if" || command == "ifdef" || command == "ifndef") {
  1347. // Hmm, a nested if block. Even more to skip.
  1348. level++;
  1349. } else if (command == "else") {
  1350. if (level == 0 && consider_elifs) {
  1351. // This will do!
  1352. _save_comments = true;
  1353. return;
  1354. }
  1355. } else if (command == "elif") {
  1356. if (level == 0 && consider_elifs) {
  1357. // If we pass this test, we're in.
  1358. _save_comments = true;
  1359. handle_if_directive(args, first_line, first_col, first_file);
  1360. return;
  1361. }
  1362. } else if (command == "endif") {
  1363. // Skip any args.
  1364. if (level == 0) {
  1365. // Here's the end!
  1366. _save_comments = true;
  1367. return;
  1368. }
  1369. level--;
  1370. }
  1371. } else {
  1372. c = skip_comment(get());
  1373. }
  1374. }
  1375. _save_comments = true;
  1376. }
  1377. ////////////////////////////////////////////////////////////////////
  1378. // Function: CPPPreprocessor::get_quoted_char
  1379. // Access: Private
  1380. // Description:
  1381. ////////////////////////////////////////////////////////////////////
  1382. CPPToken CPPPreprocessor::
  1383. get_quoted_char(int c) {
  1384. CPPFile first_file = get_file();
  1385. int first_line = get_line_number();
  1386. int first_col = get_col_number();
  1387. string str = scan_quoted(c);
  1388. YYSTYPE result;
  1389. if (!str.empty()) {
  1390. result.u.integer = (int)str[0];
  1391. } else {
  1392. result.u.integer = 0;
  1393. }
  1394. return CPPToken(CHAR_TOK, first_line, first_col, first_file, str, result);
  1395. }
  1396. ////////////////////////////////////////////////////////////////////
  1397. // Function: CPPPreprocessor::get_quoted_string
  1398. // Access: Private
  1399. // Description:
  1400. ////////////////////////////////////////////////////////////////////
  1401. CPPToken CPPPreprocessor::
  1402. get_quoted_string(int c) {
  1403. CPPFile first_file = get_file();
  1404. int first_line = get_line_number();
  1405. int first_col = get_col_number();
  1406. string str = scan_quoted(c);
  1407. return CPPToken(STRING, first_line, first_col, first_file, str);
  1408. }
  1409. ////////////////////////////////////////////////////////////////////
  1410. // Function: CPPPreprocessor::get_identifier
  1411. // Access: Private
  1412. // Description:
  1413. ////////////////////////////////////////////////////////////////////
  1414. CPPToken CPPPreprocessor::
  1415. get_identifier(int c) {
  1416. CPPFile first_file = get_file();
  1417. int first_line = get_line_number();
  1418. int first_col = get_col_number();
  1419. string name(1, (char)c);
  1420. c = get();
  1421. while (c != EOF && (isalnum(c) || c == '_')) {
  1422. name += c;
  1423. c = get();
  1424. }
  1425. if (c == '\'' || c == '"') {
  1426. // This is actually a wide-character or wide-string literal or
  1427. // some such: a string with an alphanumeric prefix. We don't
  1428. // necessarily try to parse it correctly; for most purposes, we
  1429. // don't care.
  1430. CPPToken token(0);
  1431. if (c == '\'') {
  1432. token = get_quoted_char(c);
  1433. } else {
  1434. token = get_quoted_string(c);
  1435. }
  1436. token._lloc.first_column = first_col;
  1437. return token;
  1438. }
  1439. _last_c = c;
  1440. // Is it a manifest?
  1441. Manifests::const_iterator mi = _manifests.find(name);
  1442. if (mi != _manifests.end() && !should_ignore_manifest((*mi).second)) {
  1443. return expand_manifest((*mi).second);
  1444. }
  1445. // Check for keywords.
  1446. int kw = check_keyword(name);
  1447. // Update our internal visibility flag.
  1448. switch (kw) {
  1449. case KW_BEGIN_PUBLISH:
  1450. preprocessor_vis = V_published;
  1451. break;
  1452. case KW_END_PUBLISH:
  1453. preprocessor_vis = V_public;
  1454. break;
  1455. }
  1456. if (kw != 0) {
  1457. YYSTYPE result;
  1458. result.u.identifier = (CPPIdentifier *)NULL;
  1459. return CPPToken(kw, first_line, first_col, first_file, name, result);
  1460. }
  1461. return CPPToken(SIMPLE_IDENTIFIER, first_line, first_col, first_file,
  1462. name);
  1463. }
  1464. ////////////////////////////////////////////////////////////////////
  1465. // Function: CPPPreprocessor::expand_manifest
  1466. // Access: Private
  1467. // Description:
  1468. ////////////////////////////////////////////////////////////////////
  1469. CPPToken CPPPreprocessor::
  1470. expand_manifest(const CPPManifest *manifest) {
  1471. vector_string args;
  1472. if (manifest->_has_parameters) {
  1473. // Hmm, we're expecting arguments.
  1474. extract_manifest_args(manifest->_name, manifest->_num_parameters,
  1475. manifest->_variadic_param, args);
  1476. }
  1477. string expanded = " " + manifest->expand(args) + " ";
  1478. push_string(expanded, true);
  1479. if (!manifest->_has_parameters) {
  1480. // If the manifest does not use arguments, then disallow recursive
  1481. // expansion.
  1482. _files.back()._ignore_manifest = manifest;
  1483. }
  1484. #ifdef CPP_VERBOSE_LEX
  1485. indent(cerr, _files.size() * 2)
  1486. << "Expanding " << manifest->_name << " to " << expanded << "\n";
  1487. #endif
  1488. return internal_get_next_token();
  1489. }
  1490. ////////////////////////////////////////////////////////////////////
  1491. // Function: CPPPreprocessor::extract_manifest_args
  1492. // Access: Private
  1493. // Description:
  1494. ////////////////////////////////////////////////////////////////////
  1495. void CPPPreprocessor::
  1496. extract_manifest_args(const string &name, int num_args, int va_arg,
  1497. vector_string &args) {
  1498. CPPFile first_file = get_file();
  1499. int first_line = get_line_number();
  1500. int first_col = get_col_number();
  1501. // Skip whitespace till paren.
  1502. int c = _last_c;
  1503. _last_c = '\0';
  1504. while (c != EOF && isspace(c)) {
  1505. c = get();
  1506. }
  1507. if (c != '(') {
  1508. // No paren, so we have only one arg.
  1509. string arg;
  1510. while (c != EOF && (isalnum(c) || c == '_')) {
  1511. arg += c;
  1512. c = get();
  1513. }
  1514. args.push_back(arg);
  1515. } else {
  1516. // Skip paren.
  1517. c = skip_whitespace(get());
  1518. int paren_level = 1;
  1519. string arg;
  1520. while (c != EOF) {
  1521. if (c == ',' && paren_level == 1) {
  1522. args.push_back(arg);
  1523. arg = "";
  1524. c = get();
  1525. } else if (c == '"' || c == '\'') {
  1526. // Quoted string or character.
  1527. int quote_mark = c;
  1528. arg += c;
  1529. c = get();
  1530. while (c != EOF && c != quote_mark && c != '\n') {
  1531. if (c == '\\') {
  1532. arg += c;
  1533. c = get();
  1534. }
  1535. if (c != EOF) {
  1536. arg += c;
  1537. c = get();
  1538. }
  1539. }
  1540. arg += c;
  1541. c = get();
  1542. } else if (c == '(') {
  1543. arg += '(';
  1544. ++paren_level;
  1545. c = get();
  1546. } else if (c == ')') {
  1547. --paren_level;
  1548. if (paren_level == 0) {
  1549. break;
  1550. }
  1551. arg += ')';
  1552. c = get();
  1553. } else if (isspace(c)) {
  1554. // Skip extra whitespace.
  1555. c = skip_whitespace(c);
  1556. if (!arg.empty()) {
  1557. arg += ' ';
  1558. }
  1559. } else if (c == '\\') {
  1560. // It could be a slash before a newline.
  1561. // If so, that's whitespace as well.
  1562. c = get();
  1563. if (c != '\n') {
  1564. arg += '\\';
  1565. } else if (!arg.empty()) {
  1566. arg += ' ';
  1567. c = skip_whitespace(get());
  1568. }
  1569. } else {
  1570. arg += c;
  1571. c = get();
  1572. }
  1573. }
  1574. if (num_args != 0 || !arg.empty()) {
  1575. args.push_back(arg);
  1576. }
  1577. }
  1578. if ((int)args.size() < num_args) {
  1579. warning("Not enough arguments for manifest " + name,
  1580. first_line, first_col, first_file);
  1581. } else if (va_arg < 0 && (int)args.size() > num_args) {
  1582. warning("Too many arguments for manifest " + name,
  1583. first_line, first_col, first_file);
  1584. }
  1585. }
  1586. ////////////////////////////////////////////////////////////////////
  1587. // Function: CPPPreprocessor::expand_defined_function
  1588. // Access: Private
  1589. // Description: Expands the defined(manifest) function to either
  1590. // 1 or 0, depending on whether the manifest exists.
  1591. ////////////////////////////////////////////////////////////////////
  1592. void CPPPreprocessor::
  1593. expand_defined_function(string &expr, size_t q, size_t &p) {
  1594. string result;
  1595. vector_string args;
  1596. extract_manifest_args_inline("defined", 1, -1, args, expr, p);
  1597. if (args.size() >= 1) {
  1598. const string &manifest_name = args[0];
  1599. Manifests::const_iterator mi = _manifests.find(manifest_name);
  1600. if (mi != _manifests.end()) {
  1601. // The macro is defined; the result is "1".
  1602. result = "1";
  1603. } else {
  1604. // The macro is undefined; the result is "0".
  1605. result = "0";
  1606. }
  1607. }
  1608. expr = expr.substr(0, q) + result + expr.substr(p);
  1609. p = q + result.size();
  1610. }
  1611. ////////////////////////////////////////////////////////////////////
  1612. // Function: CPPPreprocessor::expand_manifest_inline
  1613. // Access: Private
  1614. // Description:
  1615. ////////////////////////////////////////////////////////////////////
  1616. void CPPPreprocessor::
  1617. expand_manifest_inline(string &expr, size_t q, size_t &p,
  1618. const CPPManifest *manifest) {
  1619. vector_string args;
  1620. if (manifest->_has_parameters) {
  1621. extract_manifest_args_inline(manifest->_name, manifest->_num_parameters,
  1622. manifest->_variadic_param, args, expr, p);
  1623. }
  1624. string result = manifest->expand(args);
  1625. expr = expr.substr(0, q) + result + expr.substr(p);
  1626. p = q + result.size();
  1627. }
  1628. ////////////////////////////////////////////////////////////////////
  1629. // Function: CPPPreprocessor::extract_manifest_args_inline
  1630. // Access: Private
  1631. // Description:
  1632. ////////////////////////////////////////////////////////////////////
  1633. void CPPPreprocessor::
  1634. extract_manifest_args_inline(const string &name, int num_args,
  1635. int va_arg, vector_string &args,
  1636. const string &expr, size_t &p) {
  1637. // Skip whitespace till paren.
  1638. while (p < expr.size() && isspace(expr[p])) {
  1639. p++;
  1640. }
  1641. if (p >= expr.size() || expr[p] != '(') {
  1642. // No paren, so we have only one arg.
  1643. size_t q = p;
  1644. while (p < expr.size() && (isalnum(expr[p]) || expr[p] == '_')) {
  1645. p++;
  1646. }
  1647. args.push_back(expr.substr(q, p - q));
  1648. } else if (expr[p] == '"' || expr[p] == '\'') {
  1649. // Quoted string or character.
  1650. int quote_mark = expr[p];
  1651. p++;
  1652. while (p < expr.size() && expr[p] != quote_mark && expr[p] != '\n') {
  1653. if (expr[p] == '\\') {
  1654. p++;
  1655. }
  1656. if (p < expr.size()) {
  1657. p++;
  1658. }
  1659. }
  1660. p++;
  1661. } else {
  1662. // Skip paren.
  1663. p++;
  1664. size_t q = p;
  1665. while (p < expr.size() && expr[p] != ')') {
  1666. if (expr[p] == ',') {
  1667. args.push_back(expr.substr(q, p - q));
  1668. q = p+1;
  1669. } else if (expr[p] == '(') {
  1670. // Nested parens.
  1671. int paren_level = 1;
  1672. while (p+1 < expr.size() && paren_level > 0) {
  1673. p++;
  1674. if (expr[p] == '(') {
  1675. paren_level++;
  1676. } else if (expr[p] == ')') {
  1677. paren_level--;
  1678. }
  1679. }
  1680. }
  1681. p++;
  1682. }
  1683. args.push_back(expr.substr(q, p - q));
  1684. if (p < expr.size() && expr[p] == ')') {
  1685. p++;
  1686. }
  1687. }
  1688. if ((int)args.size() < num_args) {
  1689. warning("Not enough arguments for manifest " + name);
  1690. } else if (va_arg < 0 && (int)args.size() > num_args) {
  1691. warning("Too many arguments for manifest " + name);
  1692. }
  1693. }
  1694. ////////////////////////////////////////////////////////////////////
  1695. // Function: CPPPreprocessor::get_number
  1696. // Access: Private
  1697. // Description:
  1698. ////////////////////////////////////////////////////////////////////
  1699. CPPToken CPPPreprocessor::
  1700. get_number(int c, int c2) {
  1701. CPPFile first_file = get_file();
  1702. int first_line = get_line_number();
  1703. int first_col = get_col_number();
  1704. string num(1, (char)c);
  1705. bool leading_zero = (c == '0');
  1706. bool decimal_point = (c == '.');
  1707. if (c2 == 0) {
  1708. c = get();
  1709. } else {
  1710. c = c2;
  1711. }
  1712. if (leading_zero && c == 'x') {
  1713. // Here we have a hex number.
  1714. num += c;
  1715. c = get();
  1716. while (c != EOF && (isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f'))) {
  1717. num += c;
  1718. c = get();
  1719. }
  1720. while (c == 'L' || c == 'U') {
  1721. // We allow (and ignore) an 'L' and/or 'U' following the number.
  1722. c = get();
  1723. }
  1724. _last_c = c;
  1725. YYSTYPE result;
  1726. result.u.integer = strtol(num.c_str(), (char **)NULL, 16);
  1727. return CPPToken(INTEGER, first_line, first_col, first_file, num, result);
  1728. }
  1729. while (c != EOF && isdigit(c)) {
  1730. num += c;
  1731. c = get();
  1732. }
  1733. if (c == '.' && !decimal_point) {
  1734. // Now we have a floating-point number.
  1735. decimal_point = true;
  1736. num += c;
  1737. c = get();
  1738. while (c != EOF && isdigit(c)) {
  1739. num += c;
  1740. c = get();
  1741. }
  1742. }
  1743. if (decimal_point) {
  1744. if (tolower(c) == 'e') {
  1745. // An exponent is allowed.
  1746. num += c;
  1747. c = get();
  1748. if (c == '-' || c == '+') {
  1749. num += c;
  1750. c = get();
  1751. }
  1752. while (c != EOF && isdigit(c)) {
  1753. num += c;
  1754. c = get();
  1755. }
  1756. }
  1757. if (c == 'f') {
  1758. // We allow (and ignore) an 'f' following the number.
  1759. c = get();
  1760. }
  1761. _last_c = c;
  1762. YYSTYPE result;
  1763. result.u.real = pstrtod(num.c_str(), (char **)NULL);
  1764. return CPPToken(REAL, first_line, first_col, first_file, num, result);
  1765. }
  1766. // This is a decimal or octal integer number.
  1767. while (c == 'L' || c == 'U') {
  1768. // We allow (and ignore) an 'L' and/or 'U' following the number.
  1769. c = get();
  1770. }
  1771. _last_c = c;
  1772. YYSTYPE result;
  1773. if (leading_zero) {
  1774. // A leading zero implies an octal number. strtol() is supposed
  1775. // to be able to make this distinction by itself, but we'll do it
  1776. // explicitly just to be sure.
  1777. result.u.integer = strtol(num.c_str(), (char **)NULL, 8);
  1778. } else {
  1779. // A decimal (base 10) integer.
  1780. result.u.integer = strtol(num.c_str(), (char **)NULL, 10);
  1781. }
  1782. return CPPToken(INTEGER, first_line, first_col, first_file, num, result);
  1783. }
  1784. ////////////////////////////////////////////////////////////////////
  1785. // Function: CPPPreprocessor::check_keyword
  1786. // Access: Private, Static
  1787. // Description:
  1788. ////////////////////////////////////////////////////////////////////
  1789. int CPPPreprocessor::
  1790. check_keyword(const string &name) {
  1791. if (name == "__begin_publish") return KW_BEGIN_PUBLISH;
  1792. if (name == "__blocking") return KW_BLOCKING;
  1793. if (name == "bool") return KW_BOOL;
  1794. if (name == "catch") return KW_CATCH;
  1795. if (name == "char") return KW_CHAR;
  1796. if (name == "char16_t") return KW_CHAR16_T;
  1797. if (name == "char32_t") return KW_CHAR32_T;
  1798. if (name == "class") return KW_CLASS;
  1799. if (name == "const") return KW_CONST;
  1800. if (name == "delete") return KW_DELETE;
  1801. if (name == "double") return KW_DOUBLE;
  1802. if (name == "dynamic_cast") return KW_DYNAMIC_CAST;
  1803. if (name == "else") return KW_ELSE;
  1804. if (name == "__end_publish") return KW_END_PUBLISH;
  1805. if (name == "enum") return KW_ENUM;
  1806. if (name == "extern") return KW_EXTERN;
  1807. if (name == "__extension") return KW_EXTENSION;
  1808. if (name == "explicit") return KW_EXPLICIT;
  1809. if (name == "__published") return KW_PUBLISHED;
  1810. if (name == "false") return KW_FALSE;
  1811. if (name == "float") return KW_FLOAT;
  1812. if (name == "friend") return KW_FRIEND;
  1813. if (name == "for") return KW_FOR;
  1814. if (name == "goto") return KW_GOTO;
  1815. if (name == "if") return KW_IF;
  1816. if (name == "inline") return KW_INLINE;
  1817. if (name == "int") return KW_INT;
  1818. if (name == "long") return KW_LONG;
  1819. if (name == "__make_property") return KW_MAKE_PROPERTY;
  1820. if (name == "__make_seq") return KW_MAKE_SEQ;
  1821. if (name == "mutable") return KW_MUTABLE;
  1822. if (name == "namespace") return KW_NAMESPACE;
  1823. if (name == "noexcept") return KW_NOEXCEPT;
  1824. if (name == "new") return KW_NEW;
  1825. if (name == "operator") return KW_OPERATOR;
  1826. if (name == "private") return KW_PRIVATE;
  1827. if (name == "protected") return KW_PROTECTED;
  1828. if (name == "public") return KW_PUBLIC;
  1829. if (name == "register") return KW_REGISTER;
  1830. if (name == "return") return KW_RETURN;
  1831. if (name == "short") return KW_SHORT;
  1832. if (name == "signed") return KW_SIGNED;
  1833. if (name == "sizeof") return KW_SIZEOF;
  1834. if (name == "static") return KW_STATIC;
  1835. if (name == "static_cast") return KW_STATIC_CAST;
  1836. if (name == "struct") return KW_STRUCT;
  1837. if (name == "template") return KW_TEMPLATE;
  1838. if (name == "throw") return KW_THROW;
  1839. if (name == "true") return KW_TRUE;
  1840. if (name == "try") return KW_TRY;
  1841. if (name == "typedef") return KW_TYPEDEF;
  1842. if (name == "typename") return KW_TYPENAME;
  1843. if (name == "union") return KW_UNION;
  1844. if (name == "unsigned") return KW_UNSIGNED;
  1845. if (name == "using") return KW_USING;
  1846. if (name == "virtual") return KW_VIRTUAL;
  1847. if (name == "void") return KW_VOID;
  1848. if (name == "volatile") return KW_VOLATILE;
  1849. if (name == "wchar_t") return KW_WCHAR_T;
  1850. if (name == "while") return KW_WHILE;
  1851. // These are alternative ways to refer to built-in operators.
  1852. if (name == "and") return ANDAND;
  1853. if (name == "and_eq") return ANDEQUAL;
  1854. if (name == "bitand") return '&';
  1855. if (name == "bitor") return '|';
  1856. if (name == "compl") return '~';
  1857. if (name == "not") return '!';
  1858. if (name == "not_eq") return NECOMPARE;
  1859. if (name == "or") return OROR;
  1860. if (name == "or_eq") return OREQUAL;
  1861. if (name == "xor") return '^';
  1862. if (name == "xor_eq") return XOREQUAL;
  1863. if (!cpp_longlong_keyword.empty() && name == cpp_longlong_keyword) {
  1864. return KW_LONGLONG;
  1865. }
  1866. return 0;
  1867. }
  1868. ////////////////////////////////////////////////////////////////////
  1869. // Function: CPPPreprocessor::scan_escape_sequence
  1870. // Access: Private
  1871. // Description:
  1872. ////////////////////////////////////////////////////////////////////
  1873. int CPPPreprocessor::
  1874. scan_escape_sequence(int c) {
  1875. if (c != '\\') {
  1876. return c;
  1877. }
  1878. c = get();
  1879. switch (c) {
  1880. case 'a':
  1881. return '\a';
  1882. case 'b':
  1883. return '\b';
  1884. case 'f':
  1885. return '\f';
  1886. case 'n':
  1887. return '\n';
  1888. case 'r':
  1889. return '\r';
  1890. case 't':
  1891. return '\t';
  1892. case 'v':
  1893. return '\v';
  1894. case 'e':
  1895. // \e is non-standard, buT GCC supports it.
  1896. return '\x1B';
  1897. case 'x':
  1898. // hex character.
  1899. c = get();
  1900. if (isxdigit(c)) {
  1901. int val = hex_val(c);
  1902. c = get();
  1903. if (isxdigit(c)) {
  1904. val = (val << 4) | hex_val(c);
  1905. } else {
  1906. unget(c);
  1907. }
  1908. return val;
  1909. }
  1910. break;
  1911. case '0':
  1912. case '1':
  1913. case '2':
  1914. case '3':
  1915. case '4':
  1916. case '5':
  1917. case '6':
  1918. case '7':
  1919. // Octal character.
  1920. {
  1921. int val = (c - '0');
  1922. c = get();
  1923. if (c >= '0' && c <= '7') {
  1924. val = (val << 3) | (c - '0');
  1925. c = get();
  1926. if (c >= '0' && c <= '7') {
  1927. val = (val << 3) | (c - '0');
  1928. } else {
  1929. unget(c);
  1930. }
  1931. } else {
  1932. unget(c);
  1933. }
  1934. return val;
  1935. }
  1936. }
  1937. // Simply output the following character.
  1938. return c;
  1939. }
  1940. ////////////////////////////////////////////////////////////////////
  1941. // Function: CPPPreprocessor::scan_quoted
  1942. // Access: Private
  1943. // Description:
  1944. ////////////////////////////////////////////////////////////////////
  1945. string CPPPreprocessor::
  1946. scan_quoted(int c) {
  1947. int quote_mark = c;
  1948. string str;
  1949. c = get();
  1950. while (c != EOF && c != '\n' && c != quote_mark) {
  1951. if (c == '\\') {
  1952. // Backslash means a special character follows.
  1953. c = scan_escape_sequence(c);
  1954. }
  1955. str += c;
  1956. c = get();
  1957. }
  1958. if (c != quote_mark) {
  1959. warning("Unclosed string");
  1960. }
  1961. return str;
  1962. }
  1963. ////////////////////////////////////////////////////////////////////
  1964. // Function: CPPPreprocessor::should_ignore_manifest
  1965. // Access: Public
  1966. // Description: Returns true if the manifest is one that is being
  1967. // ignored right now (presumably because we are
  1968. // presently expanding it).
  1969. ////////////////////////////////////////////////////////////////////
  1970. bool CPPPreprocessor::
  1971. should_ignore_manifest(const CPPManifest *manifest) const {
  1972. Files::const_iterator fi;
  1973. for (fi = _files.begin(); fi != _files.end(); ++fi) {
  1974. if ((*fi)._ignore_manifest == manifest) {
  1975. return true;
  1976. }
  1977. }
  1978. return false;
  1979. }
  1980. ////////////////////////////////////////////////////////////////////
  1981. // Function: CPPPreprocessor::should_ignore_preprocessor
  1982. // Access: Public
  1983. // Description: Returns true if we should ignore any preprocessor
  1984. // directives (e.g. we're presently expanding a
  1985. // manifest).
  1986. ////////////////////////////////////////////////////////////////////
  1987. bool CPPPreprocessor::
  1988. should_ignore_preprocessor() const {
  1989. Files::const_iterator fi;
  1990. for (fi = _files.begin(); fi != _files.end(); ++fi) {
  1991. if ((*fi)._ignore_manifest != NULL) {
  1992. return true;
  1993. }
  1994. }
  1995. return false;
  1996. }
  1997. ////////////////////////////////////////////////////////////////////
  1998. // Function: CPPPreprocessor::get
  1999. // Access: Private
  2000. // Description:
  2001. ////////////////////////////////////////////////////////////////////
  2002. int CPPPreprocessor::
  2003. get() {
  2004. if (_unget != '\0') {
  2005. int c = _unget;
  2006. _unget = '\0';
  2007. return c;
  2008. }
  2009. if (_files.empty()) {
  2010. return EOF;
  2011. }
  2012. int c = _files.back().get();
  2013. while (c == EOF && !_files.empty()) {
  2014. #ifdef CPP_VERBOSE_LEX
  2015. indent(cerr, _files.size() * 2)
  2016. << "End of input stream, restoring to previous input\n";
  2017. #endif
  2018. int last_c = _files.back()._prev_last_c;
  2019. _files.pop_back();
  2020. if (last_c != '\0') {
  2021. c = last_c;
  2022. } else if (!_files.empty()) {
  2023. c = _files.back().get();
  2024. }
  2025. }
  2026. if (c == '\n') {
  2027. _start_of_line = true;
  2028. } else if (!isspace(c) && c != '#') {
  2029. _start_of_line = false;
  2030. }
  2031. return c;
  2032. }
  2033. ////////////////////////////////////////////////////////////////////
  2034. // Function: CPPPreprocessor::unget
  2035. // Access: Private
  2036. // Description:
  2037. ////////////////////////////////////////////////////////////////////
  2038. void CPPPreprocessor::
  2039. unget(int c) {
  2040. assert(_unget == '\0');
  2041. _unget = c;
  2042. }
  2043. ////////////////////////////////////////////////////////////////////
  2044. // Function: CPPPreprocessor::nested_parse_template_instantiation
  2045. // Access: Private
  2046. // Description: Recursively invokes yacc to parse the stuff within
  2047. // angle brackets that's the template instantiation part
  2048. // of an identifier. This involves setting and
  2049. // restoring some state flags so we can return EOF when
  2050. // we reach the closing bracket.
  2051. ////////////////////////////////////////////////////////////////////
  2052. CPPTemplateParameterList *CPPPreprocessor::
  2053. nested_parse_template_instantiation(CPPTemplateScope *scope) {
  2054. #ifdef CPP_VERBOSE_LEX
  2055. indent(cerr, _files.size() * 2)
  2056. << "Beginning nested parse\n";
  2057. #endif
  2058. assert(scope != NULL);
  2059. State old_state = _state;
  2060. int old_nesting = _paren_nesting;
  2061. bool old_parsing_params = _parsing_template_params;
  2062. const CPPTemplateParameterList &formal_params = scope->_parameters;
  2063. CPPTemplateParameterList::Parameters::const_iterator pi;
  2064. _state = S_nested;
  2065. _paren_nesting = 0;
  2066. _parsing_template_params = true;
  2067. CPPToken token = internal_get_next_token();
  2068. if (token._token == '>') {
  2069. _parsing_template_params = false;
  2070. } else {
  2071. _saved_tokens.push_back(token);
  2072. }
  2073. CPPTemplateParameterList *actual_params = new CPPTemplateParameterList;
  2074. for (pi = formal_params._parameters.begin();
  2075. pi != formal_params._parameters.end() && _parsing_template_params;
  2076. ++pi) {
  2077. CPPFile first_file = get_file();
  2078. int first_line = get_line_number();
  2079. int first_col = get_col_number();
  2080. CPPDeclaration *decl = (*pi);
  2081. if (decl->as_type()) {
  2082. // Parse a typename template parameter.
  2083. _saved_tokens.push_back(CPPToken(START_TYPE));
  2084. CPPType *type = ::parse_type(this, current_scope, global_scope);
  2085. if (type == NULL) {
  2086. warning("Invalid type", first_line, first_col, first_file);
  2087. skip_to_end_nested();
  2088. type = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  2089. }
  2090. actual_params->_parameters.push_back(type);
  2091. } else {
  2092. // Parse a constant expression template parameter.
  2093. _saved_tokens.push_back(CPPToken(START_CONST_EXPR));
  2094. CPPExpression *expr = parse_const_expr(this, current_scope, global_scope);
  2095. if (expr == NULL) {
  2096. warning("Invalid expression", first_line, first_col, first_file);
  2097. skip_to_end_nested();
  2098. expr = new CPPExpression(0);
  2099. }
  2100. actual_params->_parameters.push_back(expr);
  2101. }
  2102. _state = S_nested;
  2103. _paren_nesting = 0;
  2104. }
  2105. if (_parsing_template_params) {
  2106. warning("Ignoring extra parameters in template instantiation");
  2107. skip_to_angle_bracket();
  2108. }
  2109. _state = old_state;
  2110. _paren_nesting = old_nesting;
  2111. _parsing_template_params = old_parsing_params;
  2112. #ifdef CPP_VERBOSE_LEX
  2113. indent(cerr, _files.size() * 2)
  2114. << "Ending nested parse\n";
  2115. #endif
  2116. return actual_params;
  2117. }
  2118. ////////////////////////////////////////////////////////////////////
  2119. // Function: CPPPreprocessor::skip_to_end_nested
  2120. // Access: Private
  2121. // Description: This is an error-recovery function, called after
  2122. // returning from a nested parse. If the state is not
  2123. // S_end_nested, there was an error in parsing the
  2124. // nested tokens, and not all of the nested tokens may
  2125. // have been consumed. This function will consume the
  2126. // rest of the nested tokens.
  2127. ////////////////////////////////////////////////////////////////////
  2128. void CPPPreprocessor::
  2129. skip_to_end_nested() {
  2130. #ifdef CPP_VERBOSE_LEX
  2131. indent(cerr, _files.size() * 2)
  2132. << "Skipping tokens:\n";
  2133. #endif
  2134. // Eat any eof tokens on the pushback stack.
  2135. while (!_saved_tokens.empty() && _saved_tokens.back().is_eof()) {
  2136. _saved_tokens.pop_back();
  2137. }
  2138. while (_state != S_end_nested && _state != S_eof) {
  2139. get_next_token();
  2140. }
  2141. #ifdef CPP_VERBOSE_LEX
  2142. indent(cerr, _files.size() * 2)
  2143. << "Done skipping tokens.\n";
  2144. #endif
  2145. }
  2146. ////////////////////////////////////////////////////////////////////
  2147. // Function: CPPPreprocessor::skip_to_angle_bracket
  2148. // Access: Private
  2149. // Description: This is an error-recovery function, called after
  2150. // returning from a nested parse. If we haven't yet
  2151. // consumed the closing angle bracket on the template
  2152. // instantiation, keep consuming tokens until we do.
  2153. ////////////////////////////////////////////////////////////////////
  2154. void CPPPreprocessor::
  2155. skip_to_angle_bracket() {
  2156. #ifdef CPP_VERBOSE_LEX
  2157. indent(cerr, _files.size() * 2)
  2158. << "Skipping tokens:\n";
  2159. #endif
  2160. while (_parsing_template_params && _state != S_eof) {
  2161. _state = S_nested;
  2162. while (_state != S_end_nested && _state != S_eof) {
  2163. get_next_token();
  2164. }
  2165. }
  2166. // Eat any eof tokens on the pushback stack.
  2167. while (!_saved_tokens.empty() && _saved_tokens.back().is_eof()) {
  2168. _saved_tokens.pop_back();
  2169. }
  2170. #ifdef CPP_VERBOSE_LEX
  2171. indent(cerr, _files.size() * 2)
  2172. << "Done skipping tokens.\n";
  2173. #endif
  2174. }