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 "../pp/sl_pp_token.h"
33 #include "sl_cl_parse.h"
36 /* revision number - increment after each change affecting emitted output */
39 /* external declaration (or precision or invariant stmt) */
40 #define EXTERNAL_NULL 0
41 #define EXTERNAL_FUNCTION_DEFINITION 1
42 #define EXTERNAL_DECLARATION 2
43 #define DEFAULT_PRECISION 3
44 #define INVARIANT_STMT 4
47 #define PRECISION_DEFAULT 0
48 #define PRECISION_LOW 1
49 #define PRECISION_MEDIUM 2
50 #define PRECISION_HIGH 3
53 #define DECLARATION_FUNCTION_PROTOTYPE 1
54 #define DECLARATION_INIT_DECLARATOR_LIST 2
57 #define FUNCTION_ORDINARY 0
58 #define FUNCTION_CONSTRUCTOR 1
59 #define FUNCTION_OPERATOR 2
61 /* function call type */
62 #define FUNCTION_CALL_NONARRAY 0
63 #define FUNCTION_CALL_ARRAY 1
66 #define OPERATOR_ADDASSIGN 1
67 #define OPERATOR_SUBASSIGN 2
68 #define OPERATOR_MULASSIGN 3
69 #define OPERATOR_DIVASSIGN 4
70 /*#define OPERATOR_MODASSIGN 5*/
71 /*#define OPERATOR_LSHASSIGN 6*/
72 /*#define OPERATOR_RSHASSIGN 7*/
73 /*#define OPERATOR_ORASSIGN 8*/
74 /*#define OPERATOR_XORASSIGN 9*/
75 /*#define OPERATOR_ANDASSIGN 10*/
76 #define OPERATOR_LOGICALXOR 11
77 /*#define OPERATOR_BITOR 12*/
78 /*#define OPERATOR_BITXOR 13*/
79 /*#define OPERATOR_BITAND 14*/
80 #define OPERATOR_LESS 15
81 #define OPERATOR_GREATER 16
82 #define OPERATOR_LESSEQUAL 17
83 #define OPERATOR_GREATEREQUAL 18
84 /*#define OPERATOR_LSHIFT 19*/
85 /*#define OPERATOR_RSHIFT 20*/
86 #define OPERATOR_MULTIPLY 21
87 #define OPERATOR_DIVIDE 22
88 /*#define OPERATOR_MODULUS 23*/
89 #define OPERATOR_INCREMENT 24
90 #define OPERATOR_DECREMENT 25
91 #define OPERATOR_PLUS 26
92 #define OPERATOR_MINUS 27
93 /*#define OPERATOR_COMPLEMENT 28*/
94 #define OPERATOR_NOT 29
96 /* init declarator list */
97 #define DECLARATOR_NONE 0
98 #define DECLARATOR_NEXT 1
100 /* variable declaration */
101 #define VARIABLE_NONE 0
102 #define VARIABLE_IDENTIFIER 1
103 #define VARIABLE_INITIALIZER 2
104 #define VARIABLE_ARRAY_EXPLICIT 3
105 #define VARIABLE_ARRAY_UNKNOWN 4
108 #define TYPE_QUALIFIER_NONE 0
109 #define TYPE_QUALIFIER_CONST 1
110 #define TYPE_QUALIFIER_ATTRIBUTE 2
111 #define TYPE_QUALIFIER_VARYING 3
112 #define TYPE_QUALIFIER_UNIFORM 4
113 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
114 #define TYPE_QUALIFIER_FIXEDINPUT 6
116 /* invariant qualifier */
117 #define TYPE_VARIANT 90
118 #define TYPE_INVARIANT 91
120 /* centroid qualifier */
121 #define TYPE_CENTER 95
122 #define TYPE_CENTROID 96
124 /* layout qualifiers */
125 #define LAYOUT_QUALIFIER_NONE 0
126 #define LAYOUT_QUALIFIER_UPPER_LEFT 1
127 #define LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER 2
130 #define TYPE_SPECIFIER_VOID 0
131 #define TYPE_SPECIFIER_BOOL 1
132 #define TYPE_SPECIFIER_BVEC2 2
133 #define TYPE_SPECIFIER_BVEC3 3
134 #define TYPE_SPECIFIER_BVEC4 4
135 #define TYPE_SPECIFIER_INT 5
136 #define TYPE_SPECIFIER_IVEC2 6
137 #define TYPE_SPECIFIER_IVEC3 7
138 #define TYPE_SPECIFIER_IVEC4 8
139 #define TYPE_SPECIFIER_FLOAT 9
140 #define TYPE_SPECIFIER_VEC2 10
141 #define TYPE_SPECIFIER_VEC3 11
142 #define TYPE_SPECIFIER_VEC4 12
143 #define TYPE_SPECIFIER_MAT2 13
144 #define TYPE_SPECIFIER_MAT3 14
145 #define TYPE_SPECIFIER_MAT4 15
146 #define TYPE_SPECIFIER_SAMPLER1D 16
147 #define TYPE_SPECIFIER_SAMPLER2D 17
148 #define TYPE_SPECIFIER_SAMPLER3D 18
149 #define TYPE_SPECIFIER_SAMPLERCUBE 19
150 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
151 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
152 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
153 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
154 #define TYPE_SPECIFIER_STRUCT 24
155 #define TYPE_SPECIFIER_TYPENAME 25
158 #define TYPE_SPECIFIER_MAT23 26
159 #define TYPE_SPECIFIER_MAT32 27
160 #define TYPE_SPECIFIER_MAT24 28
161 #define TYPE_SPECIFIER_MAT42 29
162 #define TYPE_SPECIFIER_MAT34 30
163 #define TYPE_SPECIFIER_MAT43 31
165 /* GL_EXT_texture_array */
166 #define TYPE_SPECIFIER_SAMPLER_1D_ARRAY 32
167 #define TYPE_SPECIFIER_SAMPLER_2D_ARRAY 33
168 #define TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW 34
169 #define TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW 35
171 /* type specifier array */
172 #define TYPE_SPECIFIER_NONARRAY 0
173 #define TYPE_SPECIFIER_ARRAY 1
175 /* structure field */
178 #define FIELD_ARRAY 2
182 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
183 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
187 #define OP_CONTINUE 6
190 #define OP_EXPRESSION 9
195 #define OP_PUSH_VOID 14
196 #define OP_PUSH_BOOL 15
197 #define OP_PUSH_INT 16
198 #define OP_PUSH_FLOAT 17
199 #define OP_PUSH_IDENTIFIER 18
200 #define OP_SEQUENCE 19
202 #define OP_ADDASSIGN 21
203 #define OP_SUBASSIGN 22
204 #define OP_MULASSIGN 23
205 #define OP_DIVASSIGN 24
206 /*#define OP_MODASSIGN 25*/
207 /*#define OP_LSHASSIGN 26*/
208 /*#define OP_RSHASSIGN 27*/
209 /*#define OP_ORASSIGN 28*/
210 /*#define OP_XORASSIGN 29*/
211 /*#define OP_ANDASSIGN 30*/
213 #define OP_LOGICALOR 32
214 #define OP_LOGICALXOR 33
215 #define OP_LOGICALAND 34
216 /*#define OP_BITOR 35*/
217 /*#define OP_BITXOR 36*/
218 /*#define OP_BITAND 37*/
220 #define OP_NOTEQUAL 39
222 #define OP_GREATER 41
223 #define OP_LESSEQUAL 42
224 #define OP_GREATEREQUAL 43
225 /*#define OP_LSHIFT 44*/
226 /*#define OP_RSHIFT 45*/
228 #define OP_SUBTRACT 47
229 #define OP_MULTIPLY 48
231 /*#define OP_MODULUS 50*/
232 #define OP_PREINCREMENT 51
233 #define OP_PREDECREMENT 52
236 /*#define OP_COMPLEMENT 55*/
238 #define OP_SUBSCRIPT 57
241 #define OP_POSTINCREMENT 60
242 #define OP_POSTDECREMENT 61
243 #define OP_PRECISION 62
246 /* parameter qualifier */
247 #define PARAM_QUALIFIER_IN 0
248 #define PARAM_QUALIFIER_OUT 1
249 #define PARAM_QUALIFIER_INOUT 2
250 #define PARAM_QUALIFIER_NONE 3
252 /* function parameter */
253 #define PARAMETER_NONE 0
254 #define PARAMETER_NEXT 1
256 /* function parameter array presence */
257 #define PARAMETER_ARRAY_NOT_PRESENT 0
258 #define PARAMETER_ARRAY_PRESENT 1
291 int sampler2DRectShadow
;
294 int sampler1DArrayShadow
;
295 int sampler2DArrayShadow
;
318 int origin_upper_left
;
319 int pixel_center_integer
;
342 int _GL_ARB_fragment_coord_conventions
;
343 int _GL_ARB_texture_rectangle
;
347 struct parse_context
{
348 struct sl_pp_context
*context
;
350 struct parse_dict dict
;
352 struct sl_pp_token_info
*tokens
;
353 unsigned int tokens_read
;
354 unsigned int tokens_cap
;
356 unsigned char *out_buf
;
357 unsigned int out_cap
;
359 unsigned int shader_type
;
360 unsigned int parsing_builtin
;
362 unsigned int fragment_coord_conventions
:1;
363 unsigned int texture_rectangle
:1;
377 _emit(struct parse_context
*ctx
,
381 if (*out
== ctx
->out_cap
) {
382 ctx
->out_cap
+= 4096;
383 ctx
->out_buf
= (unsigned char *)realloc(ctx
->out_buf
, ctx
->out_cap
* sizeof(unsigned char));
385 ctx
->out_buf
[*out
] = b
;
391 _update(struct parse_context
*ctx
,
395 ctx
->out_buf
[out
] = b
;
400 _error(struct parse_context
*ctx
,
403 if (ctx
->error
[0] == '\0') {
404 strncpy(ctx
->error
, msg
, sizeof(ctx
->error
) - 1);
405 ctx
->error
[sizeof(ctx
->error
) - 1] = '\0';
410 static const struct sl_pp_token_info
*
411 _fetch_token(struct parse_context
*ctx
,
414 if (ctx
->process_error
) {
418 while (pos
>= ctx
->tokens_read
) {
419 if (ctx
->tokens_read
== ctx
->tokens_cap
) {
420 ctx
->tokens_cap
+= 1024;
421 ctx
->tokens
= realloc(ctx
->tokens
,
422 ctx
->tokens_cap
* sizeof(struct sl_pp_token_info
));
424 _error(ctx
, "out of memory");
425 ctx
->process_error
= 1;
429 if (sl_pp_process_get(ctx
->context
, &ctx
->tokens
[ctx
->tokens_read
])) {
430 _error(ctx
, sl_pp_context_error_message(ctx
->context
));
431 ctx
->process_error
= 1;
434 switch (ctx
->tokens
[ctx
->tokens_read
].token
) {
436 case SL_PP_SEMICOLON
:
444 case SL_PP_INCREMENT
:
445 case SL_PP_ADDASSIGN
:
447 case SL_PP_DECREMENT
:
448 case SL_PP_SUBASSIGN
:
453 case SL_PP_MULASSIGN
:
455 case SL_PP_DIVASSIGN
:
457 case SL_PP_MODASSIGN
:
459 case SL_PP_LSHIFTASSIGN
:
461 case SL_PP_LESSEQUAL
:
463 case SL_PP_RSHIFTASSIGN
:
465 case SL_PP_GREATEREQUAL
:
470 case SL_PP_BITANDASSIGN
:
473 case SL_PP_BITXORASSIGN
:
476 case SL_PP_BITORASSIGN
:
480 case SL_PP_IDENTIFIER
:
483 case SL_PP_EXTENSION_REQUIRE
:
484 case SL_PP_EXTENSION_ENABLE
:
485 case SL_PP_EXTENSION_WARN
:
486 case SL_PP_EXTENSION_DISABLE
:
494 return &ctx
->tokens
[pos
];
499 * Try to parse/match a particular token.
500 * \return 0 for success, -1 for error.
503 _parse_token(struct parse_context
*ctx
,
504 enum sl_pp_token token
,
505 struct parse_state
*ps
)
507 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
509 if (input
&& input
->token
== token
) {
518 * Try to parse an identifer.
519 * \return 0 for success, -1 for error
522 _parse_id(struct parse_context
*ctx
,
524 struct parse_state
*ps
)
526 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
528 if (input
&& input
->token
== SL_PP_IDENTIFIER
&& input
->data
.identifier
== id
) {
537 _parse_identifier(struct parse_context
*ctx
,
538 struct parse_state
*ps
)
540 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
542 if (input
&& input
->token
== SL_PP_IDENTIFIER
) {
543 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
.identifier
);
546 _emit(ctx
, &ps
->out
, *cstr
);
556 _parse_float(struct parse_context
*ctx
,
557 struct parse_state
*ps
)
559 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
561 if (input
&& input
->token
== SL_PP_FLOAT
) {
562 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._float
);
564 _emit(ctx
, &ps
->out
, 1);
566 _emit(ctx
, &ps
->out
, *cstr
);
576 _parse_uint(struct parse_context
*ctx
,
577 struct parse_state
*ps
)
579 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
581 if (input
&& input
->token
== SL_PP_UINT
) {
582 const char *cstr
= sl_pp_context_cstr(ctx
->context
, input
->data
._uint
);
584 _emit(ctx
, &ps
->out
, 1);
586 _emit(ctx
, &ps
->out
, *cstr
);
595 /**************************************/
599 _parse_unary_expression(struct parse_context
*ctx
,
600 struct parse_state
*ps
);
603 _parse_conditional_expression(struct parse_context
*ctx
,
604 struct parse_state
*ps
);
608 _parse_constant_expression(struct parse_context
*ctx
,
609 struct parse_state
*ps
);
613 _parse_primary_expression(struct parse_context
*ctx
,
614 struct parse_state
*ps
);
618 _parse_statement(struct parse_context
*ctx
,
619 struct parse_state
*ps
);
623 _parse_type_specifier(struct parse_context
*ctx
,
624 struct parse_state
*ps
);
628 _parse_declaration(struct parse_context
*ctx
,
629 struct parse_state
*ps
);
633 _parse_statement_list(struct parse_context
*ctx
,
634 struct parse_state
*ps
);
638 _parse_assignment_expression(struct parse_context
*ctx
,
639 struct parse_state
*ps
);
643 _parse_precision(struct parse_context
*ctx
,
644 struct parse_state
*ps
);
648 _parse_overriden_operator(struct parse_context
*ctx
,
649 struct parse_state
*ps
)
653 if (_parse_token(ctx
, SL_PP_INCREMENT
, ps
) == 0) {
654 op
= OPERATOR_INCREMENT
;
655 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, ps
) == 0) {
656 op
= OPERATOR_ADDASSIGN
;
657 } else if (_parse_token(ctx
, SL_PP_PLUS
, ps
) == 0) {
659 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, ps
) == 0) {
660 op
= OPERATOR_DECREMENT
;
661 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, ps
) == 0) {
662 op
= OPERATOR_SUBASSIGN
;
663 } else if (_parse_token(ctx
, SL_PP_MINUS
, ps
) == 0) {
665 } else if (_parse_token(ctx
, SL_PP_NOT
, ps
) == 0) {
667 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, ps
) == 0) {
668 op
= OPERATOR_MULASSIGN
;
669 } else if (_parse_token(ctx
, SL_PP_STAR
, ps
) == 0) {
670 op
= OPERATOR_MULTIPLY
;
671 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, ps
) == 0) {
672 op
= OPERATOR_DIVASSIGN
;
673 } else if (_parse_token(ctx
, SL_PP_SLASH
, ps
) == 0) {
674 op
= OPERATOR_DIVIDE
;
675 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, ps
) == 0) {
676 op
= OPERATOR_LESSEQUAL
;
677 } else if (_parse_token(ctx
, SL_PP_LESS
, ps
) == 0) {
679 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, ps
) == 0) {
680 op
= OPERATOR_GREATEREQUAL
;
681 } else if (_parse_token(ctx
, SL_PP_GREATER
, ps
) == 0) {
682 op
= OPERATOR_GREATER
;
683 } else if (_parse_token(ctx
, SL_PP_XOR
, ps
) == 0) {
684 op
= OPERATOR_LOGICALXOR
;
689 _emit(ctx
, &ps
->out
, op
);
695 _parse_function_decl_identifier(struct parse_context
*ctx
,
696 struct parse_state
*ps
)
698 struct parse_state p
= *ps
;
699 unsigned int e
= _emit(ctx
, &p
.out
, 0);
701 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__constructor
, &p
) == 0) {
702 _update(ctx
, e
, FUNCTION_CONSTRUCTOR
);
707 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__operator
, &p
) == 0) {
708 _update(ctx
, e
, FUNCTION_OPERATOR
);
709 if (_parse_overriden_operator(ctx
, &p
) == 0) {
716 if (_parse_identifier(ctx
, &p
) == 0) {
717 _update(ctx
, e
, FUNCTION_ORDINARY
);
727 _parse_invariant_qualifier(struct parse_context
*ctx
,
728 struct parse_state
*ps
)
730 if (_parse_id(ctx
, ctx
->dict
.invariant
, ps
)) {
733 _emit(ctx
, &ps
->out
, TYPE_INVARIANT
);
739 _parse_centroid_qualifier(struct parse_context
*ctx
,
740 struct parse_state
*ps
)
742 if (_parse_id(ctx
, ctx
->dict
.centroid
, ps
)) {
745 _emit(ctx
, &ps
->out
, TYPE_CENTROID
);
751 _parse_layout_qualifier(struct parse_context
*ctx
,
752 struct parse_state
*ps
)
754 if (_parse_id(ctx
, ctx
->dict
.layout
, ps
) == 0) {
755 if (!ctx
->fragment_coord_conventions
) {
756 _error(ctx
, "GL_ARB_fragment_coord_conventions extension must be enabled "
757 "in order to use a layout qualifier");
761 /* Layout qualifiers are only defined for fragment shaders,
762 * so do an early check.
764 if (ctx
->shader_type
!= 1) {
765 _error(ctx
, "layout qualifiers are only valid for fragment shaders");
769 /* start of a parenthesised list of layout qualifiers */
771 if (_parse_token(ctx
, SL_PP_LPAREN
, ps
)) {
772 _error(ctx
, "expected `('");
776 /* parse comma-separated ID list */
778 if (_parse_id(ctx
, ctx
->dict
.origin_upper_left
, ps
) == 0) {
779 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_UPPER_LEFT
);
781 else if (_parse_id(ctx
, ctx
->dict
.pixel_center_integer
, ps
) == 0) {
782 _emit(ctx
, &ps
->out
, LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER
);
785 _error(ctx
, "expected a layout qualifier name");
789 if (_parse_token(ctx
, SL_PP_RPAREN
, ps
) == 0) {
793 else if (_parse_token(ctx
, SL_PP_COMMA
, ps
) == 0) {
794 /* another layout qualifier is coming */
797 _error(ctx
, "expected `,' or `)'");
808 _parse_storage_qualifier(struct parse_context
*ctx
,
809 struct parse_state
*ps
)
811 struct parse_state p
= *ps
;
812 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, p
.in
);
813 unsigned int e
= _emit(ctx
, &p
.out
, 0);
816 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
819 id
= input
->data
.identifier
;
821 if (id
== ctx
->dict
._const
) {
822 _update(ctx
, e
, TYPE_QUALIFIER_CONST
);
823 } else if (ctx
->shader_type
== 2 && id
== ctx
->dict
.attribute
) {
824 _update(ctx
, e
, TYPE_QUALIFIER_ATTRIBUTE
);
825 } else if (id
== ctx
->dict
.varying
) {
826 _update(ctx
, e
, TYPE_QUALIFIER_VARYING
);
827 } else if (id
== ctx
->dict
.uniform
) {
828 _update(ctx
, e
, TYPE_QUALIFIER_UNIFORM
);
829 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_output
) {
830 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDOUTPUT
);
831 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_input
) {
832 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDINPUT
);
836 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
842 _parse_struct_declarator(struct parse_context
*ctx
,
843 struct parse_state
*ps
)
845 struct parse_state p
= *ps
;
848 if (_parse_identifier(ctx
, &p
)) {
851 e
= _emit(ctx
, &p
.out
, FIELD_NONE
);
854 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
857 if (_parse_constant_expression(ctx
, &p
)) {
858 _error(ctx
, "expected constant integral expression");
861 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
862 _error(ctx
, "expected `]'");
865 _update(ctx
, e
, FIELD_ARRAY
);
872 _parse_struct_declarator_list(struct parse_context
*ctx
,
873 struct parse_state
*ps
)
875 struct parse_state p
= *ps
;
877 if (_parse_struct_declarator(ctx
, &p
)) {
883 _emit(ctx
, &p
.out
, FIELD_NEXT
);
884 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
887 if (_parse_struct_declarator(ctx
, &p
)) {
895 _parse_struct_declaration(struct parse_context
*ctx
,
896 struct parse_state
*ps
)
898 struct parse_state p
= *ps
;
900 if (_parse_type_specifier(ctx
, &p
)) {
903 if (_parse_struct_declarator_list(ctx
, &p
)) {
906 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
909 _emit(ctx
, &p
.out
, FIELD_NONE
);
916 _parse_struct_declaration_list(struct parse_context
*ctx
,
917 struct parse_state
*ps
)
919 struct parse_state p
= *ps
;
921 if (_parse_struct_declaration(ctx
, &p
)) {
927 _emit(ctx
, &p
.out
, FIELD_NEXT
);
928 if (_parse_struct_declaration(ctx
, &p
)) {
936 _parse_struct_specifier(struct parse_context
*ctx
,
937 struct parse_state
*ps
)
939 struct parse_state p
= *ps
;
941 if (_parse_id(ctx
, ctx
->dict
._struct
, &p
)) {
944 if (_parse_identifier(ctx
, &p
)) {
945 _emit(ctx
, &p
.out
, '\0');
947 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
948 _error(ctx
, "expected `{'");
951 if (_parse_struct_declaration_list(ctx
, &p
)) {
954 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
957 _emit(ctx
, &p
.out
, FIELD_NONE
);
964 _parse_type_specifier_nonarray(struct parse_context
*ctx
,
965 struct parse_state
*ps
)
967 struct parse_state p
= *ps
;
968 unsigned int e
= _emit(ctx
, &p
.out
, 0);
969 const struct sl_pp_token_info
*input
;
972 if (_parse_struct_specifier(ctx
, &p
) == 0) {
973 _update(ctx
, e
, TYPE_SPECIFIER_STRUCT
);
978 input
= _fetch_token(ctx
, p
.in
);
979 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
982 id
= input
->data
.identifier
;
984 if (id
== ctx
->dict
._void
) {
985 _update(ctx
, e
, TYPE_SPECIFIER_VOID
);
986 } else if (id
== ctx
->dict
._float
) {
987 _update(ctx
, e
, TYPE_SPECIFIER_FLOAT
);
988 } else if (id
== ctx
->dict
._int
) {
989 _update(ctx
, e
, TYPE_SPECIFIER_INT
);
990 } else if (id
== ctx
->dict
._bool
) {
991 _update(ctx
, e
, TYPE_SPECIFIER_BOOL
);
992 } else if (id
== ctx
->dict
.vec2
) {
993 _update(ctx
, e
, TYPE_SPECIFIER_VEC2
);
994 } else if (id
== ctx
->dict
.vec3
) {
995 _update(ctx
, e
, TYPE_SPECIFIER_VEC3
);
996 } else if (id
== ctx
->dict
.vec4
) {
997 _update(ctx
, e
, TYPE_SPECIFIER_VEC4
);
998 } else if (id
== ctx
->dict
.bvec2
) {
999 _update(ctx
, e
, TYPE_SPECIFIER_BVEC2
);
1000 } else if (id
== ctx
->dict
.bvec3
) {
1001 _update(ctx
, e
, TYPE_SPECIFIER_BVEC3
);
1002 } else if (id
== ctx
->dict
.bvec4
) {
1003 _update(ctx
, e
, TYPE_SPECIFIER_BVEC4
);
1004 } else if (id
== ctx
->dict
.ivec2
) {
1005 _update(ctx
, e
, TYPE_SPECIFIER_IVEC2
);
1006 } else if (id
== ctx
->dict
.ivec3
) {
1007 _update(ctx
, e
, TYPE_SPECIFIER_IVEC3
);
1008 } else if (id
== ctx
->dict
.ivec4
) {
1009 _update(ctx
, e
, TYPE_SPECIFIER_IVEC4
);
1010 } else if (id
== ctx
->dict
.mat2
) {
1011 _update(ctx
, e
, TYPE_SPECIFIER_MAT2
);
1012 } else if (id
== ctx
->dict
.mat3
) {
1013 _update(ctx
, e
, TYPE_SPECIFIER_MAT3
);
1014 } else if (id
== ctx
->dict
.mat4
) {
1015 _update(ctx
, e
, TYPE_SPECIFIER_MAT4
);
1016 } else if (id
== ctx
->dict
.mat2x3
) {
1017 _update(ctx
, e
, TYPE_SPECIFIER_MAT23
);
1018 } else if (id
== ctx
->dict
.mat3x2
) {
1019 _update(ctx
, e
, TYPE_SPECIFIER_MAT32
);
1020 } else if (id
== ctx
->dict
.mat2x4
) {
1021 _update(ctx
, e
, TYPE_SPECIFIER_MAT24
);
1022 } else if (id
== ctx
->dict
.mat4x2
) {
1023 _update(ctx
, e
, TYPE_SPECIFIER_MAT42
);
1024 } else if (id
== ctx
->dict
.mat3x4
) {
1025 _update(ctx
, e
, TYPE_SPECIFIER_MAT34
);
1026 } else if (id
== ctx
->dict
.mat4x3
) {
1027 _update(ctx
, e
, TYPE_SPECIFIER_MAT43
);
1028 } else if (id
== ctx
->dict
.sampler1D
) {
1029 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1D
);
1030 } else if (id
== ctx
->dict
.sampler2D
) {
1031 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2D
);
1032 } else if (id
== ctx
->dict
.sampler3D
) {
1033 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER3D
);
1034 } else if (id
== ctx
->dict
.samplerCube
) {
1035 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLERCUBE
);
1036 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1037 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1DSHADOW
);
1038 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1039 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DSHADOW
);
1040 } else if (id
== ctx
->dict
.sampler2DRect
) {
1041 if (!ctx
->texture_rectangle
) {
1042 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1043 "in order to use a rect sampler");
1046 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECT
);
1047 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1048 if (!ctx
->texture_rectangle
) {
1049 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1050 "in order to use a rect sampler");
1053 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
);
1054 } else if (id
== ctx
->dict
.sampler1DArray
) {
1055 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_1D_ARRAY
);
1056 } else if (id
== ctx
->dict
.sampler2DArray
) {
1057 /* XXX check for GL_EXT_texture_array */
1058 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_2D_ARRAY
);
1059 } else if (id
== ctx
->dict
.sampler1DArrayShadow
) {
1060 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
);
1061 } else if (id
== ctx
->dict
.sampler2DArrayShadow
) {
1062 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
);
1063 } else if (_parse_identifier(ctx
, &p
) == 0) {
1064 _update(ctx
, e
, TYPE_SPECIFIER_TYPENAME
);
1071 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
1078 _parse_type_specifier_array(struct parse_context
*ctx
,
1079 struct parse_state
*ps
)
1081 struct parse_state p
= *ps
;
1083 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1086 if (_parse_constant_expression(ctx
, &p
)) {
1087 _error(ctx
, "expected constant integral expression");
1090 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1091 _error(ctx
, "expected `]'");
1100 _parse_type_specifier(struct parse_context
*ctx
,
1101 struct parse_state
*ps
)
1103 struct parse_state p
= *ps
;
1106 if (_parse_type_specifier_nonarray(ctx
, &p
)) {
1110 e
= _emit(ctx
, &p
.out
, TYPE_SPECIFIER_ARRAY
);
1111 if (_parse_type_specifier_array(ctx
, &p
)) {
1112 _update(ctx
, e
, TYPE_SPECIFIER_NONARRAY
);
1119 _parse_parameter_qualifier(struct parse_context
*ctx
,
1120 struct parse_state
*ps
)
1122 unsigned int e
= _emit(ctx
, &ps
->out
, PARAM_QUALIFIER_NONE
);
1124 if (_parse_id(ctx
, ctx
->dict
.in
, ps
) == 0) {
1125 _update(ctx
, e
, PARAM_QUALIFIER_IN
);
1126 } else if (_parse_id(ctx
, ctx
->dict
.out
, ps
) == 0) {
1127 _update(ctx
, e
, PARAM_QUALIFIER_OUT
);
1128 } else if (_parse_id(ctx
, ctx
->dict
.inout
, ps
) == 0) {
1129 _update(ctx
, e
, PARAM_QUALIFIER_INOUT
);
1135 _parse_fully_specified_type(struct parse_context
*ctx
,
1136 struct parse_state
*ps
)
1138 struct parse_state p
= *ps
;
1140 if (_parse_layout_qualifier(ctx
, &p
)) {
1143 _emit(ctx
, &p
.out
, LAYOUT_QUALIFIER_NONE
);
1145 if (_parse_invariant_qualifier(ctx
, &p
)) {
1146 _emit(ctx
, &p
.out
, TYPE_VARIANT
);
1149 if (_parse_centroid_qualifier(ctx
, &p
)) {
1150 _emit(ctx
, &p
.out
, TYPE_CENTER
);
1152 if (_parse_storage_qualifier(ctx
, &p
)) {
1153 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1155 _parse_parameter_qualifier(ctx
, &p
);
1156 if (_parse_precision(ctx
, &p
)) {
1157 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1159 if (_parse_type_specifier(ctx
, &p
)) {
1168 _parse_function_header(struct parse_context
*ctx
,
1169 struct parse_state
*ps
)
1171 struct parse_state p
= *ps
;
1173 if (_parse_fully_specified_type(ctx
, &p
)) {
1176 if (_parse_function_decl_identifier(ctx
, &p
)) {
1179 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1188 _parse_function_identifier(struct parse_context
*ctx
,
1189 struct parse_state
*ps
)
1191 struct parse_state p
;
1194 if (_parse_identifier(ctx
, ps
)) {
1197 e
= _emit(ctx
, &ps
->out
, FUNCTION_CALL_NONARRAY
);
1200 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1203 if (_parse_constant_expression(ctx
, &p
)) {
1204 _error(ctx
, "expected constant integral expression");
1207 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1208 _error(ctx
, "expected `]'");
1211 _update(ctx
, e
, FUNCTION_CALL_ARRAY
);
1218 _parse_function_call_header(struct parse_context
*ctx
,
1219 struct parse_state
*ps
)
1221 struct parse_state p
= *ps
;
1223 if (_parse_function_identifier(ctx
, &p
)) {
1226 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1235 _parse_assign_expression(struct parse_context
*ctx
,
1236 struct parse_state
*ps
)
1238 struct parse_state p
= *ps
;
1241 if (_parse_unary_expression(ctx
, &p
)) {
1245 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
1247 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, &p
) == 0) {
1249 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, &p
) == 0) {
1251 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, &p
) == 0) {
1253 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, &p
) == 0) {
1259 if (_parse_assignment_expression(ctx
, &p
)) {
1262 _emit(ctx
, &p
.out
, op
);
1270 _parse_assignment_expression(struct parse_context
*ctx
,
1271 struct parse_state
*ps
)
1273 if (_parse_assign_expression(ctx
, ps
) == 0) {
1277 if (_parse_conditional_expression(ctx
, ps
) == 0) {
1286 _parse_function_call_header_with_parameters(struct parse_context
*ctx
,
1287 struct parse_state
*ps
)
1289 struct parse_state p
= *ps
;
1291 if (_parse_function_call_header(ctx
, &p
)) {
1294 if (_parse_assignment_expression(ctx
, &p
)) {
1297 _emit(ctx
, &p
.out
, OP_END
);
1300 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1303 if (_parse_assignment_expression(ctx
, &p
)) {
1306 _emit(ctx
, &p
.out
, OP_END
);
1312 _parse_function_call_header_no_parameters(struct parse_context
*ctx
,
1313 struct parse_state
*ps
)
1315 if (_parse_function_call_header(ctx
, ps
)) {
1318 _parse_id(ctx
, ctx
->dict
._void
, ps
);
1324 _parse_function_call_generic(struct parse_context
*ctx
,
1325 struct parse_state
*ps
)
1327 struct parse_state p
= *ps
;
1329 if (_parse_function_call_header_with_parameters(ctx
, &p
) == 0) {
1330 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1334 _error(ctx
, "expected `)'");
1339 if (_parse_function_call_header_no_parameters(ctx
, &p
) == 0) {
1340 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1344 _error(ctx
, "expected `)'");
1353 _parse_method_call(struct parse_context
*ctx
,
1354 struct parse_state
*ps
)
1356 struct parse_state p
= *ps
;
1358 _emit(ctx
, &p
.out
, OP_METHOD
);
1359 if (_parse_identifier(ctx
, &p
)) {
1362 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1365 if (_parse_function_call_generic(ctx
, &p
)) {
1368 _emit(ctx
, &p
.out
, OP_END
);
1375 _parse_regular_function_call(struct parse_context
*ctx
,
1376 struct parse_state
*ps
)
1378 struct parse_state p
= *ps
;
1380 _emit(ctx
, &p
.out
, OP_CALL
);
1381 if (_parse_function_call_generic(ctx
, &p
)) {
1384 _emit(ctx
, &p
.out
, OP_END
);
1391 _parse_function_call(struct parse_context
*ctx
,
1392 struct parse_state
*ps
)
1394 if (_parse_regular_function_call(ctx
, ps
) == 0) {
1398 if (_parse_method_call(ctx
, ps
) == 0) {
1407 _parse_expression(struct parse_context
*ctx
,
1408 struct parse_state
*ps
)
1410 struct parse_state p
= *ps
;
1412 if (_parse_assignment_expression(ctx
, &p
)) {
1418 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1421 if (_parse_assignment_expression(ctx
, &p
)) {
1424 _emit(ctx
, &p
.out
, OP_SEQUENCE
);
1430 _parse_postfix_expression(struct parse_context
*ctx
,
1431 struct parse_state
*ps
)
1433 struct parse_state p
;
1435 if (_parse_function_call(ctx
, ps
)) {
1436 if (_parse_primary_expression(ctx
, ps
)) {
1443 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1444 _emit(ctx
, &p
.out
, OP_POSTINCREMENT
);
1445 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1446 _emit(ctx
, &p
.out
, OP_POSTDECREMENT
);
1447 } else if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
1448 if (_parse_expression(ctx
, &p
)) {
1449 _error(ctx
, "expected an integral expression");
1452 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1453 _error(ctx
, "expected `]'");
1456 _emit(ctx
, &p
.out
, OP_SUBSCRIPT
);
1457 } else if (_parse_token(ctx
, SL_PP_DOT
, &p
) == 0) {
1458 _emit(ctx
, &p
.out
, OP_FIELD
);
1459 if (_parse_identifier(ctx
, &p
)) {
1470 _parse_unary_expression(struct parse_context
*ctx
,
1471 struct parse_state
*ps
)
1473 struct parse_state p
;
1476 if (_parse_postfix_expression(ctx
, ps
) == 0) {
1481 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1482 op
= OP_PREINCREMENT
;
1483 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1484 op
= OP_PREDECREMENT
;
1485 } else if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1487 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1489 } else if (_parse_token(ctx
, SL_PP_NOT
, &p
) == 0) {
1495 if (_parse_unary_expression(ctx
, &p
)) {
1498 _emit(ctx
, &p
.out
, op
);
1505 _parse_multiplicative_expression(struct parse_context
*ctx
,
1506 struct parse_state
*ps
)
1508 struct parse_state p
= *ps
;
1510 if (_parse_unary_expression(ctx
, &p
)) {
1517 if (_parse_token(ctx
, SL_PP_STAR
, &p
) == 0) {
1519 } else if (_parse_token(ctx
, SL_PP_SLASH
, &p
) == 0) {
1524 if (_parse_unary_expression(ctx
, &p
)) {
1527 _emit(ctx
, &p
.out
, op
);
1533 _parse_additive_expression(struct parse_context
*ctx
,
1534 struct parse_state
*ps
)
1536 struct parse_state p
= *ps
;
1538 if (_parse_multiplicative_expression(ctx
, &p
)) {
1545 if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1547 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1552 if (_parse_multiplicative_expression(ctx
, &p
)) {
1555 _emit(ctx
, &p
.out
, op
);
1561 _parse_relational_expression(struct parse_context
*ctx
,
1562 struct parse_state
*ps
)
1564 struct parse_state p
= *ps
;
1566 if (_parse_additive_expression(ctx
, &p
)) {
1573 if (_parse_token(ctx
, SL_PP_LESS
, &p
) == 0) {
1575 } else if (_parse_token(ctx
, SL_PP_GREATER
, &p
) == 0) {
1577 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, &p
) == 0) {
1579 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, &p
) == 0) {
1580 op
= OP_GREATEREQUAL
;
1584 if (_parse_additive_expression(ctx
, &p
)) {
1587 _emit(ctx
, &p
.out
, op
);
1593 _parse_equality_expression(struct parse_context
*ctx
,
1594 struct parse_state
*ps
)
1596 struct parse_state p
= *ps
;
1598 if (_parse_relational_expression(ctx
, &p
)) {
1605 if (_parse_token(ctx
, SL_PP_EQUAL
, &p
) == 0) {
1607 } else if (_parse_token(ctx
, SL_PP_NOTEQUAL
, &p
) == 0) {
1612 if (_parse_relational_expression(ctx
, &p
)) {
1615 _emit(ctx
, &p
.out
, op
);
1621 _parse_logical_and_expression(struct parse_context
*ctx
,
1622 struct parse_state
*ps
)
1624 struct parse_state p
= *ps
;
1626 if (_parse_equality_expression(ctx
, &p
)) {
1631 if (_parse_token(ctx
, SL_PP_AND
, &p
)) {
1634 if (_parse_equality_expression(ctx
, &p
)) {
1637 _emit(ctx
, &p
.out
, OP_LOGICALAND
);
1643 _parse_logical_xor_expression(struct parse_context
*ctx
,
1644 struct parse_state
*ps
)
1646 struct parse_state p
= *ps
;
1648 if (_parse_logical_and_expression(ctx
, &p
)) {
1653 if (_parse_token(ctx
, SL_PP_XOR
, &p
)) {
1656 if (_parse_logical_and_expression(ctx
, &p
)) {
1659 _emit(ctx
, &p
.out
, OP_LOGICALXOR
);
1665 _parse_logical_or_expression(struct parse_context
*ctx
,
1666 struct parse_state
*ps
)
1668 struct parse_state p
= *ps
;
1670 if (_parse_logical_xor_expression(ctx
, &p
)) {
1675 if (_parse_token(ctx
, SL_PP_OR
, &p
)) {
1678 if (_parse_logical_xor_expression(ctx
, &p
)) {
1681 _emit(ctx
, &p
.out
, OP_LOGICALOR
);
1687 _parse_conditional_expression(struct parse_context
*ctx
,
1688 struct parse_state
*ps
)
1690 struct parse_state p
= *ps
;
1692 if (_parse_logical_or_expression(ctx
, &p
)) {
1697 if (_parse_token(ctx
, SL_PP_QUESTION
, &p
)) {
1700 if (_parse_expression(ctx
, &p
)) {
1703 if (_parse_token(ctx
, SL_PP_COLON
, &p
)) {
1706 if (_parse_conditional_expression(ctx
, &p
)) {
1709 _emit(ctx
, &p
.out
, OP_SELECT
);
1715 _parse_constant_expression(struct parse_context
*ctx
,
1716 struct parse_state
*ps
)
1718 if (_parse_conditional_expression(ctx
, ps
)) {
1721 _emit(ctx
, &ps
->out
, OP_END
);
1727 _parse_parameter_declarator_array(struct parse_context
*ctx
,
1728 struct parse_state
*ps
)
1730 struct parse_state p
= *ps
;
1732 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1735 if (_parse_constant_expression(ctx
, &p
)) {
1736 _error(ctx
, "expected constant integral expression");
1739 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1740 _error(ctx
, "expected `]'");
1749 _parse_parameter_declarator(struct parse_context
*ctx
,
1750 struct parse_state
*ps
)
1752 struct parse_state p
= *ps
;
1755 if (_parse_type_specifier(ctx
, &p
)) {
1758 if (_parse_identifier(ctx
, &p
)) {
1761 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1762 if (_parse_parameter_declarator_array(ctx
, &p
)) {
1763 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1771 _parse_parameter_type_specifier_array(struct parse_context
*ctx
,
1772 struct parse_state
*ps
)
1774 struct parse_state p
= *ps
;
1776 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1779 if (_parse_constant_expression(ctx
, &p
)) {
1780 _error(ctx
, "expected constant integral expression");
1783 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1784 _error(ctx
, "expected `]'");
1793 _parse_parameter_type_specifier(struct parse_context
*ctx
,
1794 struct parse_state
*ps
)
1796 struct parse_state p
= *ps
;
1799 if (_parse_type_specifier(ctx
, &p
)) {
1802 _emit(ctx
, &p
.out
, '\0');
1804 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1805 if (_parse_parameter_type_specifier_array(ctx
, &p
)) {
1806 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1814 _parse_parameter_declaration(struct parse_context
*ctx
,
1815 struct parse_state
*ps
)
1817 struct parse_state p
= *ps
;
1818 unsigned int e
= _emit(ctx
, &p
.out
, PARAMETER_NEXT
);
1822 if (_parse_storage_qualifier(ctx
, &p
)) {
1823 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1825 _parse_parameter_qualifier(ctx
, &p
);
1826 if (_parse_precision(ctx
, &p
)) {
1827 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1829 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1833 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1843 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1844 struct parse_state
*ps
)
1846 struct parse_state p
= *ps
;
1848 if (_parse_function_header(ctx
, &p
)) {
1851 if (_parse_parameter_declaration(ctx
, &p
)) {
1857 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1860 if (_parse_parameter_declaration(ctx
, &p
)) {
1868 _parse_function_declarator(struct parse_context
*ctx
,
1869 struct parse_state
*ps
)
1871 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1875 if (_parse_function_header(ctx
, ps
) == 0) {
1884 _parse_function_prototype(struct parse_context
*ctx
,
1885 struct parse_state
*ps
)
1887 struct parse_state p
= *ps
;
1889 if (_parse_function_header(ctx
, &p
) == 0) {
1890 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1891 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1892 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1896 _error(ctx
, "expected `)'");
1902 if (_parse_function_declarator(ctx
, &p
) == 0) {
1903 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1904 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1908 _error(ctx
, "expected `)'");
1917 _parse_precision(struct parse_context
*ctx
,
1918 struct parse_state
*ps
)
1920 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1922 unsigned int precision
;
1924 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1927 id
= input
->data
.identifier
;
1929 if (id
== ctx
->dict
.lowp
) {
1930 precision
= PRECISION_LOW
;
1931 } else if (id
== ctx
->dict
.mediump
) {
1932 precision
= PRECISION_MEDIUM
;
1933 } else if (id
== ctx
->dict
.highp
) {
1934 precision
= PRECISION_HIGH
;
1939 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1940 _emit(ctx
, &ps
->out
, precision
);
1946 _parse_prectype(struct parse_context
*ctx
,
1947 struct parse_state
*ps
)
1949 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
1953 if (!input
|| input
->token
!= SL_PP_IDENTIFIER
) {
1956 id
= input
->data
.identifier
;
1958 if (id
== ctx
->dict
._int
) {
1959 type
= TYPE_SPECIFIER_INT
;
1960 } else if (id
== ctx
->dict
._float
) {
1961 type
= TYPE_SPECIFIER_FLOAT
;
1962 } else if (id
== ctx
->dict
.sampler1D
) {
1963 type
= TYPE_SPECIFIER_SAMPLER1D
;
1964 } else if (id
== ctx
->dict
.sampler2D
) {
1965 type
= TYPE_SPECIFIER_SAMPLER2D
;
1966 } else if (id
== ctx
->dict
.sampler3D
) {
1967 type
= TYPE_SPECIFIER_SAMPLER3D
;
1968 } else if (id
== ctx
->dict
.samplerCube
) {
1969 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1970 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1971 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1972 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1973 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1974 } else if (id
== ctx
->dict
.sampler2DRect
) {
1975 if (!ctx
->texture_rectangle
) {
1976 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1977 "in order to use a rect sampler");
1980 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1981 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1982 if (!ctx
->texture_rectangle
) {
1983 _error(ctx
, "GL_ARB_texture_rectangle extension must be enabled "
1984 "in order to use a rect sampler");
1987 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1988 } else if (id
== ctx
->dict
.sampler1DArray
) {
1989 type
= TYPE_SPECIFIER_SAMPLER_1D_ARRAY
;
1990 } else if (id
== ctx
->dict
.sampler2DArray
) {
1991 type
= TYPE_SPECIFIER_SAMPLER_2D_ARRAY
;
1992 } else if (id
== ctx
->dict
.sampler1DArrayShadow
) {
1993 type
= TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
;
1994 } else if (id
== ctx
->dict
.sampler2DArrayShadow
) {
1995 type
= TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
;
2000 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
2001 _emit(ctx
, &ps
->out
, type
);
2007 _parse_precision_stmt(struct parse_context
*ctx
,
2008 struct parse_state
*ps
)
2010 struct parse_state p
= *ps
;
2012 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
2015 if (_parse_precision(ctx
, &p
)) {
2018 if (_parse_prectype(ctx
, &p
)) {
2021 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2030 _parse_floatconstant(struct parse_context
*ctx
,
2031 struct parse_state
*ps
)
2033 struct parse_state p
= *ps
;
2035 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
2036 if (_parse_float(ctx
, &p
)) {
2045 _parse_intconstant(struct parse_context
*ctx
,
2046 struct parse_state
*ps
)
2048 struct parse_state p
= *ps
;
2050 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
2051 if (_parse_uint(ctx
, &p
)) {
2060 _parse_boolconstant(struct parse_context
*ctx
,
2061 struct parse_state
*ps
)
2063 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
2064 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2065 _emit(ctx
, &ps
->out
, 2); /* radix */
2066 _emit(ctx
, &ps
->out
, '0');
2067 _emit(ctx
, &ps
->out
, '\0');
2071 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
2072 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
2073 _emit(ctx
, &ps
->out
, 2); /* radix */
2074 _emit(ctx
, &ps
->out
, '1');
2075 _emit(ctx
, &ps
->out
, '\0');
2084 _parse_variable_identifier(struct parse_context
*ctx
,
2085 struct parse_state
*ps
)
2087 struct parse_state p
= *ps
;
2089 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
2090 if (_parse_identifier(ctx
, &p
)) {
2099 _parse_primary_expression(struct parse_context
*ctx
,
2100 struct parse_state
*ps
)
2102 struct parse_state p
;
2104 if (_parse_floatconstant(ctx
, ps
) == 0) {
2107 if (_parse_boolconstant(ctx
, ps
) == 0) {
2110 if (_parse_intconstant(ctx
, ps
) == 0) {
2113 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2118 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2121 if (_parse_expression(ctx
, &p
)) {
2124 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2134 _parse_asm_argument(struct parse_context
*ctx
,
2135 struct parse_state
*ps
)
2137 if (_parse_variable_identifier(ctx
, ps
) == 0) {
2138 struct parse_state p
= *ps
;
2140 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
2143 _emit(ctx
, &p
.out
, OP_FIELD
);
2144 if (_parse_identifier(ctx
, &p
)) {
2151 if (_parse_floatconstant(ctx
, ps
) == 0) {
2160 _parse_asm_arguments(struct parse_context
*ctx
,
2161 struct parse_state
*ps
)
2163 struct parse_state p
= *ps
;
2165 if (_parse_asm_argument(ctx
, &p
)) {
2168 _emit(ctx
, &p
.out
, OP_END
);
2172 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2175 if (_parse_asm_argument(ctx
, &p
)) {
2178 _emit(ctx
, &p
.out
, OP_END
);
2184 _parse_asm_statement(struct parse_context
*ctx
,
2185 struct parse_state
*ps
)
2187 struct parse_state p
= *ps
;
2189 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
2192 if (_parse_identifier(ctx
, &p
)) {
2195 /* optional arguments */
2196 _parse_asm_arguments(ctx
, &p
);
2197 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2200 _emit(ctx
, &p
.out
, OP_END
);
2207 _parse_selection_statement(struct parse_context
*ctx
,
2208 struct parse_state
*ps
)
2210 struct parse_state p
= *ps
;
2212 _emit(ctx
, &p
.out
, OP_IF
);
2213 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
2216 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2217 _error(ctx
, "expected `('");
2220 if (_parse_expression(ctx
, &p
)) {
2221 _error(ctx
, "expected an expression");
2224 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2225 _error(ctx
, "expected `)'");
2228 _emit(ctx
, &p
.out
, OP_END
);
2229 if (_parse_statement(ctx
, &p
)) {
2234 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
2235 if (_parse_statement(ctx
, &p
) == 0) {
2241 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
2242 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
2243 _emit(ctx
, &ps
->out
, OP_END
);
2249 _parse_expression_statement(struct parse_context
*ctx
,
2250 struct parse_state
*ps
)
2252 struct parse_state p
= *ps
;
2254 if (_parse_expression(ctx
, &p
)) {
2255 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2257 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2260 _emit(ctx
, &p
.out
, OP_END
);
2267 _parse_for_init_statement(struct parse_context
*ctx
,
2268 struct parse_state
*ps
)
2270 struct parse_state p
= *ps
;
2271 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2273 if (_parse_expression_statement(ctx
, &p
) == 0) {
2278 if (_parse_declaration(ctx
, &p
) == 0) {
2279 _update(ctx
, e
, OP_DECLARE
);
2289 _parse_initializer(struct parse_context
*ctx
,
2290 struct parse_state
*ps
)
2292 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2293 _emit(ctx
, &ps
->out
, OP_END
);
2301 _parse_condition_initializer(struct parse_context
*ctx
,
2302 struct parse_state
*ps
)
2304 struct parse_state p
= *ps
;
2306 _emit(ctx
, &p
.out
, OP_DECLARE
);
2307 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2308 if (_parse_fully_specified_type(ctx
, &p
)) {
2311 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2312 if (_parse_identifier(ctx
, &p
)) {
2315 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2316 _error(ctx
, "expected `='");
2319 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2320 if (_parse_initializer(ctx
, &p
)) {
2321 _error(ctx
, "expected an initialiser");
2324 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2331 _parse_condition(struct parse_context
*ctx
,
2332 struct parse_state
*ps
)
2334 struct parse_state p
;
2336 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2341 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2342 if (_parse_expression(ctx
, &p
) == 0) {
2343 _emit(ctx
, &p
.out
, OP_END
);
2353 _parse_for_rest_statement(struct parse_context
*ctx
,
2354 struct parse_state
*ps
)
2356 struct parse_state p
= *ps
;
2358 if (_parse_condition(ctx
, &p
)) {
2359 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2360 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2361 _emit(ctx
, &p
.out
, 2);
2362 _emit(ctx
, &p
.out
, '1');
2363 _emit(ctx
, &p
.out
, '\0');
2364 _emit(ctx
, &p
.out
, OP_END
);
2366 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2369 if (_parse_expression(ctx
, &p
)) {
2370 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2372 _emit(ctx
, &p
.out
, OP_END
);
2379 _parse_iteration_statement(struct parse_context
*ctx
,
2380 struct parse_state
*ps
)
2382 struct parse_state p
= *ps
;
2384 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2385 _emit(ctx
, &p
.out
, OP_WHILE
);
2386 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2387 _error(ctx
, "expected `('");
2390 if (_parse_condition(ctx
, &p
)) {
2391 _error(ctx
, "expected an expression");
2394 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2395 _error(ctx
, "expected `)'");
2398 if (_parse_statement(ctx
, &p
)) {
2405 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2406 _emit(ctx
, &p
.out
, OP_DO
);
2407 if (_parse_statement(ctx
, &p
)) {
2410 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2413 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2414 _error(ctx
, "expected `('");
2417 if (_parse_expression(ctx
, &p
)) {
2418 _error(ctx
, "expected an expression");
2421 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2422 _error(ctx
, "expected `)'");
2425 _emit(ctx
, &p
.out
, OP_END
);
2426 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2427 _error(ctx
, "expected `;'");
2434 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2435 _emit(ctx
, &p
.out
, OP_FOR
);
2436 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2437 _error(ctx
, "expected `('");
2440 if (_parse_for_init_statement(ctx
, &p
)) {
2443 if (_parse_for_rest_statement(ctx
, &p
)) {
2446 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2447 _error(ctx
, "expected `)'");
2450 if (_parse_statement(ctx
, &p
)) {
2462 _parse_jump_statement(struct parse_context
*ctx
,
2463 struct parse_state
*ps
)
2465 struct parse_state p
= *ps
;
2466 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2468 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2469 _update(ctx
, e
, OP_CONTINUE
);
2470 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2471 _update(ctx
, e
, OP_BREAK
);
2472 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2473 _update(ctx
, e
, OP_RETURN
);
2474 if (_parse_expression(ctx
, &p
)) {
2475 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2477 _emit(ctx
, &p
.out
, OP_END
);
2478 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2479 _update(ctx
, e
, OP_DISCARD
);
2483 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2492 _parse_simple_statement(struct parse_context
*ctx
,
2493 struct parse_state
*ps
)
2495 struct parse_state p
;
2498 if (_parse_selection_statement(ctx
, ps
) == 0) {
2502 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2506 if (_parse_jump_statement(ctx
, ps
) == 0) {
2511 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2512 if (_parse_expression_statement(ctx
, &p
) == 0) {
2517 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2518 _update(ctx
, e
, OP_PRECISION
);
2523 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2524 _update(ctx
, e
, OP_ASM
);
2529 if (_parse_declaration(ctx
, &p
) == 0) {
2530 _update(ctx
, e
, OP_DECLARE
);
2540 _parse_compound_statement(struct parse_context
*ctx
,
2541 struct parse_state
*ps
)
2543 struct parse_state p
= *ps
;
2545 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2548 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2549 _parse_statement_list(ctx
, &p
);
2550 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2553 _emit(ctx
, &p
.out
, OP_END
);
2560 _parse_statement(struct parse_context
*ctx
,
2561 struct parse_state
*ps
)
2563 if (_parse_compound_statement(ctx
, ps
) == 0) {
2567 if (_parse_simple_statement(ctx
, ps
) == 0) {
2576 _parse_statement_list(struct parse_context
*ctx
,
2577 struct parse_state
*ps
)
2579 struct parse_state p
= *ps
;
2581 if (_parse_statement(ctx
, &p
)) {
2587 if (_parse_statement(ctx
, &p
)) {
2595 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2596 struct parse_state
*ps
)
2598 struct parse_state p
= *ps
;
2600 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2603 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2604 _parse_statement_list(ctx
, &p
);
2605 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2608 _emit(ctx
, &p
.out
, OP_END
);
2615 _parse_function_definition(struct parse_context
*ctx
,
2616 struct parse_state
*ps
)
2618 struct parse_state p
= *ps
;
2620 if (_parse_function_prototype(ctx
, &p
)) {
2623 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2632 _parse_invariant_stmt(struct parse_context
*ctx
,
2633 struct parse_state
*ps
)
2635 struct parse_state p
= *ps
;
2637 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2640 if (_parse_identifier(ctx
, &p
)) {
2643 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2652 _parse_single_declaration(struct parse_context
*ctx
,
2653 struct parse_state
*ps
)
2655 struct parse_state p
= *ps
;
2658 if (_parse_fully_specified_type(ctx
, &p
)) {
2662 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2663 if (_parse_identifier(ctx
, &p
)) {
2664 _update(ctx
, e
, VARIABLE_NONE
);
2669 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2672 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2673 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2674 if (_parse_initializer(ctx
, &p
) == 0) {
2678 _error(ctx
, "expected an initialiser");
2683 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2684 if (_parse_constant_expression(ctx
, &p
)) {
2685 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2687 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2689 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2693 _error(ctx
, "expected `]'");
2701 _parse_init_declarator_list(struct parse_context
*ctx
,
2702 struct parse_state
*ps
)
2704 struct parse_state p
= *ps
;
2706 if (_parse_single_declaration(ctx
, &p
)) {
2714 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2717 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2718 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2719 if (_parse_identifier(ctx
, &p
)) {
2723 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2726 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2727 if (_parse_initializer(ctx
, &p
) == 0) {
2728 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2732 _error(ctx
, "expected an initialiser");
2737 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2740 if (_parse_constant_expression(ctx
, &p
)) {
2741 arr
= VARIABLE_ARRAY_UNKNOWN
;
2743 arr
= VARIABLE_ARRAY_EXPLICIT
;
2745 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2746 _update(ctx
, e
, arr
);
2750 _error(ctx
, "expected `]'");
2756 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2762 _parse_declaration(struct parse_context
*ctx
,
2763 struct parse_state
*ps
)
2765 struct parse_state p
= *ps
;
2766 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2768 if (_parse_function_prototype(ctx
, &p
)) {
2769 if (_parse_init_declarator_list(ctx
, &p
)) {
2772 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2774 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2775 _error(ctx
, "expected `;'");
2784 _parse_external_declaration(struct parse_context
*ctx
,
2785 struct parse_state
*ps
)
2787 struct parse_state p
= *ps
;
2788 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2790 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2791 _update(ctx
, e
, DEFAULT_PRECISION
);
2796 if (_parse_function_definition(ctx
, &p
) == 0) {
2797 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2802 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2803 _update(ctx
, e
, INVARIANT_STMT
);
2808 if (_parse_declaration(ctx
, &p
) == 0) {
2809 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2814 _error(ctx
, "expected an identifier");
2820 _parse_extensions(struct parse_context
*ctx
,
2821 struct parse_state
*ps
)
2824 const struct sl_pp_token_info
*input
= _fetch_token(ctx
, ps
->in
);
2825 unsigned int enable
;
2831 switch (input
->token
) {
2832 case SL_PP_EXTENSION_REQUIRE
:
2833 case SL_PP_EXTENSION_ENABLE
:
2834 case SL_PP_EXTENSION_WARN
:
2837 case SL_PP_EXTENSION_DISABLE
:
2845 if (input
->data
.extension
== ctx
->dict
.all
) {
2846 ctx
->fragment_coord_conventions
= enable
;
2848 else if (input
->data
.extension
== ctx
->dict
._GL_ARB_fragment_coord_conventions
) {
2849 ctx
->fragment_coord_conventions
= enable
;
2851 else if (input
->data
.extension
== ctx
->dict
._GL_ARB_texture_rectangle
) {
2852 ctx
->texture_rectangle
= enable
;
2859 _parse_translation_unit(struct parse_context
*ctx
,
2860 struct parse_state
*ps
)
2862 _emit(ctx
, &ps
->out
, REVISION
);
2863 if (_parse_extensions(ctx
, ps
)) {
2866 if (_parse_external_declaration(ctx
, ps
)) {
2870 if (_parse_extensions(ctx
, ps
)) {
2873 if (_parse_external_declaration(ctx
, ps
)) {
2877 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2878 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2885 #define ADD_NAME_STR(CTX, NAME, STR)\
2887 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2888 if ((CTX).dict.NAME == -1) {\
2893 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2897 sl_cl_compile(struct sl_pp_context
*context
,
2898 unsigned int shader_type
,
2899 unsigned int parsing_builtin
,
2900 unsigned char **output
,
2901 unsigned int *cboutput
,
2903 unsigned int cberror
)
2905 struct parse_context ctx
;
2906 struct parse_state ps
;
2908 ctx
.context
= context
;
2910 ADD_NAME_STR(ctx
, _void
, "void");
2911 ADD_NAME_STR(ctx
, _float
, "float");
2912 ADD_NAME_STR(ctx
, _int
, "int");
2913 ADD_NAME_STR(ctx
, _bool
, "bool");
2914 ADD_NAME(ctx
, vec2
);
2915 ADD_NAME(ctx
, vec3
);
2916 ADD_NAME(ctx
, vec4
);
2917 ADD_NAME(ctx
, bvec2
);
2918 ADD_NAME(ctx
, bvec3
);
2919 ADD_NAME(ctx
, bvec4
);
2920 ADD_NAME(ctx
, ivec2
);
2921 ADD_NAME(ctx
, ivec3
);
2922 ADD_NAME(ctx
, ivec4
);
2923 ADD_NAME(ctx
, mat2
);
2924 ADD_NAME(ctx
, mat3
);
2925 ADD_NAME(ctx
, mat4
);
2926 ADD_NAME(ctx
, mat2x3
);
2927 ADD_NAME(ctx
, mat3x2
);
2928 ADD_NAME(ctx
, mat2x4
);
2929 ADD_NAME(ctx
, mat4x2
);
2930 ADD_NAME(ctx
, mat3x4
);
2931 ADD_NAME(ctx
, mat4x3
);
2932 ADD_NAME(ctx
, sampler1D
);
2933 ADD_NAME(ctx
, sampler2D
);
2934 ADD_NAME(ctx
, sampler3D
);
2935 ADD_NAME(ctx
, samplerCube
);
2936 ADD_NAME(ctx
, sampler1DShadow
);
2937 ADD_NAME(ctx
, sampler2DShadow
);
2938 ADD_NAME(ctx
, sampler2DRect
);
2939 ADD_NAME(ctx
, sampler2DRectShadow
);
2940 ADD_NAME(ctx
, sampler1DArray
);
2941 ADD_NAME(ctx
, sampler2DArray
);
2942 ADD_NAME(ctx
, sampler1DArrayShadow
);
2943 ADD_NAME(ctx
, sampler2DArrayShadow
);
2945 ADD_NAME(ctx
, invariant
);
2947 ADD_NAME(ctx
, centroid
);
2949 ADD_NAME(ctx
, precision
);
2950 ADD_NAME(ctx
, lowp
);
2951 ADD_NAME(ctx
, mediump
);
2952 ADD_NAME(ctx
, highp
);
2954 ADD_NAME_STR(ctx
, _const
, "const");
2955 ADD_NAME(ctx
, attribute
);
2956 ADD_NAME(ctx
, varying
);
2957 ADD_NAME(ctx
, uniform
);
2958 ADD_NAME(ctx
, __fixed_output
);
2959 ADD_NAME(ctx
, __fixed_input
);
2963 ADD_NAME(ctx
, inout
);
2965 ADD_NAME(ctx
, layout
);
2966 ADD_NAME(ctx
, origin_upper_left
);
2967 ADD_NAME(ctx
, pixel_center_integer
);
2969 ADD_NAME_STR(ctx
, _struct
, "struct");
2971 ADD_NAME(ctx
, __constructor
);
2972 ADD_NAME(ctx
, __operator
);
2973 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2975 ADD_NAME_STR(ctx
, _if
, "if");
2976 ADD_NAME_STR(ctx
, _else
, "else");
2977 ADD_NAME_STR(ctx
, _for
, "for");
2978 ADD_NAME_STR(ctx
, _while
, "while");
2979 ADD_NAME_STR(ctx
, _do
, "do");
2981 ADD_NAME_STR(ctx
, _continue
, "continue");
2982 ADD_NAME_STR(ctx
, _break
, "break");
2983 ADD_NAME_STR(ctx
, _return
, "return");
2984 ADD_NAME(ctx
, discard
);
2986 ADD_NAME_STR(ctx
, _false
, "false");
2987 ADD_NAME_STR(ctx
, _true
, "true");
2990 ADD_NAME_STR(ctx
, _GL_ARB_fragment_coord_conventions
, "GL_ARB_fragment_coord_conventions");
2991 ADD_NAME_STR(ctx
, _GL_ARB_texture_rectangle
, "GL_ARB_texture_rectangle");
2996 ctx
.shader_type
= shader_type
;
2997 ctx
.parsing_builtin
= 1;
2999 ctx
.fragment_coord_conventions
= 0;
3000 ctx
.texture_rectangle
= 1;
3002 ctx
.error
[0] = '\0';
3003 ctx
.process_error
= 0;
3005 ctx
.tokens_cap
= 1024;
3006 ctx
.tokens_read
= 0;
3007 ctx
.tokens
= malloc(ctx
.tokens_cap
* sizeof(struct sl_pp_token_info
));
3009 strncpy(error
, "out of memory", cberror
- 1);
3010 error
[cberror
- 1] = '\0';
3017 if (_parse_translation_unit(&ctx
, &ps
)) {
3018 strncpy(error
, ctx
.error
, cberror
);
3023 *output
= ctx
.out_buf
;