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
249 #define PARAM_QUALIFIER_NONE 3
251 /* function parameter */
252 #define PARAMETER_NONE 0
253 #define PARAMETER_NEXT 1
255 /* function parameter array presence */
256 #define PARAMETER_ARRAY_NOT_PRESENT 0
257 #define PARAMETER_ARRAY_PRESENT 1
290 int sampler2DRectShadow
;
293 int sampler1DArrayShadow
;
294 int sampler2DArrayShadow
;
317 int origin_upper_left
;
318 int pixel_center_integer
;
341 int _GL_ARB_fragment_coord_conventions
;
342 int _GL_ARB_texture_rectangle
;
346 struct parse_context
{
347 struct sl_pp_context
*context
;
349 struct parse_dict dict
;
351 struct sl_pp_token_info
*tokens
;
352 unsigned int tokens_read
;
353 unsigned int tokens_cap
;
355 unsigned char *out_buf
;
356 unsigned int out_cap
;
358 unsigned int shader_type
;
359 unsigned int parsing_builtin
;
361 unsigned int fragment_coord_conventions
:1;
362 unsigned int texture_rectangle
:1;
376 _emit(struct parse_context
*ctx
,
380 if (*out
== ctx
->out_cap
) {
381 ctx
->out_cap
+= 4096;
382 ctx
->out_buf
= (unsigned char *)realloc(ctx
->out_buf
, ctx
->out_cap
* sizeof(unsigned char));
384 ctx
->out_buf
[*out
] = b
;
390 _update(struct parse_context
*ctx
,
394 ctx
->out_buf
[out
] = b
;
399 _error(struct parse_context
*ctx
,
402 if (ctx
->error
[0] == '\0') {
403 strncpy(ctx
->error
, msg
, sizeof(ctx
->error
) - 1);
404 ctx
->error
[sizeof(ctx
->error
) - 1] = '\0';
409 static const struct sl_pp_token_info
*
410 _fetch_token(struct parse_context
*ctx
,
413 if (ctx
->process_error
) {
417 while (pos
>= ctx
->tokens_read
) {
418 if (ctx
->tokens_read
== ctx
->tokens_cap
) {
419 ctx
->tokens_cap
+= 1024;
420 ctx
->tokens
= realloc(ctx
->tokens
,
421 ctx
->tokens_cap
* sizeof(struct sl_pp_token_info
));
423 _error(ctx
, "out of memory");
424 ctx
->process_error
= 1;
428 if (sl_pp_process_get(ctx
->context
, &ctx
->tokens
[ctx
->tokens_read
])) {
429 _error(ctx
, sl_pp_context_error_message(ctx
->context
));
430 ctx
->process_error
= 1;
433 switch (ctx
->tokens
[ctx
->tokens_read
].token
) {
435 case SL_PP_SEMICOLON
:
443 case SL_PP_INCREMENT
:
444 case SL_PP_ADDASSIGN
:
446 case SL_PP_DECREMENT
:
447 case SL_PP_SUBASSIGN
:
452 case SL_PP_MULASSIGN
:
454 case SL_PP_DIVASSIGN
:
456 case SL_PP_MODASSIGN
:
458 case SL_PP_LSHIFTASSIGN
:
460 case SL_PP_LESSEQUAL
:
462 case SL_PP_RSHIFTASSIGN
:
464 case SL_PP_GREATEREQUAL
:
469 case SL_PP_BITANDASSIGN
:
472 case SL_PP_BITXORASSIGN
:
475 case SL_PP_BITORASSIGN
:
479 case SL_PP_IDENTIFIER
:
482 case SL_PP_EXTENSION_REQUIRE
:
483 case SL_PP_EXTENSION_ENABLE
:
484 case SL_PP_EXTENSION_WARN
:
485 case SL_PP_EXTENSION_DISABLE
:
493 return &ctx
->tokens
[pos
];
498 * Try to parse/match a particular token.
499 * \return 0 for success, -1 for error.
502 _parse_token(struct parse_context
*ctx
,
503 enum sl_pp_token token
,
504 struct parse_state
*ps
)
506 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
508 if (input
&& input
->token
== token
) {
517 * Try to parse an identifer.
518 * \return 0 for success, -1 for error
521 _parse_id(struct parse_context
*ctx
,
523 struct parse_state
*ps
)
525 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
527 if (input
&& input
->token
== SL_PP_IDENTIFIER
&& input
->data
.identifier
== id
) {
536 _parse_identifier(struct parse_context
*ctx
,
537 struct parse_state
*ps
)
539 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
541 if (input
&& input
->token
== SL_PP_IDENTIFIER
) {
542 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
.identifier
);
545 _emit(ctx
, &ps
->out
, *cstr
);
555 _parse_float(struct parse_context
*ctx
,
556 struct parse_state
*ps
)
558 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
560 if (input
&& input
->token
== SL_PP_FLOAT
) {
561 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._float
);
563 _emit(ctx
, &ps
->out
, 1);
565 _emit(ctx
, &ps
->out
, *cstr
);
575 _parse_uint(struct parse_context
*ctx
,
576 struct parse_state
*ps
)
578 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
580 if (input
&& input
->token
== SL_PP_UINT
) {
581 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._uint
);
583 _emit(ctx
, &ps
->out
, 1);
585 _emit(ctx
, &ps
->out
, *cstr
);
594 /**************************************/
598 _parse_unary_expression(struct parse_context
*ctx
,
599 struct parse_state
*ps
);
602 _parse_conditional_expression(struct parse_context
*ctx
,
603 struct parse_state
*ps
);
607 _parse_constant_expression(struct parse_context
*ctx
,
608 struct parse_state
*ps
);
612 _parse_primary_expression(struct parse_context
*ctx
,
613 struct parse_state
*ps
);
617 _parse_statement(struct parse_context
*ctx
,
618 struct parse_state
*ps
);
622 _parse_type_specifier(struct parse_context
*ctx
,
623 struct parse_state
*ps
);
627 _parse_declaration(struct parse_context
*ctx
,
628 struct parse_state
*ps
);
632 _parse_statement_list(struct parse_context
*ctx
,
633 struct parse_state
*ps
);
637 _parse_assignment_expression(struct parse_context
*ctx
,
638 struct parse_state
*ps
);
642 _parse_precision(struct parse_context
*ctx
,
643 struct parse_state
*ps
);
647 _parse_overriden_operator(struct parse_context
*ctx
,
648 struct parse_state
*ps
)
652 if (_parse_token(ctx
, SL_PP_INCREMENT
, ps
) == 0) {
653 op
= OPERATOR_INCREMENT
;
654 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, ps
) == 0) {
655 op
= OPERATOR_ADDASSIGN
;
656 } else if (_parse_token(ctx
, SL_PP_PLUS
, ps
) == 0) {
658 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, ps
) == 0) {
659 op
= OPERATOR_DECREMENT
;
660 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, ps
) == 0) {
661 op
= OPERATOR_SUBASSIGN
;
662 } else if (_parse_token(ctx
, SL_PP_MINUS
, ps
) == 0) {
664 } else if (_parse_token(ctx
, SL_PP_NOT
, ps
) == 0) {
666 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, ps
) == 0) {
667 op
= OPERATOR_MULASSIGN
;
668 } else if (_parse_token(ctx
, SL_PP_STAR
, ps
) == 0) {
669 op
= OPERATOR_MULTIPLY
;
670 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, ps
) == 0) {
671 op
= OPERATOR_DIVASSIGN
;
672 } else if (_parse_token(ctx
, SL_PP_SLASH
, ps
) == 0) {
673 op
= OPERATOR_DIVIDE
;
674 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, ps
) == 0) {
675 op
= OPERATOR_LESSEQUAL
;
676 } else if (_parse_token(ctx
, SL_PP_LESS
, ps
) == 0) {
678 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, ps
) == 0) {
679 op
= OPERATOR_GREATEREQUAL
;
680 } else if (_parse_token(ctx
, SL_PP_GREATER
, ps
) == 0) {
681 op
= OPERATOR_GREATER
;
682 } else if (_parse_token(ctx
, SL_PP_XOR
, ps
) == 0) {
683 op
= OPERATOR_LOGICALXOR
;
688 _emit(ctx
, &ps
->out
, op
);
694 _parse_function_decl_identifier(struct parse_context
*ctx
,
695 struct parse_state
*ps
)
697 struct parse_state p
= *ps
;
698 unsigned int e
= _emit(ctx
, &p
.out
, 0);
700 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__constructor
, &p
) == 0) {
701 _update(ctx
, e
, FUNCTION_CONSTRUCTOR
);
706 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__operator
, &p
) == 0) {
707 _update(ctx
, e
, FUNCTION_OPERATOR
);
708 if (_parse_overriden_operator(ctx
, &p
) == 0) {
715 if (_parse_identifier(ctx
, &p
) == 0) {
716 _update(ctx
, e
, FUNCTION_ORDINARY
);
726 _parse_invariant_qualifier(struct parse_context
*ctx
,
727 struct parse_state
*ps
)
729 if (_parse_id(ctx
, ctx
->dict
.invariant
, ps
)) {
732 _emit(ctx
, &ps
->out
, TYPE_INVARIANT
);
738 _parse_centroid_qualifier(struct parse_context
*ctx
,
739 struct parse_state
*ps
)
741 if (_parse_id(ctx
, ctx
->dict
.centroid
, ps
)) {
744 _emit(ctx
, &ps
->out
, TYPE_CENTROID
);
750 _parse_layout_qualifier(struct parse_context
*ctx
,
751 struct parse_state
*ps
)
753 if (_parse_id(ctx
, ctx
->dict
.layout
, ps
) == 0) {
754 if (!ctx
->fragment_coord_conventions
) {
755 _error(ctx
, "GL_ARB_fragment_coord_conventions extension must be enabled "
756 "in order to use a layout qualifier");
760 /* Layout qualifiers are only defined for fragment shaders,
761 * so do an early check.
763 if (ctx
->shader_type
!= 1) {
764 _error(ctx
, "layout qualifiers are only valid for fragment shaders");
768 /* start of a parenthesised list of layout qualifiers */
770 if (_parse_token(ctx
, SL_PP_LPAREN
, ps
)) {
771 _error(ctx
, "expected `('");
775 /* parse comma-separated ID list */
777 if (_parse_id(ctx
, ctx
->dict
.origin_upper_left
, ps
) == 0) {
778 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_UPPER_LEFT
);
780 else if (_parse_id(ctx
, ctx
->dict
.pixel_center_integer
, ps
) == 0) {
781 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER
);
784 _error(ctx
, "expected a layout qualifier name");
788 if (_parse_token(ctx
, SL_PP_RPAREN
, ps
) == 0) {
792 else if (_parse_token(ctx
, SL_PP_COMMA
, ps
) == 0) {
793 /* another layout qualifier is coming */
796 _error(ctx
, "expected `,' or `)'");
807 _parse_storage_qualifier(struct parse_context
*ctx
,
808 struct parse_state
*ps
)
810 struct parse_state p
= *ps
;
811 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, p
.in
);
812 unsigned int e
= _emit(ctx
, &p
.out
, 0);
815 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
818 id
= input
->data
.identifier
;
820 if (id
== ctx
->dict
._const
) {
821 _update(ctx
, e
, TYPE_QUALIFIER_CONST
);
822 } else if (ctx
->shader_type
== 2 && id
== ctx
->dict
.attribute
) {
823 _update(ctx
, e
, TYPE_QUALIFIER_ATTRIBUTE
);
824 } else if (id
== ctx
->dict
.varying
) {
825 _update(ctx
, e
, TYPE_QUALIFIER_VARYING
);
826 } else if (id
== ctx
->dict
.uniform
) {
827 _update(ctx
, e
, TYPE_QUALIFIER_UNIFORM
);
828 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_output
) {
829 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDOUTPUT
);
830 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_input
) {
831 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDINPUT
);
835 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
841 _parse_struct_declarator(struct parse_context
*ctx
,
842 struct parse_state
*ps
)
844 struct parse_state p
= *ps
;
847 if (_parse_identifier(ctx
, &p
)) {
850 e
= _emit(ctx
, &p
.out
, FIELD_NONE
);
853 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
856 if (_parse_constant_expression(ctx
, &p
)) {
857 _error(ctx
, "expected constant integral expression");
860 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
861 _error(ctx
, "expected `]'");
864 _update(ctx
, e
, FIELD_ARRAY
);
871 _parse_struct_declarator_list(struct parse_context
*ctx
,
872 struct parse_state
*ps
)
874 struct parse_state p
= *ps
;
876 if (_parse_struct_declarator(ctx
, &p
)) {
882 _emit(ctx
, &p
.out
, FIELD_NEXT
);
883 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
886 if (_parse_struct_declarator(ctx
, &p
)) {
894 _parse_struct_declaration(struct parse_context
*ctx
,
895 struct parse_state
*ps
)
897 struct parse_state p
= *ps
;
899 if (_parse_type_specifier(ctx
, &p
)) {
902 if (_parse_struct_declarator_list(ctx
, &p
)) {
905 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
908 _emit(ctx
, &p
.out
, FIELD_NONE
);
915 _parse_struct_declaration_list(struct parse_context
*ctx
,
916 struct parse_state
*ps
)
918 struct parse_state p
= *ps
;
920 if (_parse_struct_declaration(ctx
, &p
)) {
926 _emit(ctx
, &p
.out
, FIELD_NEXT
);
927 if (_parse_struct_declaration(ctx
, &p
)) {
935 _parse_struct_specifier(struct parse_context
*ctx
,
936 struct parse_state
*ps
)
938 struct parse_state p
= *ps
;
940 if (_parse_id(ctx
, ctx
->dict
._struct
, &p
)) {
943 if (_parse_identifier(ctx
, &p
)) {
944 _emit(ctx
, &p
.out
, '\0');
946 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
947 _error(ctx
, "expected `{'");
950 if (_parse_struct_declaration_list(ctx
, &p
)) {
953 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
956 _emit(ctx
, &p
.out
, FIELD_NONE
);
963 _parse_type_specifier_nonarray(struct parse_context
*ctx
,
964 struct parse_state
*ps
)
966 struct parse_state p
= *ps
;
967 unsigned int e
= _emit(ctx
, &p
.out
, 0);
968 const struct sl_pp_token_info
*input
;
971 if (_parse_struct_specifier(ctx
, &p
) == 0) {
972 _update(ctx
, e
, TYPE_SPECIFIER_STRUCT
);
977 input
= _fetch_token(ctx
, p
.in
);
978 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
981 id
= input
->data
.identifier
;
983 if (id
== ctx
->dict
._void
) {
984 _update(ctx
, e
, TYPE_SPECIFIER_VOID
);
985 } else if (id
== ctx
->dict
._float
) {
986 _update(ctx
, e
, TYPE_SPECIFIER_FLOAT
);
987 } else if (id
== ctx
->dict
._int
) {
988 _update(ctx
, e
, TYPE_SPECIFIER_INT
);
989 } else if (id
== ctx
->dict
._bool
) {
990 _update(ctx
, e
, TYPE_SPECIFIER_BOOL
);
991 } else if (id
== ctx
->dict
.vec2
) {
992 _update(ctx
, e
, TYPE_SPECIFIER_VEC2
);
993 } else if (id
== ctx
->dict
.vec3
) {
994 _update(ctx
, e
, TYPE_SPECIFIER_VEC3
);
995 } else if (id
== ctx
->dict
.vec4
) {
996 _update(ctx
, e
, TYPE_SPECIFIER_VEC4
);
997 } else if (id
== ctx
->dict
.bvec2
) {
998 _update(ctx
, e
, TYPE_SPECIFIER_BVEC2
);
999 } else if (id
== ctx
->dict
.bvec3
) {
1000 _update(ctx
, e
, TYPE_SPECIFIER_BVEC3
);
1001 } else if (id
== ctx
->dict
.bvec4
) {
1002 _update(ctx
, e
, TYPE_SPECIFIER_BVEC4
);
1003 } else if (id
== ctx
->dict
.ivec2
) {
1004 _update(ctx
, e
, TYPE_SPECIFIER_IVEC2
);
1005 } else if (id
== ctx
->dict
.ivec3
) {
1006 _update(ctx
, e
, TYPE_SPECIFIER_IVEC3
);
1007 } else if (id
== ctx
->dict
.ivec4
) {
1008 _update(ctx
, e
, TYPE_SPECIFIER_IVEC4
);
1009 } else if (id
== ctx
->dict
.mat2
) {
1010 _update(ctx
, e
, TYPE_SPECIFIER_MAT2
);
1011 } else if (id
== ctx
->dict
.mat3
) {
1012 _update(ctx
, e
, TYPE_SPECIFIER_MAT3
);
1013 } else if (id
== ctx
->dict
.mat4
) {
1014 _update(ctx
, e
, TYPE_SPECIFIER_MAT4
);
1015 } else if (id
== ctx
->dict
.mat2x3
) {
1016 _update(ctx
, e
, TYPE_SPECIFIER_MAT23
);
1017 } else if (id
== ctx
->dict
.mat3x2
) {
1018 _update(ctx
, e
, TYPE_SPECIFIER_MAT32
);
1019 } else if (id
== ctx
->dict
.mat2x4
) {
1020 _update(ctx
, e
, TYPE_SPECIFIER_MAT24
);
1021 } else if (id
== ctx
->dict
.mat4x2
) {
1022 _update(ctx
, e
, TYPE_SPECIFIER_MAT42
);
1023 } else if (id
== ctx
->dict
.mat3x4
) {
1024 _update(ctx
, e
, TYPE_SPECIFIER_MAT34
);
1025 } else if (id
== ctx
->dict
.mat4x3
) {
1026 _update(ctx
, e
, TYPE_SPECIFIER_MAT43
);
1027 } else if (id
== ctx
->dict
.sampler1D
) {
1028 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1D
);
1029 } else if (id
== ctx
->dict
.sampler2D
) {
1030 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2D
);
1031 } else if (id
== ctx
->dict
.sampler3D
) {
1032 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER3D
);
1033 } else if (id
== ctx
->dict
.samplerCube
) {
1034 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLERCUBE
);
1035 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1036 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1DSHADOW
);
1037 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1038 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DSHADOW
);
1039 } else if (id
== ctx
->dict
.sampler2DRect
) {
1040 if (!ctx
->texture_rectangle
) {
1041 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1042 "in order to use a rect sampler");
1045 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECT
);
1046 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1047 if (!ctx
->texture_rectangle
) {
1048 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1049 "in order to use a rect sampler");
1052 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
);
1053 } else if (id
== ctx
->dict
.sampler1DArray
) {
1054 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_1D_ARRAY
);
1055 } else if (id
== ctx
->dict
.sampler2DArray
) {
1056 /* XXX check for GL_EXT_texture_array */
1057 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_2D_ARRAY
);
1058 } else if (id
== ctx
->dict
.sampler1DArrayShadow
) {
1059 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
);
1060 } else if (id
== ctx
->dict
.sampler2DArrayShadow
) {
1061 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
);
1062 } else if (_parse_identifier(ctx
, &p
) == 0) {
1063 _update(ctx
, e
, TYPE_SPECIFIER_TYPENAME
);
1070 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
1077 _parse_type_specifier_array(struct parse_context
*ctx
,
1078 struct parse_state
*ps
)
1080 struct parse_state p
= *ps
;
1082 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1085 if (_parse_constant_expression(ctx
, &p
)) {
1086 _error(ctx
, "expected constant integral expression");
1089 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1090 _error(ctx
, "expected `]'");
1099 _parse_type_specifier(struct parse_context
*ctx
,
1100 struct parse_state
*ps
)
1102 struct parse_state p
= *ps
;
1105 if (_parse_type_specifier_nonarray(ctx
, &p
)) {
1109 e
= _emit(ctx
, &p
.out
, TYPE_SPECIFIER_ARRAY
);
1110 if (_parse_type_specifier_array(ctx
, &p
)) {
1111 _update(ctx
, e
, TYPE_SPECIFIER_NONARRAY
);
1118 _parse_parameter_qualifier(struct parse_context
*ctx
,
1119 struct parse_state
*ps
)
1121 unsigned int e
= _emit(ctx
, &ps
->out
, PARAM_QUALIFIER_NONE
);
1123 if (_parse_id(ctx
, ctx
->dict
.in
, ps
) == 0) {
1124 _update(ctx
, e
, PARAM_QUALIFIER_IN
);
1125 } else if (_parse_id(ctx
, ctx
->dict
.out
, ps
) == 0) {
1126 _update(ctx
, e
, PARAM_QUALIFIER_OUT
);
1127 } else if (_parse_id(ctx
, ctx
->dict
.inout
, ps
) == 0) {
1128 _update(ctx
, e
, PARAM_QUALIFIER_INOUT
);
1134 _parse_fully_specified_type(struct parse_context
*ctx
,
1135 struct parse_state
*ps
)
1137 struct parse_state p
= *ps
;
1139 if (_parse_layout_qualifier(ctx
, &p
)) {
1142 _emit(ctx
, &p
.out
, LAYOUT_QUALIFIER_NONE
);
1144 if (_parse_invariant_qualifier(ctx
, &p
)) {
1145 _emit(ctx
, &p
.out
, TYPE_VARIANT
);
1148 if (_parse_centroid_qualifier(ctx
, &p
)) {
1149 _emit(ctx
, &p
.out
, TYPE_CENTER
);
1151 if (_parse_storage_qualifier(ctx
, &p
)) {
1152 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1154 _parse_parameter_qualifier(ctx
, &p
);
1155 if (_parse_precision(ctx
, &p
)) {
1156 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1158 if (_parse_type_specifier(ctx
, &p
)) {
1167 _parse_function_header(struct parse_context
*ctx
,
1168 struct parse_state
*ps
)
1170 struct parse_state p
= *ps
;
1172 if (_parse_fully_specified_type(ctx
, &p
)) {
1175 if (_parse_function_decl_identifier(ctx
, &p
)) {
1178 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1187 _parse_function_identifier(struct parse_context
*ctx
,
1188 struct parse_state
*ps
)
1190 struct parse_state p
;
1193 if (_parse_identifier(ctx
, ps
)) {
1196 e
= _emit(ctx
, &ps
->out
, FUNCTION_CALL_NONARRAY
);
1199 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1202 if (_parse_constant_expression(ctx
, &p
)) {
1203 _error(ctx
, "expected constant integral expression");
1206 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1207 _error(ctx
, "expected `]'");
1210 _update(ctx
, e
, FUNCTION_CALL_ARRAY
);
1217 _parse_function_call_header(struct parse_context
*ctx
,
1218 struct parse_state
*ps
)
1220 struct parse_state p
= *ps
;
1222 if (_parse_function_identifier(ctx
, &p
)) {
1225 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1234 _parse_assign_expression(struct parse_context
*ctx
,
1235 struct parse_state
*ps
)
1237 struct parse_state p
= *ps
;
1240 if (_parse_unary_expression(ctx
, &p
)) {
1244 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
1246 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, &p
) == 0) {
1248 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, &p
) == 0) {
1250 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, &p
) == 0) {
1252 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, &p
) == 0) {
1258 if (_parse_assignment_expression(ctx
, &p
)) {
1261 _emit(ctx
, &p
.out
, op
);
1269 _parse_assignment_expression(struct parse_context
*ctx
,
1270 struct parse_state
*ps
)
1272 if (_parse_assign_expression(ctx
, ps
) == 0) {
1276 if (_parse_conditional_expression(ctx
, ps
) == 0) {
1285 _parse_function_call_header_with_parameters(struct parse_context
*ctx
,
1286 struct parse_state
*ps
)
1288 struct parse_state p
= *ps
;
1290 if (_parse_function_call_header(ctx
, &p
)) {
1293 if (_parse_assignment_expression(ctx
, &p
)) {
1296 _emit(ctx
, &p
.out
, OP_END
);
1299 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1302 if (_parse_assignment_expression(ctx
, &p
)) {
1305 _emit(ctx
, &p
.out
, OP_END
);
1311 _parse_function_call_header_no_parameters(struct parse_context
*ctx
,
1312 struct parse_state
*ps
)
1314 if (_parse_function_call_header(ctx
, ps
)) {
1317 _parse_id(ctx
, ctx
->dict
._void
, ps
);
1323 _parse_function_call_generic(struct parse_context
*ctx
,
1324 struct parse_state
*ps
)
1326 struct parse_state p
= *ps
;
1328 if (_parse_function_call_header_with_parameters(ctx
, &p
) == 0) {
1329 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1333 _error(ctx
, "expected `)'");
1338 if (_parse_function_call_header_no_parameters(ctx
, &p
) == 0) {
1339 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1343 _error(ctx
, "expected `)'");
1352 _parse_method_call(struct parse_context
*ctx
,
1353 struct parse_state
*ps
)
1355 struct parse_state p
= *ps
;
1357 _emit(ctx
, &p
.out
, OP_METHOD
);
1358 if (_parse_identifier(ctx
, &p
)) {
1361 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1364 if (_parse_function_call_generic(ctx
, &p
)) {
1367 _emit(ctx
, &p
.out
, OP_END
);
1374 _parse_regular_function_call(struct parse_context
*ctx
,
1375 struct parse_state
*ps
)
1377 struct parse_state p
= *ps
;
1379 _emit(ctx
, &p
.out
, OP_CALL
);
1380 if (_parse_function_call_generic(ctx
, &p
)) {
1383 _emit(ctx
, &p
.out
, OP_END
);
1390 _parse_function_call(struct parse_context
*ctx
,
1391 struct parse_state
*ps
)
1393 if (_parse_regular_function_call(ctx
, ps
) == 0) {
1397 if (_parse_method_call(ctx
, ps
) == 0) {
1406 _parse_expression(struct parse_context
*ctx
,
1407 struct parse_state
*ps
)
1409 struct parse_state p
= *ps
;
1411 if (_parse_assignment_expression(ctx
, &p
)) {
1417 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1420 if (_parse_assignment_expression(ctx
, &p
)) {
1423 _emit(ctx
, &p
.out
, OP_SEQUENCE
);
1429 _parse_postfix_expression(struct parse_context
*ctx
,
1430 struct parse_state
*ps
)
1432 struct parse_state p
;
1434 if (_parse_function_call(ctx
, ps
)) {
1435 if (_parse_primary_expression(ctx
, ps
)) {
1442 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1443 _emit(ctx
, &p
.out
, OP_POSTINCREMENT
);
1444 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1445 _emit(ctx
, &p
.out
, OP_POSTDECREMENT
);
1446 } else if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
1447 if (_parse_expression(ctx
, &p
)) {
1448 _error(ctx
, "expected an integral expression");
1451 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1452 _error(ctx
, "expected `]'");
1455 _emit(ctx
, &p
.out
, OP_SUBSCRIPT
);
1456 } else if (_parse_token(ctx
, SL_PP_DOT
, &p
) == 0) {
1457 _emit(ctx
, &p
.out
, OP_FIELD
);
1458 if (_parse_identifier(ctx
, &p
)) {
1469 _parse_unary_expression(struct parse_context
*ctx
,
1470 struct parse_state
*ps
)
1472 struct parse_state p
;
1475 if (_parse_postfix_expression(ctx
, ps
) == 0) {
1480 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1481 op
= OP_PREINCREMENT
;
1482 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1483 op
= OP_PREDECREMENT
;
1484 } else if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1486 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1488 } else if (_parse_token(ctx
, SL_PP_NOT
, &p
) == 0) {
1494 if (_parse_unary_expression(ctx
, &p
)) {
1497 _emit(ctx
, &p
.out
, op
);
1504 _parse_multiplicative_expression(struct parse_context
*ctx
,
1505 struct parse_state
*ps
)
1507 struct parse_state p
= *ps
;
1509 if (_parse_unary_expression(ctx
, &p
)) {
1516 if (_parse_token(ctx
, SL_PP_STAR
, &p
) == 0) {
1518 } else if (_parse_token(ctx
, SL_PP_SLASH
, &p
) == 0) {
1523 if (_parse_unary_expression(ctx
, &p
)) {
1526 _emit(ctx
, &p
.out
, op
);
1532 _parse_additive_expression(struct parse_context
*ctx
,
1533 struct parse_state
*ps
)
1535 struct parse_state p
= *ps
;
1537 if (_parse_multiplicative_expression(ctx
, &p
)) {
1544 if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1546 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1551 if (_parse_multiplicative_expression(ctx
, &p
)) {
1554 _emit(ctx
, &p
.out
, op
);
1560 _parse_relational_expression(struct parse_context
*ctx
,
1561 struct parse_state
*ps
)
1563 struct parse_state p
= *ps
;
1565 if (_parse_additive_expression(ctx
, &p
)) {
1572 if (_parse_token(ctx
, SL_PP_LESS
, &p
) == 0) {
1574 } else if (_parse_token(ctx
, SL_PP_GREATER
, &p
) == 0) {
1576 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, &p
) == 0) {
1578 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, &p
) == 0) {
1579 op
= OP_GREATEREQUAL
;
1583 if (_parse_additive_expression(ctx
, &p
)) {
1586 _emit(ctx
, &p
.out
, op
);
1592 _parse_equality_expression(struct parse_context
*ctx
,
1593 struct parse_state
*ps
)
1595 struct parse_state p
= *ps
;
1597 if (_parse_relational_expression(ctx
, &p
)) {
1604 if (_parse_token(ctx
, SL_PP_EQUAL
, &p
) == 0) {
1606 } else if (_parse_token(ctx
, SL_PP_NOTEQUAL
, &p
) == 0) {
1611 if (_parse_relational_expression(ctx
, &p
)) {
1614 _emit(ctx
, &p
.out
, op
);
1620 _parse_logical_and_expression(struct parse_context
*ctx
,
1621 struct parse_state
*ps
)
1623 struct parse_state p
= *ps
;
1625 if (_parse_equality_expression(ctx
, &p
)) {
1630 if (_parse_token(ctx
, SL_PP_AND
, &p
)) {
1633 if (_parse_equality_expression(ctx
, &p
)) {
1636 _emit(ctx
, &p
.out
, OP_LOGICALAND
);
1642 _parse_logical_xor_expression(struct parse_context
*ctx
,
1643 struct parse_state
*ps
)
1645 struct parse_state p
= *ps
;
1647 if (_parse_logical_and_expression(ctx
, &p
)) {
1652 if (_parse_token(ctx
, SL_PP_XOR
, &p
)) {
1655 if (_parse_logical_and_expression(ctx
, &p
)) {
1658 _emit(ctx
, &p
.out
, OP_LOGICALXOR
);
1664 _parse_logical_or_expression(struct parse_context
*ctx
,
1665 struct parse_state
*ps
)
1667 struct parse_state p
= *ps
;
1669 if (_parse_logical_xor_expression(ctx
, &p
)) {
1674 if (_parse_token(ctx
, SL_PP_OR
, &p
)) {
1677 if (_parse_logical_xor_expression(ctx
, &p
)) {
1680 _emit(ctx
, &p
.out
, OP_LOGICALOR
);
1686 _parse_conditional_expression(struct parse_context
*ctx
,
1687 struct parse_state
*ps
)
1689 struct parse_state p
= *ps
;
1691 if (_parse_logical_or_expression(ctx
, &p
)) {
1696 if (_parse_token(ctx
, SL_PP_QUESTION
, &p
)) {
1699 if (_parse_expression(ctx
, &p
)) {
1702 if (_parse_token(ctx
, SL_PP_COLON
, &p
)) {
1705 if (_parse_conditional_expression(ctx
, &p
)) {
1708 _emit(ctx
, &p
.out
, OP_SELECT
);
1714 _parse_constant_expression(struct parse_context
*ctx
,
1715 struct parse_state
*ps
)
1717 if (_parse_conditional_expression(ctx
, ps
)) {
1720 _emit(ctx
, &ps
->out
, OP_END
);
1726 _parse_parameter_declarator_array(struct parse_context
*ctx
,
1727 struct parse_state
*ps
)
1729 struct parse_state p
= *ps
;
1731 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1734 if (_parse_constant_expression(ctx
, &p
)) {
1735 _error(ctx
, "expected constant integral expression");
1738 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1739 _error(ctx
, "expected `]'");
1748 _parse_parameter_declarator(struct parse_context
*ctx
,
1749 struct parse_state
*ps
)
1751 struct parse_state p
= *ps
;
1754 if (_parse_type_specifier(ctx
, &p
)) {
1757 if (_parse_identifier(ctx
, &p
)) {
1760 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1761 if (_parse_parameter_declarator_array(ctx
, &p
)) {
1762 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1770 _parse_parameter_type_specifier_array(struct parse_context
*ctx
,
1771 struct parse_state
*ps
)
1773 struct parse_state p
= *ps
;
1775 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1778 if (_parse_constant_expression(ctx
, &p
)) {
1779 _error(ctx
, "expected constant integral expression");
1782 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1783 _error(ctx
, "expected `]'");
1792 _parse_parameter_type_specifier(struct parse_context
*ctx
,
1793 struct parse_state
*ps
)
1795 struct parse_state p
= *ps
;
1798 if (_parse_type_specifier(ctx
, &p
)) {
1801 _emit(ctx
, &p
.out
, '\0');
1803 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1804 if (_parse_parameter_type_specifier_array(ctx
, &p
)) {
1805 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1813 _parse_parameter_declaration(struct parse_context
*ctx
,
1814 struct parse_state
*ps
)
1816 struct parse_state p
= *ps
;
1817 unsigned int e
= _emit(ctx
, &p
.out
, PARAMETER_NEXT
);
1821 if (_parse_storage_qualifier(ctx
, &p
)) {
1822 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1824 _parse_parameter_qualifier(ctx
, &p
);
1825 if (_parse_precision(ctx
, &p
)) {
1826 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1828 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1832 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1842 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1843 struct parse_state
*ps
)
1845 struct parse_state p
= *ps
;
1847 if (_parse_function_header(ctx
, &p
)) {
1850 if (_parse_parameter_declaration(ctx
, &p
)) {
1856 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1859 if (_parse_parameter_declaration(ctx
, &p
)) {
1867 _parse_function_declarator(struct parse_context
*ctx
,
1868 struct parse_state
*ps
)
1870 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1874 if (_parse_function_header(ctx
, ps
) == 0) {
1883 _parse_function_prototype(struct parse_context
*ctx
,
1884 struct parse_state
*ps
)
1886 struct parse_state p
= *ps
;
1888 if (_parse_function_header(ctx
, &p
) == 0) {
1889 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1890 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1891 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1895 _error(ctx
, "expected `)'");
1901 if (_parse_function_declarator(ctx
, &p
) == 0) {
1902 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1903 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1907 _error(ctx
, "expected `)'");
1916 _parse_precision(struct parse_context
*ctx
,
1917 struct parse_state
*ps
)
1919 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1921 unsigned int precision
;
1923 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1926 id
= input
->data
.identifier
;
1928 if (id
== ctx
->dict
.lowp
) {
1929 precision
= PRECISION_LOW
;
1930 } else if (id
== ctx
->dict
.mediump
) {
1931 precision
= PRECISION_MEDIUM
;
1932 } else if (id
== ctx
->dict
.highp
) {
1933 precision
= PRECISION_HIGH
;
1938 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1939 _emit(ctx
, &ps
->out
, precision
);
1945 _parse_prectype(struct parse_context
*ctx
,
1946 struct parse_state
*ps
)
1948 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1952 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1955 id
= input
->data
.identifier
;
1957 if (id
== ctx
->dict
._int
) {
1958 type
= TYPE_SPECIFIER_INT
;
1959 } else if (id
== ctx
->dict
._float
) {
1960 type
= TYPE_SPECIFIER_FLOAT
;
1961 } else if (id
== ctx
->dict
.sampler1D
) {
1962 type
= TYPE_SPECIFIER_SAMPLER1D
;
1963 } else if (id
== ctx
->dict
.sampler2D
) {
1964 type
= TYPE_SPECIFIER_SAMPLER2D
;
1965 } else if (id
== ctx
->dict
.sampler3D
) {
1966 type
= TYPE_SPECIFIER_SAMPLER3D
;
1967 } else if (id
== ctx
->dict
.samplerCube
) {
1968 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1969 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1970 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1971 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1972 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1973 } else if (id
== ctx
->dict
.sampler2DRect
) {
1974 if (!ctx
->texture_rectangle
) {
1975 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1976 "in order to use a rect sampler");
1979 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1980 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1981 if (!ctx
->texture_rectangle
) {
1982 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1983 "in order to use a rect sampler");
1986 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1987 } else if (id
== ctx
->dict
.sampler1DArray
) {
1988 type
= TYPE_SPECIFIER_SAMPLER_1D_ARRAY
;
1989 } else if (id
== ctx
->dict
.sampler2DArray
) {
1990 type
= TYPE_SPECIFIER_SAMPLER_2D_ARRAY
;
1991 } else if (id
== ctx
->dict
.sampler1DArrayShadow
) {
1992 type
= TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
;
1993 } else if (id
== ctx
->dict
.sampler2DArrayShadow
) {
1994 type
= TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
;
1999 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
2000 _emit(ctx
, &ps
->out
, type
);
2006 _parse_precision_stmt(struct parse_context
*ctx
,
2007 struct parse_state
*ps
)
2009 struct parse_state p
= *ps
;
2011 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
2014 if (_parse_precision(ctx
, &p
)) {
2017 if (_parse_prectype(ctx
, &p
)) {
2020 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2029 _parse_floatconstant(struct parse_context
*ctx
,
2030 struct parse_state
*ps
)
2032 struct parse_state p
= *ps
;
2034 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
2035 if (_parse_float(ctx
, &p
)) {
2044 _parse_intconstant(struct parse_context
*ctx
,
2045 struct parse_state
*ps
)
2047 struct parse_state p
= *ps
;
2049 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
2050 if (_parse_uint(ctx
, &p
)) {
2059 _parse_boolconstant(struct parse_context
*ctx
,
2060 struct parse_state
*ps
)
2062 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
2063 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2064 _emit(ctx
, &ps
->out
, 2); /* radix */
2065 _emit(ctx
, &ps
->out
, '0');
2066 _emit(ctx
, &ps
->out
, '\0');
2070 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
2071 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2072 _emit(ctx
, &ps
->out
, 2); /* radix */
2073 _emit(ctx
, &ps
->out
, '1');
2074 _emit(ctx
, &ps
->out
, '\0');
2083 _parse_variable_identifier(struct parse_context
*ctx
,
2084 struct parse_state
*ps
)
2086 struct parse_state p
= *ps
;
2088 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
2089 if (_parse_identifier(ctx
, &p
)) {
2098 _parse_primary_expression(struct parse_context
*ctx
,
2099 struct parse_state
*ps
)
2101 struct parse_state p
;
2103 if (_parse_floatconstant(ctx
, ps
) == 0) {
2106 if (_parse_boolconstant(ctx
, ps
) == 0) {
2109 if (_parse_intconstant(ctx
, ps
) == 0) {
2112 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2117 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2120 if (_parse_expression(ctx
, &p
)) {
2123 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2133 _parse_asm_argument(struct parse_context
*ctx
,
2134 struct parse_state
*ps
)
2136 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2137 struct parse_state p
= *ps
;
2139 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
2142 _emit(ctx
, &p
.out
, OP_FIELD
);
2143 if (_parse_identifier(ctx
, &p
)) {
2150 if (_parse_floatconstant(ctx
, ps
) == 0) {
2159 _parse_asm_arguments(struct parse_context
*ctx
,
2160 struct parse_state
*ps
)
2162 struct parse_state p
= *ps
;
2164 if (_parse_asm_argument(ctx
, &p
)) {
2167 _emit(ctx
, &p
.out
, OP_END
);
2171 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2174 if (_parse_asm_argument(ctx
, &p
)) {
2177 _emit(ctx
, &p
.out
, OP_END
);
2183 _parse_asm_statement(struct parse_context
*ctx
,
2184 struct parse_state
*ps
)
2186 struct parse_state p
= *ps
;
2188 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
2191 if (_parse_identifier(ctx
, &p
)) {
2194 /* optional arguments */
2195 _parse_asm_arguments(ctx
, &p
);
2196 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2199 _emit(ctx
, &p
.out
, OP_END
);
2206 _parse_selection_statement(struct parse_context
*ctx
,
2207 struct parse_state
*ps
)
2209 struct parse_state p
= *ps
;
2211 _emit(ctx
, &p
.out
, OP_IF
);
2212 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
2215 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2216 _error(ctx
, "expected `('");
2219 if (_parse_expression(ctx
, &p
)) {
2220 _error(ctx
, "expected an expression");
2223 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2224 _error(ctx
, "expected `)'");
2227 _emit(ctx
, &p
.out
, OP_END
);
2228 if (_parse_statement(ctx
, &p
)) {
2233 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
2234 if (_parse_statement(ctx
, &p
) == 0) {
2240 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
2241 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
2242 _emit(ctx
, &ps
->out
, OP_END
);
2248 _parse_expression_statement(struct parse_context
*ctx
,
2249 struct parse_state
*ps
)
2251 struct parse_state p
= *ps
;
2253 if (_parse_expression(ctx
, &p
)) {
2254 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2256 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2259 _emit(ctx
, &p
.out
, OP_END
);
2266 _parse_for_init_statement(struct parse_context
*ctx
,
2267 struct parse_state
*ps
)
2269 struct parse_state p
= *ps
;
2270 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2272 if (_parse_expression_statement(ctx
, &p
) == 0) {
2277 if (_parse_declaration(ctx
, &p
) == 0) {
2278 _update(ctx
, e
, OP_DECLARE
);
2288 _parse_initializer(struct parse_context
*ctx
,
2289 struct parse_state
*ps
)
2291 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2292 _emit(ctx
, &ps
->out
, OP_END
);
2300 _parse_condition_initializer(struct parse_context
*ctx
,
2301 struct parse_state
*ps
)
2303 struct parse_state p
= *ps
;
2305 _emit(ctx
, &p
.out
, OP_DECLARE
);
2306 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2307 if (_parse_fully_specified_type(ctx
, &p
)) {
2310 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2311 if (_parse_identifier(ctx
, &p
)) {
2314 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2315 _error(ctx
, "expected `='");
2318 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2319 if (_parse_initializer(ctx
, &p
)) {
2320 _error(ctx
, "expected an initialiser");
2323 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2330 _parse_condition(struct parse_context
*ctx
,
2331 struct parse_state
*ps
)
2333 struct parse_state p
;
2335 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2340 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2341 if (_parse_expression(ctx
, &p
) == 0) {
2342 _emit(ctx
, &p
.out
, OP_END
);
2352 _parse_for_rest_statement(struct parse_context
*ctx
,
2353 struct parse_state
*ps
)
2355 struct parse_state p
= *ps
;
2357 if (_parse_condition(ctx
, &p
)) {
2358 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2359 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2360 _emit(ctx
, &p
.out
, 2);
2361 _emit(ctx
, &p
.out
, '1');
2362 _emit(ctx
, &p
.out
, '\0');
2363 _emit(ctx
, &p
.out
, OP_END
);
2365 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2368 if (_parse_expression(ctx
, &p
)) {
2369 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2371 _emit(ctx
, &p
.out
, OP_END
);
2378 _parse_iteration_statement(struct parse_context
*ctx
,
2379 struct parse_state
*ps
)
2381 struct parse_state p
= *ps
;
2383 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2384 _emit(ctx
, &p
.out
, OP_WHILE
);
2385 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2386 _error(ctx
, "expected `('");
2389 if (_parse_condition(ctx
, &p
)) {
2390 _error(ctx
, "expected an expression");
2393 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2394 _error(ctx
, "expected `)'");
2397 if (_parse_statement(ctx
, &p
)) {
2404 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2405 _emit(ctx
, &p
.out
, OP_DO
);
2406 if (_parse_statement(ctx
, &p
)) {
2409 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2412 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2413 _error(ctx
, "expected `('");
2416 if (_parse_expression(ctx
, &p
)) {
2417 _error(ctx
, "expected an expression");
2420 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2421 _error(ctx
, "expected `)'");
2424 _emit(ctx
, &p
.out
, OP_END
);
2425 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2426 _error(ctx
, "expected `;'");
2433 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2434 _emit(ctx
, &p
.out
, OP_FOR
);
2435 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2436 _error(ctx
, "expected `('");
2439 if (_parse_for_init_statement(ctx
, &p
)) {
2442 if (_parse_for_rest_statement(ctx
, &p
)) {
2445 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2446 _error(ctx
, "expected `)'");
2449 if (_parse_statement(ctx
, &p
)) {
2461 _parse_jump_statement(struct parse_context
*ctx
,
2462 struct parse_state
*ps
)
2464 struct parse_state p
= *ps
;
2465 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2467 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2468 _update(ctx
, e
, OP_CONTINUE
);
2469 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2470 _update(ctx
, e
, OP_BREAK
);
2471 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2472 _update(ctx
, e
, OP_RETURN
);
2473 if (_parse_expression(ctx
, &p
)) {
2474 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2476 _emit(ctx
, &p
.out
, OP_END
);
2477 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2478 _update(ctx
, e
, OP_DISCARD
);
2482 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2491 _parse_simple_statement(struct parse_context
*ctx
,
2492 struct parse_state
*ps
)
2494 struct parse_state p
;
2497 if (_parse_selection_statement(ctx
, ps
) == 0) {
2501 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2505 if (_parse_jump_statement(ctx
, ps
) == 0) {
2510 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2511 if (_parse_expression_statement(ctx
, &p
) == 0) {
2516 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2517 _update(ctx
, e
, OP_PRECISION
);
2522 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2523 _update(ctx
, e
, OP_ASM
);
2528 if (_parse_declaration(ctx
, &p
) == 0) {
2529 _update(ctx
, e
, OP_DECLARE
);
2539 _parse_compound_statement(struct parse_context
*ctx
,
2540 struct parse_state
*ps
)
2542 struct parse_state p
= *ps
;
2544 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2547 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2548 _parse_statement_list(ctx
, &p
);
2549 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2552 _emit(ctx
, &p
.out
, OP_END
);
2559 _parse_statement(struct parse_context
*ctx
,
2560 struct parse_state
*ps
)
2562 if (_parse_compound_statement(ctx
, ps
) == 0) {
2566 if (_parse_simple_statement(ctx
, ps
) == 0) {
2575 _parse_statement_list(struct parse_context
*ctx
,
2576 struct parse_state
*ps
)
2578 struct parse_state p
= *ps
;
2580 if (_parse_statement(ctx
, &p
)) {
2586 if (_parse_statement(ctx
, &p
)) {
2594 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2595 struct parse_state
*ps
)
2597 struct parse_state p
= *ps
;
2599 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2602 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2603 _parse_statement_list(ctx
, &p
);
2604 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2607 _emit(ctx
, &p
.out
, OP_END
);
2614 _parse_function_definition(struct parse_context
*ctx
,
2615 struct parse_state
*ps
)
2617 struct parse_state p
= *ps
;
2619 if (_parse_function_prototype(ctx
, &p
)) {
2622 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2631 _parse_invariant_stmt(struct parse_context
*ctx
,
2632 struct parse_state
*ps
)
2634 struct parse_state p
= *ps
;
2636 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2639 if (_parse_identifier(ctx
, &p
)) {
2642 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2651 _parse_single_declaration(struct parse_context
*ctx
,
2652 struct parse_state
*ps
)
2654 struct parse_state p
= *ps
;
2657 if (_parse_fully_specified_type(ctx
, &p
)) {
2661 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2662 if (_parse_identifier(ctx
, &p
)) {
2663 _update(ctx
, e
, VARIABLE_NONE
);
2668 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2671 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2672 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2673 if (_parse_initializer(ctx
, &p
) == 0) {
2677 _error(ctx
, "expected an initialiser");
2682 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2683 if (_parse_constant_expression(ctx
, &p
)) {
2684 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2686 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2688 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2692 _error(ctx
, "expected `]'");
2700 _parse_init_declarator_list(struct parse_context
*ctx
,
2701 struct parse_state
*ps
)
2703 struct parse_state p
= *ps
;
2705 if (_parse_single_declaration(ctx
, &p
)) {
2713 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2716 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2717 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2718 if (_parse_identifier(ctx
, &p
)) {
2722 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2725 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2726 if (_parse_initializer(ctx
, &p
) == 0) {
2727 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2731 _error(ctx
, "expected an initialiser");
2736 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2739 if (_parse_constant_expression(ctx
, &p
)) {
2740 arr
= VARIABLE_ARRAY_UNKNOWN
;
2742 arr
= VARIABLE_ARRAY_EXPLICIT
;
2744 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2745 _update(ctx
, e
, arr
);
2749 _error(ctx
, "expected `]'");
2755 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2761 _parse_declaration(struct parse_context
*ctx
,
2762 struct parse_state
*ps
)
2764 struct parse_state p
= *ps
;
2765 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2767 if (_parse_function_prototype(ctx
, &p
)) {
2768 if (_parse_init_declarator_list(ctx
, &p
)) {
2771 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2773 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2774 _error(ctx
, "expected `;'");
2783 _parse_external_declaration(struct parse_context
*ctx
,
2784 struct parse_state
*ps
)
2786 struct parse_state p
= *ps
;
2787 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2789 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2790 _update(ctx
, e
, DEFAULT_PRECISION
);
2795 if (_parse_function_definition(ctx
, &p
) == 0) {
2796 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2801 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2802 _update(ctx
, e
, INVARIANT_STMT
);
2807 if (_parse_declaration(ctx
, &p
) == 0) {
2808 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2813 _error(ctx
, "expected an identifier");
2819 _parse_extensions(struct parse_context
*ctx
,
2820 struct parse_state
*ps
)
2823 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
2824 unsigned int enable
;
2830 switch (input
->token
) {
2831 case SL_PP_EXTENSION_REQUIRE
:
2832 case SL_PP_EXTENSION_ENABLE
:
2833 case SL_PP_EXTENSION_WARN
:
2836 case SL_PP_EXTENSION_DISABLE
:
2844 if (input
->data
.extension
== ctx
->dict
.all
) {
2845 ctx
->fragment_coord_conventions
= enable
;
2847 else if (input
->data
.extension
== ctx
->dict
._GL_ARB_fragment_coord_conventions
) {
2848 ctx
->fragment_coord_conventions
= enable
;
2850 else if (input
->data
.extension
== ctx
->dict
._GL_ARB_texture_rectangle
) {
2851 ctx
->texture_rectangle
= enable
;
2858 _parse_translation_unit(struct parse_context
*ctx
,
2859 struct parse_state
*ps
)
2861 _emit(ctx
, &ps
->out
, REVISION
);
2862 if (_parse_extensions(ctx
, ps
)) {
2865 if (_parse_external_declaration(ctx
, ps
)) {
2869 if (_parse_extensions(ctx
, ps
)) {
2872 if (_parse_external_declaration(ctx
, ps
)) {
2876 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2877 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2884 #define ADD_NAME_STR(CTX, NAME, STR)\
2886 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2887 if ((CTX).dict.NAME == -1) {\
2892 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2896 sl_cl_compile(struct sl_pp_context
*context
,
2897 unsigned int shader_type
,
2898 unsigned int parsing_builtin
,
2899 unsigned char **output
,
2900 unsigned int *cboutput
,
2902 unsigned int cberror
)
2904 struct parse_context ctx
;
2905 struct parse_state ps
;
2907 ctx
.context
= context
;
2909 ADD_NAME_STR(ctx
, _void
, "void");
2910 ADD_NAME_STR(ctx
, _float
, "float");
2911 ADD_NAME_STR(ctx
, _int
, "int");
2912 ADD_NAME_STR(ctx
, _bool
, "bool");
2913 ADD_NAME(ctx
, vec2
);
2914 ADD_NAME(ctx
, vec3
);
2915 ADD_NAME(ctx
, vec4
);
2916 ADD_NAME(ctx
, bvec2
);
2917 ADD_NAME(ctx
, bvec3
);
2918 ADD_NAME(ctx
, bvec4
);
2919 ADD_NAME(ctx
, ivec2
);
2920 ADD_NAME(ctx
, ivec3
);
2921 ADD_NAME(ctx
, ivec4
);
2922 ADD_NAME(ctx
, mat2
);
2923 ADD_NAME(ctx
, mat3
);
2924 ADD_NAME(ctx
, mat4
);
2925 ADD_NAME(ctx
, mat2x3
);
2926 ADD_NAME(ctx
, mat3x2
);
2927 ADD_NAME(ctx
, mat2x4
);
2928 ADD_NAME(ctx
, mat4x2
);
2929 ADD_NAME(ctx
, mat3x4
);
2930 ADD_NAME(ctx
, mat4x3
);
2931 ADD_NAME(ctx
, sampler1D
);
2932 ADD_NAME(ctx
, sampler2D
);
2933 ADD_NAME(ctx
, sampler3D
);
2934 ADD_NAME(ctx
, samplerCube
);
2935 ADD_NAME(ctx
, sampler1DShadow
);
2936 ADD_NAME(ctx
, sampler2DShadow
);
2937 ADD_NAME(ctx
, sampler2DRect
);
2938 ADD_NAME(ctx
, sampler2DRectShadow
);
2939 ADD_NAME(ctx
, sampler1DArray
);
2940 ADD_NAME(ctx
, sampler2DArray
);
2941 ADD_NAME(ctx
, sampler1DArrayShadow
);
2942 ADD_NAME(ctx
, sampler2DArrayShadow
);
2944 ADD_NAME(ctx
, invariant
);
2946 ADD_NAME(ctx
, centroid
);
2948 ADD_NAME(ctx
, precision
);
2949 ADD_NAME(ctx
, lowp
);
2950 ADD_NAME(ctx
, mediump
);
2951 ADD_NAME(ctx
, highp
);
2953 ADD_NAME_STR(ctx
, _const
, "const");
2954 ADD_NAME(ctx
, attribute
);
2955 ADD_NAME(ctx
, varying
);
2956 ADD_NAME(ctx
, uniform
);
2957 ADD_NAME(ctx
, __fixed_output
);
2958 ADD_NAME(ctx
, __fixed_input
);
2962 ADD_NAME(ctx
, inout
);
2964 ADD_NAME(ctx
, layout
);
2965 ADD_NAME(ctx
, origin_upper_left
);
2966 ADD_NAME(ctx
, pixel_center_integer
);
2968 ADD_NAME_STR(ctx
, _struct
, "struct");
2970 ADD_NAME(ctx
, __constructor
);
2971 ADD_NAME(ctx
, __operator
);
2972 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2974 ADD_NAME_STR(ctx
, _if
, "if");
2975 ADD_NAME_STR(ctx
, _else
, "else");
2976 ADD_NAME_STR(ctx
, _for
, "for");
2977 ADD_NAME_STR(ctx
, _while
, "while");
2978 ADD_NAME_STR(ctx
, _do
, "do");
2980 ADD_NAME_STR(ctx
, _continue
, "continue");
2981 ADD_NAME_STR(ctx
, _break
, "break");
2982 ADD_NAME_STR(ctx
, _return
, "return");
2983 ADD_NAME(ctx
, discard
);
2985 ADD_NAME_STR(ctx
, _false
, "false");
2986 ADD_NAME_STR(ctx
, _true
, "true");
2989 ADD_NAME_STR(ctx
, _GL_ARB_fragment_coord_conventions
, "GL_ARB_fragment_coord_conventions");
2990 ADD_NAME_STR(ctx
, _GL_ARB_texture_rectangle
, "GL_ARB_texture_rectangle");
2995 ctx
.shader_type
= shader_type
;
2996 ctx
.parsing_builtin
= 1;
2998 ctx
.fragment_coord_conventions
= 0;
2999 ctx
.texture_rectangle
= 1;
3001 ctx
.error
[0] = '\0';
3002 ctx
.process_error
= 0;
3004 ctx
.tokens_cap
= 1024;
3005 ctx
.tokens_read
= 0;
3006 ctx
.tokens
= malloc(ctx
.tokens_cap
* sizeof(struct sl_pp_token_info
));
3008 strncpy(error
, "out of memory", cberror
- 1);
3009 error
[cberror
- 1] = '\0';
3016 if (_parse_translation_unit(&ctx
, &ps
)) {
3017 strncpy(error
, ctx
.error
, cberror
);
3022 *output
= ctx
.out_buf
;