shader_preprocessor.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. /**************************************************************************/
  2. /* shader_preprocessor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "shader_preprocessor.h"
  31. #include "core/math/expression.h"
  32. const char32_t CURSOR = 0xFFFF;
  33. // Tokenizer
  34. void ShaderPreprocessor::Tokenizer::add_generated(const ShaderPreprocessor::Token &p_t) {
  35. generated.push_back(p_t);
  36. }
  37. char32_t ShaderPreprocessor::Tokenizer::next() {
  38. if (index < size) {
  39. return code[index++];
  40. }
  41. return 0;
  42. }
  43. int ShaderPreprocessor::Tokenizer::get_line() const {
  44. return line;
  45. }
  46. int ShaderPreprocessor::Tokenizer::get_index() const {
  47. return index;
  48. }
  49. void ShaderPreprocessor::Tokenizer::get_and_clear_generated(LocalVector<char32_t> *r_out) {
  50. for (uint32_t i = 0; i < generated.size(); i++) {
  51. r_out->push_back(generated[i].text);
  52. }
  53. generated.clear();
  54. }
  55. void ShaderPreprocessor::Tokenizer::backtrack(char32_t p_what) {
  56. while (index >= 0) {
  57. char32_t c = code[index];
  58. if (c == p_what) {
  59. break;
  60. }
  61. index--;
  62. }
  63. }
  64. char32_t ShaderPreprocessor::Tokenizer::peek() {
  65. if (index < size) {
  66. return code[index];
  67. }
  68. return 0;
  69. }
  70. int ShaderPreprocessor::Tokenizer::consume_line_continuations(int p_offset) {
  71. int skips = 0;
  72. for (int i = index + p_offset; i < size; i++) {
  73. char32_t c = code[i];
  74. if (c == '\\') {
  75. if (i + 1 < size && code[i + 1] == '\n') {
  76. // This line ends with "\" and "\n" continuation.
  77. add_generated(Token('\n', line));
  78. line++;
  79. skips++;
  80. i = i + 2;
  81. index = i;
  82. } else {
  83. break;
  84. }
  85. } else if (!is_whitespace(c)) {
  86. break;
  87. }
  88. }
  89. return skips;
  90. }
  91. LocalVector<ShaderPreprocessor::Token> ShaderPreprocessor::Tokenizer::advance(char32_t p_what) {
  92. LocalVector<ShaderPreprocessor::Token> tokens;
  93. while (index < size) {
  94. char32_t c = code[index++];
  95. if (c == '\\' && consume_line_continuations(-1) > 0) {
  96. continue;
  97. }
  98. if (c == '\n') {
  99. add_generated(ShaderPreprocessor::Token('\n', line));
  100. line++;
  101. }
  102. tokens.push_back(ShaderPreprocessor::Token(c, line));
  103. if (c == p_what || c == 0) {
  104. return tokens;
  105. }
  106. }
  107. return LocalVector<ShaderPreprocessor::Token>();
  108. }
  109. void ShaderPreprocessor::Tokenizer::skip_whitespace() {
  110. while (is_char_space(peek())) {
  111. next();
  112. }
  113. }
  114. bool ShaderPreprocessor::Tokenizer::consume_empty_line() {
  115. // Read until newline and return true if the content was all whitespace/empty.
  116. return tokens_to_string(advance('\n')).strip_edges().is_empty();
  117. }
  118. String ShaderPreprocessor::Tokenizer::get_identifier(bool *r_is_cursor, bool p_started) {
  119. if (r_is_cursor != nullptr) {
  120. *r_is_cursor = false;
  121. }
  122. LocalVector<char32_t> text;
  123. while (true) {
  124. char32_t c = peek();
  125. if (c == '\\' && consume_line_continuations(0) > 0) {
  126. continue;
  127. }
  128. if (is_char_end(c) || c == '(' || c == ')' || c == ',' || c == ';') {
  129. break;
  130. }
  131. if (is_whitespace(c) && p_started) {
  132. break;
  133. }
  134. if (!is_whitespace(c)) {
  135. p_started = true;
  136. }
  137. char32_t n = next();
  138. if (n == CURSOR) {
  139. if (r_is_cursor != nullptr) {
  140. *r_is_cursor = true;
  141. }
  142. } else {
  143. if (p_started) {
  144. text.push_back(n);
  145. }
  146. }
  147. }
  148. String id = vector_to_string(text);
  149. if (!id.is_valid_ascii_identifier()) {
  150. return "";
  151. }
  152. return id;
  153. }
  154. String ShaderPreprocessor::Tokenizer::peek_identifier() {
  155. const int original = index;
  156. const int original_line = line;
  157. String id = get_identifier();
  158. index = original;
  159. line = original_line;
  160. return id;
  161. }
  162. ShaderPreprocessor::Token ShaderPreprocessor::Tokenizer::get_token() {
  163. while (index < size) {
  164. const char32_t c = code[index++];
  165. const Token t = ShaderPreprocessor::Token(c, line);
  166. switch (c) {
  167. case ' ':
  168. case '\t':
  169. skip_whitespace();
  170. return ShaderPreprocessor::Token(' ', line);
  171. case '\n':
  172. line++;
  173. return t;
  174. default:
  175. return t;
  176. }
  177. }
  178. return ShaderPreprocessor::Token(char32_t(0), line);
  179. }
  180. ShaderPreprocessor::Tokenizer::Tokenizer(const String &p_code) {
  181. code = p_code;
  182. line = 0;
  183. index = 0;
  184. size = code.size();
  185. }
  186. // ShaderPreprocessor::CommentRemover
  187. String ShaderPreprocessor::CommentRemover::get_error() const {
  188. if (comments_open != 0) {
  189. return "Block comment mismatch";
  190. }
  191. return "";
  192. }
  193. int ShaderPreprocessor::CommentRemover::get_error_line() const {
  194. if (comments_open != 0) {
  195. return comment_line_open;
  196. }
  197. return -1;
  198. }
  199. char32_t ShaderPreprocessor::CommentRemover::peek() const {
  200. if (index < code.size()) {
  201. return code[index];
  202. }
  203. return 0;
  204. }
  205. bool ShaderPreprocessor::CommentRemover::advance(char32_t p_what) {
  206. while (index < code.size()) {
  207. char32_t c = code[index++];
  208. if (c == '\n') {
  209. line++;
  210. stripped.push_back('\n');
  211. }
  212. if (c == p_what) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. String ShaderPreprocessor::CommentRemover::strip() {
  219. stripped.clear();
  220. index = 0;
  221. line = 0;
  222. comment_line_open = 0;
  223. comments_open = 0;
  224. strings_open = 0;
  225. while (index < code.size()) {
  226. char32_t c = code[index++];
  227. if (c == CURSOR) {
  228. // Cursor. Maintain.
  229. stripped.push_back(c);
  230. } else if (c == '"') {
  231. if (strings_open <= 0) {
  232. strings_open++;
  233. } else {
  234. strings_open--;
  235. }
  236. stripped.push_back(c);
  237. } else if (c == '/' && strings_open == 0) {
  238. char32_t p = peek();
  239. if (p == '/') { // Single line comment.
  240. advance('\n');
  241. } else if (p == '*') { // Start of a block comment.
  242. index++;
  243. comment_line_open = line;
  244. comments_open++;
  245. while (advance('*')) {
  246. if (peek() == '/') { // End of a block comment.
  247. comments_open--;
  248. index++;
  249. break;
  250. }
  251. }
  252. } else {
  253. stripped.push_back(c);
  254. }
  255. } else if (c == '*' && strings_open == 0) {
  256. if (peek() == '/') { // Unmatched end of a block comment.
  257. comment_line_open = line;
  258. comments_open--;
  259. } else {
  260. stripped.push_back(c);
  261. }
  262. } else if (c == '\n') {
  263. line++;
  264. stripped.push_back(c);
  265. } else {
  266. stripped.push_back(c);
  267. }
  268. }
  269. return vector_to_string(stripped);
  270. }
  271. ShaderPreprocessor::CommentRemover::CommentRemover(const String &p_code) {
  272. code = p_code;
  273. index = 0;
  274. line = 0;
  275. comment_line_open = 0;
  276. comments_open = 0;
  277. strings_open = 0;
  278. }
  279. // ShaderPreprocessor::Token
  280. ShaderPreprocessor::Token::Token() {
  281. text = 0;
  282. line = -1;
  283. }
  284. ShaderPreprocessor::Token::Token(char32_t p_text, int p_line) {
  285. text = p_text;
  286. line = p_line;
  287. }
  288. // ShaderPreprocessor
  289. bool ShaderPreprocessor::is_char_word(char32_t p_char) {
  290. if ((p_char >= '0' && p_char <= '9') ||
  291. (p_char >= 'a' && p_char <= 'z') ||
  292. (p_char >= 'A' && p_char <= 'Z') ||
  293. p_char == '_') {
  294. return true;
  295. }
  296. return false;
  297. }
  298. bool ShaderPreprocessor::is_char_space(char32_t p_char) {
  299. return p_char == ' ' || p_char == '\t';
  300. }
  301. bool ShaderPreprocessor::is_char_end(char32_t p_char) {
  302. return p_char == '\n' || p_char == 0;
  303. }
  304. String ShaderPreprocessor::vector_to_string(const LocalVector<char32_t> &p_v, int p_start, int p_end) {
  305. const int stop = (p_end == -1) ? p_v.size() : p_end;
  306. const int count = stop - p_start;
  307. String result;
  308. result.resize_uninitialized(count + 1);
  309. for (int i = 0; i < count; i++) {
  310. result[i] = p_v[p_start + i];
  311. }
  312. result[count] = 0; // Ensure string is null terminated for length() to work.
  313. return result;
  314. }
  315. String ShaderPreprocessor::tokens_to_string(const LocalVector<Token> &p_tokens) {
  316. LocalVector<char32_t> result;
  317. for (const Token &token : p_tokens) {
  318. result.push_back(token.text);
  319. }
  320. return vector_to_string(result);
  321. }
  322. void ShaderPreprocessor::process_directive(Tokenizer *p_tokenizer) {
  323. bool is_cursor;
  324. String directive = p_tokenizer->get_identifier(&is_cursor, true);
  325. if (is_cursor) {
  326. state->completion_type = COMPLETION_TYPE_DIRECTIVE;
  327. }
  328. if (directive == "if") {
  329. process_if(p_tokenizer);
  330. } else if (directive == "ifdef") {
  331. process_ifdef(p_tokenizer);
  332. } else if (directive == "ifndef") {
  333. process_ifndef(p_tokenizer);
  334. } else if (directive == "elif") {
  335. process_elif(p_tokenizer);
  336. } else if (directive == "else") {
  337. process_else(p_tokenizer);
  338. } else if (directive == "endif") {
  339. process_endif(p_tokenizer);
  340. } else if (directive == "error") {
  341. process_error(p_tokenizer);
  342. } else if (directive == "define") {
  343. process_define(p_tokenizer);
  344. } else if (directive == "undef") {
  345. process_undef(p_tokenizer);
  346. } else if (directive == "include") {
  347. process_include(p_tokenizer);
  348. } else if (directive == "pragma") {
  349. process_pragma(p_tokenizer);
  350. } else {
  351. set_error(RTR("Unknown directive."), p_tokenizer->get_line());
  352. }
  353. }
  354. void ShaderPreprocessor::process_define(Tokenizer *p_tokenizer) {
  355. const int line = p_tokenizer->get_line();
  356. String label = p_tokenizer->get_identifier();
  357. if (label.is_empty()) {
  358. set_error(RTR("Invalid macro name."), line);
  359. return;
  360. }
  361. if (state->defines.has(label)) {
  362. set_error(RTR("Macro redefinition."), line);
  363. return;
  364. }
  365. Vector<String> args;
  366. if (p_tokenizer->peek() == '(') {
  367. // Macro has arguments.
  368. p_tokenizer->get_token();
  369. while (true) {
  370. String name = p_tokenizer->get_identifier();
  371. if (name.is_empty()) {
  372. set_error(RTR("Invalid argument name."), line);
  373. return;
  374. }
  375. args.push_back(name);
  376. p_tokenizer->skip_whitespace();
  377. char32_t next = p_tokenizer->get_token().text;
  378. if (next == ')') {
  379. break;
  380. } else if (next != ',') {
  381. set_error(RTR("Expected a comma in the macro argument list."), line);
  382. return;
  383. }
  384. }
  385. }
  386. String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  387. if (body.begins_with("##")) {
  388. set_error(RTR("'##' must not appear at beginning of macro expansion."), line);
  389. return;
  390. }
  391. if (body.ends_with("##")) {
  392. set_error(RTR("'##' must not appear at end of macro expansion."), line);
  393. return;
  394. }
  395. Define *define = memnew(Define);
  396. if (!args.is_empty()) {
  397. define->arguments = args;
  398. }
  399. define->body = body;
  400. state->defines[label] = define;
  401. }
  402. void ShaderPreprocessor::process_elif(Tokenizer *p_tokenizer) {
  403. const int line = p_tokenizer->get_line();
  404. if (state->current_branch == nullptr || state->current_branch->else_defined) {
  405. set_error(vformat(RTR("Unmatched '%s' directive."), "elif"), line);
  406. return;
  407. }
  408. if (state->previous_region != nullptr) {
  409. state->previous_region->to_line = line - 1;
  410. }
  411. String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  412. if (body.is_empty()) {
  413. set_error(RTR("Missing condition."), line);
  414. return;
  415. }
  416. Error error = expand_condition(body, line, body);
  417. if (error != OK) {
  418. return;
  419. }
  420. error = expand_macros(body, line, body);
  421. if (error != OK) {
  422. return;
  423. }
  424. Expression expression;
  425. Vector<String> names;
  426. error = expression.parse(body, names);
  427. if (error != OK) {
  428. set_error(expression.get_error_text(), line);
  429. return;
  430. }
  431. Variant v = expression.execute(Array(), nullptr, false);
  432. if (v.get_type() == Variant::NIL) {
  433. set_error(RTR("Condition evaluation error."), line);
  434. return;
  435. }
  436. bool skip = false;
  437. for (int i = 0; i < state->current_branch->conditions.size(); i++) {
  438. if (state->current_branch->conditions[i]) {
  439. skip = true;
  440. break;
  441. }
  442. }
  443. bool success = !skip && v.booleanize();
  444. start_branch_condition(p_tokenizer, success, true);
  445. if (state->save_regions) {
  446. add_region(line + 1, success, state->previous_region->parent);
  447. }
  448. }
  449. void ShaderPreprocessor::process_else(Tokenizer *p_tokenizer) {
  450. const int line = p_tokenizer->get_line();
  451. if (state->current_branch == nullptr || state->current_branch->else_defined) {
  452. set_error(vformat(RTR("Unmatched '%s' directive."), "else"), line);
  453. return;
  454. }
  455. if (state->previous_region != nullptr) {
  456. state->previous_region->to_line = line - 1;
  457. }
  458. if (!p_tokenizer->consume_empty_line()) {
  459. set_error(vformat(RTR("Invalid '%s' directive."), "else"), line);
  460. }
  461. bool skip = false;
  462. for (int i = 0; i < state->current_branch->conditions.size(); i++) {
  463. if (state->current_branch->conditions[i]) {
  464. skip = true;
  465. break;
  466. }
  467. }
  468. state->current_branch->else_defined = true;
  469. if (state->save_regions) {
  470. add_region(line + 1, !skip, state->previous_region->parent);
  471. }
  472. if (skip) {
  473. Vector<String> ends;
  474. ends.push_back("endif");
  475. // Legacy return value.
  476. _ALLOW_DISCARD_ next_directive(p_tokenizer, ends);
  477. }
  478. }
  479. void ShaderPreprocessor::process_endif(Tokenizer *p_tokenizer) {
  480. const int line = p_tokenizer->get_line();
  481. state->condition_depth--;
  482. if (state->condition_depth < 0) {
  483. set_error(vformat(RTR("Unmatched '%s' directive."), "endif"), line);
  484. return;
  485. }
  486. if (state->previous_region != nullptr) {
  487. state->previous_region->to_line = line - 1;
  488. state->previous_region = state->previous_region->parent;
  489. }
  490. if (!p_tokenizer->consume_empty_line()) {
  491. set_error(vformat(RTR("Invalid '%s' directive."), "endif"), line);
  492. }
  493. state->current_branch = state->current_branch->parent;
  494. state->branches.pop_back();
  495. }
  496. void ShaderPreprocessor::process_error(Tokenizer *p_tokenizer) {
  497. const int line = p_tokenizer->get_line();
  498. const String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  499. if (body.is_empty()) {
  500. set_error(" ", line);
  501. } else {
  502. set_error(body, line);
  503. }
  504. if (!p_tokenizer->consume_empty_line()) {
  505. set_error(vformat(RTR("Invalid '%s' directive."), "error"), line);
  506. }
  507. }
  508. void ShaderPreprocessor::process_if(Tokenizer *p_tokenizer) {
  509. const int line = p_tokenizer->get_line();
  510. String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  511. if (body.is_empty()) {
  512. set_error(RTR("Missing condition."), line);
  513. return;
  514. }
  515. Error error = expand_condition(body, line, body);
  516. if (error != OK) {
  517. return;
  518. }
  519. error = expand_macros(body, line, body);
  520. if (error != OK) {
  521. return;
  522. }
  523. Expression expression;
  524. Vector<String> names;
  525. error = expression.parse(body, names);
  526. if (error != OK) {
  527. set_error(expression.get_error_text(), line);
  528. return;
  529. }
  530. Variant v = expression.execute(Array(), nullptr, false);
  531. if (v.get_type() == Variant::NIL) {
  532. set_error(RTR("Condition evaluation error."), line);
  533. return;
  534. }
  535. bool success = v.booleanize();
  536. start_branch_condition(p_tokenizer, success);
  537. if (state->save_regions) {
  538. add_region(line + 1, success, state->previous_region);
  539. }
  540. }
  541. void ShaderPreprocessor::process_ifdef(Tokenizer *p_tokenizer) {
  542. const int line = p_tokenizer->get_line();
  543. String label = p_tokenizer->get_identifier();
  544. if (label.is_empty()) {
  545. set_error(RTR("Invalid macro name."), line);
  546. return;
  547. }
  548. if (!p_tokenizer->consume_empty_line()) {
  549. set_error(vformat(RTR("Invalid '%s' directive."), "ifdef"), line);
  550. return;
  551. }
  552. bool success = state->defines.has(label);
  553. start_branch_condition(p_tokenizer, success);
  554. if (state->save_regions) {
  555. add_region(line + 1, success, state->previous_region);
  556. }
  557. }
  558. void ShaderPreprocessor::process_ifndef(Tokenizer *p_tokenizer) {
  559. const int line = p_tokenizer->get_line();
  560. String label = p_tokenizer->get_identifier();
  561. if (label.is_empty()) {
  562. set_error(RTR("Invalid macro name."), line);
  563. return;
  564. }
  565. if (!p_tokenizer->consume_empty_line()) {
  566. set_error(vformat(RTR("Invalid '%s' directive."), "ifndef"), line);
  567. return;
  568. }
  569. bool success = !state->defines.has(label);
  570. start_branch_condition(p_tokenizer, success);
  571. if (state->save_regions) {
  572. add_region(line + 1, success, state->previous_region);
  573. }
  574. }
  575. void ShaderPreprocessor::process_include(Tokenizer *p_tokenizer) {
  576. const int line = p_tokenizer->get_line();
  577. p_tokenizer->advance('"');
  578. String path = tokens_to_string(p_tokenizer->advance('"'));
  579. for (int i = 0; i < path.length(); i++) {
  580. if (path[i] == '\n') {
  581. break; //stop parsing
  582. }
  583. if (path[i] == CURSOR) {
  584. state->completion_type = COMPLETION_TYPE_INCLUDE_PATH;
  585. break;
  586. }
  587. }
  588. path = path.substr(0, path.length() - 1);
  589. if (path.is_empty() || !p_tokenizer->consume_empty_line()) {
  590. set_error(RTR("Invalid path."), line);
  591. return;
  592. }
  593. path = path.simplify_path();
  594. if (path.is_relative_path()) {
  595. path = state->current_filename.get_base_dir().path_join(path);
  596. }
  597. if (!ResourceLoader::exists(path)) {
  598. set_error(RTR("Shader include file does not exist:") + " " + path, line);
  599. return;
  600. }
  601. Ref<Resource> res = ResourceLoader::load(path);
  602. if (res.is_null()) {
  603. set_error(RTR("Shader include load failed. Does the shader include exist? Is there a cyclic dependency?"), line);
  604. return;
  605. }
  606. Ref<ShaderInclude> shader_inc = res;
  607. if (shader_inc.is_null()) {
  608. set_error(RTR("Shader include resource type is wrong."), line);
  609. return;
  610. }
  611. String included = shader_inc->get_code();
  612. if (!included.is_empty()) {
  613. uint64_t code_hash = included.hash64();
  614. if (state->cyclic_include_hashes.find(code_hash)) {
  615. set_error(RTR("Cyclic include found") + ": " + path, line);
  616. return;
  617. }
  618. }
  619. state->shader_includes.insert(shader_inc);
  620. const String real_path = shader_inc->get_path();
  621. if (state->includes.has(real_path)) {
  622. // Already included, skip.
  623. // This is a valid check because 2 separate include paths could use some
  624. // of the same shared functions from a common shader include.
  625. return;
  626. }
  627. // Mark as included.
  628. state->includes.insert(real_path);
  629. state->include_depth++;
  630. if (state->include_depth > 25) {
  631. set_error(RTR("Shader max include depth exceeded."), line);
  632. return;
  633. }
  634. String old_filename = state->current_filename;
  635. state->current_filename = real_path;
  636. ShaderPreprocessor processor;
  637. int prev_condition_depth = state->condition_depth;
  638. state->condition_depth = 0;
  639. FilePosition fp;
  640. fp.file = state->current_filename;
  641. fp.line = line + 1;
  642. state->include_positions.push_back(fp);
  643. String result;
  644. processor.preprocess(state, included, result);
  645. add_to_output("@@>" + real_path + "\n"); // Add token for enter include path
  646. add_to_output(result);
  647. add_to_output("\n@@<" + real_path + "\n"); // Add token for exit include path.
  648. // Reset to last include if there are no errors. We want to use this as context.
  649. if (state->error.is_empty()) {
  650. state->current_filename = old_filename;
  651. state->include_positions.pop_back();
  652. } else {
  653. return;
  654. }
  655. state->include_depth--;
  656. state->condition_depth = prev_condition_depth;
  657. }
  658. void ShaderPreprocessor::process_pragma(Tokenizer *p_tokenizer) {
  659. const int line = p_tokenizer->get_line();
  660. bool is_cursor;
  661. const String label = p_tokenizer->get_identifier(&is_cursor);
  662. if (is_cursor) {
  663. state->completion_type = COMPLETION_TYPE_PRAGMA;
  664. }
  665. if (label.is_empty()) {
  666. set_error(vformat(RTR("Invalid '%s' directive."), "pragma"), line);
  667. return;
  668. }
  669. // Explicitly handle pragma values here.
  670. // If more pragma options are created, then refactor into a more defined structure.
  671. if (label == "disable_preprocessor") {
  672. state->disabled = true;
  673. } else {
  674. set_error(vformat(RTR("Invalid '%s' directive."), "pragma"), line);
  675. return;
  676. }
  677. if (!p_tokenizer->consume_empty_line()) {
  678. set_error(vformat(RTR("Invalid '%s' directive."), "pragma"), line);
  679. return;
  680. }
  681. }
  682. void ShaderPreprocessor::process_undef(Tokenizer *p_tokenizer) {
  683. const int line = p_tokenizer->get_line();
  684. const String label = p_tokenizer->get_identifier();
  685. if (label.is_empty() || !p_tokenizer->consume_empty_line()) {
  686. set_error(vformat(RTR("Invalid '%s' directive."), "undef"), line);
  687. return;
  688. }
  689. if (state->defines.has(label)) {
  690. if (state->defines[label]->is_builtin) {
  691. set_error(vformat(RTR("Cannot use '%s' on built-in define."), "undef"), line);
  692. return;
  693. }
  694. memdelete(state->defines[label]);
  695. state->defines.erase(label);
  696. }
  697. }
  698. void ShaderPreprocessor::add_region(int p_line, bool p_enabled, Region *p_parent_region) {
  699. Region region;
  700. region.file = state->current_filename;
  701. region.enabled = p_enabled;
  702. region.from_line = p_line;
  703. region.parent = p_parent_region;
  704. state->previous_region = &state->regions[region.file].push_back(region)->get();
  705. }
  706. void ShaderPreprocessor::start_branch_condition(Tokenizer *p_tokenizer, bool p_success, bool p_continue) {
  707. if (!p_continue) {
  708. state->condition_depth++;
  709. state->current_branch = &state->branches.push_back(Branch(p_success, state->current_branch))->get();
  710. } else {
  711. state->current_branch->conditions.push_back(p_success);
  712. }
  713. if (!p_success) {
  714. Vector<String> ends;
  715. ends.push_back("elif");
  716. ends.push_back("else");
  717. ends.push_back("endif");
  718. // Legacy return value.
  719. _ALLOW_DISCARD_ next_directive(p_tokenizer, ends);
  720. }
  721. }
  722. void ShaderPreprocessor::expand_output_macros(int p_start, int p_line_number) {
  723. String line = vector_to_string(output, p_start, output.size());
  724. Error error = expand_macros(line, p_line_number - 1, line); // We are already on next line, so -1.
  725. if (error != OK) {
  726. return;
  727. }
  728. output.resize(p_start);
  729. add_to_output(line);
  730. }
  731. Error ShaderPreprocessor::expand_condition(const String &p_string, int p_line, String &r_expanded) {
  732. // Checks bracket count to be even + check the cursor position.
  733. {
  734. int bracket_start_count = 0;
  735. int bracket_end_count = 0;
  736. for (int i = 0; i < p_string.size(); i++) {
  737. switch (p_string[i]) {
  738. case CURSOR:
  739. state->completion_type = COMPLETION_TYPE_CONDITION;
  740. break;
  741. case '(':
  742. bracket_start_count++;
  743. break;
  744. case ')':
  745. bracket_end_count++;
  746. break;
  747. }
  748. }
  749. if (bracket_start_count > bracket_end_count) {
  750. _set_expected_error(")", p_line);
  751. return FAILED;
  752. }
  753. if (bracket_end_count > bracket_start_count) {
  754. _set_expected_error("(", p_line);
  755. return FAILED;
  756. }
  757. }
  758. String result = p_string;
  759. int index = 0;
  760. int index_start = 0;
  761. int index_end = 0;
  762. while (find_match(result, "defined", index, index_start)) {
  763. bool open_bracket = false;
  764. bool found_word = false;
  765. bool word_completed = false;
  766. LocalVector<char32_t> text;
  767. int post_bracket_index = -1;
  768. int size = result.size();
  769. for (int i = (index_start - 1); i < size; i++) {
  770. char32_t c = result[i];
  771. if (c == 0) {
  772. if (found_word) {
  773. word_completed = true;
  774. }
  775. break;
  776. }
  777. char32_t cs[] = { c, '\0' };
  778. String s = String(cs);
  779. bool is_space = is_char_space(c);
  780. if (word_completed) {
  781. if (c == ')') {
  782. continue;
  783. }
  784. if (c == '|' || c == '&') {
  785. if (open_bracket) {
  786. _set_unexpected_token_error(s, p_line);
  787. return FAILED;
  788. }
  789. break;
  790. } else if (!is_space) {
  791. _set_unexpected_token_error(s, p_line);
  792. return FAILED;
  793. }
  794. } else if (is_space) {
  795. if (found_word && !open_bracket) {
  796. index_end = i;
  797. word_completed = true;
  798. }
  799. } else if (c == '(') {
  800. if (open_bracket) {
  801. _set_unexpected_token_error(s, p_line);
  802. return FAILED;
  803. }
  804. open_bracket = true;
  805. } else if (c == ')') {
  806. if (open_bracket) {
  807. if (!found_word) {
  808. _set_unexpected_token_error(s, p_line);
  809. return FAILED;
  810. }
  811. open_bracket = false;
  812. post_bracket_index = i + 1;
  813. } else {
  814. index_end = i;
  815. }
  816. word_completed = true;
  817. } else if (is_char_word(c)) {
  818. text.push_back(c);
  819. found_word = true;
  820. } else {
  821. _set_unexpected_token_error(s, p_line);
  822. return FAILED;
  823. }
  824. }
  825. if (word_completed) {
  826. if (open_bracket) {
  827. _set_expected_error(")", p_line);
  828. return FAILED;
  829. }
  830. if (post_bracket_index != -1) {
  831. index_end = post_bracket_index;
  832. }
  833. String body = state->defines.has(vector_to_string(text)) ? "true" : "false";
  834. String temp = result;
  835. result = result.substr(0, index) + body;
  836. index_start = result.length();
  837. if (index_end > 0) {
  838. result += temp.substr(index_end);
  839. }
  840. } else {
  841. set_error(RTR("Invalid macro name."), p_line);
  842. return FAILED;
  843. }
  844. }
  845. r_expanded = result;
  846. return OK;
  847. }
  848. Error ShaderPreprocessor::expand_macros(const String &p_string, int p_line, String &r_expanded) {
  849. String iterative = p_string;
  850. int pass_count = 0;
  851. bool expanded = true;
  852. while (expanded) {
  853. expanded = false;
  854. // As long as we find something to expand, keep going.
  855. for (const RBMap<String, Define *>::Element *E = state->defines.front(); E; E = E->next()) {
  856. if (expand_macros_once(iterative, p_line, E, iterative)) {
  857. expanded = true;
  858. }
  859. }
  860. pass_count++;
  861. if (pass_count > 50) {
  862. set_error(RTR("Macro expansion limit exceeded."), p_line);
  863. break;
  864. }
  865. }
  866. r_expanded = iterative;
  867. if (!state->error.is_empty()) {
  868. return FAILED;
  869. }
  870. return OK;
  871. }
  872. bool ShaderPreprocessor::expand_macros_once(const String &p_line, int p_line_number, const RBMap<String, Define *>::Element *p_define_pair, String &r_expanded) {
  873. String result = p_line;
  874. const String &key = p_define_pair->key();
  875. const Define *define = p_define_pair->value();
  876. int index_start = 0;
  877. int index = 0;
  878. if (find_match(result, key, index, index_start)) {
  879. String body = define->body;
  880. if (define->arguments.size() > 0) {
  881. // Complex macro with arguments.
  882. int args_start = -1;
  883. int args_end = -1;
  884. int brackets_open = 0;
  885. Vector<String> args;
  886. for (int i = index_start - 1; i < p_line.length(); i++) {
  887. bool add_argument = false;
  888. bool reached_end = false;
  889. char32_t c = p_line[i];
  890. if (c == '(') {
  891. brackets_open++;
  892. if (brackets_open == 1) {
  893. args_start = i + 1;
  894. args_end = -1;
  895. }
  896. } else if (c == ')') {
  897. brackets_open--;
  898. if (brackets_open == 0) {
  899. args_end = i;
  900. add_argument = true;
  901. reached_end = true;
  902. }
  903. } else if (c == ',') {
  904. if (brackets_open == 1) {
  905. args_end = i;
  906. add_argument = true;
  907. }
  908. }
  909. if (add_argument) {
  910. if (args_start == -1 || args_end == -1) {
  911. set_error(RTR("Invalid macro argument list."), p_line_number);
  912. return false;
  913. }
  914. String arg = p_line.substr(args_start, args_end - args_start).strip_edges();
  915. if (arg.is_empty()) {
  916. set_error(RTR("Invalid macro argument."), p_line_number);
  917. return false;
  918. }
  919. args.append(arg);
  920. args_start = args_end + 1;
  921. }
  922. if (reached_end) {
  923. break;
  924. }
  925. }
  926. if (args.size() != define->arguments.size()) {
  927. set_error(RTR("Invalid macro argument count."), p_line_number);
  928. return false;
  929. }
  930. // Insert macro arguments into the body.
  931. for (int i = 0; i < args.size(); i++) {
  932. String arg_name = define->arguments[i];
  933. int arg_index_start = 0;
  934. int arg_index = 0;
  935. while (find_match(body, arg_name, arg_index, arg_index_start)) {
  936. body = body.substr(0, arg_index) + args[i] + body.substr(arg_index + arg_name.length());
  937. // Manually reset arg_index_start to where the arg value of the define finishes.
  938. // This ensures we don't skip the other args of this macro in the string.
  939. arg_index_start = arg_index + args[i].length() + 1;
  940. }
  941. }
  942. concatenate_macro_body(body);
  943. result = result.substr(0, index) + " " + body + " " + result.substr(args_end + 1);
  944. } else {
  945. concatenate_macro_body(body);
  946. result = result.substr(0, index) + " " + body + " " + result.substr(index + key.length());
  947. }
  948. r_expanded = result;
  949. return true;
  950. }
  951. return false;
  952. }
  953. bool ShaderPreprocessor::find_match(const String &p_string, const String &p_value, int &r_index, int &r_index_start) {
  954. // Looks for value in string and then determines if the boundaries
  955. // are non-word characters. This method semi-emulates \b in regex.
  956. r_index = p_string.find(p_value, r_index_start);
  957. while (r_index > -1) {
  958. if (r_index > 0) {
  959. if (is_char_word(p_string[r_index - 1])) {
  960. r_index_start = r_index + 1;
  961. r_index = p_string.find(p_value, r_index_start);
  962. continue;
  963. }
  964. }
  965. if (r_index + p_value.length() < p_string.length()) {
  966. if (is_char_word(p_string[r_index + p_value.length()])) {
  967. r_index_start = r_index + p_value.length() + 1;
  968. r_index = p_string.find(p_value, r_index_start);
  969. continue;
  970. }
  971. }
  972. // Return and shift index start automatically for next call.
  973. r_index_start = r_index + p_value.length() + 1;
  974. return true;
  975. }
  976. return false;
  977. }
  978. void ShaderPreprocessor::concatenate_macro_body(String &r_body) {
  979. int index_start = r_body.find("##");
  980. while (index_start > -1) {
  981. int index_end = index_start + 2; // First character after ##.
  982. // The macro was checked during creation so this should never happen.
  983. ERR_FAIL_INDEX(index_end, r_body.size());
  984. // If there more than two # in a row, then it's not a concatenation.
  985. bool is_concat = true;
  986. while (index_end <= r_body.length() && r_body[index_end] == '#') {
  987. index_end++;
  988. is_concat = false;
  989. }
  990. if (!is_concat) {
  991. index_start = r_body.find("##", index_end);
  992. continue;
  993. }
  994. // Skip whitespace after ##.
  995. while (index_end < r_body.length() && is_char_space(r_body[index_end])) {
  996. index_end++;
  997. }
  998. // Skip whitespace before ##.
  999. while (index_start >= 1 && is_char_space(r_body[index_start - 1])) {
  1000. index_start--;
  1001. }
  1002. r_body = r_body.substr(0, index_start) + r_body.substr(index_end);
  1003. index_start = r_body.find("##", index_start);
  1004. }
  1005. }
  1006. String ShaderPreprocessor::next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives) {
  1007. const int line = p_tokenizer->get_line();
  1008. int nesting = 0;
  1009. while (true) {
  1010. p_tokenizer->advance('#');
  1011. String id = p_tokenizer->peek_identifier();
  1012. if (id.is_empty()) {
  1013. break;
  1014. }
  1015. if (nesting == 0) {
  1016. for (int i = 0; i < p_directives.size(); i++) {
  1017. if (p_directives[i] == id) {
  1018. p_tokenizer->backtrack('#');
  1019. return id;
  1020. }
  1021. }
  1022. }
  1023. if (id == "ifdef" || id == "ifndef" || id == "if") {
  1024. nesting++;
  1025. } else if (id == "endif") {
  1026. nesting--;
  1027. }
  1028. }
  1029. set_error(RTR("Can't find matching branch directive."), line);
  1030. return "";
  1031. }
  1032. void ShaderPreprocessor::add_to_output(const String &p_str) {
  1033. for (int i = 0; i < p_str.length(); i++) {
  1034. output.push_back(p_str[i]);
  1035. }
  1036. }
  1037. void ShaderPreprocessor::set_error(const String &p_error, int p_line) {
  1038. if (state->error.is_empty()) {
  1039. state->error = p_error;
  1040. FilePosition fp;
  1041. fp.file = state->current_filename;
  1042. fp.line = p_line + 1;
  1043. state->include_positions.push_back(fp);
  1044. }
  1045. }
  1046. ShaderPreprocessor::Define *ShaderPreprocessor::create_define(const String &p_body) {
  1047. ShaderPreprocessor::Define *define = memnew(Define);
  1048. define->body = p_body;
  1049. return define;
  1050. }
  1051. void ShaderPreprocessor::insert_builtin_define(String p_name, String p_value, State &p_state) {
  1052. Define *define = memnew(Define);
  1053. define->is_builtin = true;
  1054. define->body = p_value;
  1055. p_state.defines[p_name] = define;
  1056. }
  1057. void ShaderPreprocessor::clear_state() {
  1058. if (state != nullptr) {
  1059. for (const RBMap<String, Define *>::Element *E = state->defines.front(); E; E = E->next()) {
  1060. memdelete(E->get());
  1061. }
  1062. state->defines.clear();
  1063. }
  1064. state = nullptr;
  1065. }
  1066. Error ShaderPreprocessor::preprocess(State *p_state, const String &p_code, String &r_result) {
  1067. output.clear();
  1068. state = p_state;
  1069. CommentRemover remover(p_code);
  1070. String stripped = remover.strip();
  1071. String error = remover.get_error();
  1072. if (!error.is_empty()) {
  1073. set_error(error, remover.get_error_line());
  1074. return FAILED;
  1075. }
  1076. // Track code hashes to prevent cyclic include.
  1077. uint64_t code_hash = p_code.hash64();
  1078. state->cyclic_include_hashes.push_back(code_hash);
  1079. Tokenizer p_tokenizer(stripped);
  1080. int last_size = 0;
  1081. bool has_symbols_before_directive = false;
  1082. while (true) {
  1083. const Token &t = p_tokenizer.get_token();
  1084. if (t.text == 0) {
  1085. break;
  1086. }
  1087. // Add autogenerated tokens if there are any.
  1088. p_tokenizer.get_and_clear_generated(&output);
  1089. if (state->disabled) {
  1090. // Preprocessor was disabled.
  1091. // Read the rest of the file into the output.
  1092. output.push_back(t.text);
  1093. continue;
  1094. }
  1095. if (t.text == '#') {
  1096. if (has_symbols_before_directive) {
  1097. set_error(RTR("Invalid symbols placed before directive."), p_tokenizer.get_line());
  1098. state->cyclic_include_hashes.erase(code_hash); // Remove this hash.
  1099. return FAILED;
  1100. }
  1101. process_directive(&p_tokenizer);
  1102. } else {
  1103. if (is_char_end(t.text)) {
  1104. expand_output_macros(last_size, p_tokenizer.get_line());
  1105. last_size = output.size();
  1106. has_symbols_before_directive = false;
  1107. } else if (!is_char_space(t.text)) {
  1108. has_symbols_before_directive = true;
  1109. }
  1110. output.push_back(t.text);
  1111. }
  1112. if (!state->error.is_empty()) {
  1113. state->cyclic_include_hashes.erase(code_hash); // Remove this hash.
  1114. return FAILED;
  1115. }
  1116. }
  1117. state->cyclic_include_hashes.erase(code_hash); // Remove this hash.
  1118. if (!state->disabled) {
  1119. if (state->condition_depth != 0) {
  1120. set_error(RTR("Unmatched conditional statement."), p_tokenizer.line);
  1121. return FAILED;
  1122. }
  1123. expand_output_macros(last_size, p_tokenizer.get_line());
  1124. }
  1125. r_result = vector_to_string(output);
  1126. return OK;
  1127. }
  1128. Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filename, String &r_result, String *r_error_text, List<FilePosition> *r_error_position, List<Region> *r_regions, HashSet<Ref<ShaderInclude>> *r_includes, List<ScriptLanguage::CodeCompletionOption> *r_completion_options, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines, IncludeCompletionFunction p_include_completion_func) {
  1129. State pp_state;
  1130. if (!p_filename.is_empty()) {
  1131. pp_state.current_filename = p_filename;
  1132. pp_state.save_regions = r_regions != nullptr;
  1133. }
  1134. // Built-in defines.
  1135. {
  1136. const String rendering_method = OS::get_singleton()->get_current_rendering_method();
  1137. if (rendering_method == "forward_plus") {
  1138. insert_builtin_define("CURRENT_RENDERER", _MKSTR(2), pp_state);
  1139. } else if (rendering_method == "mobile") {
  1140. insert_builtin_define("CURRENT_RENDERER", _MKSTR(1), pp_state);
  1141. } else { // gl_compatibility
  1142. insert_builtin_define("CURRENT_RENDERER", _MKSTR(0), pp_state);
  1143. }
  1144. insert_builtin_define("RENDERER_COMPATIBILITY", _MKSTR(0), pp_state);
  1145. insert_builtin_define("RENDERER_MOBILE", _MKSTR(1), pp_state);
  1146. insert_builtin_define("RENDERER_FORWARD_PLUS", _MKSTR(2), pp_state);
  1147. }
  1148. Error err = preprocess(&pp_state, p_code, r_result);
  1149. if (err != OK) {
  1150. if (r_error_text) {
  1151. *r_error_text = pp_state.error;
  1152. }
  1153. if (r_error_position) {
  1154. *r_error_position = pp_state.include_positions;
  1155. }
  1156. }
  1157. if (r_regions) {
  1158. *r_regions = pp_state.regions[p_filename];
  1159. }
  1160. if (r_includes) {
  1161. *r_includes = pp_state.shader_includes;
  1162. }
  1163. if (r_completion_defines) {
  1164. for (const KeyValue<String, Define *> &E : state->defines) {
  1165. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1166. r_completion_defines->push_back(option);
  1167. }
  1168. }
  1169. if (r_completion_options) {
  1170. switch (pp_state.completion_type) {
  1171. case COMPLETION_TYPE_DIRECTIVE: {
  1172. List<String> options;
  1173. get_keyword_list(&options, true, true);
  1174. for (const String &E : options) {
  1175. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1176. r_completion_options->push_back(option);
  1177. }
  1178. } break;
  1179. case COMPLETION_TYPE_PRAGMA: {
  1180. List<String> options;
  1181. ShaderPreprocessor::get_pragma_list(&options);
  1182. for (const String &E : options) {
  1183. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1184. r_completion_options->push_back(option);
  1185. }
  1186. } break;
  1187. case COMPLETION_TYPE_CONDITION: {
  1188. ScriptLanguage::CodeCompletionOption option("defined", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1189. r_completion_options->push_back(option);
  1190. } break;
  1191. case COMPLETION_TYPE_INCLUDE_PATH: {
  1192. if (p_include_completion_func && r_completion_options) {
  1193. p_include_completion_func(r_completion_options);
  1194. }
  1195. } break;
  1196. default: {
  1197. }
  1198. }
  1199. }
  1200. clear_state();
  1201. return err;
  1202. }
  1203. void ShaderPreprocessor::get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords) {
  1204. r_keywords->push_back("define");
  1205. if (!p_ignore_context_keywords) {
  1206. r_keywords->push_back("defined");
  1207. }
  1208. r_keywords->push_back("elif");
  1209. if (p_include_shader_keywords) {
  1210. r_keywords->push_back("else");
  1211. }
  1212. r_keywords->push_back("endif");
  1213. r_keywords->push_back("error");
  1214. if (p_include_shader_keywords) {
  1215. r_keywords->push_back("if");
  1216. }
  1217. r_keywords->push_back("ifdef");
  1218. r_keywords->push_back("ifndef");
  1219. r_keywords->push_back("include");
  1220. r_keywords->push_back("pragma");
  1221. r_keywords->push_back("undef");
  1222. }
  1223. void ShaderPreprocessor::get_pragma_list(List<String> *r_pragmas) {
  1224. r_pragmas->push_back("disable_preprocessor");
  1225. }
  1226. ShaderPreprocessor::ShaderPreprocessor() {
  1227. }
  1228. ShaderPreprocessor::~ShaderPreprocessor() {
  1229. }