Pp.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. //
  2. // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
  3. // Copyright (C) 2013 LunarG, Inc.
  4. // Copyright (C) 2015-2018 Google, Inc.
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions
  9. // are met:
  10. //
  11. // Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. //
  14. // Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following
  16. // disclaimer in the documentation and/or other materials provided
  17. // with the distribution.
  18. //
  19. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  20. // contributors may be used to endorse or promote products derived
  21. // from this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. /****************************************************************************\
  37. Copyright (c) 2002, NVIDIA Corporation.
  38. NVIDIA Corporation("NVIDIA") supplies this software to you in
  39. consideration of your agreement to the following terms, and your use,
  40. installation, modification or redistribution of this NVIDIA software
  41. constitutes acceptance of these terms. If you do not agree with these
  42. terms, please do not use, install, modify or redistribute this NVIDIA
  43. software.
  44. In consideration of your agreement to abide by the following terms, and
  45. subject to these terms, NVIDIA grants you a personal, non-exclusive
  46. license, under NVIDIA's copyrights in this original NVIDIA software (the
  47. "NVIDIA Software"), to use, reproduce, modify and redistribute the
  48. NVIDIA Software, with or without modifications, in source and/or binary
  49. forms; provided that if you redistribute the NVIDIA Software, you must
  50. retain the copyright notice of NVIDIA, this notice and the following
  51. text and disclaimers in all such redistributions of the NVIDIA Software.
  52. Neither the name, trademarks, service marks nor logos of NVIDIA
  53. Corporation may be used to endorse or promote products derived from the
  54. NVIDIA Software without specific prior written permission from NVIDIA.
  55. Except as expressly stated in this notice, no other rights or licenses
  56. express or implied, are granted by NVIDIA herein, including but not
  57. limited to any patent rights that may be infringed by your derivative
  58. works or by other works in which the NVIDIA Software may be
  59. incorporated. No hardware is licensed hereunder.
  60. THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
  61. WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  62. INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
  63. NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
  64. ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
  65. PRODUCTS.
  66. IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
  67. INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  68. TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  69. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
  70. OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
  71. NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
  72. TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  73. NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  74. \****************************************************************************/
  75. #ifndef _CRT_SECURE_NO_WARNINGS
  76. #define _CRT_SECURE_NO_WARNINGS
  77. #endif
  78. #include <sstream>
  79. #include <cstdlib>
  80. #include <cstring>
  81. #include <cctype>
  82. #include <climits>
  83. #include "PpContext.h"
  84. #include "PpTokens.h"
  85. namespace glslang {
  86. // Handle #define
  87. int TPpContext::CPPdefine(TPpToken* ppToken)
  88. {
  89. MacroSymbol mac;
  90. // get the macro name
  91. int token = scanToken(ppToken);
  92. if (token != PpAtomIdentifier) {
  93. parseContext.ppError(ppToken->loc, "must be followed by macro name", "#define", "");
  94. return token;
  95. }
  96. if (ppToken->loc.string >= 0) {
  97. // We are in user code; check for reserved name use:
  98. parseContext.reservedPpErrorCheck(ppToken->loc, ppToken->name, "#define");
  99. }
  100. // save the macro name
  101. const int defAtom = atomStrings.getAddAtom(ppToken->name);
  102. TSourceLoc defineLoc = ppToken->loc; // because ppToken might go to the next line before we report errors
  103. // gather parameters to the macro, between (...)
  104. token = scanToken(ppToken);
  105. if (token == '(' && !ppToken->space) {
  106. mac.functionLike = 1;
  107. do {
  108. token = scanToken(ppToken);
  109. if (mac.args.size() == 0 && token == ')')
  110. break;
  111. if (token != PpAtomIdentifier) {
  112. parseContext.ppError(ppToken->loc, "bad argument", "#define", "");
  113. return token;
  114. }
  115. const int argAtom = atomStrings.getAddAtom(ppToken->name);
  116. // check for duplication of parameter name
  117. bool duplicate = false;
  118. for (size_t a = 0; a < mac.args.size(); ++a) {
  119. if (mac.args[a] == argAtom) {
  120. parseContext.ppError(ppToken->loc, "duplicate macro parameter", "#define", "");
  121. duplicate = true;
  122. break;
  123. }
  124. }
  125. if (! duplicate)
  126. mac.args.push_back(argAtom);
  127. token = scanToken(ppToken);
  128. } while (token == ',');
  129. if (token != ')') {
  130. parseContext.ppError(ppToken->loc, "missing parenthesis", "#define", "");
  131. return token;
  132. }
  133. token = scanToken(ppToken);
  134. } else if (token != '\n' && token != EndOfInput && !ppToken->space) {
  135. parseContext.ppWarn(ppToken->loc, "missing space after macro name", "#define", "");
  136. return token;
  137. }
  138. // record the definition of the macro
  139. while (token != '\n' && token != EndOfInput) {
  140. mac.body.putToken(token, ppToken);
  141. token = scanToken(ppToken);
  142. if (token != '\n' && ppToken->space)
  143. mac.body.putToken(' ', ppToken);
  144. }
  145. // check for duplicate definition
  146. MacroSymbol* existing = lookupMacroDef(defAtom);
  147. if (existing != nullptr) {
  148. if (! existing->undef) {
  149. // Already defined -- need to make sure they are identical:
  150. // "Two replacement lists are identical if and only if the
  151. // preprocessing tokens in both have the same number,
  152. // ordering, spelling, and white-space separation, where all
  153. // white-space separations are considered identical."
  154. if (existing->functionLike != mac.functionLike) {
  155. parseContext.ppError(defineLoc, "Macro redefined; function-like versus object-like:", "#define",
  156. atomStrings.getString(defAtom));
  157. } else if (existing->args.size() != mac.args.size()) {
  158. parseContext.ppError(defineLoc, "Macro redefined; different number of arguments:", "#define",
  159. atomStrings.getString(defAtom));
  160. } else {
  161. if (existing->args != mac.args) {
  162. parseContext.ppError(defineLoc, "Macro redefined; different argument names:", "#define",
  163. atomStrings.getString(defAtom));
  164. }
  165. // set up to compare the two
  166. existing->body.reset();
  167. mac.body.reset();
  168. int newToken;
  169. bool firstToken = true;
  170. do {
  171. int oldToken;
  172. TPpToken oldPpToken;
  173. TPpToken newPpToken;
  174. oldToken = existing->body.getToken(parseContext, &oldPpToken);
  175. newToken = mac.body.getToken(parseContext, &newPpToken);
  176. // for the first token, preceding spaces don't matter
  177. if (firstToken) {
  178. newPpToken.space = oldPpToken.space;
  179. firstToken = false;
  180. }
  181. if (oldToken != newToken || oldPpToken != newPpToken) {
  182. parseContext.ppError(defineLoc, "Macro redefined; different substitutions:", "#define",
  183. atomStrings.getString(defAtom));
  184. break;
  185. }
  186. } while (newToken != EndOfInput);
  187. }
  188. }
  189. *existing = mac;
  190. } else
  191. addMacroDef(defAtom, mac);
  192. return '\n';
  193. }
  194. // Handle #undef
  195. int TPpContext::CPPundef(TPpToken* ppToken)
  196. {
  197. int token = scanToken(ppToken);
  198. if (token != PpAtomIdentifier) {
  199. parseContext.ppError(ppToken->loc, "must be followed by macro name", "#undef", "");
  200. return token;
  201. }
  202. parseContext.reservedPpErrorCheck(ppToken->loc, ppToken->name, "#undef");
  203. MacroSymbol* macro = lookupMacroDef(atomStrings.getAtom(ppToken->name));
  204. if (macro != nullptr)
  205. macro->undef = 1;
  206. token = scanToken(ppToken);
  207. if (token != '\n')
  208. parseContext.ppError(ppToken->loc, "can only be followed by a single macro name", "#undef", "");
  209. return token;
  210. }
  211. // Handle #else
  212. /* Skip forward to appropriate spot. This is used both
  213. ** to skip to a #endif after seeing an #else, AND to skip to a #else,
  214. ** #elif, or #endif after a #if/#ifdef/#ifndef/#elif test was false.
  215. */
  216. int TPpContext::CPPelse(int matchelse, TPpToken* ppToken)
  217. {
  218. int depth = 0;
  219. int token = scanToken(ppToken);
  220. while (token != EndOfInput) {
  221. if (token != '#') {
  222. while (token != '\n' && token != EndOfInput)
  223. token = scanToken(ppToken);
  224. if (token == EndOfInput)
  225. return token;
  226. token = scanToken(ppToken);
  227. continue;
  228. }
  229. if ((token = scanToken(ppToken)) != PpAtomIdentifier)
  230. continue;
  231. int nextAtom = atomStrings.getAtom(ppToken->name);
  232. if (nextAtom == PpAtomIf || nextAtom == PpAtomIfdef || nextAtom == PpAtomIfndef) {
  233. depth++;
  234. if (ifdepth >= maxIfNesting || elsetracker >= maxIfNesting) {
  235. parseContext.ppError(ppToken->loc, "maximum nesting depth exceeded", "#if/#ifdef/#ifndef", "");
  236. return EndOfInput;
  237. } else {
  238. ifdepth++;
  239. elsetracker++;
  240. }
  241. } else if (nextAtom == PpAtomEndif) {
  242. token = extraTokenCheck(nextAtom, ppToken, scanToken(ppToken));
  243. elseSeen[elsetracker] = false;
  244. --elsetracker;
  245. if (depth == 0) {
  246. // found the #endif we are looking for
  247. if (ifdepth > 0)
  248. --ifdepth;
  249. break;
  250. }
  251. --depth;
  252. --ifdepth;
  253. } else if (matchelse && depth == 0) {
  254. if (nextAtom == PpAtomElse) {
  255. elseSeen[elsetracker] = true;
  256. token = extraTokenCheck(nextAtom, ppToken, scanToken(ppToken));
  257. // found the #else we are looking for
  258. break;
  259. } else if (nextAtom == PpAtomElif) {
  260. if (elseSeen[elsetracker])
  261. parseContext.ppError(ppToken->loc, "#elif after #else", "#elif", "");
  262. /* we decrement ifdepth here, because CPPif will increment
  263. * it and we really want to leave it alone */
  264. if (ifdepth > 0) {
  265. --ifdepth;
  266. elseSeen[elsetracker] = false;
  267. --elsetracker;
  268. }
  269. return CPPif(ppToken);
  270. }
  271. } else if (nextAtom == PpAtomElse) {
  272. if (elseSeen[elsetracker])
  273. parseContext.ppError(ppToken->loc, "#else after #else", "#else", "");
  274. else
  275. elseSeen[elsetracker] = true;
  276. token = extraTokenCheck(nextAtom, ppToken, scanToken(ppToken));
  277. } else if (nextAtom == PpAtomElif) {
  278. if (elseSeen[elsetracker])
  279. parseContext.ppError(ppToken->loc, "#elif after #else", "#elif", "");
  280. }
  281. }
  282. return token;
  283. }
  284. // Call when there should be no more tokens left on a line.
  285. int TPpContext::extraTokenCheck(int contextAtom, TPpToken* ppToken, int token)
  286. {
  287. if (token != '\n' && token != EndOfInput) {
  288. static const char* message = "unexpected tokens following directive";
  289. const char* label;
  290. if (contextAtom == PpAtomElse)
  291. label = "#else";
  292. else if (contextAtom == PpAtomElif)
  293. label = "#elif";
  294. else if (contextAtom == PpAtomEndif)
  295. label = "#endif";
  296. else if (contextAtom == PpAtomIf)
  297. label = "#if";
  298. else if (contextAtom == PpAtomLine)
  299. label = "#line";
  300. else
  301. label = "";
  302. if (parseContext.relaxedErrors())
  303. parseContext.ppWarn(ppToken->loc, message, label, "");
  304. else
  305. parseContext.ppError(ppToken->loc, message, label, "");
  306. while (token != '\n' && token != EndOfInput)
  307. token = scanToken(ppToken);
  308. }
  309. return token;
  310. }
  311. enum eval_prec {
  312. MIN_PRECEDENCE,
  313. COND, LOGOR, LOGAND, OR, XOR, AND, EQUAL, RELATION, SHIFT, ADD, MUL, UNARY,
  314. MAX_PRECEDENCE
  315. };
  316. namespace {
  317. int op_logor(int a, int b) { return a || b; }
  318. int op_logand(int a, int b) { return a && b; }
  319. int op_or(int a, int b) { return a | b; }
  320. int op_xor(int a, int b) { return a ^ b; }
  321. int op_and(int a, int b) { return a & b; }
  322. int op_eq(int a, int b) { return a == b; }
  323. int op_ne(int a, int b) { return a != b; }
  324. int op_ge(int a, int b) { return a >= b; }
  325. int op_le(int a, int b) { return a <= b; }
  326. int op_gt(int a, int b) { return a > b; }
  327. int op_lt(int a, int b) { return a < b; }
  328. int op_shl(int a, int b) { return a << b; }
  329. int op_shr(int a, int b) { return a >> b; }
  330. int op_add(int a, int b) { return a + b; }
  331. int op_sub(int a, int b) { return a - b; }
  332. int op_mul(int a, int b) { return a * b; }
  333. int op_div(int a, int b) { return a == INT_MIN && b == -1 ? 0 : a / b; }
  334. int op_mod(int a, int b) { return a == INT_MIN && b == -1 ? 0 : a % b; }
  335. int op_pos(int a) { return a; }
  336. int op_neg(int a) { return -a; }
  337. int op_cmpl(int a) { return ~a; }
  338. int op_not(int a) { return !a; }
  339. };
  340. struct TBinop {
  341. int token, precedence, (*op)(int, int);
  342. } binop[] = {
  343. { PpAtomOr, LOGOR, op_logor },
  344. { PpAtomAnd, LOGAND, op_logand },
  345. { '|', OR, op_or },
  346. { '^', XOR, op_xor },
  347. { '&', AND, op_and },
  348. { PpAtomEQ, EQUAL, op_eq },
  349. { PpAtomNE, EQUAL, op_ne },
  350. { '>', RELATION, op_gt },
  351. { PpAtomGE, RELATION, op_ge },
  352. { '<', RELATION, op_lt },
  353. { PpAtomLE, RELATION, op_le },
  354. { PpAtomLeft, SHIFT, op_shl },
  355. { PpAtomRight, SHIFT, op_shr },
  356. { '+', ADD, op_add },
  357. { '-', ADD, op_sub },
  358. { '*', MUL, op_mul },
  359. { '/', MUL, op_div },
  360. { '%', MUL, op_mod },
  361. };
  362. struct TUnop {
  363. int token, (*op)(int);
  364. } unop[] = {
  365. { '+', op_pos },
  366. { '-', op_neg },
  367. { '~', op_cmpl },
  368. { '!', op_not },
  369. };
  370. #define NUM_ELEMENTS(A) (sizeof(A) / sizeof(A[0]))
  371. int TPpContext::eval(int token, int precedence, bool shortCircuit, int& res, bool& err, TPpToken* ppToken)
  372. {
  373. TSourceLoc loc = ppToken->loc; // because we sometimes read the newline before reporting the error
  374. if (token == PpAtomIdentifier) {
  375. if (strcmp("defined", ppToken->name) == 0) {
  376. if (! parseContext.isReadingHLSL() && isMacroInput()) {
  377. if (parseContext.relaxedErrors())
  378. parseContext.ppWarn(ppToken->loc, "nonportable when expanded from macros for preprocessor expression",
  379. "defined", "");
  380. else
  381. parseContext.ppError(ppToken->loc, "cannot use in preprocessor expression when expanded from macros",
  382. "defined", "");
  383. }
  384. bool needclose = 0;
  385. token = scanToken(ppToken);
  386. if (token == '(') {
  387. needclose = true;
  388. token = scanToken(ppToken);
  389. }
  390. if (token != PpAtomIdentifier) {
  391. parseContext.ppError(loc, "incorrect directive, expected identifier", "preprocessor evaluation", "");
  392. err = true;
  393. res = 0;
  394. return token;
  395. }
  396. MacroSymbol* macro = lookupMacroDef(atomStrings.getAtom(ppToken->name));
  397. res = macro != nullptr ? !macro->undef : 0;
  398. token = scanToken(ppToken);
  399. if (needclose) {
  400. if (token != ')') {
  401. parseContext.ppError(loc, "expected ')'", "preprocessor evaluation", "");
  402. err = true;
  403. res = 0;
  404. return token;
  405. }
  406. token = scanToken(ppToken);
  407. }
  408. } else {
  409. token = tokenPaste(token, *ppToken);
  410. token = evalToToken(token, shortCircuit, res, err, ppToken);
  411. return eval(token, precedence, shortCircuit, res, err, ppToken);
  412. }
  413. } else if (token == PpAtomConstInt) {
  414. res = ppToken->ival;
  415. token = scanToken(ppToken);
  416. } else if (token == '(') {
  417. token = scanToken(ppToken);
  418. token = eval(token, MIN_PRECEDENCE, shortCircuit, res, err, ppToken);
  419. if (! err) {
  420. if (token != ')') {
  421. parseContext.ppError(loc, "expected ')'", "preprocessor evaluation", "");
  422. err = true;
  423. res = 0;
  424. return token;
  425. }
  426. token = scanToken(ppToken);
  427. }
  428. } else {
  429. int op = NUM_ELEMENTS(unop) - 1;
  430. for (; op >= 0; op--) {
  431. if (unop[op].token == token)
  432. break;
  433. }
  434. if (op >= 0) {
  435. token = scanToken(ppToken);
  436. token = eval(token, UNARY, shortCircuit, res, err, ppToken);
  437. res = unop[op].op(res);
  438. } else {
  439. parseContext.ppError(loc, "bad expression", "preprocessor evaluation", "");
  440. err = true;
  441. res = 0;
  442. return token;
  443. }
  444. }
  445. token = evalToToken(token, shortCircuit, res, err, ppToken);
  446. // Perform evaluation of binary operation, if there is one, otherwise we are done.
  447. while (! err) {
  448. if (token == ')' || token == '\n')
  449. break;
  450. int op;
  451. for (op = NUM_ELEMENTS(binop) - 1; op >= 0; op--) {
  452. if (binop[op].token == token)
  453. break;
  454. }
  455. if (op < 0 || binop[op].precedence <= precedence)
  456. break;
  457. int leftSide = res;
  458. // Setup short-circuiting, needed for ES, unless already in a short circuit.
  459. // (Once in a short-circuit, can't turn off again, until that whole subexpression is done.
  460. if (! shortCircuit) {
  461. if ((token == PpAtomOr && leftSide == 1) ||
  462. (token == PpAtomAnd && leftSide == 0))
  463. shortCircuit = true;
  464. }
  465. token = scanToken(ppToken);
  466. token = eval(token, binop[op].precedence, shortCircuit, res, err, ppToken);
  467. if (binop[op].op == op_div || binop[op].op == op_mod) {
  468. if (res == 0) {
  469. parseContext.ppError(loc, "division by 0", "preprocessor evaluation", "");
  470. res = 1;
  471. }
  472. }
  473. res = binop[op].op(leftSide, res);
  474. }
  475. return token;
  476. }
  477. // Expand macros, skipping empty expansions, to get to the first real token in those expansions.
  478. int TPpContext::evalToToken(int token, bool shortCircuit, int& res, bool& err, TPpToken* ppToken)
  479. {
  480. while (token == PpAtomIdentifier && strcmp("defined", ppToken->name) != 0) {
  481. switch (MacroExpand(ppToken, true, false)) {
  482. case MacroExpandNotStarted:
  483. case MacroExpandError:
  484. parseContext.ppError(ppToken->loc, "can't evaluate expression", "preprocessor evaluation", "");
  485. err = true;
  486. res = 0;
  487. break;
  488. case MacroExpandStarted:
  489. break;
  490. case MacroExpandUndef:
  491. if (! shortCircuit && parseContext.isEsProfile()) {
  492. const char* message = "undefined macro in expression not allowed in es profile";
  493. if (parseContext.relaxedErrors())
  494. parseContext.ppWarn(ppToken->loc, message, "preprocessor evaluation", ppToken->name);
  495. else
  496. parseContext.ppError(ppToken->loc, message, "preprocessor evaluation", ppToken->name);
  497. }
  498. break;
  499. }
  500. token = scanToken(ppToken);
  501. if (err)
  502. break;
  503. }
  504. return token;
  505. }
  506. // Handle #if
  507. int TPpContext::CPPif(TPpToken* ppToken)
  508. {
  509. int token = scanToken(ppToken);
  510. if (ifdepth >= maxIfNesting || elsetracker >= maxIfNesting) {
  511. parseContext.ppError(ppToken->loc, "maximum nesting depth exceeded", "#if", "");
  512. return EndOfInput;
  513. } else {
  514. elsetracker++;
  515. ifdepth++;
  516. }
  517. int res = 0;
  518. bool err = false;
  519. token = eval(token, MIN_PRECEDENCE, false, res, err, ppToken);
  520. token = extraTokenCheck(PpAtomIf, ppToken, token);
  521. if (!res && !err)
  522. token = CPPelse(1, ppToken);
  523. return token;
  524. }
  525. // Handle #ifdef
  526. int TPpContext::CPPifdef(int defined, TPpToken* ppToken)
  527. {
  528. int token = scanToken(ppToken);
  529. if (ifdepth > maxIfNesting || elsetracker > maxIfNesting) {
  530. parseContext.ppError(ppToken->loc, "maximum nesting depth exceeded", "#ifdef", "");
  531. return EndOfInput;
  532. } else {
  533. elsetracker++;
  534. ifdepth++;
  535. }
  536. if (token != PpAtomIdentifier) {
  537. if (defined)
  538. parseContext.ppError(ppToken->loc, "must be followed by macro name", "#ifdef", "");
  539. else
  540. parseContext.ppError(ppToken->loc, "must be followed by macro name", "#ifndef", "");
  541. } else {
  542. MacroSymbol* macro = lookupMacroDef(atomStrings.getAtom(ppToken->name));
  543. token = scanToken(ppToken);
  544. if (token != '\n') {
  545. parseContext.ppError(ppToken->loc, "unexpected tokens following #ifdef directive - expected a newline", "#ifdef", "");
  546. while (token != '\n' && token != EndOfInput)
  547. token = scanToken(ppToken);
  548. }
  549. if (((macro != nullptr && !macro->undef) ? 1 : 0) != defined)
  550. token = CPPelse(1, ppToken);
  551. }
  552. return token;
  553. }
  554. // Handle #include ...
  555. // TODO: Handle macro expansions for the header name
  556. int TPpContext::CPPinclude(TPpToken* ppToken)
  557. {
  558. const TSourceLoc directiveLoc = ppToken->loc;
  559. bool startWithLocalSearch = true; // to additionally include the extra "" paths
  560. int token;
  561. // Find the first non-whitespace char after #include
  562. int ch = getChar();
  563. while (ch == ' ' || ch == '\t') {
  564. ch = getChar();
  565. }
  566. if (ch == '<') {
  567. // <header-name> style
  568. startWithLocalSearch = false;
  569. token = scanHeaderName(ppToken, '>');
  570. } else if (ch == '"') {
  571. // "header-name" style
  572. token = scanHeaderName(ppToken, '"');
  573. } else {
  574. // unexpected, get the full token to generate the error
  575. ungetChar();
  576. token = scanToken(ppToken);
  577. }
  578. if (token != PpAtomConstString) {
  579. parseContext.ppError(directiveLoc, "must be followed by a header name", "#include", "");
  580. return token;
  581. }
  582. // Make a copy of the name because it will be overwritten by the next token scan.
  583. const std::string filename = ppToken->name;
  584. // See if the directive was well formed
  585. token = scanToken(ppToken);
  586. if (token != '\n') {
  587. if (token == EndOfInput)
  588. parseContext.ppError(ppToken->loc, "expected newline after header name:", "#include", "%s", filename.c_str());
  589. else
  590. parseContext.ppError(ppToken->loc, "extra content after header name:", "#include", "%s", filename.c_str());
  591. return token;
  592. }
  593. // Process well-formed directive
  594. // Find the inclusion, first look in "Local" ("") paths, if requested,
  595. // otherwise, only search the "System" (<>) paths.
  596. TShader::Includer::IncludeResult* res = nullptr;
  597. if (startWithLocalSearch)
  598. res = includer.includeLocal(filename.c_str(), currentSourceFile.c_str(), includeStack.size() + 1);
  599. if (res == nullptr || res->headerName.empty()) {
  600. includer.releaseInclude(res);
  601. res = includer.includeSystem(filename.c_str(), currentSourceFile.c_str(), includeStack.size() + 1);
  602. }
  603. // Process the results
  604. if (res != nullptr && !res->headerName.empty()) {
  605. if (res->headerData != nullptr && res->headerLength > 0) {
  606. // path for processing one or more tokens from an included header, hand off 'res'
  607. const bool forNextLine = parseContext.lineDirectiveShouldSetNextLine();
  608. std::ostringstream prologue;
  609. std::ostringstream epilogue;
  610. prologue << "#line " << forNextLine << " " << "\"" << res->headerName << "\"\n";
  611. epilogue << (res->headerData[res->headerLength - 1] == '\n'? "" : "\n") <<
  612. "#line " << directiveLoc.line + forNextLine << " " << directiveLoc.getStringNameOrNum() << "\n";
  613. pushInput(new TokenizableIncludeFile(directiveLoc, prologue.str(), res, epilogue.str(), this));
  614. parseContext.intermediate.addIncludeText(res->headerName.c_str(), res->headerData, res->headerLength);
  615. // There's no "current" location anymore.
  616. parseContext.setCurrentColumn(0);
  617. } else {
  618. // things are okay, but there is nothing to process
  619. includer.releaseInclude(res);
  620. }
  621. } else {
  622. // error path, clean up
  623. std::string message =
  624. res != nullptr ? std::string(res->headerData, res->headerLength)
  625. : std::string("Could not process include directive");
  626. parseContext.ppError(directiveLoc, message.c_str(), "#include", "for header name: %s", filename.c_str());
  627. includer.releaseInclude(res);
  628. }
  629. return token;
  630. }
  631. // Handle #line
  632. int TPpContext::CPPline(TPpToken* ppToken)
  633. {
  634. // "#line must have, after macro substitution, one of the following forms:
  635. // "#line line
  636. // "#line line source-string-number"
  637. int token = scanToken(ppToken);
  638. const TSourceLoc directiveLoc = ppToken->loc;
  639. if (token == '\n') {
  640. parseContext.ppError(ppToken->loc, "must by followed by an integral literal", "#line", "");
  641. return token;
  642. }
  643. int lineRes = 0; // Line number after macro expansion.
  644. int lineToken = 0;
  645. bool hasFile = false;
  646. int fileRes = 0; // Source file number after macro expansion.
  647. const char* sourceName = nullptr; // Optional source file name.
  648. bool lineErr = false;
  649. bool fileErr = false;
  650. disableEscapeSequences = true;
  651. token = eval(token, MIN_PRECEDENCE, false, lineRes, lineErr, ppToken);
  652. disableEscapeSequences = false;
  653. if (! lineErr) {
  654. lineToken = lineRes;
  655. if (token == '\n')
  656. ++lineRes;
  657. if (parseContext.lineDirectiveShouldSetNextLine())
  658. --lineRes;
  659. parseContext.setCurrentLine(lineRes);
  660. if (token != '\n') {
  661. #ifndef GLSLANG_WEB
  662. if (token == PpAtomConstString) {
  663. parseContext.ppRequireExtensions(directiveLoc, 1, &E_GL_GOOGLE_cpp_style_line_directive, "filename-based #line");
  664. // We need to save a copy of the string instead of pointing
  665. // to the name field of the token since the name field
  666. // will likely be overwritten by the next token scan.
  667. sourceName = atomStrings.getString(atomStrings.getAddAtom(ppToken->name));
  668. parseContext.setCurrentSourceName(sourceName);
  669. hasFile = true;
  670. token = scanToken(ppToken);
  671. } else
  672. #endif
  673. {
  674. token = eval(token, MIN_PRECEDENCE, false, fileRes, fileErr, ppToken);
  675. if (! fileErr) {
  676. parseContext.setCurrentString(fileRes);
  677. hasFile = true;
  678. }
  679. }
  680. }
  681. }
  682. if (!fileErr && !lineErr) {
  683. parseContext.notifyLineDirective(directiveLoc.line, lineToken, hasFile, fileRes, sourceName);
  684. }
  685. token = extraTokenCheck(PpAtomLine, ppToken, token);
  686. return token;
  687. }
  688. // Handle #error
  689. int TPpContext::CPPerror(TPpToken* ppToken)
  690. {
  691. disableEscapeSequences = true;
  692. int token = scanToken(ppToken);
  693. disableEscapeSequences = false;
  694. std::string message;
  695. TSourceLoc loc = ppToken->loc;
  696. while (token != '\n' && token != EndOfInput) {
  697. if (token == PpAtomConstInt16 || token == PpAtomConstUint16 ||
  698. token == PpAtomConstInt || token == PpAtomConstUint ||
  699. token == PpAtomConstInt64 || token == PpAtomConstUint64 ||
  700. token == PpAtomConstFloat16 ||
  701. token == PpAtomConstFloat || token == PpAtomConstDouble) {
  702. message.append(ppToken->name);
  703. } else if (token == PpAtomIdentifier || token == PpAtomConstString) {
  704. message.append(ppToken->name);
  705. } else {
  706. message.append(atomStrings.getString(token));
  707. }
  708. message.append(" ");
  709. token = scanToken(ppToken);
  710. }
  711. parseContext.notifyErrorDirective(loc.line, message.c_str());
  712. // store this msg into the shader's information log..set the Compile Error flag!!!!
  713. parseContext.ppError(loc, message.c_str(), "#error", "");
  714. return '\n';
  715. }
  716. // Handle #pragma
  717. int TPpContext::CPPpragma(TPpToken* ppToken)
  718. {
  719. char SrcStrName[2];
  720. TVector<TString> tokens;
  721. TSourceLoc loc = ppToken->loc; // because we go to the next line before processing
  722. int token = scanToken(ppToken);
  723. while (token != '\n' && token != EndOfInput) {
  724. switch (token) {
  725. case PpAtomIdentifier:
  726. case PpAtomConstInt:
  727. case PpAtomConstUint:
  728. case PpAtomConstInt64:
  729. case PpAtomConstUint64:
  730. case PpAtomConstInt16:
  731. case PpAtomConstUint16:
  732. case PpAtomConstFloat:
  733. case PpAtomConstDouble:
  734. case PpAtomConstFloat16:
  735. tokens.push_back(ppToken->name);
  736. break;
  737. default:
  738. SrcStrName[0] = (char)token;
  739. SrcStrName[1] = '\0';
  740. tokens.push_back(SrcStrName);
  741. }
  742. token = scanToken(ppToken);
  743. }
  744. if (token == EndOfInput)
  745. parseContext.ppError(loc, "directive must end with a newline", "#pragma", "");
  746. else
  747. parseContext.handlePragma(loc, tokens);
  748. return token;
  749. }
  750. // #version: This is just for error checking: the version and profile are decided before preprocessing starts
  751. int TPpContext::CPPversion(TPpToken* ppToken)
  752. {
  753. int token = scanToken(ppToken);
  754. if (errorOnVersion || versionSeen) {
  755. if (parseContext.isReadingHLSL())
  756. parseContext.ppError(ppToken->loc, "invalid preprocessor command", "#version", "");
  757. else
  758. parseContext.ppError(ppToken->loc, "must occur first in shader", "#version", "");
  759. }
  760. versionSeen = true;
  761. if (token == '\n') {
  762. parseContext.ppError(ppToken->loc, "must be followed by version number", "#version", "");
  763. return token;
  764. }
  765. if (token != PpAtomConstInt)
  766. parseContext.ppError(ppToken->loc, "must be followed by version number", "#version", "");
  767. ppToken->ival = atoi(ppToken->name);
  768. int versionNumber = ppToken->ival;
  769. int line = ppToken->loc.line;
  770. token = scanToken(ppToken);
  771. if (token == '\n') {
  772. parseContext.notifyVersion(line, versionNumber, nullptr);
  773. return token;
  774. } else {
  775. int profileAtom = atomStrings.getAtom(ppToken->name);
  776. if (profileAtom != PpAtomCore &&
  777. profileAtom != PpAtomCompatibility &&
  778. profileAtom != PpAtomEs)
  779. parseContext.ppError(ppToken->loc, "bad profile name; use es, core, or compatibility", "#version", "");
  780. parseContext.notifyVersion(line, versionNumber, ppToken->name);
  781. token = scanToken(ppToken);
  782. if (token == '\n')
  783. return token;
  784. else
  785. parseContext.ppError(ppToken->loc, "bad tokens following profile -- expected newline", "#version", "");
  786. }
  787. return token;
  788. }
  789. // Handle #extension
  790. int TPpContext::CPPextension(TPpToken* ppToken)
  791. {
  792. int line = ppToken->loc.line;
  793. int token = scanToken(ppToken);
  794. char extensionName[MaxTokenLength + 1];
  795. if (token=='\n') {
  796. parseContext.ppError(ppToken->loc, "extension name not specified", "#extension", "");
  797. return token;
  798. }
  799. if (token != PpAtomIdentifier)
  800. parseContext.ppError(ppToken->loc, "extension name expected", "#extension", "");
  801. snprintf(extensionName, sizeof(extensionName), "%s", ppToken->name);
  802. token = scanToken(ppToken);
  803. if (token != ':') {
  804. parseContext.ppError(ppToken->loc, "':' missing after extension name", "#extension", "");
  805. return token;
  806. }
  807. token = scanToken(ppToken);
  808. if (token != PpAtomIdentifier) {
  809. parseContext.ppError(ppToken->loc, "behavior for extension not specified", "#extension", "");
  810. return token;
  811. }
  812. parseContext.updateExtensionBehavior(line, extensionName, ppToken->name);
  813. parseContext.notifyExtensionDirective(line, extensionName, ppToken->name);
  814. token = scanToken(ppToken);
  815. if (token == '\n')
  816. return token;
  817. else
  818. parseContext.ppError(ppToken->loc, "extra tokens -- expected newline", "#extension","");
  819. return token;
  820. }
  821. int TPpContext::readCPPline(TPpToken* ppToken)
  822. {
  823. int token = scanToken(ppToken);
  824. if (token == PpAtomIdentifier) {
  825. switch (atomStrings.getAtom(ppToken->name)) {
  826. case PpAtomDefine:
  827. token = CPPdefine(ppToken);
  828. break;
  829. case PpAtomElse:
  830. if (elseSeen[elsetracker])
  831. parseContext.ppError(ppToken->loc, "#else after #else", "#else", "");
  832. elseSeen[elsetracker] = true;
  833. if (ifdepth == 0)
  834. parseContext.ppError(ppToken->loc, "mismatched statements", "#else", "");
  835. token = extraTokenCheck(PpAtomElse, ppToken, scanToken(ppToken));
  836. token = CPPelse(0, ppToken);
  837. break;
  838. case PpAtomElif:
  839. if (ifdepth == 0)
  840. parseContext.ppError(ppToken->loc, "mismatched statements", "#elif", "");
  841. if (elseSeen[elsetracker])
  842. parseContext.ppError(ppToken->loc, "#elif after #else", "#elif", "");
  843. // this token is really a dont care, but we still need to eat the tokens
  844. token = scanToken(ppToken);
  845. while (token != '\n' && token != EndOfInput)
  846. token = scanToken(ppToken);
  847. token = CPPelse(0, ppToken);
  848. break;
  849. case PpAtomEndif:
  850. if (ifdepth == 0)
  851. parseContext.ppError(ppToken->loc, "mismatched statements", "#endif", "");
  852. else {
  853. elseSeen[elsetracker] = false;
  854. --elsetracker;
  855. --ifdepth;
  856. }
  857. token = extraTokenCheck(PpAtomEndif, ppToken, scanToken(ppToken));
  858. break;
  859. case PpAtomIf:
  860. token = CPPif(ppToken);
  861. break;
  862. case PpAtomIfdef:
  863. token = CPPifdef(1, ppToken);
  864. break;
  865. case PpAtomIfndef:
  866. token = CPPifdef(0, ppToken);
  867. break;
  868. case PpAtomLine:
  869. token = CPPline(ppToken);
  870. break;
  871. #ifndef GLSLANG_WEB
  872. case PpAtomInclude:
  873. if(!parseContext.isReadingHLSL()) {
  874. parseContext.ppRequireExtensions(ppToken->loc, 1, &E_GL_GOOGLE_include_directive, "#include");
  875. }
  876. token = CPPinclude(ppToken);
  877. break;
  878. case PpAtomPragma:
  879. token = CPPpragma(ppToken);
  880. break;
  881. #endif
  882. case PpAtomUndef:
  883. token = CPPundef(ppToken);
  884. break;
  885. case PpAtomError:
  886. token = CPPerror(ppToken);
  887. break;
  888. case PpAtomVersion:
  889. token = CPPversion(ppToken);
  890. break;
  891. case PpAtomExtension:
  892. token = CPPextension(ppToken);
  893. break;
  894. default:
  895. parseContext.ppError(ppToken->loc, "invalid directive:", "#", ppToken->name);
  896. break;
  897. }
  898. } else if (token != '\n' && token != EndOfInput)
  899. parseContext.ppError(ppToken->loc, "invalid directive", "#", "");
  900. while (token != '\n' && token != EndOfInput)
  901. token = scanToken(ppToken);
  902. return token;
  903. }
  904. // Context-dependent parsing of a #include <header-name>.
  905. // Assumes no macro expansions etc. are being done; the name is just on the current input.
  906. // Always creates a name and returns PpAtomicConstString, unless we run out of input.
  907. int TPpContext::scanHeaderName(TPpToken* ppToken, char delimit)
  908. {
  909. bool tooLong = false;
  910. if (inputStack.empty())
  911. return EndOfInput;
  912. int len = 0;
  913. ppToken->name[0] = '\0';
  914. do {
  915. int ch = inputStack.back()->getch();
  916. // done yet?
  917. if (ch == delimit) {
  918. ppToken->name[len] = '\0';
  919. if (tooLong)
  920. parseContext.ppError(ppToken->loc, "header name too long", "", "");
  921. return PpAtomConstString;
  922. } else if (ch == EndOfInput)
  923. return EndOfInput;
  924. // found a character to expand the name with
  925. if (len < MaxTokenLength)
  926. ppToken->name[len++] = (char)ch;
  927. else
  928. tooLong = true;
  929. } while (true);
  930. }
  931. // Macro-expand a macro argument 'arg' to create 'expandedArg'.
  932. // Does not replace 'arg'.
  933. // Returns nullptr if no expanded argument is created.
  934. TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream& arg, TPpToken* ppToken, bool newLineOkay)
  935. {
  936. // expand the argument
  937. TokenStream* expandedArg = new TokenStream;
  938. pushInput(new tMarkerInput(this));
  939. pushTokenStreamInput(arg);
  940. int token;
  941. while ((token = scanToken(ppToken)) != tMarkerInput::marker && token != EndOfInput) {
  942. token = tokenPaste(token, *ppToken);
  943. if (token == PpAtomIdentifier) {
  944. switch (MacroExpand(ppToken, false, newLineOkay)) {
  945. case MacroExpandNotStarted:
  946. break;
  947. case MacroExpandError:
  948. // toss the rest of the pushed-input argument by scanning until tMarkerInput
  949. while ((token = scanToken(ppToken)) != tMarkerInput::marker && token != EndOfInput)
  950. ;
  951. break;
  952. case MacroExpandStarted:
  953. case MacroExpandUndef:
  954. continue;
  955. }
  956. }
  957. if (token == tMarkerInput::marker || token == EndOfInput)
  958. break;
  959. expandedArg->putToken(token, ppToken);
  960. }
  961. if (token != tMarkerInput::marker) {
  962. // Error, or MacroExpand ate the marker, so had bad input, recover
  963. delete expandedArg;
  964. expandedArg = nullptr;
  965. }
  966. return expandedArg;
  967. }
  968. //
  969. // Return the next token for a macro expansion, handling macro arguments,
  970. // whose semantics are dependent on being adjacent to ##.
  971. //
  972. int TPpContext::tMacroInput::scan(TPpToken* ppToken)
  973. {
  974. int token;
  975. do {
  976. token = mac->body.getToken(pp->parseContext, ppToken);
  977. } while (token == ' '); // handle white space in macro
  978. // Hash operators basically turn off a round of macro substitution
  979. // (the round done on the argument before the round done on the RHS of the
  980. // macro definition):
  981. //
  982. // "A parameter in the replacement list, unless preceded by a # or ##
  983. // preprocessing token or followed by a ## preprocessing token (see below),
  984. // is replaced by the corresponding argument after all macros contained
  985. // therein have been expanded."
  986. //
  987. // "If, in the replacement list, a parameter is immediately preceded or
  988. // followed by a ## preprocessing token, the parameter is replaced by the
  989. // corresponding argument's preprocessing token sequence."
  990. bool pasting = false;
  991. if (postpaste) {
  992. // don't expand next token
  993. pasting = true;
  994. postpaste = false;
  995. }
  996. if (prepaste) {
  997. // already know we should be on a ##, verify
  998. assert(token == PpAtomPaste);
  999. prepaste = false;
  1000. postpaste = true;
  1001. }
  1002. // see if are preceding a ##
  1003. if (mac->body.peekUntokenizedPasting()) {
  1004. prepaste = true;
  1005. pasting = true;
  1006. }
  1007. // HLSL does expand macros before concatenation
  1008. if (pasting && pp->parseContext.isReadingHLSL())
  1009. pasting = false;
  1010. // TODO: preprocessor: properly handle whitespace (or lack of it) between tokens when expanding
  1011. if (token == PpAtomIdentifier) {
  1012. int i;
  1013. for (i = (int)mac->args.size() - 1; i >= 0; i--)
  1014. if (strcmp(pp->atomStrings.getString(mac->args[i]), ppToken->name) == 0)
  1015. break;
  1016. if (i >= 0) {
  1017. TokenStream* arg = expandedArgs[i];
  1018. if (arg == nullptr || pasting)
  1019. arg = args[i];
  1020. pp->pushTokenStreamInput(*arg, prepaste);
  1021. return pp->scanToken(ppToken);
  1022. }
  1023. }
  1024. if (token == EndOfInput)
  1025. mac->busy = 0;
  1026. return token;
  1027. }
  1028. // return a textual zero, for scanning a macro that was never defined
  1029. int TPpContext::tZeroInput::scan(TPpToken* ppToken)
  1030. {
  1031. if (done)
  1032. return EndOfInput;
  1033. ppToken->name[0] = '0';
  1034. ppToken->name[1] = 0;
  1035. ppToken->ival = 0;
  1036. ppToken->space = false;
  1037. done = true;
  1038. return PpAtomConstInt;
  1039. }
  1040. //
  1041. // Check a token to see if it is a macro that should be expanded:
  1042. // - If it is, and defined, push a tInput that will produce the appropriate
  1043. // expansion and return MacroExpandStarted.
  1044. // - If it is, but undefined, and expandUndef is requested, push a tInput
  1045. // that will expand to 0 and return MacroExpandUndef.
  1046. // - Otherwise, there is no expansion, and there are two cases:
  1047. // * It might be okay there is no expansion, and no specific error was
  1048. // detected. Returns MacroExpandNotStarted.
  1049. // * The expansion was started, but could not be completed, due to an error
  1050. // that cannot be recovered from. Returns MacroExpandError.
  1051. //
  1052. MacroExpandResult TPpContext::MacroExpand(TPpToken* ppToken, bool expandUndef, bool newLineOkay)
  1053. {
  1054. ppToken->space = false;
  1055. int macroAtom = atomStrings.getAtom(ppToken->name);
  1056. switch (macroAtom) {
  1057. case PpAtomLineMacro:
  1058. // Arguments which are macro have been replaced in the first stage.
  1059. if (ppToken->ival == 0)
  1060. ppToken->ival = parseContext.getCurrentLoc().line;
  1061. snprintf(ppToken->name, sizeof(ppToken->name), "%d", ppToken->ival);
  1062. UngetToken(PpAtomConstInt, ppToken);
  1063. return MacroExpandStarted;
  1064. case PpAtomFileMacro: {
  1065. if (parseContext.getCurrentLoc().name)
  1066. parseContext.ppRequireExtensions(ppToken->loc, 1, &E_GL_GOOGLE_cpp_style_line_directive, "filename-based __FILE__");
  1067. ppToken->ival = parseContext.getCurrentLoc().string;
  1068. snprintf(ppToken->name, sizeof(ppToken->name), "%s", ppToken->loc.getStringNameOrNum().c_str());
  1069. UngetToken(PpAtomConstInt, ppToken);
  1070. return MacroExpandStarted;
  1071. }
  1072. case PpAtomVersionMacro:
  1073. ppToken->ival = parseContext.version;
  1074. snprintf(ppToken->name, sizeof(ppToken->name), "%d", ppToken->ival);
  1075. UngetToken(PpAtomConstInt, ppToken);
  1076. return MacroExpandStarted;
  1077. default:
  1078. break;
  1079. }
  1080. MacroSymbol* macro = macroAtom == 0 ? nullptr : lookupMacroDef(macroAtom);
  1081. // no recursive expansions
  1082. if (macro != nullptr && macro->busy)
  1083. return MacroExpandNotStarted;
  1084. // not expanding undefined macros
  1085. if ((macro == nullptr || macro->undef) && ! expandUndef)
  1086. return MacroExpandNotStarted;
  1087. // 0 is the value of an undefined macro
  1088. if ((macro == nullptr || macro->undef) && expandUndef) {
  1089. pushInput(new tZeroInput(this));
  1090. return MacroExpandUndef;
  1091. }
  1092. tMacroInput *in = new tMacroInput(this);
  1093. TSourceLoc loc = ppToken->loc; // in case we go to the next line before discovering the error
  1094. in->mac = macro;
  1095. if (macro->functionLike) {
  1096. // We don't know yet if this will be a successful call of a
  1097. // function-like macro; need to look for a '(', but without trashing
  1098. // the passed in ppToken, until we know we are no longer speculative.
  1099. TPpToken parenToken;
  1100. int token = scanToken(&parenToken);
  1101. if (newLineOkay) {
  1102. while (token == '\n')
  1103. token = scanToken(&parenToken);
  1104. }
  1105. if (token != '(') {
  1106. // Function-like macro called with object-like syntax: okay, don't expand.
  1107. // (We ate exactly one token that might not be white space; put it back.
  1108. UngetToken(token, &parenToken);
  1109. delete in;
  1110. return MacroExpandNotStarted;
  1111. }
  1112. in->args.resize(in->mac->args.size());
  1113. for (size_t i = 0; i < in->mac->args.size(); i++)
  1114. in->args[i] = new TokenStream;
  1115. in->expandedArgs.resize(in->mac->args.size());
  1116. for (size_t i = 0; i < in->mac->args.size(); i++)
  1117. in->expandedArgs[i] = nullptr;
  1118. size_t arg = 0;
  1119. bool tokenRecorded = false;
  1120. do {
  1121. TVector<char> nestStack;
  1122. while (true) {
  1123. token = scanToken(ppToken);
  1124. if (token == EndOfInput || token == tMarkerInput::marker) {
  1125. parseContext.ppError(loc, "End of input in macro", "macro expansion", atomStrings.getString(macroAtom));
  1126. delete in;
  1127. return MacroExpandError;
  1128. }
  1129. if (token == '\n') {
  1130. if (! newLineOkay) {
  1131. parseContext.ppError(loc, "End of line in macro substitution:", "macro expansion", atomStrings.getString(macroAtom));
  1132. delete in;
  1133. return MacroExpandError;
  1134. }
  1135. continue;
  1136. }
  1137. if (token == '#') {
  1138. parseContext.ppError(ppToken->loc, "unexpected '#'", "macro expansion", atomStrings.getString(macroAtom));
  1139. delete in;
  1140. return MacroExpandError;
  1141. }
  1142. if (in->mac->args.size() == 0 && token != ')')
  1143. break;
  1144. if (nestStack.size() == 0 && (token == ',' || token == ')'))
  1145. break;
  1146. if (token == '(')
  1147. nestStack.push_back(')');
  1148. else if (token == '{' && parseContext.isReadingHLSL())
  1149. nestStack.push_back('}');
  1150. else if (nestStack.size() > 0 && token == nestStack.back())
  1151. nestStack.pop_back();
  1152. //Macro replacement list is expanded in the last stage.
  1153. if (atomStrings.getAtom(ppToken->name) == PpAtomLineMacro)
  1154. ppToken->ival = parseContext.getCurrentLoc().line;
  1155. in->args[arg]->putToken(token, ppToken);
  1156. tokenRecorded = true;
  1157. }
  1158. // end of single argument scan
  1159. if (token == ')') {
  1160. // closing paren of call
  1161. if (in->mac->args.size() == 1 && !tokenRecorded)
  1162. break;
  1163. arg++;
  1164. break;
  1165. }
  1166. arg++;
  1167. } while (arg < in->mac->args.size());
  1168. // end of all arguments scan
  1169. if (arg < in->mac->args.size())
  1170. parseContext.ppError(loc, "Too few args in Macro", "macro expansion", atomStrings.getString(macroAtom));
  1171. else if (token != ')') {
  1172. // Error recover code; find end of call, if possible
  1173. int depth = 0;
  1174. while (token != EndOfInput && (depth > 0 || token != ')')) {
  1175. if (token == ')' || token == '}')
  1176. depth--;
  1177. token = scanToken(ppToken);
  1178. if (token == '(' || token == '{')
  1179. depth++;
  1180. }
  1181. if (token == EndOfInput) {
  1182. parseContext.ppError(loc, "End of input in macro", "macro expansion", atomStrings.getString(macroAtom));
  1183. delete in;
  1184. return MacroExpandError;
  1185. }
  1186. parseContext.ppError(loc, "Too many args in macro", "macro expansion", atomStrings.getString(macroAtom));
  1187. }
  1188. // We need both expanded and non-expanded forms of the argument, for whether or
  1189. // not token pasting will be applied later when the argument is consumed next to ##.
  1190. for (size_t i = 0; i < in->mac->args.size(); i++)
  1191. in->expandedArgs[i] = PrescanMacroArg(*in->args[i], ppToken, newLineOkay);
  1192. }
  1193. pushInput(in);
  1194. macro->busy = 1;
  1195. macro->body.reset();
  1196. return MacroExpandStarted;
  1197. }
  1198. } // end namespace glslang