1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 #include "../pp/sl_pp_public.h"
32 #include "sl_cl_parse.h"
35 /* revision number - increment after each change affecting emitted output */
38 /* external declaration (or precision or invariant stmt) */
39 #define EXTERNAL_NULL 0
40 #define EXTERNAL_FUNCTION_DEFINITION 1
41 #define EXTERNAL_DECLARATION 2
42 #define DEFAULT_PRECISION 3
43 #define INVARIANT_STMT 4
46 #define PRECISION_DEFAULT 0
47 #define PRECISION_LOW 1
48 #define PRECISION_MEDIUM 2
49 #define PRECISION_HIGH 3
52 #define DECLARATION_FUNCTION_PROTOTYPE 1
53 #define DECLARATION_INIT_DECLARATOR_LIST 2
56 #define FUNCTION_ORDINARY 0
57 #define FUNCTION_CONSTRUCTOR 1
58 #define FUNCTION_OPERATOR 2
60 /* function call type */
61 #define FUNCTION_CALL_NONARRAY 0
62 #define FUNCTION_CALL_ARRAY 1
65 #define OPERATOR_ADDASSIGN 1
66 #define OPERATOR_SUBASSIGN 2
67 #define OPERATOR_MULASSIGN 3
68 #define OPERATOR_DIVASSIGN 4
69 /*#define OPERATOR_MODASSIGN 5*/
70 /*#define OPERATOR_LSHASSIGN 6*/
71 /*#define OPERATOR_RSHASSIGN 7*/
72 /*#define OPERATOR_ORASSIGN 8*/
73 /*#define OPERATOR_XORASSIGN 9*/
74 /*#define OPERATOR_ANDASSIGN 10*/
75 #define OPERATOR_LOGICALXOR 11
76 /*#define OPERATOR_BITOR 12*/
77 /*#define OPERATOR_BITXOR 13*/
78 /*#define OPERATOR_BITAND 14*/
79 #define OPERATOR_LESS 15
80 #define OPERATOR_GREATER 16
81 #define OPERATOR_LESSEQUAL 17
82 #define OPERATOR_GREATEREQUAL 18
83 /*#define OPERATOR_LSHIFT 19*/
84 /*#define OPERATOR_RSHIFT 20*/
85 #define OPERATOR_MULTIPLY 21
86 #define OPERATOR_DIVIDE 22
87 /*#define OPERATOR_MODULUS 23*/
88 #define OPERATOR_INCREMENT 24
89 #define OPERATOR_DECREMENT 25
90 #define OPERATOR_PLUS 26
91 #define OPERATOR_MINUS 27
92 /*#define OPERATOR_COMPLEMENT 28*/
93 #define OPERATOR_NOT 29
95 /* init declarator list */
96 #define DECLARATOR_NONE 0
97 #define DECLARATOR_NEXT 1
99 /* variable declaration */
100 #define VARIABLE_NONE 0
101 #define VARIABLE_IDENTIFIER 1
102 #define VARIABLE_INITIALIZER 2
103 #define VARIABLE_ARRAY_EXPLICIT 3
104 #define VARIABLE_ARRAY_UNKNOWN 4
107 #define TYPE_QUALIFIER_NONE 0
108 #define TYPE_QUALIFIER_CONST 1
109 #define TYPE_QUALIFIER_ATTRIBUTE 2
110 #define TYPE_QUALIFIER_VARYING 3
111 #define TYPE_QUALIFIER_UNIFORM 4
112 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
113 #define TYPE_QUALIFIER_FIXEDINPUT 6
115 /* invariant qualifier */
116 #define TYPE_VARIANT 90
117 #define TYPE_INVARIANT 91
119 /* centroid qualifier */
120 #define TYPE_CENTER 95
121 #define TYPE_CENTROID 96
123 /* layout qualifiers */
124 #define LAYOUT_QUALIFIER_NONE 0
125 #define LAYOUT_QUALIFIER_UPPER_LEFT 1
126 #define LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER 2
129 #define TYPE_SPECIFIER_VOID 0
130 #define TYPE_SPECIFIER_BOOL 1
131 #define TYPE_SPECIFIER_BVEC2 2
132 #define TYPE_SPECIFIER_BVEC3 3
133 #define TYPE_SPECIFIER_BVEC4 4
134 #define TYPE_SPECIFIER_INT 5
135 #define TYPE_SPECIFIER_IVEC2 6
136 #define TYPE_SPECIFIER_IVEC3 7
137 #define TYPE_SPECIFIER_IVEC4 8
138 #define TYPE_SPECIFIER_FLOAT 9
139 #define TYPE_SPECIFIER_VEC2 10
140 #define TYPE_SPECIFIER_VEC3 11
141 #define TYPE_SPECIFIER_VEC4 12
142 #define TYPE_SPECIFIER_MAT2 13
143 #define TYPE_SPECIFIER_MAT3 14
144 #define TYPE_SPECIFIER_MAT4 15
145 #define TYPE_SPECIFIER_SAMPLER1D 16
146 #define TYPE_SPECIFIER_SAMPLER2D 17
147 #define TYPE_SPECIFIER_SAMPLER3D 18
148 #define TYPE_SPECIFIER_SAMPLERCUBE 19
149 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
150 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
151 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
152 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
153 #define TYPE_SPECIFIER_STRUCT 24
154 #define TYPE_SPECIFIER_TYPENAME 25
157 #define TYPE_SPECIFIER_MAT23 26
158 #define TYPE_SPECIFIER_MAT32 27
159 #define TYPE_SPECIFIER_MAT24 28
160 #define TYPE_SPECIFIER_MAT42 29
161 #define TYPE_SPECIFIER_MAT34 30
162 #define TYPE_SPECIFIER_MAT43 31
164 /* GL_EXT_texture_array */
165 #define TYPE_SPECIFIER_SAMPLER_1D_ARRAY 32
166 #define TYPE_SPECIFIER_SAMPLER_2D_ARRAY 33
167 #define TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW 34
168 #define TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW 35
170 /* type specifier array */
171 #define TYPE_SPECIFIER_NONARRAY 0
172 #define TYPE_SPECIFIER_ARRAY 1
174 /* structure field */
177 #define FIELD_ARRAY 2
181 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
182 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
186 #define OP_CONTINUE 6
189 #define OP_EXPRESSION 9
194 #define OP_PUSH_VOID 14
195 #define OP_PUSH_BOOL 15
196 #define OP_PUSH_INT 16
197 #define OP_PUSH_FLOAT 17
198 #define OP_PUSH_IDENTIFIER 18
199 #define OP_SEQUENCE 19
201 #define OP_ADDASSIGN 21
202 #define OP_SUBASSIGN 22
203 #define OP_MULASSIGN 23
204 #define OP_DIVASSIGN 24
205 /*#define OP_MODASSIGN 25*/
206 /*#define OP_LSHASSIGN 26*/
207 /*#define OP_RSHASSIGN 27*/
208 /*#define OP_ORASSIGN 28*/
209 /*#define OP_XORASSIGN 29*/
210 /*#define OP_ANDASSIGN 30*/
212 #define OP_LOGICALOR 32
213 #define OP_LOGICALXOR 33
214 #define OP_LOGICALAND 34
215 /*#define OP_BITOR 35*/
216 /*#define OP_BITXOR 36*/
217 /*#define OP_BITAND 37*/
219 #define OP_NOTEQUAL 39
221 #define OP_GREATER 41
222 #define OP_LESSEQUAL 42
223 #define OP_GREATEREQUAL 43
224 /*#define OP_LSHIFT 44*/
225 /*#define OP_RSHIFT 45*/
227 #define OP_SUBTRACT 47
228 #define OP_MULTIPLY 48
230 /*#define OP_MODULUS 50*/
231 #define OP_PREINCREMENT 51
232 #define OP_PREDECREMENT 52
235 /*#define OP_COMPLEMENT 55*/
237 #define OP_SUBSCRIPT 57
240 #define OP_POSTINCREMENT 60
241 #define OP_POSTDECREMENT 61
242 #define OP_PRECISION 62
245 /* parameter qualifier */
246 #define PARAM_QUALIFIER_IN 0
247 #define PARAM_QUALIFIER_OUT 1
248 #define PARAM_QUALIFIER_INOUT 2
250 /* function parameter */
251 #define PARAMETER_NONE 0
252 #define PARAMETER_NEXT 1
254 /* function parameter array presence */
255 #define PARAMETER_ARRAY_NOT_PRESENT 0
256 #define PARAMETER_ARRAY_PRESENT 1
289 int sampler2DRectShadow
;
292 int sampler1DArrayShadow
;
293 int sampler2DArrayShadow
;
316 int origin_upper_left
;
317 int pixel_center_integer
;
340 int _GL_ARB_fragment_coord_conventions
;
344 struct parse_context
{
345 struct sl_pp_context
*context
;
347 struct parse_dict dict
;
349 struct sl_pp_token_info
*tokens
;
350 unsigned int tokens_read
;
351 unsigned int tokens_cap
;
353 unsigned char *out_buf
;
354 unsigned int out_cap
;
356 unsigned int shader_type
;
357 unsigned int parsing_builtin
;
359 unsigned int fragment_coord_conventions
:1;
373 _emit(struct parse_context
*ctx
,
377 if (*out
== ctx
->out_cap
) {
378 ctx
->out_cap
+= 4096;
379 ctx
->out_buf
= (unsigned char *)realloc(ctx
->out_buf
, ctx
->out_cap
* sizeof(unsigned char));
381 ctx
->out_buf
[*out
] = b
;
387 _update(struct parse_context
*ctx
,
391 ctx
->out_buf
[out
] = b
;
396 _error(struct parse_context
*ctx
,
399 if (ctx
->error
[0] == '\0') {
400 strncpy(ctx
->error
, msg
, sizeof(ctx
->error
) - 1);
401 ctx
->error
[sizeof(ctx
->error
) - 1] = '\0';
406 static const struct sl_pp_token_info
*
407 _fetch_token(struct parse_context
*ctx
,
410 if (ctx
->process_error
) {
414 while (pos
>= ctx
->tokens_read
) {
415 if (ctx
->tokens_read
== ctx
->tokens_cap
) {
416 ctx
->tokens_cap
+= 1024;
417 ctx
->tokens
= realloc(ctx
->tokens
,
418 ctx
->tokens_cap
* sizeof(struct sl_pp_token_info
));
420 _error(ctx
, "out of memory");
421 ctx
->process_error
= 1;
425 if (sl_pp_process_get(ctx
->context
, &ctx
->tokens
[ctx
->tokens_read
])) {
426 _error(ctx
, sl_pp_context_error_message(ctx
->context
));
427 ctx
->process_error
= 1;
430 switch (ctx
->tokens
[ctx
->tokens_read
].token
) {
432 case SL_PP_SEMICOLON
:
440 case SL_PP_INCREMENT
:
441 case SL_PP_ADDASSIGN
:
443 case SL_PP_DECREMENT
:
444 case SL_PP_SUBASSIGN
:
449 case SL_PP_MULASSIGN
:
451 case SL_PP_DIVASSIGN
:
453 case SL_PP_MODASSIGN
:
455 case SL_PP_LSHIFTASSIGN
:
457 case SL_PP_LESSEQUAL
:
459 case SL_PP_RSHIFTASSIGN
:
461 case SL_PP_GREATEREQUAL
:
466 case SL_PP_BITANDASSIGN
:
469 case SL_PP_BITXORASSIGN
:
472 case SL_PP_BITORASSIGN
:
476 case SL_PP_IDENTIFIER
:
479 case SL_PP_EXTENSION_REQUIRE
:
480 case SL_PP_EXTENSION_ENABLE
:
481 case SL_PP_EXTENSION_WARN
:
482 case SL_PP_EXTENSION_DISABLE
:
490 return &ctx
->tokens
[pos
];
495 * Try to parse/match a particular token.
496 * \return 0 for success, -1 for error.
499 _parse_token(struct parse_context
*ctx
,
500 enum sl_pp_token token
,
501 struct parse_state
*ps
)
503 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
505 if (input
&& input
->token
== token
) {
514 * Try to parse an identifer.
515 * \return 0 for success, -1 for error
518 _parse_id(struct parse_context
*ctx
,
520 struct parse_state
*ps
)
522 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
524 if (input
&& input
->token
== SL_PP_IDENTIFIER
&& input
->data
.identifier
== id
) {
533 _parse_identifier(struct parse_context
*ctx
,
534 struct parse_state
*ps
)
536 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
538 if (input
&& input
->token
== SL_PP_IDENTIFIER
) {
539 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
.identifier
);
542 _emit(ctx
, &ps
->out
, *cstr
);
552 _parse_float(struct parse_context
*ctx
,
553 struct parse_state
*ps
)
555 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
557 if (input
&& input
->token
== SL_PP_FLOAT
) {
558 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._float
);
560 _emit(ctx
, &ps
->out
, 1);
562 _emit(ctx
, &ps
->out
, *cstr
);
572 _parse_uint(struct parse_context
*ctx
,
573 struct parse_state
*ps
)
575 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
577 if (input
&& input
->token
== SL_PP_UINT
) {
578 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._uint
);
580 _emit(ctx
, &ps
->out
, 1);
582 _emit(ctx
, &ps
->out
, *cstr
);
591 /**************************************/
595 _parse_unary_expression(struct parse_context
*ctx
,
596 struct parse_state
*ps
);
599 _parse_conditional_expression(struct parse_context
*ctx
,
600 struct parse_state
*ps
);
604 _parse_constant_expression(struct parse_context
*ctx
,
605 struct parse_state
*ps
);
609 _parse_primary_expression(struct parse_context
*ctx
,
610 struct parse_state
*ps
);
614 _parse_statement(struct parse_context
*ctx
,
615 struct parse_state
*ps
);
619 _parse_type_specifier(struct parse_context
*ctx
,
620 struct parse_state
*ps
);
624 _parse_declaration(struct parse_context
*ctx
,
625 struct parse_state
*ps
);
629 _parse_statement_list(struct parse_context
*ctx
,
630 struct parse_state
*ps
);
634 _parse_assignment_expression(struct parse_context
*ctx
,
635 struct parse_state
*ps
);
639 _parse_precision(struct parse_context
*ctx
,
640 struct parse_state
*ps
);
644 _parse_overriden_operator(struct parse_context
*ctx
,
645 struct parse_state
*ps
)
649 if (_parse_token(ctx
, SL_PP_INCREMENT
, ps
) == 0) {
650 op
= OPERATOR_INCREMENT
;
651 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, ps
) == 0) {
652 op
= OPERATOR_ADDASSIGN
;
653 } else if (_parse_token(ctx
, SL_PP_PLUS
, ps
) == 0) {
655 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, ps
) == 0) {
656 op
= OPERATOR_DECREMENT
;
657 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, ps
) == 0) {
658 op
= OPERATOR_SUBASSIGN
;
659 } else if (_parse_token(ctx
, SL_PP_MINUS
, ps
) == 0) {
661 } else if (_parse_token(ctx
, SL_PP_NOT
, ps
) == 0) {
663 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, ps
) == 0) {
664 op
= OPERATOR_MULASSIGN
;
665 } else if (_parse_token(ctx
, SL_PP_STAR
, ps
) == 0) {
666 op
= OPERATOR_MULTIPLY
;
667 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, ps
) == 0) {
668 op
= OPERATOR_DIVASSIGN
;
669 } else if (_parse_token(ctx
, SL_PP_SLASH
, ps
) == 0) {
670 op
= OPERATOR_DIVIDE
;
671 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, ps
) == 0) {
672 op
= OPERATOR_LESSEQUAL
;
673 } else if (_parse_token(ctx
, SL_PP_LESS
, ps
) == 0) {
675 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, ps
) == 0) {
676 op
= OPERATOR_GREATEREQUAL
;
677 } else if (_parse_token(ctx
, SL_PP_GREATER
, ps
) == 0) {
678 op
= OPERATOR_GREATER
;
679 } else if (_parse_token(ctx
, SL_PP_XOR
, ps
) == 0) {
680 op
= OPERATOR_LOGICALXOR
;
685 _emit(ctx
, &ps
->out
, op
);
691 _parse_function_decl_identifier(struct parse_context
*ctx
,
692 struct parse_state
*ps
)
694 struct parse_state p
= *ps
;
695 unsigned int e
= _emit(ctx
, &p
.out
, 0);
697 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__constructor
, &p
) == 0) {
698 _update(ctx
, e
, FUNCTION_CONSTRUCTOR
);
703 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__operator
, &p
) == 0) {
704 _update(ctx
, e
, FUNCTION_OPERATOR
);
705 if (_parse_overriden_operator(ctx
, &p
) == 0) {
712 if (_parse_identifier(ctx
, &p
) == 0) {
713 _update(ctx
, e
, FUNCTION_ORDINARY
);
723 _parse_invariant_qualifier(struct parse_context
*ctx
,
724 struct parse_state
*ps
)
726 if (_parse_id(ctx
, ctx
->dict
.invariant
, ps
)) {
729 _emit(ctx
, &ps
->out
, TYPE_INVARIANT
);
735 _parse_centroid_qualifier(struct parse_context
*ctx
,
736 struct parse_state
*ps
)
738 if (_parse_id(ctx
, ctx
->dict
.centroid
, ps
)) {
741 _emit(ctx
, &ps
->out
, TYPE_CENTROID
);
747 _parse_layout_qualifier(struct parse_context
*ctx
,
748 struct parse_state
*ps
)
750 if (_parse_id(ctx
, ctx
->dict
.layout
, ps
) == 0) {
751 if (!ctx
->fragment_coord_conventions
) {
752 _error(ctx
, "GL_ARB_fragment_coord_conventions extension must be enabled "
753 "in order to use a layout qualifier");
757 /* Layout qualifiers are only defined for fragment shaders,
758 * so do an early check.
760 if (ctx
->shader_type
!= 1) {
761 _error(ctx
, "layout qualifiers are only valid for fragment shaders");
765 /* start of a parenthesised list of layout qualifiers */
767 if (_parse_token(ctx
, SL_PP_LPAREN
, ps
)) {
768 _error(ctx
, "expected `('");
772 /* parse comma-separated ID list */
774 if (_parse_id(ctx
, ctx
->dict
.origin_upper_left
, ps
) == 0) {
775 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_UPPER_LEFT
);
777 else if (_parse_id(ctx
, ctx
->dict
.pixel_center_integer
, ps
) == 0) {
778 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER
);
781 _error(ctx
, "expected a layout qualifier name");
785 if (_parse_token(ctx
, SL_PP_RPAREN
, ps
) == 0) {
789 else if (_parse_token(ctx
, SL_PP_COMMA
, ps
) == 0) {
790 /* another layout qualifier is coming */
793 _error(ctx
, "expected `,' or `)'");
804 _parse_storage_qualifier(struct parse_context
*ctx
,
805 struct parse_state
*ps
)
807 struct parse_state p
= *ps
;
808 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, p
.in
);
809 unsigned int e
= _emit(ctx
, &p
.out
, 0);
812 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
815 id
= input
->data
.identifier
;
817 if (id
== ctx
->dict
._const
) {
818 _update(ctx
, e
, TYPE_QUALIFIER_CONST
);
819 } else if (ctx
->shader_type
== 2 && id
== ctx
->dict
.attribute
) {
820 _update(ctx
, e
, TYPE_QUALIFIER_ATTRIBUTE
);
821 } else if (id
== ctx
->dict
.varying
) {
822 _update(ctx
, e
, TYPE_QUALIFIER_VARYING
);
823 } else if (id
== ctx
->dict
.uniform
) {
824 _update(ctx
, e
, TYPE_QUALIFIER_UNIFORM
);
825 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_output
) {
826 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDOUTPUT
);
827 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_input
) {
828 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDINPUT
);
832 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
839 _parse_struct_declarator(struct parse_context
*ctx
,
840 struct parse_state
*ps
)
842 struct parse_state p
= *ps
;
845 if (_parse_identifier(ctx
, &p
)) {
848 e
= _emit(ctx
, &p
.out
, FIELD_NONE
);
851 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
854 if (_parse_constant_expression(ctx
, &p
)) {
855 _error(ctx
, "expected constant integral expression");
858 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
859 _error(ctx
, "expected `]'");
862 _update(ctx
, e
, FIELD_ARRAY
);
869 _parse_struct_declarator_list(struct parse_context
*ctx
,
870 struct parse_state
*ps
)
872 struct parse_state p
= *ps
;
874 if (_parse_struct_declarator(ctx
, &p
)) {
880 _emit(ctx
, &p
.out
, FIELD_NEXT
);
881 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
884 if (_parse_struct_declarator(ctx
, &p
)) {
892 _parse_struct_declaration(struct parse_context
*ctx
,
893 struct parse_state
*ps
)
895 struct parse_state p
= *ps
;
897 if (_parse_type_specifier(ctx
, &p
)) {
900 if (_parse_struct_declarator_list(ctx
, &p
)) {
903 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
906 _emit(ctx
, &p
.out
, FIELD_NONE
);
913 _parse_struct_declaration_list(struct parse_context
*ctx
,
914 struct parse_state
*ps
)
916 struct parse_state p
= *ps
;
918 if (_parse_struct_declaration(ctx
, &p
)) {
924 _emit(ctx
, &p
.out
, FIELD_NEXT
);
925 if (_parse_struct_declaration(ctx
, &p
)) {
933 _parse_struct_specifier(struct parse_context
*ctx
,
934 struct parse_state
*ps
)
936 struct parse_state p
= *ps
;
938 if (_parse_id(ctx
, ctx
->dict
._struct
, &p
)) {
941 if (_parse_identifier(ctx
, &p
)) {
942 _emit(ctx
, &p
.out
, '\0');
944 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
945 _error(ctx
, "expected `{'");
948 if (_parse_struct_declaration_list(ctx
, &p
)) {
951 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
954 _emit(ctx
, &p
.out
, FIELD_NONE
);
961 _parse_type_specifier_nonarray(struct parse_context
*ctx
,
962 struct parse_state
*ps
)
964 struct parse_state p
= *ps
;
965 unsigned int e
= _emit(ctx
, &p
.out
, 0);
966 const struct sl_pp_token_info
*input
;
969 if (_parse_struct_specifier(ctx
, &p
) == 0) {
970 _update(ctx
, e
, TYPE_SPECIFIER_STRUCT
);
975 input
= _fetch_token(ctx
, p
.in
);
976 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
979 id
= input
->data
.identifier
;
981 if (id
== ctx
->dict
._void
) {
982 _update(ctx
, e
, TYPE_SPECIFIER_VOID
);
983 } else if (id
== ctx
->dict
._float
) {
984 _update(ctx
, e
, TYPE_SPECIFIER_FLOAT
);
985 } else if (id
== ctx
->dict
._int
) {
986 _update(ctx
, e
, TYPE_SPECIFIER_INT
);
987 } else if (id
== ctx
->dict
._bool
) {
988 _update(ctx
, e
, TYPE_SPECIFIER_BOOL
);
989 } else if (id
== ctx
->dict
.vec2
) {
990 _update(ctx
, e
, TYPE_SPECIFIER_VEC2
);
991 } else if (id
== ctx
->dict
.vec3
) {
992 _update(ctx
, e
, TYPE_SPECIFIER_VEC3
);
993 } else if (id
== ctx
->dict
.vec4
) {
994 _update(ctx
, e
, TYPE_SPECIFIER_VEC4
);
995 } else if (id
== ctx
->dict
.bvec2
) {
996 _update(ctx
, e
, TYPE_SPECIFIER_BVEC2
);
997 } else if (id
== ctx
->dict
.bvec3
) {
998 _update(ctx
, e
, TYPE_SPECIFIER_BVEC3
);
999 } else if (id
== ctx
->dict
.bvec4
) {
1000 _update(ctx
, e
, TYPE_SPECIFIER_BVEC4
);
1001 } else if (id
== ctx
->dict
.ivec2
) {
1002 _update(ctx
, e
, TYPE_SPECIFIER_IVEC2
);
1003 } else if (id
== ctx
->dict
.ivec3
) {
1004 _update(ctx
, e
, TYPE_SPECIFIER_IVEC3
);
1005 } else if (id
== ctx
->dict
.ivec4
) {
1006 _update(ctx
, e
, TYPE_SPECIFIER_IVEC4
);
1007 } else if (id
== ctx
->dict
.mat2
) {
1008 _update(ctx
, e
, TYPE_SPECIFIER_MAT2
);
1009 } else if (id
== ctx
->dict
.mat3
) {
1010 _update(ctx
, e
, TYPE_SPECIFIER_MAT3
);
1011 } else if (id
== ctx
->dict
.mat4
) {
1012 _update(ctx
, e
, TYPE_SPECIFIER_MAT4
);
1013 } else if (id
== ctx
->dict
.mat2x3
) {
1014 _update(ctx
, e
, TYPE_SPECIFIER_MAT23
);
1015 } else if (id
== ctx
->dict
.mat3x2
) {
1016 _update(ctx
, e
, TYPE_SPECIFIER_MAT32
);
1017 } else if (id
== ctx
->dict
.mat2x4
) {
1018 _update(ctx
, e
, TYPE_SPECIFIER_MAT24
);
1019 } else if (id
== ctx
->dict
.mat4x2
) {
1020 _update(ctx
, e
, TYPE_SPECIFIER_MAT42
);
1021 } else if (id
== ctx
->dict
.mat3x4
) {
1022 _update(ctx
, e
, TYPE_SPECIFIER_MAT34
);
1023 } else if (id
== ctx
->dict
.mat4x3
) {
1024 _update(ctx
, e
, TYPE_SPECIFIER_MAT43
);
1025 } else if (id
== ctx
->dict
.sampler1D
) {
1026 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1D
);
1027 } else if (id
== ctx
->dict
.sampler2D
) {
1028 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2D
);
1029 } else if (id
== ctx
->dict
.sampler3D
) {
1030 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER3D
);
1031 } else if (id
== ctx
->dict
.samplerCube
) {
1032 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLERCUBE
);
1033 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1034 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1DSHADOW
);
1035 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1036 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DSHADOW
);
1037 } else if (id
== ctx
->dict
.sampler2DRect
) {
1038 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECT
);
1039 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1040 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
);
1041 } else if (id
== ctx
->dict
.sampler1DArray
) {
1042 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_1D_ARRAY
);
1043 } else if (id
== ctx
->dict
.sampler2DArray
) {
1044 /* XXX check for GL_EXT_texture_array */
1045 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_2D_ARRAY
);
1046 } else if (id
== ctx
->dict
.sampler1DArrayShadow
) {
1047 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
);
1048 } else if (id
== ctx
->dict
.sampler2DArrayShadow
) {
1049 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
);
1050 } else if (_parse_identifier(ctx
, &p
) == 0) {
1051 _update(ctx
, e
, TYPE_SPECIFIER_TYPENAME
);
1058 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
1065 _parse_type_specifier_array(struct parse_context
*ctx
,
1066 struct parse_state
*ps
)
1068 struct parse_state p
= *ps
;
1070 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1073 if (_parse_constant_expression(ctx
, &p
)) {
1074 _error(ctx
, "expected constant integral expression");
1077 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1078 _error(ctx
, "expected `]'");
1087 _parse_type_specifier(struct parse_context
*ctx
,
1088 struct parse_state
*ps
)
1090 struct parse_state p
= *ps
;
1093 if (_parse_type_specifier_nonarray(ctx
, &p
)) {
1097 e
= _emit(ctx
, &p
.out
, TYPE_SPECIFIER_ARRAY
);
1098 if (_parse_type_specifier_array(ctx
, &p
)) {
1099 _update(ctx
, e
, TYPE_SPECIFIER_NONARRAY
);
1107 _parse_fully_specified_type(struct parse_context
*ctx
,
1108 struct parse_state
*ps
)
1110 struct parse_state p
= *ps
;
1112 if (_parse_layout_qualifier(ctx
, &p
)) {
1115 _emit(ctx
, &p
.out
, LAYOUT_QUALIFIER_NONE
);
1117 if (_parse_invariant_qualifier(ctx
, &p
)) {
1118 _emit(ctx
, &p
.out
, TYPE_VARIANT
);
1121 if (_parse_centroid_qualifier(ctx
, &p
)) {
1122 _emit(ctx
, &p
.out
, TYPE_CENTER
);
1124 if (_parse_storage_qualifier(ctx
, &p
)) {
1125 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1127 if (_parse_precision(ctx
, &p
)) {
1128 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1130 if (_parse_type_specifier(ctx
, &p
)) {
1139 _parse_function_header(struct parse_context
*ctx
,
1140 struct parse_state
*ps
)
1142 struct parse_state p
= *ps
;
1144 if (_parse_fully_specified_type(ctx
, &p
)) {
1147 if (_parse_function_decl_identifier(ctx
, &p
)) {
1150 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1159 _parse_parameter_qualifier(struct parse_context
*ctx
,
1160 struct parse_state
*ps
)
1162 unsigned int e
= _emit(ctx
, &ps
->out
, PARAM_QUALIFIER_IN
);
1164 if (_parse_id(ctx
, ctx
->dict
.out
, ps
) == 0) {
1165 _update(ctx
, e
, PARAM_QUALIFIER_OUT
);
1166 } else if (_parse_id(ctx
, ctx
->dict
.inout
, ps
) == 0) {
1167 _update(ctx
, e
, PARAM_QUALIFIER_INOUT
);
1169 _parse_id(ctx
, ctx
->dict
.in
, ps
);
1176 _parse_function_identifier(struct parse_context
*ctx
,
1177 struct parse_state
*ps
)
1179 struct parse_state p
;
1182 if (_parse_identifier(ctx
, ps
)) {
1185 e
= _emit(ctx
, &ps
->out
, FUNCTION_CALL_NONARRAY
);
1188 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1191 if (_parse_constant_expression(ctx
, &p
)) {
1192 _error(ctx
, "expected constant integral expression");
1195 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1196 _error(ctx
, "expected `]'");
1199 _update(ctx
, e
, FUNCTION_CALL_ARRAY
);
1206 _parse_function_call_header(struct parse_context
*ctx
,
1207 struct parse_state
*ps
)
1209 struct parse_state p
= *ps
;
1211 if (_parse_function_identifier(ctx
, &p
)) {
1214 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1223 _parse_assign_expression(struct parse_context
*ctx
,
1224 struct parse_state
*ps
)
1226 struct parse_state p
= *ps
;
1229 if (_parse_unary_expression(ctx
, &p
)) {
1233 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
1235 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, &p
) == 0) {
1237 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, &p
) == 0) {
1239 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, &p
) == 0) {
1241 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, &p
) == 0) {
1247 if (_parse_assignment_expression(ctx
, &p
)) {
1250 _emit(ctx
, &p
.out
, op
);
1258 _parse_assignment_expression(struct parse_context
*ctx
,
1259 struct parse_state
*ps
)
1261 if (_parse_assign_expression(ctx
, ps
) == 0) {
1265 if (_parse_conditional_expression(ctx
, ps
) == 0) {
1274 _parse_function_call_header_with_parameters(struct parse_context
*ctx
,
1275 struct parse_state
*ps
)
1277 struct parse_state p
= *ps
;
1279 if (_parse_function_call_header(ctx
, &p
)) {
1282 if (_parse_assignment_expression(ctx
, &p
)) {
1285 _emit(ctx
, &p
.out
, OP_END
);
1288 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1291 if (_parse_assignment_expression(ctx
, &p
)) {
1294 _emit(ctx
, &p
.out
, OP_END
);
1300 _parse_function_call_header_no_parameters(struct parse_context
*ctx
,
1301 struct parse_state
*ps
)
1303 if (_parse_function_call_header(ctx
, ps
)) {
1306 _parse_id(ctx
, ctx
->dict
._void
, ps
);
1312 _parse_function_call_generic(struct parse_context
*ctx
,
1313 struct parse_state
*ps
)
1315 struct parse_state p
= *ps
;
1317 if (_parse_function_call_header_with_parameters(ctx
, &p
) == 0) {
1318 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1322 _error(ctx
, "expected `)'");
1327 if (_parse_function_call_header_no_parameters(ctx
, &p
) == 0) {
1328 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1332 _error(ctx
, "expected `)'");
1341 _parse_method_call(struct parse_context
*ctx
,
1342 struct parse_state
*ps
)
1344 struct parse_state p
= *ps
;
1346 _emit(ctx
, &p
.out
, OP_METHOD
);
1347 if (_parse_identifier(ctx
, &p
)) {
1350 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1353 if (_parse_function_call_generic(ctx
, &p
)) {
1356 _emit(ctx
, &p
.out
, OP_END
);
1363 _parse_regular_function_call(struct parse_context
*ctx
,
1364 struct parse_state
*ps
)
1366 struct parse_state p
= *ps
;
1368 _emit(ctx
, &p
.out
, OP_CALL
);
1369 if (_parse_function_call_generic(ctx
, &p
)) {
1372 _emit(ctx
, &p
.out
, OP_END
);
1379 _parse_function_call(struct parse_context
*ctx
,
1380 struct parse_state
*ps
)
1382 if (_parse_regular_function_call(ctx
, ps
) == 0) {
1386 if (_parse_method_call(ctx
, ps
) == 0) {
1395 _parse_expression(struct parse_context
*ctx
,
1396 struct parse_state
*ps
)
1398 struct parse_state p
= *ps
;
1400 if (_parse_assignment_expression(ctx
, &p
)) {
1406 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1409 if (_parse_assignment_expression(ctx
, &p
)) {
1412 _emit(ctx
, &p
.out
, OP_SEQUENCE
);
1418 _parse_postfix_expression(struct parse_context
*ctx
,
1419 struct parse_state
*ps
)
1421 struct parse_state p
;
1423 if (_parse_function_call(ctx
, ps
)) {
1424 if (_parse_primary_expression(ctx
, ps
)) {
1431 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1432 _emit(ctx
, &p
.out
, OP_POSTINCREMENT
);
1433 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1434 _emit(ctx
, &p
.out
, OP_POSTDECREMENT
);
1435 } else if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
1436 if (_parse_expression(ctx
, &p
)) {
1437 _error(ctx
, "expected an integral expression");
1440 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1441 _error(ctx
, "expected `]'");
1444 _emit(ctx
, &p
.out
, OP_SUBSCRIPT
);
1445 } else if (_parse_token(ctx
, SL_PP_DOT
, &p
) == 0) {
1446 _emit(ctx
, &p
.out
, OP_FIELD
);
1447 if (_parse_identifier(ctx
, &p
)) {
1458 _parse_unary_expression(struct parse_context
*ctx
,
1459 struct parse_state
*ps
)
1461 struct parse_state p
;
1464 if (_parse_postfix_expression(ctx
, ps
) == 0) {
1469 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1470 op
= OP_PREINCREMENT
;
1471 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1472 op
= OP_PREDECREMENT
;
1473 } else if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1475 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1477 } else if (_parse_token(ctx
, SL_PP_NOT
, &p
) == 0) {
1483 if (_parse_unary_expression(ctx
, &p
)) {
1486 _emit(ctx
, &p
.out
, op
);
1493 _parse_multiplicative_expression(struct parse_context
*ctx
,
1494 struct parse_state
*ps
)
1496 struct parse_state p
= *ps
;
1498 if (_parse_unary_expression(ctx
, &p
)) {
1505 if (_parse_token(ctx
, SL_PP_STAR
, &p
) == 0) {
1507 } else if (_parse_token(ctx
, SL_PP_SLASH
, &p
) == 0) {
1512 if (_parse_unary_expression(ctx
, &p
)) {
1515 _emit(ctx
, &p
.out
, op
);
1521 _parse_additive_expression(struct parse_context
*ctx
,
1522 struct parse_state
*ps
)
1524 struct parse_state p
= *ps
;
1526 if (_parse_multiplicative_expression(ctx
, &p
)) {
1533 if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1535 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1540 if (_parse_multiplicative_expression(ctx
, &p
)) {
1543 _emit(ctx
, &p
.out
, op
);
1549 _parse_relational_expression(struct parse_context
*ctx
,
1550 struct parse_state
*ps
)
1552 struct parse_state p
= *ps
;
1554 if (_parse_additive_expression(ctx
, &p
)) {
1561 if (_parse_token(ctx
, SL_PP_LESS
, &p
) == 0) {
1563 } else if (_parse_token(ctx
, SL_PP_GREATER
, &p
) == 0) {
1565 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, &p
) == 0) {
1567 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, &p
) == 0) {
1568 op
= OP_GREATEREQUAL
;
1572 if (_parse_additive_expression(ctx
, &p
)) {
1575 _emit(ctx
, &p
.out
, op
);
1581 _parse_equality_expression(struct parse_context
*ctx
,
1582 struct parse_state
*ps
)
1584 struct parse_state p
= *ps
;
1586 if (_parse_relational_expression(ctx
, &p
)) {
1593 if (_parse_token(ctx
, SL_PP_EQUAL
, &p
) == 0) {
1595 } else if (_parse_token(ctx
, SL_PP_NOTEQUAL
, &p
) == 0) {
1600 if (_parse_relational_expression(ctx
, &p
)) {
1603 _emit(ctx
, &p
.out
, op
);
1609 _parse_logical_and_expression(struct parse_context
*ctx
,
1610 struct parse_state
*ps
)
1612 struct parse_state p
= *ps
;
1614 if (_parse_equality_expression(ctx
, &p
)) {
1619 if (_parse_token(ctx
, SL_PP_AND
, &p
)) {
1622 if (_parse_equality_expression(ctx
, &p
)) {
1625 _emit(ctx
, &p
.out
, OP_LOGICALAND
);
1631 _parse_logical_xor_expression(struct parse_context
*ctx
,
1632 struct parse_state
*ps
)
1634 struct parse_state p
= *ps
;
1636 if (_parse_logical_and_expression(ctx
, &p
)) {
1641 if (_parse_token(ctx
, SL_PP_XOR
, &p
)) {
1644 if (_parse_logical_and_expression(ctx
, &p
)) {
1647 _emit(ctx
, &p
.out
, OP_LOGICALXOR
);
1653 _parse_logical_or_expression(struct parse_context
*ctx
,
1654 struct parse_state
*ps
)
1656 struct parse_state p
= *ps
;
1658 if (_parse_logical_xor_expression(ctx
, &p
)) {
1663 if (_parse_token(ctx
, SL_PP_OR
, &p
)) {
1666 if (_parse_logical_xor_expression(ctx
, &p
)) {
1669 _emit(ctx
, &p
.out
, OP_LOGICALOR
);
1675 _parse_conditional_expression(struct parse_context
*ctx
,
1676 struct parse_state
*ps
)
1678 struct parse_state p
= *ps
;
1680 if (_parse_logical_or_expression(ctx
, &p
)) {
1685 if (_parse_token(ctx
, SL_PP_QUESTION
, &p
)) {
1688 if (_parse_expression(ctx
, &p
)) {
1691 if (_parse_token(ctx
, SL_PP_COLON
, &p
)) {
1694 if (_parse_conditional_expression(ctx
, &p
)) {
1697 _emit(ctx
, &p
.out
, OP_SELECT
);
1703 _parse_constant_expression(struct parse_context
*ctx
,
1704 struct parse_state
*ps
)
1706 if (_parse_conditional_expression(ctx
, ps
)) {
1709 _emit(ctx
, &ps
->out
, OP_END
);
1715 _parse_parameter_declarator_array(struct parse_context
*ctx
,
1716 struct parse_state
*ps
)
1718 struct parse_state p
= *ps
;
1720 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1723 if (_parse_constant_expression(ctx
, &p
)) {
1724 _error(ctx
, "expected constant integral expression");
1727 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1728 _error(ctx
, "expected `]'");
1737 _parse_parameter_declarator(struct parse_context
*ctx
,
1738 struct parse_state
*ps
)
1740 struct parse_state p
= *ps
;
1743 if (_parse_type_specifier(ctx
, &p
)) {
1746 if (_parse_identifier(ctx
, &p
)) {
1749 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1750 if (_parse_parameter_declarator_array(ctx
, &p
)) {
1751 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1759 _parse_parameter_type_specifier_array(struct parse_context
*ctx
,
1760 struct parse_state
*ps
)
1762 struct parse_state p
= *ps
;
1764 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1767 if (_parse_constant_expression(ctx
, &p
)) {
1768 _error(ctx
, "expected constant integral expression");
1771 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1772 _error(ctx
, "expected `]'");
1781 _parse_parameter_type_specifier(struct parse_context
*ctx
,
1782 struct parse_state
*ps
)
1784 struct parse_state p
= *ps
;
1787 if (_parse_type_specifier(ctx
, &p
)) {
1790 _emit(ctx
, &p
.out
, '\0');
1792 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1793 if (_parse_parameter_type_specifier_array(ctx
, &p
)) {
1794 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1802 _parse_parameter_declaration(struct parse_context
*ctx
,
1803 struct parse_state
*ps
)
1805 struct parse_state p
= *ps
;
1806 unsigned int e
= _emit(ctx
, &p
.out
, PARAMETER_NEXT
);
1810 if (_parse_storage_qualifier(ctx
, &p
)) {
1811 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1813 _parse_parameter_qualifier(ctx
, &p
);
1814 if (_parse_precision(ctx
, &p
)) {
1815 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1817 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1821 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1831 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1832 struct parse_state
*ps
)
1834 struct parse_state p
= *ps
;
1836 if (_parse_function_header(ctx
, &p
)) {
1839 if (_parse_parameter_declaration(ctx
, &p
)) {
1845 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1848 if (_parse_parameter_declaration(ctx
, &p
)) {
1856 _parse_function_declarator(struct parse_context
*ctx
,
1857 struct parse_state
*ps
)
1859 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1863 if (_parse_function_header(ctx
, ps
) == 0) {
1872 _parse_function_prototype(struct parse_context
*ctx
,
1873 struct parse_state
*ps
)
1875 struct parse_state p
= *ps
;
1877 if (_parse_function_header(ctx
, &p
) == 0) {
1878 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1879 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1880 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1884 _error(ctx
, "expected `)'");
1890 if (_parse_function_declarator(ctx
, &p
) == 0) {
1891 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1892 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1896 _error(ctx
, "expected `)'");
1905 _parse_precision(struct parse_context
*ctx
,
1906 struct parse_state
*ps
)
1908 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1910 unsigned int precision
;
1912 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1915 id
= input
->data
.identifier
;
1917 if (id
== ctx
->dict
.lowp
) {
1918 precision
= PRECISION_LOW
;
1919 } else if (id
== ctx
->dict
.mediump
) {
1920 precision
= PRECISION_MEDIUM
;
1921 } else if (id
== ctx
->dict
.highp
) {
1922 precision
= PRECISION_HIGH
;
1927 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1928 _emit(ctx
, &ps
->out
, precision
);
1934 _parse_prectype(struct parse_context
*ctx
,
1935 struct parse_state
*ps
)
1937 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1941 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1944 id
= input
->data
.identifier
;
1946 if (id
== ctx
->dict
._int
) {
1947 type
= TYPE_SPECIFIER_INT
;
1948 } else if (id
== ctx
->dict
._float
) {
1949 type
= TYPE_SPECIFIER_FLOAT
;
1950 } else if (id
== ctx
->dict
.sampler1D
) {
1951 type
= TYPE_SPECIFIER_SAMPLER1D
;
1952 } else if (id
== ctx
->dict
.sampler2D
) {
1953 type
= TYPE_SPECIFIER_SAMPLER2D
;
1954 } else if (id
== ctx
->dict
.sampler3D
) {
1955 type
= TYPE_SPECIFIER_SAMPLER3D
;
1956 } else if (id
== ctx
->dict
.samplerCube
) {
1957 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1958 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1959 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1960 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1961 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1962 } else if (id
== ctx
->dict
.sampler2DRect
) {
1963 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1964 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1965 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1966 } else if (id
== ctx
->dict
.sampler1DArray
) {
1967 type
= TYPE_SPECIFIER_SAMPLER_1D_ARRAY
;
1968 } else if (id
== ctx
->dict
.sampler2DArray
) {
1969 type
= TYPE_SPECIFIER_SAMPLER_2D_ARRAY
;
1970 } else if (id
== ctx
->dict
.sampler1DArrayShadow
) {
1971 type
= TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
;
1972 } else if (id
== ctx
->dict
.sampler2DArrayShadow
) {
1973 type
= TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
;
1978 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1979 _emit(ctx
, &ps
->out
, type
);
1985 _parse_precision_stmt(struct parse_context
*ctx
,
1986 struct parse_state
*ps
)
1988 struct parse_state p
= *ps
;
1990 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
1993 if (_parse_precision(ctx
, &p
)) {
1996 if (_parse_prectype(ctx
, &p
)) {
1999 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2008 _parse_floatconstant(struct parse_context
*ctx
,
2009 struct parse_state
*ps
)
2011 struct parse_state p
= *ps
;
2013 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
2014 if (_parse_float(ctx
, &p
)) {
2023 _parse_intconstant(struct parse_context
*ctx
,
2024 struct parse_state
*ps
)
2026 struct parse_state p
= *ps
;
2028 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
2029 if (_parse_uint(ctx
, &p
)) {
2038 _parse_boolconstant(struct parse_context
*ctx
,
2039 struct parse_state
*ps
)
2041 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
2042 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2043 _emit(ctx
, &ps
->out
, 2); /* radix */
2044 _emit(ctx
, &ps
->out
, '0');
2045 _emit(ctx
, &ps
->out
, '\0');
2049 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
2050 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2051 _emit(ctx
, &ps
->out
, 2); /* radix */
2052 _emit(ctx
, &ps
->out
, '1');
2053 _emit(ctx
, &ps
->out
, '\0');
2062 _parse_variable_identifier(struct parse_context
*ctx
,
2063 struct parse_state
*ps
)
2065 struct parse_state p
= *ps
;
2067 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
2068 if (_parse_identifier(ctx
, &p
)) {
2077 _parse_primary_expression(struct parse_context
*ctx
,
2078 struct parse_state
*ps
)
2080 struct parse_state p
;
2082 if (_parse_floatconstant(ctx
, ps
) == 0) {
2085 if (_parse_boolconstant(ctx
, ps
) == 0) {
2088 if (_parse_intconstant(ctx
, ps
) == 0) {
2091 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2096 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2099 if (_parse_expression(ctx
, &p
)) {
2102 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2112 _parse_asm_argument(struct parse_context
*ctx
,
2113 struct parse_state
*ps
)
2115 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2116 struct parse_state p
= *ps
;
2118 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
2121 _emit(ctx
, &p
.out
, OP_FIELD
);
2122 if (_parse_identifier(ctx
, &p
)) {
2129 if (_parse_floatconstant(ctx
, ps
) == 0) {
2138 _parse_asm_arguments(struct parse_context
*ctx
,
2139 struct parse_state
*ps
)
2141 struct parse_state p
= *ps
;
2143 if (_parse_asm_argument(ctx
, &p
)) {
2146 _emit(ctx
, &p
.out
, OP_END
);
2150 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2153 if (_parse_asm_argument(ctx
, &p
)) {
2156 _emit(ctx
, &p
.out
, OP_END
);
2162 _parse_asm_statement(struct parse_context
*ctx
,
2163 struct parse_state
*ps
)
2165 struct parse_state p
= *ps
;
2167 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
2170 if (_parse_identifier(ctx
, &p
)) {
2173 if (_parse_asm_arguments(ctx
, &p
)) {
2176 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2179 _emit(ctx
, &p
.out
, OP_END
);
2186 _parse_selection_statement(struct parse_context
*ctx
,
2187 struct parse_state
*ps
)
2189 struct parse_state p
= *ps
;
2191 _emit(ctx
, &p
.out
, OP_IF
);
2192 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
2195 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2196 _error(ctx
, "expected `('");
2199 if (_parse_expression(ctx
, &p
)) {
2200 _error(ctx
, "expected an expression");
2203 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2204 _error(ctx
, "expected `)'");
2207 _emit(ctx
, &p
.out
, OP_END
);
2208 if (_parse_statement(ctx
, &p
)) {
2213 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
2214 if (_parse_statement(ctx
, &p
) == 0) {
2220 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
2221 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
2222 _emit(ctx
, &ps
->out
, OP_END
);
2228 _parse_expression_statement(struct parse_context
*ctx
,
2229 struct parse_state
*ps
)
2231 struct parse_state p
= *ps
;
2233 if (_parse_expression(ctx
, &p
)) {
2234 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2236 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2239 _emit(ctx
, &p
.out
, OP_END
);
2246 _parse_for_init_statement(struct parse_context
*ctx
,
2247 struct parse_state
*ps
)
2249 struct parse_state p
= *ps
;
2250 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2252 if (_parse_expression_statement(ctx
, &p
) == 0) {
2257 if (_parse_declaration(ctx
, &p
) == 0) {
2258 _update(ctx
, e
, OP_DECLARE
);
2268 _parse_initializer(struct parse_context
*ctx
,
2269 struct parse_state
*ps
)
2271 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2272 _emit(ctx
, &ps
->out
, OP_END
);
2280 _parse_condition_initializer(struct parse_context
*ctx
,
2281 struct parse_state
*ps
)
2283 struct parse_state p
= *ps
;
2285 _emit(ctx
, &p
.out
, OP_DECLARE
);
2286 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2287 if (_parse_fully_specified_type(ctx
, &p
)) {
2290 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2291 if (_parse_identifier(ctx
, &p
)) {
2294 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2295 _error(ctx
, "expected `='");
2298 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2299 if (_parse_initializer(ctx
, &p
)) {
2300 _error(ctx
, "expected an initialiser");
2303 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2310 _parse_condition(struct parse_context
*ctx
,
2311 struct parse_state
*ps
)
2313 struct parse_state p
;
2315 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2320 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2321 if (_parse_expression(ctx
, &p
) == 0) {
2322 _emit(ctx
, &p
.out
, OP_END
);
2332 _parse_for_rest_statement(struct parse_context
*ctx
,
2333 struct parse_state
*ps
)
2335 struct parse_state p
= *ps
;
2337 if (_parse_condition(ctx
, &p
)) {
2338 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2339 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2340 _emit(ctx
, &p
.out
, 2);
2341 _emit(ctx
, &p
.out
, '1');
2342 _emit(ctx
, &p
.out
, '\0');
2343 _emit(ctx
, &p
.out
, OP_END
);
2345 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2348 if (_parse_expression(ctx
, &p
)) {
2349 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2351 _emit(ctx
, &p
.out
, OP_END
);
2358 _parse_iteration_statement(struct parse_context
*ctx
,
2359 struct parse_state
*ps
)
2361 struct parse_state p
= *ps
;
2363 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2364 _emit(ctx
, &p
.out
, OP_WHILE
);
2365 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2366 _error(ctx
, "expected `('");
2369 if (_parse_condition(ctx
, &p
)) {
2370 _error(ctx
, "expected an expression");
2373 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2374 _error(ctx
, "expected `)'");
2377 if (_parse_statement(ctx
, &p
)) {
2384 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2385 _emit(ctx
, &p
.out
, OP_DO
);
2386 if (_parse_statement(ctx
, &p
)) {
2389 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2392 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2393 _error(ctx
, "expected `('");
2396 if (_parse_expression(ctx
, &p
)) {
2397 _error(ctx
, "expected an expression");
2400 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2401 _error(ctx
, "expected `)'");
2404 _emit(ctx
, &p
.out
, OP_END
);
2405 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2406 _error(ctx
, "expected `;'");
2413 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2414 _emit(ctx
, &p
.out
, OP_FOR
);
2415 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2416 _error(ctx
, "expected `('");
2419 if (_parse_for_init_statement(ctx
, &p
)) {
2422 if (_parse_for_rest_statement(ctx
, &p
)) {
2425 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2426 _error(ctx
, "expected `)'");
2429 if (_parse_statement(ctx
, &p
)) {
2441 _parse_jump_statement(struct parse_context
*ctx
,
2442 struct parse_state
*ps
)
2444 struct parse_state p
= *ps
;
2445 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2447 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2448 _update(ctx
, e
, OP_CONTINUE
);
2449 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2450 _update(ctx
, e
, OP_BREAK
);
2451 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2452 _update(ctx
, e
, OP_RETURN
);
2453 if (_parse_expression(ctx
, &p
)) {
2454 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2456 _emit(ctx
, &p
.out
, OP_END
);
2457 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2458 _update(ctx
, e
, OP_DISCARD
);
2462 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2471 _parse_simple_statement(struct parse_context
*ctx
,
2472 struct parse_state
*ps
)
2474 struct parse_state p
;
2477 if (_parse_selection_statement(ctx
, ps
) == 0) {
2481 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2485 if (_parse_jump_statement(ctx
, ps
) == 0) {
2490 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2491 if (_parse_expression_statement(ctx
, &p
) == 0) {
2496 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2497 _update(ctx
, e
, OP_PRECISION
);
2502 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2503 _update(ctx
, e
, OP_ASM
);
2508 if (_parse_declaration(ctx
, &p
) == 0) {
2509 _update(ctx
, e
, OP_DECLARE
);
2519 _parse_compound_statement(struct parse_context
*ctx
,
2520 struct parse_state
*ps
)
2522 struct parse_state p
= *ps
;
2524 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2527 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2528 _parse_statement_list(ctx
, &p
);
2529 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2532 _emit(ctx
, &p
.out
, OP_END
);
2539 _parse_statement(struct parse_context
*ctx
,
2540 struct parse_state
*ps
)
2542 if (_parse_compound_statement(ctx
, ps
) == 0) {
2546 if (_parse_simple_statement(ctx
, ps
) == 0) {
2555 _parse_statement_list(struct parse_context
*ctx
,
2556 struct parse_state
*ps
)
2558 struct parse_state p
= *ps
;
2560 if (_parse_statement(ctx
, &p
)) {
2566 if (_parse_statement(ctx
, &p
)) {
2574 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2575 struct parse_state
*ps
)
2577 struct parse_state p
= *ps
;
2579 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2582 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2583 _parse_statement_list(ctx
, &p
);
2584 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2587 _emit(ctx
, &p
.out
, OP_END
);
2594 _parse_function_definition(struct parse_context
*ctx
,
2595 struct parse_state
*ps
)
2597 struct parse_state p
= *ps
;
2599 if (_parse_function_prototype(ctx
, &p
)) {
2602 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2611 _parse_invariant_stmt(struct parse_context
*ctx
,
2612 struct parse_state
*ps
)
2614 struct parse_state p
= *ps
;
2616 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2619 if (_parse_identifier(ctx
, &p
)) {
2622 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2631 _parse_single_declaration(struct parse_context
*ctx
,
2632 struct parse_state
*ps
)
2634 struct parse_state p
= *ps
;
2637 if (_parse_fully_specified_type(ctx
, &p
)) {
2641 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2642 if (_parse_identifier(ctx
, &p
)) {
2643 _update(ctx
, e
, VARIABLE_NONE
);
2648 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2651 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2652 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2653 if (_parse_initializer(ctx
, &p
) == 0) {
2657 _error(ctx
, "expected an initialiser");
2662 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2663 if (_parse_constant_expression(ctx
, &p
)) {
2664 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2666 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2668 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2672 _error(ctx
, "expected `]'");
2680 _parse_init_declarator_list(struct parse_context
*ctx
,
2681 struct parse_state
*ps
)
2683 struct parse_state p
= *ps
;
2685 if (_parse_single_declaration(ctx
, &p
)) {
2693 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2696 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2697 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2698 if (_parse_identifier(ctx
, &p
)) {
2702 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2705 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2706 if (_parse_initializer(ctx
, &p
) == 0) {
2707 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2711 _error(ctx
, "expected an initialiser");
2716 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2719 if (_parse_constant_expression(ctx
, &p
)) {
2720 arr
= VARIABLE_ARRAY_UNKNOWN
;
2722 arr
= VARIABLE_ARRAY_EXPLICIT
;
2724 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2725 _update(ctx
, e
, arr
);
2729 _error(ctx
, "expected `]'");
2735 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2741 _parse_declaration(struct parse_context
*ctx
,
2742 struct parse_state
*ps
)
2744 struct parse_state p
= *ps
;
2745 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2747 if (_parse_function_prototype(ctx
, &p
)) {
2748 if (_parse_init_declarator_list(ctx
, &p
)) {
2751 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2753 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2754 _error(ctx
, "expected `;'");
2763 _parse_external_declaration(struct parse_context
*ctx
,
2764 struct parse_state
*ps
)
2766 struct parse_state p
= *ps
;
2767 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2769 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2770 _update(ctx
, e
, DEFAULT_PRECISION
);
2775 if (_parse_function_definition(ctx
, &p
) == 0) {
2776 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2781 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2782 _update(ctx
, e
, INVARIANT_STMT
);
2787 if (_parse_declaration(ctx
, &p
) == 0) {
2788 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2793 _error(ctx
, "expected an identifier");
2799 _parse_extensions(struct parse_context
*ctx
,
2800 struct parse_state
*ps
)
2803 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
2804 unsigned int enable
;
2810 switch (input
->token
) {
2811 case SL_PP_EXTENSION_REQUIRE
:
2812 case SL_PP_EXTENSION_ENABLE
:
2813 case SL_PP_EXTENSION_WARN
:
2816 case SL_PP_EXTENSION_DISABLE
:
2824 if (input
->data
.extension
== ctx
->dict
.all
) {
2825 ctx
->fragment_coord_conventions
= enable
;
2827 else if (input
->data
.extension
== ctx
->dict
._GL_ARB_fragment_coord_conventions
) {
2828 ctx
->fragment_coord_conventions
= enable
;
2835 _parse_translation_unit(struct parse_context
*ctx
,
2836 struct parse_state
*ps
)
2838 _emit(ctx
, &ps
->out
, REVISION
);
2839 if (_parse_extensions(ctx
, ps
)) {
2842 if (_parse_external_declaration(ctx
, ps
)) {
2846 if (_parse_extensions(ctx
, ps
)) {
2849 if (_parse_external_declaration(ctx
, ps
)) {
2853 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2854 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2861 #define ADD_NAME_STR(CTX, NAME, STR)\
2863 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2864 if ((CTX).dict.NAME == -1) {\
2869 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2873 sl_cl_compile(struct sl_pp_context
*context
,
2874 unsigned int shader_type
,
2875 unsigned int parsing_builtin
,
2876 unsigned char **output
,
2877 unsigned int *cboutput
,
2879 unsigned int cberror
)
2881 struct parse_context ctx
;
2882 struct parse_state ps
;
2884 ctx
.context
= context
;
2886 ADD_NAME_STR(ctx
, _void
, "void");
2887 ADD_NAME_STR(ctx
, _float
, "float");
2888 ADD_NAME_STR(ctx
, _int
, "int");
2889 ADD_NAME_STR(ctx
, _bool
, "bool");
2890 ADD_NAME(ctx
, vec2
);
2891 ADD_NAME(ctx
, vec3
);
2892 ADD_NAME(ctx
, vec4
);
2893 ADD_NAME(ctx
, bvec2
);
2894 ADD_NAME(ctx
, bvec3
);
2895 ADD_NAME(ctx
, bvec4
);
2896 ADD_NAME(ctx
, ivec2
);
2897 ADD_NAME(ctx
, ivec3
);
2898 ADD_NAME(ctx
, ivec4
);
2899 ADD_NAME(ctx
, mat2
);
2900 ADD_NAME(ctx
, mat3
);
2901 ADD_NAME(ctx
, mat4
);
2902 ADD_NAME(ctx
, mat2x3
);
2903 ADD_NAME(ctx
, mat3x2
);
2904 ADD_NAME(ctx
, mat2x4
);
2905 ADD_NAME(ctx
, mat4x2
);
2906 ADD_NAME(ctx
, mat3x4
);
2907 ADD_NAME(ctx
, mat4x3
);
2908 ADD_NAME(ctx
, sampler1D
);
2909 ADD_NAME(ctx
, sampler2D
);
2910 ADD_NAME(ctx
, sampler3D
);
2911 ADD_NAME(ctx
, samplerCube
);
2912 ADD_NAME(ctx
, sampler1DShadow
);
2913 ADD_NAME(ctx
, sampler2DShadow
);
2914 ADD_NAME(ctx
, sampler2DRect
);
2915 ADD_NAME(ctx
, sampler2DRectShadow
);
2916 ADD_NAME(ctx
, sampler1DArray
);
2917 ADD_NAME(ctx
, sampler2DArray
);
2918 ADD_NAME(ctx
, sampler1DArrayShadow
);
2919 ADD_NAME(ctx
, sampler2DArrayShadow
);
2921 ADD_NAME(ctx
, invariant
);
2923 ADD_NAME(ctx
, centroid
);
2925 ADD_NAME(ctx
, precision
);
2926 ADD_NAME(ctx
, lowp
);
2927 ADD_NAME(ctx
, mediump
);
2928 ADD_NAME(ctx
, highp
);
2930 ADD_NAME_STR(ctx
, _const
, "const");
2931 ADD_NAME(ctx
, attribute
);
2932 ADD_NAME(ctx
, varying
);
2933 ADD_NAME(ctx
, uniform
);
2934 ADD_NAME(ctx
, __fixed_output
);
2935 ADD_NAME(ctx
, __fixed_input
);
2939 ADD_NAME(ctx
, inout
);
2941 ADD_NAME(ctx
, layout
);
2942 ADD_NAME(ctx
, origin_upper_left
);
2943 ADD_NAME(ctx
, pixel_center_integer
);
2945 ADD_NAME_STR(ctx
, _struct
, "struct");
2947 ADD_NAME(ctx
, __constructor
);
2948 ADD_NAME(ctx
, __operator
);
2949 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2951 ADD_NAME_STR(ctx
, _if
, "if");
2952 ADD_NAME_STR(ctx
, _else
, "else");
2953 ADD_NAME_STR(ctx
, _for
, "for");
2954 ADD_NAME_STR(ctx
, _while
, "while");
2955 ADD_NAME_STR(ctx
, _do
, "do");
2957 ADD_NAME_STR(ctx
, _continue
, "continue");
2958 ADD_NAME_STR(ctx
, _break
, "break");
2959 ADD_NAME_STR(ctx
, _return
, "return");
2960 ADD_NAME(ctx
, discard
);
2962 ADD_NAME_STR(ctx
, _false
, "false");
2963 ADD_NAME_STR(ctx
, _true
, "true");
2966 ADD_NAME_STR(ctx
, _GL_ARB_fragment_coord_conventions
, "GL_ARB_fragment_coord_conventions");
2971 ctx
.shader_type
= shader_type
;
2972 ctx
.parsing_builtin
= 1;
2974 ctx
.fragment_coord_conventions
= 0;
2976 ctx
.error
[0] = '\0';
2977 ctx
.process_error
= 0;
2979 ctx
.tokens_cap
= 1024;
2980 ctx
.tokens_read
= 0;
2981 ctx
.tokens
= malloc(ctx
.tokens_cap
* sizeof(struct sl_pp_token_info
));
2983 strncpy(error
, "out of memory", cberror
- 1);
2984 error
[cberror
- 1] = '\0';
2991 if (_parse_translation_unit(&ctx
, &ps
)) {
2992 strncpy(error
, ctx
.error
, cberror
);
2997 *output
= ctx
.out_buf
;