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 /* type specifier array */
165 #define TYPE_SPECIFIER_NONARRAY 0
166 #define TYPE_SPECIFIER_ARRAY 1
168 /* structure field */
171 #define FIELD_ARRAY 2
175 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
176 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
180 #define OP_CONTINUE 6
183 #define OP_EXPRESSION 9
188 #define OP_PUSH_VOID 14
189 #define OP_PUSH_BOOL 15
190 #define OP_PUSH_INT 16
191 #define OP_PUSH_FLOAT 17
192 #define OP_PUSH_IDENTIFIER 18
193 #define OP_SEQUENCE 19
195 #define OP_ADDASSIGN 21
196 #define OP_SUBASSIGN 22
197 #define OP_MULASSIGN 23
198 #define OP_DIVASSIGN 24
199 /*#define OP_MODASSIGN 25*/
200 /*#define OP_LSHASSIGN 26*/
201 /*#define OP_RSHASSIGN 27*/
202 /*#define OP_ORASSIGN 28*/
203 /*#define OP_XORASSIGN 29*/
204 /*#define OP_ANDASSIGN 30*/
206 #define OP_LOGICALOR 32
207 #define OP_LOGICALXOR 33
208 #define OP_LOGICALAND 34
209 /*#define OP_BITOR 35*/
210 /*#define OP_BITXOR 36*/
211 /*#define OP_BITAND 37*/
213 #define OP_NOTEQUAL 39
215 #define OP_GREATER 41
216 #define OP_LESSEQUAL 42
217 #define OP_GREATEREQUAL 43
218 /*#define OP_LSHIFT 44*/
219 /*#define OP_RSHIFT 45*/
221 #define OP_SUBTRACT 47
222 #define OP_MULTIPLY 48
224 /*#define OP_MODULUS 50*/
225 #define OP_PREINCREMENT 51
226 #define OP_PREDECREMENT 52
229 /*#define OP_COMPLEMENT 55*/
231 #define OP_SUBSCRIPT 57
234 #define OP_POSTINCREMENT 60
235 #define OP_POSTDECREMENT 61
236 #define OP_PRECISION 62
239 /* parameter qualifier */
240 #define PARAM_QUALIFIER_IN 0
241 #define PARAM_QUALIFIER_OUT 1
242 #define PARAM_QUALIFIER_INOUT 2
244 /* function parameter */
245 #define PARAMETER_NONE 0
246 #define PARAMETER_NEXT 1
248 /* function parameter array presence */
249 #define PARAMETER_ARRAY_NOT_PRESENT 0
250 #define PARAMETER_ARRAY_PRESENT 1
283 int sampler2DRectShadow
;
306 int origin_upper_left
;
307 int pixel_center_integer
;
330 int _GL_ARB_fragment_coord_conventions
;
334 struct parse_context
{
335 struct sl_pp_context
*context
;
337 struct parse_dict dict
;
339 struct sl_pp_token_info
*tokens
;
340 unsigned int tokens_read
;
341 unsigned int tokens_cap
;
343 unsigned char *out_buf
;
344 unsigned int out_cap
;
346 unsigned int shader_type
;
347 unsigned int parsing_builtin
;
349 unsigned int fragment_coord_conventions
:1;
363 _emit(struct parse_context
*ctx
,
367 if (*out
== ctx
->out_cap
) {
368 ctx
->out_cap
+= 4096;
369 ctx
->out_buf
= (unsigned char *)realloc(ctx
->out_buf
, ctx
->out_cap
* sizeof(unsigned char));
371 ctx
->out_buf
[*out
] = b
;
377 _update(struct parse_context
*ctx
,
381 ctx
->out_buf
[out
] = b
;
386 _error(struct parse_context
*ctx
,
389 if (ctx
->error
[0] == '\0') {
390 strcpy(ctx
->error
, msg
);
395 static const struct sl_pp_token_info
*
396 _fetch_token(struct parse_context
*ctx
,
399 if (ctx
->process_error
) {
403 while (pos
>= ctx
->tokens_read
) {
404 if (ctx
->tokens_read
== ctx
->tokens_cap
) {
405 ctx
->tokens_cap
+= 1024;
406 ctx
->tokens
= realloc(ctx
->tokens
,
407 ctx
->tokens_cap
* sizeof(struct sl_pp_token_info
));
409 _error(ctx
, "out of memory");
410 ctx
->process_error
= 1;
414 if (sl_pp_process_get(ctx
->context
, &ctx
->tokens
[ctx
->tokens_read
])) {
415 _error(ctx
, sl_pp_context_error_message(ctx
->context
));
416 ctx
->process_error
= 1;
419 switch (ctx
->tokens
[ctx
->tokens_read
].token
) {
421 case SL_PP_SEMICOLON
:
429 case SL_PP_INCREMENT
:
430 case SL_PP_ADDASSIGN
:
432 case SL_PP_DECREMENT
:
433 case SL_PP_SUBASSIGN
:
438 case SL_PP_MULASSIGN
:
440 case SL_PP_DIVASSIGN
:
442 case SL_PP_MODASSIGN
:
444 case SL_PP_LSHIFTASSIGN
:
446 case SL_PP_LESSEQUAL
:
448 case SL_PP_RSHIFTASSIGN
:
450 case SL_PP_GREATEREQUAL
:
455 case SL_PP_BITANDASSIGN
:
458 case SL_PP_BITXORASSIGN
:
461 case SL_PP_BITORASSIGN
:
465 case SL_PP_IDENTIFIER
:
468 case SL_PP_EXTENSION_REQUIRE
:
469 case SL_PP_EXTENSION_ENABLE
:
470 case SL_PP_EXTENSION_WARN
:
471 case SL_PP_EXTENSION_DISABLE
:
479 return &ctx
->tokens
[pos
];
484 * Try to parse/match a particular token.
485 * \return 0 for success, -1 for error.
488 _parse_token(struct parse_context
*ctx
,
489 enum sl_pp_token token
,
490 struct parse_state
*ps
)
492 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
494 if (input
&& input
->token
== token
) {
503 * Try to parse an identifer.
504 * \return 0 for success, -1 for error
507 _parse_id(struct parse_context
*ctx
,
509 struct parse_state
*ps
)
511 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
513 if (input
&& input
->token
== SL_PP_IDENTIFIER
&& input
->data
.identifier
== id
) {
522 _parse_identifier(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
) {
528 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
.identifier
);
531 _emit(ctx
, &ps
->out
, *cstr
);
541 _parse_float(struct parse_context
*ctx
,
542 struct parse_state
*ps
)
544 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
546 if (input
&& input
->token
== SL_PP_FLOAT
) {
547 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._float
);
549 _emit(ctx
, &ps
->out
, 1);
551 _emit(ctx
, &ps
->out
, *cstr
);
561 _parse_uint(struct parse_context
*ctx
,
562 struct parse_state
*ps
)
564 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
566 if (input
&& input
->token
== SL_PP_UINT
) {
567 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._uint
);
569 _emit(ctx
, &ps
->out
, 1);
571 _emit(ctx
, &ps
->out
, *cstr
);
580 /**************************************/
584 _parse_unary_expression(struct parse_context
*ctx
,
585 struct parse_state
*ps
);
588 _parse_conditional_expression(struct parse_context
*ctx
,
589 struct parse_state
*ps
);
593 _parse_constant_expression(struct parse_context
*ctx
,
594 struct parse_state
*ps
);
598 _parse_primary_expression(struct parse_context
*ctx
,
599 struct parse_state
*ps
);
603 _parse_statement(struct parse_context
*ctx
,
604 struct parse_state
*ps
);
608 _parse_type_specifier(struct parse_context
*ctx
,
609 struct parse_state
*ps
);
613 _parse_declaration(struct parse_context
*ctx
,
614 struct parse_state
*ps
);
618 _parse_statement_list(struct parse_context
*ctx
,
619 struct parse_state
*ps
);
623 _parse_assignment_expression(struct parse_context
*ctx
,
624 struct parse_state
*ps
);
628 _parse_precision(struct parse_context
*ctx
,
629 struct parse_state
*ps
);
633 _parse_overriden_operator(struct parse_context
*ctx
,
634 struct parse_state
*ps
)
638 if (_parse_token(ctx
, SL_PP_INCREMENT
, ps
) == 0) {
639 op
= OPERATOR_INCREMENT
;
640 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, ps
) == 0) {
641 op
= OPERATOR_ADDASSIGN
;
642 } else if (_parse_token(ctx
, SL_PP_PLUS
, ps
) == 0) {
644 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, ps
) == 0) {
645 op
= OPERATOR_DECREMENT
;
646 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, ps
) == 0) {
647 op
= OPERATOR_SUBASSIGN
;
648 } else if (_parse_token(ctx
, SL_PP_MINUS
, ps
) == 0) {
650 } else if (_parse_token(ctx
, SL_PP_NOT
, ps
) == 0) {
652 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, ps
) == 0) {
653 op
= OPERATOR_MULASSIGN
;
654 } else if (_parse_token(ctx
, SL_PP_STAR
, ps
) == 0) {
655 op
= OPERATOR_MULTIPLY
;
656 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, ps
) == 0) {
657 op
= OPERATOR_DIVASSIGN
;
658 } else if (_parse_token(ctx
, SL_PP_SLASH
, ps
) == 0) {
659 op
= OPERATOR_DIVIDE
;
660 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, ps
) == 0) {
661 op
= OPERATOR_LESSEQUAL
;
662 } else if (_parse_token(ctx
, SL_PP_LESS
, ps
) == 0) {
664 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, ps
) == 0) {
665 op
= OPERATOR_GREATEREQUAL
;
666 } else if (_parse_token(ctx
, SL_PP_GREATER
, ps
) == 0) {
667 op
= OPERATOR_GREATER
;
668 } else if (_parse_token(ctx
, SL_PP_XOR
, ps
) == 0) {
669 op
= OPERATOR_LOGICALXOR
;
674 _emit(ctx
, &ps
->out
, op
);
680 _parse_function_decl_identifier(struct parse_context
*ctx
,
681 struct parse_state
*ps
)
683 struct parse_state p
= *ps
;
684 unsigned int e
= _emit(ctx
, &p
.out
, 0);
686 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__constructor
, &p
) == 0) {
687 _update(ctx
, e
, FUNCTION_CONSTRUCTOR
);
692 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__operator
, &p
) == 0) {
693 _update(ctx
, e
, FUNCTION_OPERATOR
);
694 if (_parse_overriden_operator(ctx
, &p
) == 0) {
701 if (_parse_identifier(ctx
, &p
) == 0) {
702 _update(ctx
, e
, FUNCTION_ORDINARY
);
712 _parse_invariant_qualifier(struct parse_context
*ctx
,
713 struct parse_state
*ps
)
715 if (_parse_id(ctx
, ctx
->dict
.invariant
, ps
)) {
718 _emit(ctx
, &ps
->out
, TYPE_INVARIANT
);
724 _parse_centroid_qualifier(struct parse_context
*ctx
,
725 struct parse_state
*ps
)
727 if (_parse_id(ctx
, ctx
->dict
.centroid
, ps
)) {
730 _emit(ctx
, &ps
->out
, TYPE_CENTROID
);
736 _parse_layout_qualifier(struct parse_context
*ctx
,
737 struct parse_state
*ps
)
739 if (_parse_id(ctx
, ctx
->dict
.layout
, ps
) == 0) {
740 if (!ctx
->fragment_coord_conventions
) {
741 _error(ctx
, "GL_ARB_fragment_coord_conventions extension must be enabled "
742 "in order to use a layout qualifier");
746 /* Layout qualifiers are only defined for fragment shaders,
747 * so do an early check.
749 if (ctx
->shader_type
!= 1) {
750 _error(ctx
, "layout qualifiers are only valid for fragment shaders");
754 /* start of a parenthesised list of layout qualifiers */
756 if (_parse_token(ctx
, SL_PP_LPAREN
, ps
)) {
757 _error(ctx
, "expected `('");
761 /* parse comma-separated ID list */
763 if (_parse_id(ctx
, ctx
->dict
.origin_upper_left
, ps
) == 0) {
764 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_UPPER_LEFT
);
766 else if (_parse_id(ctx
, ctx
->dict
.pixel_center_integer
, ps
) == 0) {
767 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER
);
770 _error(ctx
, "expected a layout qualifier name");
774 if (_parse_token(ctx
, SL_PP_RPAREN
, ps
) == 0) {
778 else if (_parse_token(ctx
, SL_PP_COMMA
, ps
) == 0) {
779 /* another layout qualifier is coming */
782 _error(ctx
, "expected `,' or `)'");
793 _parse_storage_qualifier(struct parse_context
*ctx
,
794 struct parse_state
*ps
)
796 struct parse_state p
= *ps
;
797 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, p
.in
);
798 unsigned int e
= _emit(ctx
, &p
.out
, 0);
801 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
804 id
= input
->data
.identifier
;
806 if (id
== ctx
->dict
._const
) {
807 _update(ctx
, e
, TYPE_QUALIFIER_CONST
);
808 } else if (ctx
->shader_type
== 2 && id
== ctx
->dict
.attribute
) {
809 _update(ctx
, e
, TYPE_QUALIFIER_ATTRIBUTE
);
810 } else if (id
== ctx
->dict
.varying
) {
811 _update(ctx
, e
, TYPE_QUALIFIER_VARYING
);
812 } else if (id
== ctx
->dict
.uniform
) {
813 _update(ctx
, e
, TYPE_QUALIFIER_UNIFORM
);
814 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_output
) {
815 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDOUTPUT
);
816 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_input
) {
817 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDINPUT
);
821 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
828 _parse_struct_declarator(struct parse_context
*ctx
,
829 struct parse_state
*ps
)
831 struct parse_state p
= *ps
;
834 if (_parse_identifier(ctx
, &p
)) {
837 e
= _emit(ctx
, &p
.out
, FIELD_NONE
);
840 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
843 if (_parse_constant_expression(ctx
, &p
)) {
844 _error(ctx
, "expected constant integral expression");
847 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
848 _error(ctx
, "expected `]'");
851 _update(ctx
, e
, FIELD_ARRAY
);
858 _parse_struct_declarator_list(struct parse_context
*ctx
,
859 struct parse_state
*ps
)
861 struct parse_state p
= *ps
;
863 if (_parse_struct_declarator(ctx
, &p
)) {
869 _emit(ctx
, &p
.out
, FIELD_NEXT
);
870 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
873 if (_parse_struct_declarator(ctx
, &p
)) {
881 _parse_struct_declaration(struct parse_context
*ctx
,
882 struct parse_state
*ps
)
884 struct parse_state p
= *ps
;
886 if (_parse_type_specifier(ctx
, &p
)) {
889 if (_parse_struct_declarator_list(ctx
, &p
)) {
892 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
895 _emit(ctx
, &p
.out
, FIELD_NONE
);
902 _parse_struct_declaration_list(struct parse_context
*ctx
,
903 struct parse_state
*ps
)
905 struct parse_state p
= *ps
;
907 if (_parse_struct_declaration(ctx
, &p
)) {
913 _emit(ctx
, &p
.out
, FIELD_NEXT
);
914 if (_parse_struct_declaration(ctx
, &p
)) {
922 _parse_struct_specifier(struct parse_context
*ctx
,
923 struct parse_state
*ps
)
925 struct parse_state p
= *ps
;
927 if (_parse_id(ctx
, ctx
->dict
._struct
, &p
)) {
930 if (_parse_identifier(ctx
, &p
)) {
931 _emit(ctx
, &p
.out
, '\0');
933 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
934 _error(ctx
, "expected `{'");
937 if (_parse_struct_declaration_list(ctx
, &p
)) {
940 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
943 _emit(ctx
, &p
.out
, FIELD_NONE
);
950 _parse_type_specifier_nonarray(struct parse_context
*ctx
,
951 struct parse_state
*ps
)
953 struct parse_state p
= *ps
;
954 unsigned int e
= _emit(ctx
, &p
.out
, 0);
955 const struct sl_pp_token_info
*input
;
958 if (_parse_struct_specifier(ctx
, &p
) == 0) {
959 _update(ctx
, e
, TYPE_SPECIFIER_STRUCT
);
964 input
= _fetch_token(ctx
, p
.in
);
965 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
968 id
= input
->data
.identifier
;
970 if (id
== ctx
->dict
._void
) {
971 _update(ctx
, e
, TYPE_SPECIFIER_VOID
);
972 } else if (id
== ctx
->dict
._float
) {
973 _update(ctx
, e
, TYPE_SPECIFIER_FLOAT
);
974 } else if (id
== ctx
->dict
._int
) {
975 _update(ctx
, e
, TYPE_SPECIFIER_INT
);
976 } else if (id
== ctx
->dict
._bool
) {
977 _update(ctx
, e
, TYPE_SPECIFIER_BOOL
);
978 } else if (id
== ctx
->dict
.vec2
) {
979 _update(ctx
, e
, TYPE_SPECIFIER_VEC2
);
980 } else if (id
== ctx
->dict
.vec3
) {
981 _update(ctx
, e
, TYPE_SPECIFIER_VEC3
);
982 } else if (id
== ctx
->dict
.vec4
) {
983 _update(ctx
, e
, TYPE_SPECIFIER_VEC4
);
984 } else if (id
== ctx
->dict
.bvec2
) {
985 _update(ctx
, e
, TYPE_SPECIFIER_BVEC2
);
986 } else if (id
== ctx
->dict
.bvec3
) {
987 _update(ctx
, e
, TYPE_SPECIFIER_BVEC3
);
988 } else if (id
== ctx
->dict
.bvec4
) {
989 _update(ctx
, e
, TYPE_SPECIFIER_BVEC4
);
990 } else if (id
== ctx
->dict
.ivec2
) {
991 _update(ctx
, e
, TYPE_SPECIFIER_IVEC2
);
992 } else if (id
== ctx
->dict
.ivec3
) {
993 _update(ctx
, e
, TYPE_SPECIFIER_IVEC3
);
994 } else if (id
== ctx
->dict
.ivec4
) {
995 _update(ctx
, e
, TYPE_SPECIFIER_IVEC4
);
996 } else if (id
== ctx
->dict
.mat2
) {
997 _update(ctx
, e
, TYPE_SPECIFIER_MAT2
);
998 } else if (id
== ctx
->dict
.mat3
) {
999 _update(ctx
, e
, TYPE_SPECIFIER_MAT3
);
1000 } else if (id
== ctx
->dict
.mat4
) {
1001 _update(ctx
, e
, TYPE_SPECIFIER_MAT4
);
1002 } else if (id
== ctx
->dict
.mat2x3
) {
1003 _update(ctx
, e
, TYPE_SPECIFIER_MAT23
);
1004 } else if (id
== ctx
->dict
.mat3x2
) {
1005 _update(ctx
, e
, TYPE_SPECIFIER_MAT32
);
1006 } else if (id
== ctx
->dict
.mat2x4
) {
1007 _update(ctx
, e
, TYPE_SPECIFIER_MAT24
);
1008 } else if (id
== ctx
->dict
.mat4x2
) {
1009 _update(ctx
, e
, TYPE_SPECIFIER_MAT42
);
1010 } else if (id
== ctx
->dict
.mat3x4
) {
1011 _update(ctx
, e
, TYPE_SPECIFIER_MAT34
);
1012 } else if (id
== ctx
->dict
.mat4x3
) {
1013 _update(ctx
, e
, TYPE_SPECIFIER_MAT43
);
1014 } else if (id
== ctx
->dict
.sampler1D
) {
1015 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1D
);
1016 } else if (id
== ctx
->dict
.sampler2D
) {
1017 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2D
);
1018 } else if (id
== ctx
->dict
.sampler3D
) {
1019 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER3D
);
1020 } else if (id
== ctx
->dict
.samplerCube
) {
1021 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLERCUBE
);
1022 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1023 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1DSHADOW
);
1024 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1025 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DSHADOW
);
1026 } else if (id
== ctx
->dict
.sampler2DRect
) {
1027 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECT
);
1028 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1029 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
);
1030 } else if (_parse_identifier(ctx
, &p
) == 0) {
1031 _update(ctx
, e
, TYPE_SPECIFIER_TYPENAME
);
1038 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
1045 _parse_type_specifier_array(struct parse_context
*ctx
,
1046 struct parse_state
*ps
)
1048 struct parse_state p
= *ps
;
1050 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1053 if (_parse_constant_expression(ctx
, &p
)) {
1054 _error(ctx
, "expected constant integral expression");
1057 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1058 _error(ctx
, "expected `]'");
1067 _parse_type_specifier(struct parse_context
*ctx
,
1068 struct parse_state
*ps
)
1070 struct parse_state p
= *ps
;
1073 if (_parse_type_specifier_nonarray(ctx
, &p
)) {
1077 e
= _emit(ctx
, &p
.out
, TYPE_SPECIFIER_ARRAY
);
1078 if (_parse_type_specifier_array(ctx
, &p
)) {
1079 _update(ctx
, e
, TYPE_SPECIFIER_NONARRAY
);
1087 _parse_fully_specified_type(struct parse_context
*ctx
,
1088 struct parse_state
*ps
)
1090 struct parse_state p
= *ps
;
1092 if (_parse_layout_qualifier(ctx
, &p
)) {
1095 _emit(ctx
, &p
.out
, LAYOUT_QUALIFIER_NONE
);
1097 if (_parse_invariant_qualifier(ctx
, &p
)) {
1098 _emit(ctx
, &p
.out
, TYPE_VARIANT
);
1101 if (_parse_centroid_qualifier(ctx
, &p
)) {
1102 _emit(ctx
, &p
.out
, TYPE_CENTER
);
1104 if (_parse_storage_qualifier(ctx
, &p
)) {
1105 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1107 if (_parse_precision(ctx
, &p
)) {
1108 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1110 if (_parse_type_specifier(ctx
, &p
)) {
1119 _parse_function_header(struct parse_context
*ctx
,
1120 struct parse_state
*ps
)
1122 struct parse_state p
= *ps
;
1124 if (_parse_fully_specified_type(ctx
, &p
)) {
1127 if (_parse_function_decl_identifier(ctx
, &p
)) {
1130 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1139 _parse_parameter_qualifier(struct parse_context
*ctx
,
1140 struct parse_state
*ps
)
1142 unsigned int e
= _emit(ctx
, &ps
->out
, PARAM_QUALIFIER_IN
);
1144 if (_parse_id(ctx
, ctx
->dict
.out
, ps
) == 0) {
1145 _update(ctx
, e
, PARAM_QUALIFIER_OUT
);
1146 } else if (_parse_id(ctx
, ctx
->dict
.inout
, ps
) == 0) {
1147 _update(ctx
, e
, PARAM_QUALIFIER_INOUT
);
1149 _parse_id(ctx
, ctx
->dict
.in
, ps
);
1156 _parse_function_identifier(struct parse_context
*ctx
,
1157 struct parse_state
*ps
)
1159 struct parse_state p
;
1162 if (_parse_identifier(ctx
, ps
)) {
1165 e
= _emit(ctx
, &ps
->out
, FUNCTION_CALL_NONARRAY
);
1168 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1171 if (_parse_constant_expression(ctx
, &p
)) {
1172 _error(ctx
, "expected constant integral expression");
1175 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1176 _error(ctx
, "expected `]'");
1179 _update(ctx
, e
, FUNCTION_CALL_ARRAY
);
1186 _parse_function_call_header(struct parse_context
*ctx
,
1187 struct parse_state
*ps
)
1189 struct parse_state p
= *ps
;
1191 if (_parse_function_identifier(ctx
, &p
)) {
1194 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1203 _parse_assign_expression(struct parse_context
*ctx
,
1204 struct parse_state
*ps
)
1206 struct parse_state p
= *ps
;
1209 if (_parse_unary_expression(ctx
, &p
)) {
1213 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
1215 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, &p
) == 0) {
1217 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, &p
) == 0) {
1219 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, &p
) == 0) {
1221 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, &p
) == 0) {
1227 if (_parse_assignment_expression(ctx
, &p
)) {
1230 _emit(ctx
, &p
.out
, op
);
1238 _parse_assignment_expression(struct parse_context
*ctx
,
1239 struct parse_state
*ps
)
1241 if (_parse_assign_expression(ctx
, ps
) == 0) {
1245 if (_parse_conditional_expression(ctx
, ps
) == 0) {
1254 _parse_function_call_header_with_parameters(struct parse_context
*ctx
,
1255 struct parse_state
*ps
)
1257 struct parse_state p
= *ps
;
1259 if (_parse_function_call_header(ctx
, &p
)) {
1262 if (_parse_assignment_expression(ctx
, &p
)) {
1265 _emit(ctx
, &p
.out
, OP_END
);
1268 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1271 if (_parse_assignment_expression(ctx
, &p
)) {
1274 _emit(ctx
, &p
.out
, OP_END
);
1280 _parse_function_call_header_no_parameters(struct parse_context
*ctx
,
1281 struct parse_state
*ps
)
1283 if (_parse_function_call_header(ctx
, ps
)) {
1286 _parse_id(ctx
, ctx
->dict
._void
, ps
);
1292 _parse_function_call_generic(struct parse_context
*ctx
,
1293 struct parse_state
*ps
)
1295 struct parse_state p
= *ps
;
1297 if (_parse_function_call_header_with_parameters(ctx
, &p
) == 0) {
1298 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1302 _error(ctx
, "expected `)'");
1307 if (_parse_function_call_header_no_parameters(ctx
, &p
) == 0) {
1308 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1312 _error(ctx
, "expected `)'");
1321 _parse_method_call(struct parse_context
*ctx
,
1322 struct parse_state
*ps
)
1324 struct parse_state p
= *ps
;
1326 _emit(ctx
, &p
.out
, OP_METHOD
);
1327 if (_parse_identifier(ctx
, &p
)) {
1330 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1333 if (_parse_function_call_generic(ctx
, &p
)) {
1336 _emit(ctx
, &p
.out
, OP_END
);
1343 _parse_regular_function_call(struct parse_context
*ctx
,
1344 struct parse_state
*ps
)
1346 struct parse_state p
= *ps
;
1348 _emit(ctx
, &p
.out
, OP_CALL
);
1349 if (_parse_function_call_generic(ctx
, &p
)) {
1352 _emit(ctx
, &p
.out
, OP_END
);
1359 _parse_function_call(struct parse_context
*ctx
,
1360 struct parse_state
*ps
)
1362 if (_parse_regular_function_call(ctx
, ps
) == 0) {
1366 if (_parse_method_call(ctx
, ps
) == 0) {
1375 _parse_expression(struct parse_context
*ctx
,
1376 struct parse_state
*ps
)
1378 struct parse_state p
= *ps
;
1380 if (_parse_assignment_expression(ctx
, &p
)) {
1386 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1389 if (_parse_assignment_expression(ctx
, &p
)) {
1392 _emit(ctx
, &p
.out
, OP_SEQUENCE
);
1398 _parse_postfix_expression(struct parse_context
*ctx
,
1399 struct parse_state
*ps
)
1401 struct parse_state p
;
1403 if (_parse_function_call(ctx
, ps
)) {
1404 if (_parse_primary_expression(ctx
, ps
)) {
1411 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1412 _emit(ctx
, &p
.out
, OP_POSTINCREMENT
);
1413 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1414 _emit(ctx
, &p
.out
, OP_POSTDECREMENT
);
1415 } else if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
1416 if (_parse_expression(ctx
, &p
)) {
1417 _error(ctx
, "expected an integral expression");
1420 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1421 _error(ctx
, "expected `]'");
1424 _emit(ctx
, &p
.out
, OP_SUBSCRIPT
);
1425 } else if (_parse_token(ctx
, SL_PP_DOT
, &p
) == 0) {
1426 _emit(ctx
, &p
.out
, OP_FIELD
);
1427 if (_parse_identifier(ctx
, &p
)) {
1438 _parse_unary_expression(struct parse_context
*ctx
,
1439 struct parse_state
*ps
)
1441 struct parse_state p
;
1444 if (_parse_postfix_expression(ctx
, ps
) == 0) {
1449 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1450 op
= OP_PREINCREMENT
;
1451 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1452 op
= OP_PREDECREMENT
;
1453 } else if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1455 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1457 } else if (_parse_token(ctx
, SL_PP_NOT
, &p
) == 0) {
1463 if (_parse_unary_expression(ctx
, &p
)) {
1466 _emit(ctx
, &p
.out
, op
);
1473 _parse_multiplicative_expression(struct parse_context
*ctx
,
1474 struct parse_state
*ps
)
1476 struct parse_state p
= *ps
;
1478 if (_parse_unary_expression(ctx
, &p
)) {
1485 if (_parse_token(ctx
, SL_PP_STAR
, &p
) == 0) {
1487 } else if (_parse_token(ctx
, SL_PP_SLASH
, &p
) == 0) {
1492 if (_parse_unary_expression(ctx
, &p
)) {
1495 _emit(ctx
, &p
.out
, op
);
1501 _parse_additive_expression(struct parse_context
*ctx
,
1502 struct parse_state
*ps
)
1504 struct parse_state p
= *ps
;
1506 if (_parse_multiplicative_expression(ctx
, &p
)) {
1513 if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1515 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1520 if (_parse_multiplicative_expression(ctx
, &p
)) {
1523 _emit(ctx
, &p
.out
, op
);
1529 _parse_relational_expression(struct parse_context
*ctx
,
1530 struct parse_state
*ps
)
1532 struct parse_state p
= *ps
;
1534 if (_parse_additive_expression(ctx
, &p
)) {
1541 if (_parse_token(ctx
, SL_PP_LESS
, &p
) == 0) {
1543 } else if (_parse_token(ctx
, SL_PP_GREATER
, &p
) == 0) {
1545 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, &p
) == 0) {
1547 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, &p
) == 0) {
1548 op
= OP_GREATEREQUAL
;
1552 if (_parse_additive_expression(ctx
, &p
)) {
1555 _emit(ctx
, &p
.out
, op
);
1561 _parse_equality_expression(struct parse_context
*ctx
,
1562 struct parse_state
*ps
)
1564 struct parse_state p
= *ps
;
1566 if (_parse_relational_expression(ctx
, &p
)) {
1573 if (_parse_token(ctx
, SL_PP_EQUAL
, &p
) == 0) {
1575 } else if (_parse_token(ctx
, SL_PP_NOTEQUAL
, &p
) == 0) {
1580 if (_parse_relational_expression(ctx
, &p
)) {
1583 _emit(ctx
, &p
.out
, op
);
1589 _parse_logical_and_expression(struct parse_context
*ctx
,
1590 struct parse_state
*ps
)
1592 struct parse_state p
= *ps
;
1594 if (_parse_equality_expression(ctx
, &p
)) {
1599 if (_parse_token(ctx
, SL_PP_AND
, &p
)) {
1602 if (_parse_equality_expression(ctx
, &p
)) {
1605 _emit(ctx
, &p
.out
, OP_LOGICALAND
);
1611 _parse_logical_xor_expression(struct parse_context
*ctx
,
1612 struct parse_state
*ps
)
1614 struct parse_state p
= *ps
;
1616 if (_parse_logical_and_expression(ctx
, &p
)) {
1621 if (_parse_token(ctx
, SL_PP_XOR
, &p
)) {
1624 if (_parse_logical_and_expression(ctx
, &p
)) {
1627 _emit(ctx
, &p
.out
, OP_LOGICALXOR
);
1633 _parse_logical_or_expression(struct parse_context
*ctx
,
1634 struct parse_state
*ps
)
1636 struct parse_state p
= *ps
;
1638 if (_parse_logical_xor_expression(ctx
, &p
)) {
1643 if (_parse_token(ctx
, SL_PP_OR
, &p
)) {
1646 if (_parse_logical_xor_expression(ctx
, &p
)) {
1649 _emit(ctx
, &p
.out
, OP_LOGICALOR
);
1655 _parse_conditional_expression(struct parse_context
*ctx
,
1656 struct parse_state
*ps
)
1658 struct parse_state p
= *ps
;
1660 if (_parse_logical_or_expression(ctx
, &p
)) {
1665 if (_parse_token(ctx
, SL_PP_QUESTION
, &p
)) {
1668 if (_parse_expression(ctx
, &p
)) {
1671 if (_parse_token(ctx
, SL_PP_COLON
, &p
)) {
1674 if (_parse_conditional_expression(ctx
, &p
)) {
1677 _emit(ctx
, &p
.out
, OP_SELECT
);
1683 _parse_constant_expression(struct parse_context
*ctx
,
1684 struct parse_state
*ps
)
1686 if (_parse_conditional_expression(ctx
, ps
)) {
1689 _emit(ctx
, &ps
->out
, OP_END
);
1695 _parse_parameter_declarator_array(struct parse_context
*ctx
,
1696 struct parse_state
*ps
)
1698 struct parse_state p
= *ps
;
1700 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1703 if (_parse_constant_expression(ctx
, &p
)) {
1704 _error(ctx
, "expected constant integral expression");
1707 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1708 _error(ctx
, "expected `]'");
1717 _parse_parameter_declarator(struct parse_context
*ctx
,
1718 struct parse_state
*ps
)
1720 struct parse_state p
= *ps
;
1723 if (_parse_type_specifier(ctx
, &p
)) {
1726 if (_parse_identifier(ctx
, &p
)) {
1729 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1730 if (_parse_parameter_declarator_array(ctx
, &p
)) {
1731 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1739 _parse_parameter_type_specifier_array(struct parse_context
*ctx
,
1740 struct parse_state
*ps
)
1742 struct parse_state p
= *ps
;
1744 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1747 if (_parse_constant_expression(ctx
, &p
)) {
1748 _error(ctx
, "expected constant integral expression");
1751 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1752 _error(ctx
, "expected `]'");
1761 _parse_parameter_type_specifier(struct parse_context
*ctx
,
1762 struct parse_state
*ps
)
1764 struct parse_state p
= *ps
;
1767 if (_parse_type_specifier(ctx
, &p
)) {
1770 _emit(ctx
, &p
.out
, '\0');
1772 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1773 if (_parse_parameter_type_specifier_array(ctx
, &p
)) {
1774 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1782 _parse_parameter_declaration(struct parse_context
*ctx
,
1783 struct parse_state
*ps
)
1785 struct parse_state p
= *ps
;
1786 unsigned int e
= _emit(ctx
, &p
.out
, PARAMETER_NEXT
);
1790 if (_parse_storage_qualifier(ctx
, &p
)) {
1791 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1793 _parse_parameter_qualifier(ctx
, &p
);
1794 if (_parse_precision(ctx
, &p
)) {
1795 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1797 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1801 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1811 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1812 struct parse_state
*ps
)
1814 struct parse_state p
= *ps
;
1816 if (_parse_function_header(ctx
, &p
)) {
1819 if (_parse_parameter_declaration(ctx
, &p
)) {
1825 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1828 if (_parse_parameter_declaration(ctx
, &p
)) {
1836 _parse_function_declarator(struct parse_context
*ctx
,
1837 struct parse_state
*ps
)
1839 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1843 if (_parse_function_header(ctx
, ps
) == 0) {
1852 _parse_function_prototype(struct parse_context
*ctx
,
1853 struct parse_state
*ps
)
1855 struct parse_state p
= *ps
;
1857 if (_parse_function_header(ctx
, &p
) == 0) {
1858 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1859 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1860 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1864 _error(ctx
, "expected `)'");
1870 if (_parse_function_declarator(ctx
, &p
) == 0) {
1871 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1872 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1876 _error(ctx
, "expected `)'");
1885 _parse_precision(struct parse_context
*ctx
,
1886 struct parse_state
*ps
)
1888 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1890 unsigned int precision
;
1892 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1895 id
= input
->data
.identifier
;
1897 if (id
== ctx
->dict
.lowp
) {
1898 precision
= PRECISION_LOW
;
1899 } else if (id
== ctx
->dict
.mediump
) {
1900 precision
= PRECISION_MEDIUM
;
1901 } else if (id
== ctx
->dict
.highp
) {
1902 precision
= PRECISION_HIGH
;
1907 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1908 _emit(ctx
, &ps
->out
, precision
);
1914 _parse_prectype(struct parse_context
*ctx
,
1915 struct parse_state
*ps
)
1917 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1921 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1924 id
= input
->data
.identifier
;
1926 if (id
== ctx
->dict
._int
) {
1927 type
= TYPE_SPECIFIER_INT
;
1928 } else if (id
== ctx
->dict
._float
) {
1929 type
= TYPE_SPECIFIER_FLOAT
;
1930 } else if (id
== ctx
->dict
.sampler1D
) {
1931 type
= TYPE_SPECIFIER_SAMPLER1D
;
1932 } else if (id
== ctx
->dict
.sampler2D
) {
1933 type
= TYPE_SPECIFIER_SAMPLER2D
;
1934 } else if (id
== ctx
->dict
.sampler3D
) {
1935 type
= TYPE_SPECIFIER_SAMPLER3D
;
1936 } else if (id
== ctx
->dict
.samplerCube
) {
1937 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1938 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1939 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1940 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1941 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1942 } else if (id
== ctx
->dict
.sampler2DRect
) {
1943 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1944 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1945 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1950 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1951 _emit(ctx
, &ps
->out
, type
);
1957 _parse_precision_stmt(struct parse_context
*ctx
,
1958 struct parse_state
*ps
)
1960 struct parse_state p
= *ps
;
1962 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
1965 if (_parse_precision(ctx
, &p
)) {
1968 if (_parse_prectype(ctx
, &p
)) {
1971 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1980 _parse_floatconstant(struct parse_context
*ctx
,
1981 struct parse_state
*ps
)
1983 struct parse_state p
= *ps
;
1985 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
1986 if (_parse_float(ctx
, &p
)) {
1995 _parse_intconstant(struct parse_context
*ctx
,
1996 struct parse_state
*ps
)
1998 struct parse_state p
= *ps
;
2000 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
2001 if (_parse_uint(ctx
, &p
)) {
2010 _parse_boolconstant(struct parse_context
*ctx
,
2011 struct parse_state
*ps
)
2013 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
2014 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2015 _emit(ctx
, &ps
->out
, 2); /* radix */
2016 _emit(ctx
, &ps
->out
, '0');
2017 _emit(ctx
, &ps
->out
, '\0');
2021 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
2022 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2023 _emit(ctx
, &ps
->out
, 2); /* radix */
2024 _emit(ctx
, &ps
->out
, '1');
2025 _emit(ctx
, &ps
->out
, '\0');
2034 _parse_variable_identifier(struct parse_context
*ctx
,
2035 struct parse_state
*ps
)
2037 struct parse_state p
= *ps
;
2039 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
2040 if (_parse_identifier(ctx
, &p
)) {
2049 _parse_primary_expression(struct parse_context
*ctx
,
2050 struct parse_state
*ps
)
2052 struct parse_state p
;
2054 if (_parse_floatconstant(ctx
, ps
) == 0) {
2057 if (_parse_boolconstant(ctx
, ps
) == 0) {
2060 if (_parse_intconstant(ctx
, ps
) == 0) {
2063 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2068 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2071 if (_parse_expression(ctx
, &p
)) {
2074 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2084 _parse_asm_argument(struct parse_context
*ctx
,
2085 struct parse_state
*ps
)
2087 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2088 struct parse_state p
= *ps
;
2090 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
2093 _emit(ctx
, &p
.out
, OP_FIELD
);
2094 if (_parse_identifier(ctx
, &p
)) {
2101 if (_parse_floatconstant(ctx
, ps
) == 0) {
2110 _parse_asm_arguments(struct parse_context
*ctx
,
2111 struct parse_state
*ps
)
2113 struct parse_state p
= *ps
;
2115 if (_parse_asm_argument(ctx
, &p
)) {
2118 _emit(ctx
, &p
.out
, OP_END
);
2122 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2125 if (_parse_asm_argument(ctx
, &p
)) {
2128 _emit(ctx
, &p
.out
, OP_END
);
2134 _parse_asm_statement(struct parse_context
*ctx
,
2135 struct parse_state
*ps
)
2137 struct parse_state p
= *ps
;
2139 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
2142 if (_parse_identifier(ctx
, &p
)) {
2145 if (_parse_asm_arguments(ctx
, &p
)) {
2148 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2151 _emit(ctx
, &p
.out
, OP_END
);
2158 _parse_selection_statement(struct parse_context
*ctx
,
2159 struct parse_state
*ps
)
2161 struct parse_state p
= *ps
;
2163 _emit(ctx
, &p
.out
, OP_IF
);
2164 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
2167 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2168 _error(ctx
, "expected `('");
2171 if (_parse_expression(ctx
, &p
)) {
2172 _error(ctx
, "expected an expression");
2175 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2176 _error(ctx
, "expected `)'");
2179 _emit(ctx
, &p
.out
, OP_END
);
2180 if (_parse_statement(ctx
, &p
)) {
2185 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
2186 if (_parse_statement(ctx
, &p
) == 0) {
2192 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
2193 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
2194 _emit(ctx
, &ps
->out
, OP_END
);
2200 _parse_expression_statement(struct parse_context
*ctx
,
2201 struct parse_state
*ps
)
2203 struct parse_state p
= *ps
;
2205 if (_parse_expression(ctx
, &p
)) {
2206 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2208 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2211 _emit(ctx
, &p
.out
, OP_END
);
2218 _parse_for_init_statement(struct parse_context
*ctx
,
2219 struct parse_state
*ps
)
2221 struct parse_state p
= *ps
;
2222 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2224 if (_parse_expression_statement(ctx
, &p
) == 0) {
2229 if (_parse_declaration(ctx
, &p
) == 0) {
2230 _update(ctx
, e
, OP_DECLARE
);
2240 _parse_initializer(struct parse_context
*ctx
,
2241 struct parse_state
*ps
)
2243 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2244 _emit(ctx
, &ps
->out
, OP_END
);
2252 _parse_condition_initializer(struct parse_context
*ctx
,
2253 struct parse_state
*ps
)
2255 struct parse_state p
= *ps
;
2257 _emit(ctx
, &p
.out
, OP_DECLARE
);
2258 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2259 if (_parse_fully_specified_type(ctx
, &p
)) {
2262 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2263 if (_parse_identifier(ctx
, &p
)) {
2266 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2267 _error(ctx
, "expected `='");
2270 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2271 if (_parse_initializer(ctx
, &p
)) {
2272 _error(ctx
, "expected an initialiser");
2275 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2282 _parse_condition(struct parse_context
*ctx
,
2283 struct parse_state
*ps
)
2285 struct parse_state p
;
2287 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2292 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2293 if (_parse_expression(ctx
, &p
) == 0) {
2294 _emit(ctx
, &p
.out
, OP_END
);
2304 _parse_for_rest_statement(struct parse_context
*ctx
,
2305 struct parse_state
*ps
)
2307 struct parse_state p
= *ps
;
2309 if (_parse_condition(ctx
, &p
)) {
2310 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2311 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2312 _emit(ctx
, &p
.out
, 2);
2313 _emit(ctx
, &p
.out
, '1');
2314 _emit(ctx
, &p
.out
, '\0');
2315 _emit(ctx
, &p
.out
, OP_END
);
2317 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2320 if (_parse_expression(ctx
, &p
)) {
2321 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2323 _emit(ctx
, &p
.out
, OP_END
);
2330 _parse_iteration_statement(struct parse_context
*ctx
,
2331 struct parse_state
*ps
)
2333 struct parse_state p
= *ps
;
2335 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2336 _emit(ctx
, &p
.out
, OP_WHILE
);
2337 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2338 _error(ctx
, "expected `('");
2341 if (_parse_condition(ctx
, &p
)) {
2342 _error(ctx
, "expected an expression");
2345 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2346 _error(ctx
, "expected `)'");
2349 if (_parse_statement(ctx
, &p
)) {
2356 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2357 _emit(ctx
, &p
.out
, OP_DO
);
2358 if (_parse_statement(ctx
, &p
)) {
2361 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2364 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2365 _error(ctx
, "expected `('");
2368 if (_parse_expression(ctx
, &p
)) {
2369 _error(ctx
, "expected an expression");
2372 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2373 _error(ctx
, "expected `)'");
2376 _emit(ctx
, &p
.out
, OP_END
);
2377 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2378 _error(ctx
, "expected `;'");
2385 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2386 _emit(ctx
, &p
.out
, OP_FOR
);
2387 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2388 _error(ctx
, "expected `('");
2391 if (_parse_for_init_statement(ctx
, &p
)) {
2394 if (_parse_for_rest_statement(ctx
, &p
)) {
2397 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2398 _error(ctx
, "expected `)'");
2401 if (_parse_statement(ctx
, &p
)) {
2413 _parse_jump_statement(struct parse_context
*ctx
,
2414 struct parse_state
*ps
)
2416 struct parse_state p
= *ps
;
2417 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2419 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2420 _update(ctx
, e
, OP_CONTINUE
);
2421 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2422 _update(ctx
, e
, OP_BREAK
);
2423 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2424 _update(ctx
, e
, OP_RETURN
);
2425 if (_parse_expression(ctx
, &p
)) {
2426 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2428 _emit(ctx
, &p
.out
, OP_END
);
2429 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2430 _update(ctx
, e
, OP_DISCARD
);
2434 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2443 _parse_simple_statement(struct parse_context
*ctx
,
2444 struct parse_state
*ps
)
2446 struct parse_state p
;
2449 if (_parse_selection_statement(ctx
, ps
) == 0) {
2453 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2457 if (_parse_jump_statement(ctx
, ps
) == 0) {
2462 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2463 if (_parse_expression_statement(ctx
, &p
) == 0) {
2468 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2469 _update(ctx
, e
, OP_PRECISION
);
2474 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2475 _update(ctx
, e
, OP_ASM
);
2480 if (_parse_declaration(ctx
, &p
) == 0) {
2481 _update(ctx
, e
, OP_DECLARE
);
2491 _parse_compound_statement(struct parse_context
*ctx
,
2492 struct parse_state
*ps
)
2494 struct parse_state p
= *ps
;
2496 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2499 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2500 _parse_statement_list(ctx
, &p
);
2501 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2504 _emit(ctx
, &p
.out
, OP_END
);
2511 _parse_statement(struct parse_context
*ctx
,
2512 struct parse_state
*ps
)
2514 if (_parse_compound_statement(ctx
, ps
) == 0) {
2518 if (_parse_simple_statement(ctx
, ps
) == 0) {
2527 _parse_statement_list(struct parse_context
*ctx
,
2528 struct parse_state
*ps
)
2530 struct parse_state p
= *ps
;
2532 if (_parse_statement(ctx
, &p
)) {
2538 if (_parse_statement(ctx
, &p
)) {
2546 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2547 struct parse_state
*ps
)
2549 struct parse_state p
= *ps
;
2551 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2554 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2555 _parse_statement_list(ctx
, &p
);
2556 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2559 _emit(ctx
, &p
.out
, OP_END
);
2566 _parse_function_definition(struct parse_context
*ctx
,
2567 struct parse_state
*ps
)
2569 struct parse_state p
= *ps
;
2571 if (_parse_function_prototype(ctx
, &p
)) {
2574 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2583 _parse_invariant_stmt(struct parse_context
*ctx
,
2584 struct parse_state
*ps
)
2586 struct parse_state p
= *ps
;
2588 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2591 if (_parse_identifier(ctx
, &p
)) {
2594 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2603 _parse_single_declaration(struct parse_context
*ctx
,
2604 struct parse_state
*ps
)
2606 struct parse_state p
= *ps
;
2609 if (_parse_fully_specified_type(ctx
, &p
)) {
2613 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2614 if (_parse_identifier(ctx
, &p
)) {
2615 _update(ctx
, e
, VARIABLE_NONE
);
2620 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2623 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2624 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2625 if (_parse_initializer(ctx
, &p
) == 0) {
2629 _error(ctx
, "expected an initialiser");
2634 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2635 if (_parse_constant_expression(ctx
, &p
)) {
2636 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2638 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2640 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2644 _error(ctx
, "expected `]'");
2652 _parse_init_declarator_list(struct parse_context
*ctx
,
2653 struct parse_state
*ps
)
2655 struct parse_state p
= *ps
;
2657 if (_parse_single_declaration(ctx
, &p
)) {
2665 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2668 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2669 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2670 if (_parse_identifier(ctx
, &p
)) {
2674 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2677 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2678 if (_parse_initializer(ctx
, &p
) == 0) {
2679 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2683 _error(ctx
, "expected an initialiser");
2688 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2691 if (_parse_constant_expression(ctx
, &p
)) {
2692 arr
= VARIABLE_ARRAY_UNKNOWN
;
2694 arr
= VARIABLE_ARRAY_EXPLICIT
;
2696 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2697 _update(ctx
, e
, arr
);
2701 _error(ctx
, "expected `]'");
2707 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2713 _parse_declaration(struct parse_context
*ctx
,
2714 struct parse_state
*ps
)
2716 struct parse_state p
= *ps
;
2717 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2719 if (_parse_function_prototype(ctx
, &p
)) {
2720 if (_parse_init_declarator_list(ctx
, &p
)) {
2723 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2725 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2726 _error(ctx
, "expected `;'");
2735 _parse_external_declaration(struct parse_context
*ctx
,
2736 struct parse_state
*ps
)
2738 struct parse_state p
= *ps
;
2739 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2741 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2742 _update(ctx
, e
, DEFAULT_PRECISION
);
2747 if (_parse_function_definition(ctx
, &p
) == 0) {
2748 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2753 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2754 _update(ctx
, e
, INVARIANT_STMT
);
2759 if (_parse_declaration(ctx
, &p
) == 0) {
2760 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2765 _error(ctx
, "expected an identifier");
2771 _parse_extensions(struct parse_context
*ctx
,
2772 struct parse_state
*ps
)
2775 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
2776 unsigned int enable
;
2782 switch (input
->token
) {
2783 case SL_PP_EXTENSION_REQUIRE
:
2784 case SL_PP_EXTENSION_ENABLE
:
2785 case SL_PP_EXTENSION_WARN
:
2788 case SL_PP_EXTENSION_DISABLE
:
2796 if (input
->data
.extension
== ctx
->dict
.all
) {
2797 ctx
->fragment_coord_conventions
= enable
;
2799 else if (input
->data
.extension
== ctx
->dict
._GL_ARB_fragment_coord_conventions
) {
2800 ctx
->fragment_coord_conventions
= enable
;
2807 _parse_translation_unit(struct parse_context
*ctx
,
2808 struct parse_state
*ps
)
2810 _emit(ctx
, &ps
->out
, REVISION
);
2811 if (_parse_extensions(ctx
, ps
)) {
2814 if (_parse_external_declaration(ctx
, ps
)) {
2818 if (_parse_extensions(ctx
, ps
)) {
2821 if (_parse_external_declaration(ctx
, ps
)) {
2825 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2826 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2833 #define ADD_NAME_STR(CTX, NAME, STR)\
2835 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2836 if ((CTX).dict.NAME == -1) {\
2841 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2845 sl_cl_compile(struct sl_pp_context
*context
,
2846 unsigned int shader_type
,
2847 unsigned int parsing_builtin
,
2848 unsigned char **output
,
2849 unsigned int *cboutput
,
2851 unsigned int cberror
)
2853 struct parse_context ctx
;
2854 struct parse_state ps
;
2856 ctx
.context
= context
;
2858 ADD_NAME_STR(ctx
, _void
, "void");
2859 ADD_NAME_STR(ctx
, _float
, "float");
2860 ADD_NAME_STR(ctx
, _int
, "int");
2861 ADD_NAME_STR(ctx
, _bool
, "bool");
2862 ADD_NAME(ctx
, vec2
);
2863 ADD_NAME(ctx
, vec3
);
2864 ADD_NAME(ctx
, vec4
);
2865 ADD_NAME(ctx
, bvec2
);
2866 ADD_NAME(ctx
, bvec3
);
2867 ADD_NAME(ctx
, bvec4
);
2868 ADD_NAME(ctx
, ivec2
);
2869 ADD_NAME(ctx
, ivec3
);
2870 ADD_NAME(ctx
, ivec4
);
2871 ADD_NAME(ctx
, mat2
);
2872 ADD_NAME(ctx
, mat3
);
2873 ADD_NAME(ctx
, mat4
);
2874 ADD_NAME(ctx
, mat2x3
);
2875 ADD_NAME(ctx
, mat3x2
);
2876 ADD_NAME(ctx
, mat2x4
);
2877 ADD_NAME(ctx
, mat4x2
);
2878 ADD_NAME(ctx
, mat3x4
);
2879 ADD_NAME(ctx
, mat4x3
);
2880 ADD_NAME(ctx
, sampler1D
);
2881 ADD_NAME(ctx
, sampler2D
);
2882 ADD_NAME(ctx
, sampler3D
);
2883 ADD_NAME(ctx
, samplerCube
);
2884 ADD_NAME(ctx
, sampler1DShadow
);
2885 ADD_NAME(ctx
, sampler2DShadow
);
2886 ADD_NAME(ctx
, sampler2DRect
);
2887 ADD_NAME(ctx
, sampler2DRectShadow
);
2889 ADD_NAME(ctx
, invariant
);
2891 ADD_NAME(ctx
, centroid
);
2893 ADD_NAME(ctx
, precision
);
2894 ADD_NAME(ctx
, lowp
);
2895 ADD_NAME(ctx
, mediump
);
2896 ADD_NAME(ctx
, highp
);
2898 ADD_NAME_STR(ctx
, _const
, "const");
2899 ADD_NAME(ctx
, attribute
);
2900 ADD_NAME(ctx
, varying
);
2901 ADD_NAME(ctx
, uniform
);
2902 ADD_NAME(ctx
, __fixed_output
);
2903 ADD_NAME(ctx
, __fixed_input
);
2907 ADD_NAME(ctx
, inout
);
2909 ADD_NAME(ctx
, layout
);
2910 ADD_NAME(ctx
, origin_upper_left
);
2911 ADD_NAME(ctx
, pixel_center_integer
);
2913 ADD_NAME_STR(ctx
, _struct
, "struct");
2915 ADD_NAME(ctx
, __constructor
);
2916 ADD_NAME(ctx
, __operator
);
2917 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2919 ADD_NAME_STR(ctx
, _if
, "if");
2920 ADD_NAME_STR(ctx
, _else
, "else");
2921 ADD_NAME_STR(ctx
, _for
, "for");
2922 ADD_NAME_STR(ctx
, _while
, "while");
2923 ADD_NAME_STR(ctx
, _do
, "do");
2925 ADD_NAME_STR(ctx
, _continue
, "continue");
2926 ADD_NAME_STR(ctx
, _break
, "break");
2927 ADD_NAME_STR(ctx
, _return
, "return");
2928 ADD_NAME(ctx
, discard
);
2930 ADD_NAME_STR(ctx
, _false
, "false");
2931 ADD_NAME_STR(ctx
, _true
, "true");
2934 ADD_NAME_STR(ctx
, _GL_ARB_fragment_coord_conventions
, "GL_ARB_fragment_coord_conventions");
2939 ctx
.shader_type
= shader_type
;
2940 ctx
.parsing_builtin
= 1;
2942 ctx
.fragment_coord_conventions
= 0;
2944 ctx
.error
[0] = '\0';
2945 ctx
.process_error
= 0;
2947 ctx
.tokens_cap
= 1024;
2948 ctx
.tokens_read
= 0;
2949 ctx
.tokens
= malloc(ctx
.tokens_cap
* sizeof(struct sl_pp_token_info
));
2951 strncpy(error
, "out of memory", cberror
);
2958 if (_parse_translation_unit(&ctx
, &ps
)) {
2959 strncpy(error
, ctx
.error
, cberror
);
2964 *output
= ctx
.out_buf
;