1 /*************************************************************************/
2 /*  shader_language.h                                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 
31 #ifndef SHADER_LANGUAGE_H
32 #define SHADER_LANGUAGE_H
33 
34 #include "core/list.h"
35 #include "core/map.h"
36 #include "core/script_language.h"
37 #include "core/string_name.h"
38 #include "core/typedefs.h"
39 #include "core/ustring.h"
40 #include "core/variant.h"
41 
42 class ShaderLanguage {
43 
44 public:
45 	enum TokenType {
46 		TK_EMPTY,
47 		TK_IDENTIFIER,
48 		TK_TRUE,
49 		TK_FALSE,
50 		TK_REAL_CONSTANT,
51 		TK_INT_CONSTANT,
52 		TK_TYPE_VOID,
53 		TK_TYPE_BOOL,
54 		TK_TYPE_BVEC2,
55 		TK_TYPE_BVEC3,
56 		TK_TYPE_BVEC4,
57 		TK_TYPE_INT,
58 		TK_TYPE_IVEC2,
59 		TK_TYPE_IVEC3,
60 		TK_TYPE_IVEC4,
61 		TK_TYPE_UINT,
62 		TK_TYPE_UVEC2,
63 		TK_TYPE_UVEC3,
64 		TK_TYPE_UVEC4,
65 		TK_TYPE_FLOAT,
66 		TK_TYPE_VEC2,
67 		TK_TYPE_VEC3,
68 		TK_TYPE_VEC4,
69 		TK_TYPE_MAT2,
70 		TK_TYPE_MAT3,
71 		TK_TYPE_MAT4,
72 		TK_TYPE_SAMPLER2D,
73 		TK_TYPE_ISAMPLER2D,
74 		TK_TYPE_USAMPLER2D,
75 		TK_TYPE_SAMPLER2DARRAY,
76 		TK_TYPE_ISAMPLER2DARRAY,
77 		TK_TYPE_USAMPLER2DARRAY,
78 		TK_TYPE_SAMPLER3D,
79 		TK_TYPE_ISAMPLER3D,
80 		TK_TYPE_USAMPLER3D,
81 		TK_TYPE_SAMPLERCUBE,
82 		TK_TYPE_SAMPLEREXT,
83 		TK_INTERPOLATION_FLAT,
84 		TK_INTERPOLATION_SMOOTH,
85 		TK_CONST,
86 		TK_PRECISION_LOW,
87 		TK_PRECISION_MID,
88 		TK_PRECISION_HIGH,
89 		TK_OP_EQUAL,
90 		TK_OP_NOT_EQUAL,
91 		TK_OP_LESS,
92 		TK_OP_LESS_EQUAL,
93 		TK_OP_GREATER,
94 		TK_OP_GREATER_EQUAL,
95 		TK_OP_AND,
96 		TK_OP_OR,
97 		TK_OP_NOT,
98 		TK_OP_ADD,
99 		TK_OP_SUB,
100 		TK_OP_MUL,
101 		TK_OP_DIV,
102 		TK_OP_MOD,
103 		TK_OP_SHIFT_LEFT,
104 		TK_OP_SHIFT_RIGHT,
105 		TK_OP_ASSIGN,
106 		TK_OP_ASSIGN_ADD,
107 		TK_OP_ASSIGN_SUB,
108 		TK_OP_ASSIGN_MUL,
109 		TK_OP_ASSIGN_DIV,
110 		TK_OP_ASSIGN_MOD,
111 		TK_OP_ASSIGN_SHIFT_LEFT,
112 		TK_OP_ASSIGN_SHIFT_RIGHT,
113 		TK_OP_ASSIGN_BIT_AND,
114 		TK_OP_ASSIGN_BIT_OR,
115 		TK_OP_ASSIGN_BIT_XOR,
116 		TK_OP_BIT_AND,
117 		TK_OP_BIT_OR,
118 		TK_OP_BIT_XOR,
119 		TK_OP_BIT_INVERT,
120 		TK_OP_INCREMENT,
121 		TK_OP_DECREMENT,
122 		TK_CF_IF,
123 		TK_CF_ELSE,
124 		TK_CF_FOR,
125 		TK_CF_WHILE,
126 		TK_CF_DO,
127 		TK_CF_SWITCH,
128 		TK_CF_CASE,
129 		TK_CF_DEFAULT,
130 		TK_CF_BREAK,
131 		TK_CF_CONTINUE,
132 		TK_CF_RETURN,
133 		TK_CF_DISCARD,
134 		TK_BRACKET_OPEN,
135 		TK_BRACKET_CLOSE,
136 		TK_CURLY_BRACKET_OPEN,
137 		TK_CURLY_BRACKET_CLOSE,
138 		TK_PARENTHESIS_OPEN,
139 		TK_PARENTHESIS_CLOSE,
140 		TK_QUESTION,
141 		TK_COMMA,
142 		TK_COLON,
143 		TK_SEMICOLON,
144 		TK_PERIOD,
145 		TK_UNIFORM,
146 		TK_VARYING,
147 		TK_ARG_IN,
148 		TK_ARG_OUT,
149 		TK_ARG_INOUT,
150 		TK_RENDER_MODE,
151 		TK_HINT_WHITE_TEXTURE,
152 		TK_HINT_BLACK_TEXTURE,
153 		TK_HINT_NORMAL_TEXTURE,
154 		TK_HINT_ANISO_TEXTURE,
155 		TK_HINT_ALBEDO_TEXTURE,
156 		TK_HINT_BLACK_ALBEDO_TEXTURE,
157 		TK_HINT_COLOR,
158 		TK_HINT_RANGE,
159 		TK_SHADER_TYPE,
160 		TK_CURSOR,
161 		TK_ERROR,
162 		TK_EOF,
163 		TK_MAX
164 	};
165 
166 /* COMPILER */
167 
168 // lame work around to Apple defining this as a macro in 10.12 SDK
169 #ifdef TYPE_BOOL
170 #undef TYPE_BOOL
171 #endif
172 
173 	enum DataType {
174 		TYPE_VOID,
175 		TYPE_BOOL,
176 		TYPE_BVEC2,
177 		TYPE_BVEC3,
178 		TYPE_BVEC4,
179 		TYPE_INT,
180 		TYPE_IVEC2,
181 		TYPE_IVEC3,
182 		TYPE_IVEC4,
183 		TYPE_UINT,
184 		TYPE_UVEC2,
185 		TYPE_UVEC3,
186 		TYPE_UVEC4,
187 		TYPE_FLOAT,
188 		TYPE_VEC2,
189 		TYPE_VEC3,
190 		TYPE_VEC4,
191 		TYPE_MAT2,
192 		TYPE_MAT3,
193 		TYPE_MAT4,
194 		TYPE_SAMPLER2D,
195 		TYPE_ISAMPLER2D,
196 		TYPE_USAMPLER2D,
197 		TYPE_SAMPLER2DARRAY,
198 		TYPE_ISAMPLER2DARRAY,
199 		TYPE_USAMPLER2DARRAY,
200 		TYPE_SAMPLER3D,
201 		TYPE_ISAMPLER3D,
202 		TYPE_USAMPLER3D,
203 		TYPE_SAMPLERCUBE,
204 		TYPE_SAMPLEREXT,
205 	};
206 
207 	enum DataPrecision {
208 		PRECISION_LOWP,
209 		PRECISION_MEDIUMP,
210 		PRECISION_HIGHP,
211 		PRECISION_DEFAULT,
212 	};
213 
214 	enum DataInterpolation {
215 		INTERPOLATION_FLAT,
216 		INTERPOLATION_SMOOTH,
217 	};
218 
219 	enum Operator {
220 		OP_EQUAL,
221 		OP_NOT_EQUAL,
222 		OP_LESS,
223 		OP_LESS_EQUAL,
224 		OP_GREATER,
225 		OP_GREATER_EQUAL,
226 		OP_AND,
227 		OP_OR,
228 		OP_NOT,
229 		OP_NEGATE,
230 		OP_ADD,
231 		OP_SUB,
232 		OP_MUL,
233 		OP_DIV,
234 		OP_MOD,
235 		OP_SHIFT_LEFT,
236 		OP_SHIFT_RIGHT,
237 		OP_ASSIGN,
238 		OP_ASSIGN_ADD,
239 		OP_ASSIGN_SUB,
240 		OP_ASSIGN_MUL,
241 		OP_ASSIGN_DIV,
242 		OP_ASSIGN_MOD,
243 		OP_ASSIGN_SHIFT_LEFT,
244 		OP_ASSIGN_SHIFT_RIGHT,
245 		OP_ASSIGN_BIT_AND,
246 		OP_ASSIGN_BIT_OR,
247 		OP_ASSIGN_BIT_XOR,
248 		OP_BIT_AND,
249 		OP_BIT_OR,
250 		OP_BIT_XOR,
251 		OP_BIT_INVERT,
252 		OP_INCREMENT,
253 		OP_DECREMENT,
254 		OP_SELECT_IF,
255 		OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
256 		OP_POST_INCREMENT,
257 		OP_POST_DECREMENT,
258 		OP_CALL,
259 		OP_CONSTRUCT,
260 		OP_INDEX,
261 		OP_MAX
262 	};
263 
264 	enum FlowOperation {
265 		FLOW_OP_IF,
266 		FLOW_OP_RETURN,
267 		FLOW_OP_FOR,
268 		FLOW_OP_WHILE,
269 		FLOW_OP_DO,
270 		FLOW_OP_BREAK,
271 		FLOW_OP_SWITCH,
272 		FLOW_OP_CASE,
273 		FLOW_OP_DEFAULT,
274 		FLOW_OP_CONTINUE,
275 		FLOW_OP_DISCARD
276 	};
277 
278 	enum ArgumentQualifier {
279 		ARGUMENT_QUALIFIER_IN,
280 		ARGUMENT_QUALIFIER_OUT,
281 		ARGUMENT_QUALIFIER_INOUT,
282 	};
283 
284 	enum SubClassTag {
285 		TAG_GLOBAL,
286 		TAG_ARRAY,
287 	};
288 
289 	struct Node {
290 		Node *next;
291 
292 		enum Type {
293 			TYPE_SHADER,
294 			TYPE_FUNCTION,
295 			TYPE_BLOCK,
296 			TYPE_VARIABLE,
297 			TYPE_VARIABLE_DECLARATION,
298 			TYPE_CONSTANT,
299 			TYPE_OPERATOR,
300 			TYPE_CONTROL_FLOW,
301 			TYPE_MEMBER,
302 			TYPE_ARRAY,
303 			TYPE_ARRAY_DECLARATION,
304 		};
305 
306 		Type type;
307 
get_datatypeNode308 		virtual DataType get_datatype() const { return TYPE_VOID; }
NodeNode309 		Node(Type t) :
310 				next(NULL),
311 				type(t) {}
~NodeNode312 		virtual ~Node() {}
313 	};
314 
315 	template <class T>
alloc_node()316 	T *alloc_node() {
317 		T *node = memnew(T);
318 		node->next = nodes;
319 		nodes = node;
320 		return node;
321 	}
322 
323 	Node *nodes;
324 
325 	struct OperatorNode : public Node {
326 		DataType return_cache;
327 		DataPrecision return_precision_cache;
328 		Operator op;
329 		Vector<Node *> arguments;
get_datatypeOperatorNode330 		virtual DataType get_datatype() const { return return_cache; }
331 
OperatorNodeOperatorNode332 		OperatorNode() :
333 				Node(TYPE_OPERATOR),
334 				return_cache(TYPE_VOID),
335 				return_precision_cache(PRECISION_DEFAULT),
336 				op(OP_EQUAL) {}
337 	};
338 
339 	struct VariableNode : public Node {
340 		DataType datatype_cache;
341 		StringName name;
get_datatypeVariableNode342 		virtual DataType get_datatype() const { return datatype_cache; }
343 		bool is_const;
344 
VariableNodeVariableNode345 		VariableNode() :
346 				Node(TYPE_VARIABLE),
347 				datatype_cache(TYPE_VOID),
348 				is_const(false) {}
349 	};
350 
351 	struct VariableDeclarationNode : public Node {
352 		DataPrecision precision;
353 		DataType datatype;
354 		bool is_const;
355 
356 		struct Declaration {
357 			StringName name;
358 			Node *initializer;
359 		};
360 
361 		Vector<Declaration> declarations;
get_datatypeVariableDeclarationNode362 		virtual DataType get_datatype() const { return datatype; }
363 
VariableDeclarationNodeVariableDeclarationNode364 		VariableDeclarationNode() :
365 				Node(TYPE_VARIABLE_DECLARATION),
366 				precision(PRECISION_DEFAULT),
367 				datatype(TYPE_VOID),
368 				is_const(false) {}
369 	};
370 
371 	struct ArrayNode : public Node {
372 		DataType datatype_cache;
373 		StringName name;
374 		Node *index_expression;
375 		Node *call_expression;
376 		bool is_const;
377 
get_datatypeArrayNode378 		virtual DataType get_datatype() const { return datatype_cache; }
379 
ArrayNodeArrayNode380 		ArrayNode() :
381 				Node(TYPE_ARRAY),
382 				datatype_cache(TYPE_VOID),
383 				index_expression(NULL),
384 				call_expression(NULL),
385 				is_const(false) {}
386 	};
387 
388 	struct ArrayDeclarationNode : public Node {
389 		DataPrecision precision;
390 		DataType datatype;
391 		bool is_const;
392 
393 		struct Declaration {
394 			StringName name;
395 			uint32_t size;
396 			Vector<Node *> initializer;
397 		};
398 
399 		Vector<Declaration> declarations;
get_datatypeArrayDeclarationNode400 		virtual DataType get_datatype() const { return datatype; }
401 
ArrayDeclarationNodeArrayDeclarationNode402 		ArrayDeclarationNode() :
403 				Node(TYPE_ARRAY_DECLARATION),
404 				precision(PRECISION_DEFAULT),
405 				datatype(TYPE_VOID),
406 				is_const(false) {}
407 	};
408 
409 	struct ConstantNode : public Node {
410 		DataType datatype;
411 
412 		union Value {
413 			bool boolean;
414 			float real;
415 			int32_t sint;
416 			uint32_t uint;
417 		};
418 
419 		Vector<Value> values;
get_datatypeConstantNode420 		virtual DataType get_datatype() const { return datatype; }
421 
ConstantNodeConstantNode422 		ConstantNode() :
423 				Node(TYPE_CONSTANT),
424 				datatype(TYPE_VOID) {}
425 	};
426 
427 	struct FunctionNode;
428 
429 	struct BlockNode : public Node {
430 		FunctionNode *parent_function;
431 		BlockNode *parent_block;
432 
433 		enum BlockType {
434 			BLOCK_TYPE_STANDART,
435 			BLOCK_TYPE_SWITCH,
436 			BLOCK_TYPE_CASE,
437 			BLOCK_TYPE_DEFAULT,
438 		};
439 
440 		int block_type;
441 		SubClassTag block_tag;
442 
443 		struct Variable {
444 			DataType type;
445 			DataPrecision precision;
446 			int line; //for completion
447 			int array_size;
448 			bool is_const;
449 		};
450 
451 		Map<StringName, Variable> variables;
452 		List<Node *> statements;
453 		bool single_statement;
454 
BlockNodeBlockNode455 		BlockNode() :
456 				Node(TYPE_BLOCK),
457 				parent_function(NULL),
458 				parent_block(NULL),
459 				block_type(BLOCK_TYPE_STANDART),
460 				block_tag(SubClassTag::TAG_GLOBAL),
461 				single_statement(false) {}
462 	};
463 
464 	struct ControlFlowNode : public Node {
465 		FlowOperation flow_op;
466 		Vector<Node *> expressions;
467 		Vector<BlockNode *> blocks;
468 
ControlFlowNodeControlFlowNode469 		ControlFlowNode() :
470 				Node(TYPE_CONTROL_FLOW),
471 				flow_op(FLOW_OP_IF) {}
472 	};
473 
474 	struct MemberNode : public Node {
475 		DataType basetype;
476 		DataType datatype;
477 		StringName name;
478 		Node *owner;
479 
get_datatypeMemberNode480 		virtual DataType get_datatype() const { return datatype; }
481 
MemberNodeMemberNode482 		MemberNode() :
483 				Node(TYPE_MEMBER),
484 				basetype(TYPE_VOID),
485 				datatype(TYPE_VOID),
486 				owner(NULL) {}
487 	};
488 
489 	struct FunctionNode : public Node {
490 		struct Argument {
491 			ArgumentQualifier qualifier;
492 			StringName name;
493 			DataType type;
494 			DataPrecision precision;
495 		};
496 
497 		StringName name;
498 		DataType return_type;
499 		DataPrecision return_precision;
500 		Vector<Argument> arguments;
501 		BlockNode *body;
502 		bool can_discard;
503 
FunctionNodeFunctionNode504 		FunctionNode() :
505 				Node(TYPE_FUNCTION),
506 				return_type(TYPE_VOID),
507 				return_precision(PRECISION_DEFAULT),
508 				body(NULL),
509 				can_discard(false) {}
510 	};
511 
512 	struct ShaderNode : public Node {
513 
514 		struct Constant {
515 			StringName name;
516 			DataType type;
517 			DataPrecision precision;
518 			ConstantNode *initializer;
519 		};
520 
521 		struct Function {
522 			StringName name;
523 			FunctionNode *function;
524 			Set<StringName> uses_function;
525 			bool callable;
526 		};
527 
528 		struct Varying {
529 			DataType type;
530 			DataInterpolation interpolation;
531 			DataPrecision precision;
532 			int array_size;
533 
VaryingShaderNode::Varying534 			Varying() :
535 					type(TYPE_VOID),
536 					interpolation(INTERPOLATION_FLAT),
537 					precision(PRECISION_DEFAULT),
538 					array_size(0) {}
539 		};
540 
541 		struct Uniform {
542 			enum Hint {
543 				HINT_NONE,
544 				HINT_COLOR,
545 				HINT_RANGE,
546 				HINT_ALBEDO,
547 				HINT_BLACK_ALBEDO,
548 				HINT_NORMAL,
549 				HINT_BLACK,
550 				HINT_WHITE,
551 				HINT_ANISO,
552 				HINT_MAX
553 			};
554 
555 			int order;
556 			int texture_order;
557 			DataType type;
558 			DataPrecision precision;
559 			Vector<ConstantNode::Value> default_value;
560 			Hint hint;
561 			float hint_range[3];
562 
UniformShaderNode::Uniform563 			Uniform() :
564 					order(0),
565 					texture_order(0),
566 					type(TYPE_VOID),
567 					precision(PRECISION_DEFAULT),
568 					hint(HINT_NONE) {
569 				hint_range[0] = 0.0f;
570 				hint_range[1] = 1.0f;
571 				hint_range[2] = 0.001f;
572 			}
573 		};
574 
575 		Map<StringName, Constant> constants;
576 		Map<StringName, Varying> varyings;
577 		Map<StringName, Uniform> uniforms;
578 		Vector<StringName> render_modes;
579 
580 		Vector<Function> functions;
581 		Vector<Constant> vconstants;
582 
ShaderNodeShaderNode583 		ShaderNode() :
584 				Node(TYPE_SHADER) {}
585 	};
586 
587 	struct Expression {
588 		bool is_op;
589 		union {
590 			Operator op;
591 			Node *node;
592 		};
593 	};
594 
595 	struct VarInfo {
596 		StringName name;
597 		DataType type;
598 	};
599 
600 	enum CompletionType {
601 		COMPLETION_NONE,
602 		COMPLETION_RENDER_MODE,
603 		COMPLETION_MAIN_FUNCTION,
604 		COMPLETION_IDENTIFIER,
605 		COMPLETION_FUNCTION_CALL,
606 		COMPLETION_CALL_ARGUMENTS,
607 		COMPLETION_INDEX,
608 	};
609 
610 	struct Token {
611 		TokenType type;
612 		StringName text;
613 		double constant;
614 		uint16_t line;
615 	};
616 
617 	static String get_operator_text(Operator p_op);
618 	static String get_token_text(Token p_token);
619 
620 	static bool is_token_datatype(TokenType p_type);
621 	static bool is_token_variable_datatype(TokenType p_type);
622 	static DataType get_token_datatype(TokenType p_type);
623 	static bool is_token_interpolation(TokenType p_type);
624 	static DataInterpolation get_token_interpolation(TokenType p_type);
625 	static bool is_token_precision(TokenType p_type);
626 	static DataPrecision get_token_precision(TokenType p_type);
627 	static String get_precision_name(DataPrecision p_type);
628 	static String get_datatype_name(DataType p_type);
629 	static bool is_token_nonvoid_datatype(TokenType p_type);
630 	static bool is_token_operator(TokenType p_type);
631 
632 	static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = NULL);
633 	static DataType get_scalar_type(DataType p_type);
634 	static int get_cardinality(DataType p_type);
635 	static bool is_scalar_type(DataType p_type);
636 	static bool is_sampler_type(DataType p_type);
637 	static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, ShaderLanguage::ShaderNode::Uniform::Hint p_hint = ShaderLanguage::ShaderNode::Uniform::HINT_NONE);
638 
639 	static void get_keyword_list(List<String> *r_keywords);
640 	static void get_builtin_funcs(List<String> *r_keywords);
641 
642 	struct BuiltInInfo {
643 		DataType type;
644 		bool constant;
645 
BuiltInInfoBuiltInInfo646 		BuiltInInfo() :
647 				type(TYPE_VOID),
648 				constant(false) {}
649 
650 		BuiltInInfo(DataType p_type, bool p_constant = false) :
typeBuiltInInfo651 				type(p_type),
652 				constant(p_constant) {}
653 	};
654 
655 	struct FunctionInfo {
656 		Map<StringName, BuiltInInfo> built_ins;
657 		bool can_discard;
658 	};
659 	static bool has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
660 
661 private:
662 	struct KeyWord {
663 		TokenType token;
664 		const char *text;
665 	};
666 
667 	static const KeyWord keyword_list[];
668 
669 	bool error_set;
670 	String error_str;
671 	int error_line;
672 
673 	String code;
674 	int char_idx;
675 	int tk_line;
676 
677 	StringName current_function;
678 
679 	struct TkPos {
680 		int char_idx;
681 		int tk_line;
682 	};
683 
_get_tkpos()684 	TkPos _get_tkpos() {
685 		TkPos tkp;
686 		tkp.char_idx = char_idx;
687 		tkp.tk_line = tk_line;
688 		return tkp;
689 	}
690 
_set_tkpos(TkPos p_pos)691 	void _set_tkpos(TkPos p_pos) {
692 		char_idx = p_pos.char_idx;
693 		tk_line = p_pos.tk_line;
694 	}
695 
_set_error(const String & p_str)696 	void _set_error(const String &p_str) {
697 		if (error_set)
698 			return;
699 
700 		error_line = tk_line;
701 		error_set = true;
702 		error_str = p_str;
703 	}
704 
705 	static const char *token_names[TK_MAX];
706 
707 	Token _make_token(TokenType p_type, const StringName &p_text = StringName());
708 	Token _get_token();
709 
710 	ShaderNode *shader;
711 
712 	enum IdentifierType {
713 		IDENTIFIER_FUNCTION,
714 		IDENTIFIER_UNIFORM,
715 		IDENTIFIER_VARYING,
716 		IDENTIFIER_FUNCTION_ARGUMENT,
717 		IDENTIFIER_LOCAL_VAR,
718 		IDENTIFIER_BUILTIN_VAR,
719 		IDENTIFIER_CONSTANT,
720 	};
721 
722 	bool _find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type = NULL, IdentifierType *r_type = NULL, bool *r_is_const = NULL, int *r_array_size = NULL);
723 	bool _is_operator_assign(Operator p_op) const;
724 	bool _validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message = NULL);
725 	bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = NULL);
726 
727 	struct BuiltinFuncDef {
728 		enum { MAX_ARGS = 5 };
729 		const char *name;
730 		DataType rettype;
731 		const DataType args[MAX_ARGS];
732 		SubClassTag tag;
733 		bool high_end;
734 	};
735 
736 	struct BuiltinFuncOutArgs { //arguments used as out in built in functions
737 		const char *name;
738 		int argument;
739 	};
740 
741 	CompletionType completion_type;
742 	int completion_line;
743 	BlockNode *completion_block;
744 	DataType completion_base;
745 	SubClassTag completion_class;
746 	StringName completion_function;
747 	int completion_argument;
748 
749 	bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
750 	static const BuiltinFuncDef builtin_func_defs[];
751 	static const BuiltinFuncOutArgs builtin_func_out_args[];
752 
753 	Error _validate_datatype(DataType p_type);
754 
755 	bool _validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type);
756 	bool _parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg = NULL);
757 
758 	Node *_parse_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types);
759 	ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
760 
761 	Node *_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types);
762 	Error _parse_block(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, bool p_just_one = false, bool p_can_break = false, bool p_can_continue = false);
763 	String _get_shader_type_list(const Set<String> &p_shader_types) const;
764 	String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
765 
766 	Error _parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types);
767 
768 	Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
769 	Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
770 
771 public:
772 	//static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
773 
774 	void clear();
775 
776 	static String get_shader_type(const String &p_code);
777 	Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types);
778 	Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint);
779 
780 	String get_error_text();
781 	int get_error_line();
782 
783 	ShaderNode *get_shader();
784 
785 	String token_debug(const String &p_code);
786 
787 	ShaderLanguage();
788 	~ShaderLanguage();
789 };
790 
791 #endif // SHADER_LANGUAGE_H
792