cppPreprocessor.cxx 84 KB

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