1 /* pkl-ast.h - Abstract Syntax Tree for Poke.  */
2 
3 /* Copyright (C) 2019, 2020, 2021 Jose E. Marchesi */
4 
5 /* This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef PKL_AST_H
20 #define PKL_AST_H
21 
22 #include <config.h>
23 
24 #include <stdio.h>
25 #include <stdint.h>
26 
27 #include "pvm.h" /* For pvm_val */
28 
29 /* The following enumeration defines the codes characterizing the
30    several types of nodes supported in the PKL abstract syntax
31    trees.  */
32 
33 enum pkl_ast_code
34 {
35   PKL_AST_PROGRAM,
36   PKL_AST_SRC,
37   /* Expressions.  */
38   PKL_AST_FIRST_EXP,
39   PKL_AST_EXP = PKL_AST_FIRST_EXP,
40   PKL_AST_COND_EXP,
41   PKL_AST_INTEGER,
42   PKL_AST_STRING,
43   PKL_AST_IDENTIFIER,
44   PKL_AST_ARRAY,
45   PKL_AST_ARRAY_INITIALIZER,
46   PKL_AST_INDEXER,
47   PKL_AST_TRIMMER,
48   PKL_AST_STRUCT,
49   PKL_AST_STRUCT_FIELD,
50   PKL_AST_STRUCT_REF,
51   PKL_AST_OFFSET,
52   PKL_AST_CAST,
53   PKL_AST_ISA,
54   PKL_AST_MAP,
55   PKL_AST_CONS,
56   PKL_AST_FUNCALL,
57   PKL_AST_FUNCALL_ARG,
58   PKL_AST_VAR,
59   PKL_AST_LAMBDA,
60   PKL_AST_INCRDECR,
61   PKL_AST_GCD,
62   PKL_AST_LAST_EXP = PKL_AST_GCD,
63   /* Types.  */
64   PKL_AST_TYPE,
65   PKL_AST_STRUCT_TYPE_FIELD,
66   PKL_AST_FUNC_TYPE_ARG,
67   PKL_AST_ENUM,
68   PKL_AST_ENUMERATOR,
69   /* Functions.  */
70   PKL_AST_FUNC,
71   PKL_AST_FUNC_ARG,
72   /* Declarations.  */
73   PKL_AST_DECL,
74   /* Statements.  */
75   PKL_AST_FIRST_STMT,
76   PKL_AST_COMP_STMT = PKL_AST_FIRST_STMT,
77   PKL_AST_NULL_STMT,
78   PKL_AST_ASS_STMT,
79   PKL_AST_IF_STMT,
80   PKL_AST_LOOP_STMT,
81   PKL_AST_LOOP_STMT_ITERATOR,
82   PKL_AST_RETURN_STMT,
83   PKL_AST_EXP_STMT,
84   PKL_AST_TRY_CATCH_STMT,
85   PKL_AST_TRY_UNTIL_STMT,
86   PKL_AST_PRINT_STMT,
87   PKL_AST_BREAK_STMT,
88   PKL_AST_CONTINUE_STMT,
89   PKL_AST_RAISE_STMT,
90   PKL_AST_LAST_STMT = PKL_AST_RAISE_STMT,
91   PKL_AST_PRINT_STMT_ARG,
92   PKL_AST_LAST
93 };
94 
95 /* The following macros implement some node code categories.  */
96 
97 #define PKL_AST_IS_EXP(AST)                              \
98   (PKL_AST_CODE ((AST)) >= PKL_AST_FIRST_EXP             \
99    && PKL_AST_CODE ((AST)) <= PKL_AST_LAST_EXP)
100 
101 #define PKL_AST_IS_STMT(AST)                            \
102   (PKL_AST_CODE ((AST)) >= PKL_AST_FIRST_STMT            \
103    && PKL_AST_CODE ((AST)) <= PKL_AST_LAST_STMT)
104 
105 /* The AST nodes representing expressions are characterized by
106    operators (see below in this file for more details on this.)  The
107    following enumeration defines the operator codes.
108 
109    The definitions of the operators are in pkl-ops.def.  */
110 
111 #define PKL_DEF_OP(SYM, STRING) SYM,
112 enum pkl_ast_op
113 {
114 #include "pkl-ops.def"
115  PKL_AST_OP_LAST
116 };
117 #undef PKL_DEF_OP
118 
119 /* Similarly, attribute operators are characterized by an attribute.
120    The following enumeration defines the attribute codes.
121 
122    The definitions of the attributes are in pkl-attrs.def.  */
123 
124 #define PKL_DEF_ATTR(SYM, STRING) SYM,
125 enum pkl_ast_attr
126 {
127 #include "pkl-attrs.def"
128  PKL_AST_ATTR_NONE
129 };
130 #undef PKL_DEF_ATTR
131 
132 /* Certain AST nodes can be characterized of featuring a byte
133    endianness.  The following values are supported:
134 
135    DFL means the default endianness, which is the endianness used by
136    the system running poke.
137 
138    MSB means that the most significative bytes come first.  This is
139    what is popularly known as big-endian.
140 
141    LSB means that the least significative bytes come first.  This is
142    what is known as little-endian.
143 
144    In both endiannesses the bits inside the bytes are ordered from
145    most significative to least significative.  */
146 
147 enum pkl_ast_endian
148 {
149   PKL_AST_ENDIAN_DFL, /* Default endian.  */
150   PKL_AST_ENDIAN_MSB, /* Big-endian.  */
151   PKL_AST_ENDIAN_LSB  /* Little-endian.  */
152 };
153 
154 enum pkl_ast_type_code
155 {
156   PKL_TYPE_INTEGRAL,
157   PKL_TYPE_STRING,
158   PKL_TYPE_VOID,
159   PKL_TYPE_ARRAY,
160   PKL_TYPE_STRUCT,
161   PKL_TYPE_FUNCTION,
162   PKL_TYPE_OFFSET,
163   PKL_TYPE_ANY,
164   PKL_TYPE_NOTYPE,
165 };
166 
167 /* Next we define the several supported types of nodes in the abstract
168    syntax tree, which are discriminated using the codes defined in the
169    `pkl_ast_code' enumeration above.
170 
171    Accessor macros are defined to access the attributes of the
172    different nodes, and should be used as both l-values and r-values
173    to inspect and modify nodes, respectively.
174 
175    Declarations for constructor functions are also provided, that can
176    be used to create new instances of nodes.  */
177 
178 typedef union pkl_ast_node *pkl_ast_node;
179 
180 /* The `pkl_ast_common' struct defines fields which are common to
181    every node in the AST, regardless of their type.
182 
183    AST is a pointer to the `pkl_ast' structure containing this node.
184 
185    UID is an unique identifier that characterizes the node.  It is
186    allocated when the node is created, and then never reused again.
187 
188    CHAIN and CHAIN2 are used to chain AST nodes together.  This serves
189    several purposes in the compiler:
190 
191    CHAIN is used to form sibling relationships in the tree.
192 
193    CHAIN2 is used to link nodes together in containers, such as hash
194    table buckets, and frames.
195 
196    The `pkl_ast_chainon' utility function is provided in order to
197    confortably add elements to a list of nodes.  It operates on CHAIN,
198    not CHAIN2.
199 
200    CODE identifies the type of node.
201 
202    LOC is the location in the source program of the entity represented
203    by the node.  It is the task of the parser to fill in this
204    information, which is used in error reporting.
205 
206    The LITERAL_P flag is used in expression nodes, and tells whether
207    the expression is constant, i.e. whether the value of the
208    expression can be calculated at compile time.  This is used to
209    implement some optimizations.
210 
211    It is possible for a node to be referred from more than one place.
212    To manage memory, we use a REFCOUNT that is initially 0.  The
213    ASTREF macro defined below tells the node a new reference is being
214    made.
215 
216    There is no constructor defined for common nodes.  */
217 
218 #define PKL_AST_AST(AST) ((AST)->common.ast)
219 #define PKL_AST_UID(AST) ((AST)->common.uid)
220 #define PKL_AST_CHAIN(AST) ((AST)->common.chain)
221 #define PKL_AST_TYPE(AST) ((AST)->common.type)
222 #define PKL_AST_CHAIN2(AST) ((AST)->common.chain2)
223 #define PKL_AST_CODE(AST) ((AST)->common.code)
224 #define PKL_AST_LOC(AST) ((AST)->common.loc)
225 #define PKL_AST_LITERAL_P(AST) ((AST)->common.literal_p)
226 #define PKL_AST_REGISTERED_P(AST) ((AST)->common.registered_p)
227 #define PKL_AST_REFCOUNT(AST) ((AST)->common.refcount)
228 
229 struct pkl_ast_loc
230 {
231   int first_line;
232   int first_column;
233   int last_line;
234   int last_column;
235 };
236 
237 typedef struct pkl_ast_loc pkl_ast_loc;
238 
239 static struct pkl_ast_loc PKL_AST_NOLOC __attribute__((unused))
240    = { 0, 0, 0, 0 };
241 
242 #define PKL_AST_LOC_VALID(L)                    \
243   (!((L).first_line == 0                        \
244      && (L).first_column == 0                   \
245      && (L).last_line == 0                      \
246      && (L).last_column == 0))
247 
248 struct pkl_ast_common
249 {
250   struct pkl_ast *ast;
251   uint64_t uid;
252   union pkl_ast_node *chain;
253   union pkl_ast_node *type;
254   union pkl_ast_node *chain2;
255   enum pkl_ast_code code : 8;
256   struct pkl_ast_loc loc;
257   int refcount;
258 
259   unsigned literal_p : 1;
260 };
261 
262 pkl_ast_node pkl_ast_chainon (pkl_ast_node ast1,
263                               pkl_ast_node ast2);
264 
265 size_t pkl_ast_chain_length (pkl_ast_node ast);
266 
267 typedef struct pkl_ast *pkl_ast; /* Forward declaration. */
268 
269 /* PKL_AST_PROGRAM nodes represent PKL programs.
270 
271    ELEMS points to a possibly empty list of struct definitions,
272    enumerations, and expressions i.e. to nodes of types
273    PKL_AST_STRUCT, PKL_AST_ENUM and PKL_AST_EXP respectively.  */
274 
275 #define PKL_AST_PROGRAM_ELEMS(AST) ((AST)->program.elems)
276 
277 struct pkl_ast_program
278 {
279   struct pkl_ast_common common;
280   union pkl_ast_node *elems;
281 };
282 
283 pkl_ast_node pkl_ast_make_program (pkl_ast ast,
284                                    pkl_ast_node declarations);
285 
286 /* PKL_AST_SRC nodes represent a change in the source file.
287 
288    FILENAME is either NULL or a C NULL-terminated string.  In this
289    context NULL denotes that the current source file is stdin.  */
290 
291 #define PKL_AST_SRC_FILENAME(AST) ((AST)->src.filename)
292 
293 struct pkl_ast_src
294 {
295   struct pkl_ast_common common;
296   char *filename;
297 };
298 
299 pkl_ast_node pkl_ast_make_src (pkl_ast ast, const char *filename);
300 
301 /* PKL_AST_IDENTIFIER nodes represent identifiers in PKL programs.
302 
303    POINTER must point to a NULL-terminated string.
304 
305    LENGTH contains the size in bytes of the identifier
306 
307    BACK and OVER conform the lexical address to find the storage of
308    the entity represented by the identifier.  See pkl-env.h for a
309    description of these.  */
310 
311 #define PKL_AST_IDENTIFIER_LENGTH(AST) ((AST)->identifier.length)
312 #define PKL_AST_IDENTIFIER_POINTER(AST) ((AST)->identifier.pointer)
313 #define PKL_AST_IDENTIFIER_BACK(AST) ((AST)->identifier.back)
314 #define PKL_AST_IDENTIFIER_OVER(AST) ((AST)->identifier.over)
315 
316 struct pkl_ast_identifier
317 {
318   struct pkl_ast_common common;
319   size_t length;
320   char *pointer;
321   int back;
322   int over;
323 };
324 
325 pkl_ast_node pkl_ast_make_identifier (pkl_ast ast,
326                                       const char *str);
327 
328 /* PKL_AST_INTEGER nodes represent integer constants in poke programs.
329 
330    VALUE contains a 64-bit unsigned integer.  This contains the
331    encoding of a Poke integer, which may be signed or unsigned.  The
332    lexer generates only unsigned integers.  */
333 
334 #define PKL_AST_INTEGER_VALUE(AST) ((AST)->integer.value)
335 
336 struct pkl_ast_integer
337 {
338   struct pkl_ast_common common;
339   uint64_t value;
340 };
341 
342 pkl_ast_node pkl_ast_make_integer (pkl_ast ast,
343                                    uint64_t value);
344 
345 /* PKL_AST_STRING nodes represent string literals in PKL programs.
346 
347    POINTER must point to a NULL-terminated string.
348    LENGTH contains the size in bytes of the string.  */
349 
350 #define PKL_AST_STRING_LENGTH(AST) ((AST)->string.length)
351 #define PKL_AST_STRING_POINTER(AST) ((AST)->string.pointer)
352 
353 struct pkl_ast_string
354 {
355   struct pkl_ast_common common;
356   size_t length;
357   char *pointer;
358 };
359 
360 pkl_ast_node pkl_ast_make_string (pkl_ast ast,
361                                   const char *str);
362 
363 /* PKL_AST_ARRAY nodes represent array literals.  Each array holds a
364    sequence of elements, all of them having the same type.  There must
365    be at least one element in the array, i.e. emtpy arrays are not
366    allowed.  */
367 
368 #define PKL_AST_ARRAY_NELEM(AST) ((AST)->array.nelem)
369 #define PKL_AST_ARRAY_NINITIALIZER(AST) ((AST)->array.ninitializer)
370 #define PKL_AST_ARRAY_INITIALIZERS(AST) ((AST)->array.initializers)
371 
372 struct pkl_ast_array
373 {
374   struct pkl_ast_common common;
375 
376   size_t nelem;
377   size_t ninitializer;
378   union pkl_ast_node *initializers;
379 };
380 
381 pkl_ast_node pkl_ast_make_array (pkl_ast ast,
382                                  size_t nelem,
383                                  size_t ninitializer,
384                                  pkl_ast_node initializers);
385 
386 
387 /* PKL_AST_ARRAY_INITIALIZER nodes represent initializers in array
388    literals.  They are characterized by an index into the array and a
389    contained expression.  */
390 
391 #define PKL_AST_ARRAY_INITIALIZER_INDEX(AST) ((AST)->array_initializer.index)
392 #define PKL_AST_ARRAY_INITIALIZER_EXP(AST) ((AST)->array_initializer.exp)
393 
394 struct pkl_ast_array_initializer
395 {
396   struct pkl_ast_common common;
397 
398   union pkl_ast_node *index;
399   union pkl_ast_node *exp;
400 };
401 
402 pkl_ast_node pkl_ast_make_array_initializer (pkl_ast ast,
403                                              pkl_ast_node index,
404                                              pkl_ast_node exp);
405 
406 
407 /* PKL_AST_STRUCT nodes represent struct literals.  */
408 
409 #define PKL_AST_STRUCT_NELEM(AST) ((AST)->sct.nelem)
410 #define PKL_AST_STRUCT_FIELDS(AST) ((AST)->sct.elems)
411 
412 struct pkl_ast_struct
413 {
414   struct pkl_ast_common common;
415 
416   size_t nelem;
417   union pkl_ast_node *elems;
418 };
419 
420 pkl_ast_node pkl_ast_make_struct (pkl_ast ast,
421                                   size_t nelem,
422                                   pkl_ast_node elems);
423 
424 /* PKL_AST_STRUCT_FIELD nodes represent elements in struct
425    literals.
426 
427    NAME is a PKL_AST_IDENTIFIER node with the name of the struct
428    element.  If no name is specified, this is NULL.
429 
430    EXP is the value of the struct field.  */
431 
432 #define PKL_AST_STRUCT_FIELD_NAME(AST) ((AST)->sct_field.name)
433 #define PKL_AST_STRUCT_FIELD_EXP(AST) ((AST)->sct_field.exp)
434 
435 struct pkl_ast_struct_field
436 {
437   struct pkl_ast_common common;
438 
439   union pkl_ast_node *name;
440   union pkl_ast_node *exp;
441 };
442 
443 pkl_ast_node pkl_ast_make_struct_field (pkl_ast ast,
444                                         pkl_ast_node name,
445                                         pkl_ast_node exp);
446 
447 /* PKL_AST_EXP nodes represent unary and binary expressions,
448    consisting on an operator and one or two operators, respectively.
449 
450    The supported operators are specified in pkl-ops.def.
451    The supported attributes are defined in pkl-attrs.def.
452 
453    In PKL_AST_OP_ATTR expressions, ATTR contains the code for the
454    invoked attribute.
455 
456    FLAG is used for several purposes.
457 
458    There are two constructors for this node type: one for unary
459    expressions, another for binary expressions.  */
460 
461 #define PKL_AST_EXP_CODE(AST) ((AST)->exp.code)
462 #define PKL_AST_EXP_ATTR(AST) ((AST)->exp.attr)
463 #define PKL_AST_EXP_NUMOPS(AST) ((AST)->exp.numops)
464 #define PKL_AST_EXP_OPERAND(AST,I) ((AST)->exp.operands[(I)])
465 #define PKL_AST_EXP_FLAG(AST) ((AST)->exp.flag)
466 
467 struct pkl_ast_exp
468 {
469   struct pkl_ast_common common;
470 
471   enum pkl_ast_op code;
472   enum pkl_ast_attr attr;
473   uint8_t numops : 8;
474   union pkl_ast_node *operands[2];
475   int flag;
476 };
477 
478 pkl_ast_node pkl_ast_make_unary_exp (pkl_ast ast,
479                                      enum pkl_ast_op code,
480                                      pkl_ast_node op);
481 
482 pkl_ast_node pkl_ast_make_binary_exp (pkl_ast ast,
483                                       enum pkl_ast_op code,
484                                       pkl_ast_node op1,
485                                       pkl_ast_node op2);
486 
487 const char *pkl_attr_name (enum pkl_ast_attr attr);
488 
489 /* PKL_AST_COND_EXP nodes represent conditional expressions, having
490    exactly the same semantics than the C tertiary operator:
491 
492                    exp1 ? exp2 : exp3
493 
494    where exp1 is evaluated and then, depending on its value, either
495    exp2 (if exp1 is not 0) or exp3 (if exp1 is 0) are executed.
496 
497    COND, THENEXP and ELSEEXP must point to expressions, i.e. to nodes
498    node of type  PKL_AST_EXP or type PKL_AST_COND_EXP.  */
499 
500 #define PKL_AST_COND_EXP_COND(AST) ((AST)->cond_exp.cond)
501 #define PKL_AST_COND_EXP_THENEXP(AST) ((AST)->cond_exp.thenexp)
502 #define PKL_AST_COND_EXP_ELSEEXP(AST) ((AST)->cond_exp.elseexp)
503 
504 struct pkl_ast_cond_exp
505 {
506   struct pkl_ast_common common;
507   union pkl_ast_node *cond;
508   union pkl_ast_node *thenexp;
509   union pkl_ast_node *elseexp;
510 };
511 
512 pkl_ast_node pkl_ast_make_cond_exp (pkl_ast ast,
513                                     pkl_ast_node cond,
514                                     pkl_ast_node thenexp,
515                                     pkl_ast_node elseexp);
516 
517 /* PKL_AST_ENUMERATOR nodes represent the definition of a constant
518    into an enumeration.
519 
520    Each constant is characterized with an IDENTIFIER that identifies
521    it globally (meaning enumerator identifiers must be unique), an
522    optional VALUE, which must be a constant expression, and an
523    optional doc-string.
524 
525    If the value is not explicitly provided, it must be calculated
526    considering the rest of the enumerators in the enumeration, exactly
527    like in C enums.  */
528 
529 #define PKL_AST_ENUMERATOR_IDENTIFIER(AST) ((AST)->enumerator.identifier)
530 #define PKL_AST_ENUMERATOR_VALUE(AST) ((AST)->enumerator.value)
531 
532 struct pkl_ast_enumerator
533 {
534   struct pkl_ast_common common;
535   union pkl_ast_node *identifier;
536   union pkl_ast_node *value;
537 };
538 
539 pkl_ast_node pkl_ast_make_enumerator (pkl_ast ast,
540                                       pkl_ast_node identifier,
541                                       pkl_ast_node value);
542 
543 /* PKL_AST_ENUM nodes represent enumerations, having semantics much
544    like the C enums.
545 
546    TAG is mandatory and must point to a PKL_AST_IDENTIFIER.  This
547    identifier characterizes the enumeration globally in the enums
548    namespace.
549 
550    VALUES must point to a chain of PKL_AST_ENUMERATOR nodes containing
551    at least one node.  This means empty enumerations are not allowed.  */
552 
553 #define PKL_AST_ENUM_TAG(AST) ((AST)->enumeration.tag)
554 #define PKL_AST_ENUM_VALUES(AST) ((AST)->enumeration.values)
555 
556 struct pkl_ast_enum
557 {
558   struct pkl_ast_common common;
559 
560   union pkl_ast_node *tag;
561   union pkl_ast_node *values;
562 };
563 
564 pkl_ast_node pkl_ast_make_enum (pkl_ast ast,
565                                 pkl_ast_node tag,
566                                 pkl_ast_node values);
567 
568 /* PKL_AST_FUNC nodes represent a function definition.
569 
570    RET_TYPE is the type of the value returned by the function, or NULL
571    if the type doesn't return any value.
572 
573    ARGS is a chain of PKL_AST_FUNC_ARG nodes describing the formal
574    arguments to the function.  It can be NULL if the function takes no
575    arguments.
576 
577    FIRST_OPT_ARG is the first argument (in ARGS) that has an
578    associated initial.  If no argument in ARGS has an associated
579    initial, this is NULL.
580 
581    BODY is a PKL_AST_COMP_STMT node containing the statements that
582    conform the function body.
583 
584    NFRAMES is a counter used by the parser as an aid in determining
585    the number of lexical frames a RETURN_STMT should pop before
586    returning from the function.  While parsing, this contains the
587    number of frames pushed to the environment at any moment.  After
588    parsing, this field is not used anymore.
589 
590    NARGS is the number of formal arguments.
591 
592    NAME is a C string containing the name used to declare the
593    function.
594 
595    If the function is a method defined in a struct type, then METHOD_P
596    is not 0.  */
597 
598 #define PKL_AST_FUNC_RET_TYPE(AST) ((AST)->func.ret_type)
599 #define PKL_AST_FUNC_ARGS(AST) ((AST)->func.args)
600 #define PKL_AST_FUNC_FIRST_OPT_ARG(AST) ((AST)->func.first_opt_arg)
601 #define PKL_AST_FUNC_BODY(AST) ((AST)->func.body)
602 #define PKL_AST_FUNC_NFRAMES(AST) ((AST)->func.nframes)
603 #define PKL_AST_FUNC_NAME(AST) ((AST)->func.name)
604 #define PKL_AST_FUNC_NARGS(AST) ((AST)->func.nargs)
605 #define PKL_AST_FUNC_METHOD_P(AST) ((AST)->func.method_p)
606 #define PKL_AST_FUNC_PROGRAM(AST) ((AST)->func.program)
607 
608 struct pkl_ast_func
609 {
610   struct pkl_ast_common common;
611 
612   union pkl_ast_node *ret_type;
613   union pkl_ast_node *args;
614   union pkl_ast_node *first_opt_arg;
615   union pkl_ast_node *body;
616 
617   int nargs;
618   int nframes;
619   char *name;
620   int method_p;
621   pvm_program program;
622 };
623 
624 pkl_ast_node pkl_ast_make_func (pkl_ast ast,
625                                 pkl_ast_node ret_type,
626                                 pkl_ast_node args,
627                                 pkl_ast_node body);
628 
629 int pkl_ast_func_all_optargs (pkl_ast_node type);
630 
631 /* PKL_AST_FUNC_ARG nodes represent a formal argument in a function
632    definition.
633 
634    TYPE is the type of the argument.
635 
636    IDENTIFIER is the name of the argument.  It is a PKL_AST_IDENTIFIER
637    node.
638 
639    VARARG is 1 if this argument is a vararg.  0  otherwise.
640 
641    INITIAL, if not NULL, is an expression providing the default value
642    for the function argument.  This expression can refer to previous
643    arguments.  */
644 
645 #define PKL_AST_FUNC_ARG_TYPE(AST) ((AST)->func_arg.type)
646 #define PKL_AST_FUNC_ARG_IDENTIFIER(AST) ((AST)->func_arg.identifier)
647 #define PKL_AST_FUNC_ARG_INITIAL(AST) ((AST)->func_arg.initial)
648 #define PKL_AST_FUNC_ARG_VARARG(AST) ((AST)->func_arg.vararg)
649 
650 struct pkl_ast_func_arg
651 {
652   struct pkl_ast_common common;
653 
654   union pkl_ast_node *type;
655   union pkl_ast_node *identifier;
656   union pkl_ast_node *initial;
657   int vararg;
658 };
659 
660 pkl_ast_node pkl_ast_make_func_arg (pkl_ast ast,
661                                     pkl_ast_node type,
662                                     pkl_ast_node identifier,
663                                     pkl_ast_node init);
664 
665 /* PKL_AST_TRIMMER nodes represent a trim of an array, or a string.
666 
667    ENTITY is either an array or a string, which is the subject of
668    the trim.
669 
670    FROM is an expression that should evaluate to an uint<64>, which is
671    the index of the first element of the trim.  If FROM is NULL, then
672    the index of the first element of the trim is 0.
673 
674    If not NULL, TO is an expression that should evaluate to an
675    uint<64>, which is the index of the last element of the trim plus
676    one.
677 
678    If not NULL, ADDEND is an expression that should evaluate to an
679    uint<64>, which is the number to be added to FROM to find the index
680    of the last element included in the trim.
681 
682    If both TO and ADDEND are NULL then the index of the last element
683    of the trim is L-1, where L is the length of ENTITY.  */
684 
685 #define PKL_AST_TRIMMER_ENTITY(AST) ((AST)->trimmer.entity)
686 #define PKL_AST_TRIMMER_FROM(AST) ((AST)->trimmer.from)
687 #define PKL_AST_TRIMMER_TO(AST) ((AST)->trimmer.to)
688 #define PKL_AST_TRIMMER_ADDEND(AST) ((AST)->trimmer.addend)
689 
690 struct pkl_ast_trimmer
691 {
692   struct pkl_ast_common common;
693 
694   union pkl_ast_node *entity;
695   union pkl_ast_node *from;
696   union pkl_ast_node *to;
697   union pkl_ast_node *addend;
698 };
699 
700 pkl_ast_node pkl_ast_make_trimmer (pkl_ast ast,
701                                    pkl_ast_node entity,
702                                    pkl_ast_node from,
703                                    pkl_ast_node to,
704                                    pkl_ast_node addend);
705 
706 /* PKL_AST_INDEXER nodes represent references to an array element.
707 
708    BASE must point to a PKL_AST_ARRAY node.
709 
710    INDEX must point to an expression whose evaluation is the offset of
711    the element into the field, in units of the field's SIZE.  */
712 
713 #define PKL_AST_INDEXER_ENTITY(AST) ((AST)->indexer.entity)
714 #define PKL_AST_INDEXER_INDEX(AST) ((AST)->indexer.index)
715 
716 struct pkl_ast_indexer
717 {
718   struct pkl_ast_common common;
719   union pkl_ast_node *entity;
720   union pkl_ast_node *index;
721 };
722 
723 pkl_ast_node pkl_ast_make_indexer (pkl_ast ast,
724                                      pkl_ast_node array,
725                                      pkl_ast_node index);
726 
727 /* PKL_AST_STRUCT_REF nodes represent references to a struct
728    element.  */
729 
730 #define PKL_AST_STRUCT_REF_STRUCT(AST) ((AST)->sref.sct)
731 #define PKL_AST_STRUCT_REF_IDENTIFIER(AST) ((AST)->sref.identifier)
732 #define PKL_AST_STRUCT_REF_IS_PARENTHESIZED(AST) ((AST)->sref.is_parenthesized)
733 
734 struct pkl_ast_struct_ref
735 {
736   struct pkl_ast_common common;
737 
738   union pkl_ast_node *sct;
739   union pkl_ast_node *identifier;
740   int is_parenthesized;
741 };
742 
743 pkl_ast_node pkl_ast_make_struct_ref (pkl_ast ast,
744                                       pkl_ast_node sct,
745                                       pkl_ast_node identifier);
746 
747 /* PKL_AST_STRUCT_TYPE_FIELD nodes represent the field part of a
748    struct type.
749 
750    NAME is a PKL_AST_IDENTIFIER node, or NULL if the struct type
751    element has no name.
752 
753    TYPE is a PKL_AST_TYPE node.
754 
755    CONSTRAINT is a constraint associated with the struct field.  It is
756    an expression that should evaluate to a boolean.
757 
758    LABEL is an expression that, if present, should evaluate to an
759    offset value.  If the struct type element doesn't have a label,
760    this is NULL.
761 
762    ENDIAN is the endianness to use when reading and writing data
763    to/from the field.
764 
765    OPTCOND is a boolean expression that, if present, specifies whether
766    the field exists in the struct or not.
767 
768    INITIALIZER is an expression, that will be used to derive an
769    implicit constraint, and also as the initialization value for this
770    field when constructing structs.  If no initializer is provided in
771    the struct field definition this is NULL.  */
772 
773 #define PKL_AST_STRUCT_TYPE_FIELD_NAME(AST) ((AST)->sct_type_elem.name)
774 #define PKL_AST_STRUCT_TYPE_FIELD_TYPE(AST) ((AST)->sct_type_elem.type)
775 #define PKL_AST_STRUCT_TYPE_FIELD_CONSTRAINT(AST) ((AST)->sct_type_elem.constraint)
776 #define PKL_AST_STRUCT_TYPE_FIELD_LABEL(AST) ((AST)->sct_type_elem.label)
777 #define PKL_AST_STRUCT_TYPE_FIELD_ENDIAN(AST) ((AST)->sct_type_elem.endian)
778 #define PKL_AST_STRUCT_TYPE_FIELD_OPTCOND(AST) ((AST)->sct_type_elem.optcond)
779 #define PKL_AST_STRUCT_TYPE_FIELD_INITIALIZER(AST) ((AST)->sct_type_elem.initializer)
780 
781 struct pkl_ast_struct_type_field
782 {
783   struct pkl_ast_common common;
784 
785   union pkl_ast_node *name;
786   union pkl_ast_node *type;
787   union pkl_ast_node *constraint;
788   union pkl_ast_node *initializer;
789   union pkl_ast_node *label;
790   union pkl_ast_node *optcond;
791   int endian;
792 };
793 
794 pkl_ast_node pkl_ast_make_struct_type_field (pkl_ast ast,
795                                              pkl_ast_node name,
796                                              pkl_ast_node type,
797                                              pkl_ast_node constraint,
798                                              pkl_ast_node initializer,
799                                              pkl_ast_node label,
800                                              int endian,
801                                              pkl_ast_node optcond);
802 
803 /* PKL_AST_FUNC_TYPE_ARG nodes represent the arguments part of a
804    function type.
805 
806    TYPE is a PKL_AST_TYPE node describing the type of the
807    argument.
808 
809    NAME, if not NULL, is an IDENTIFIER node describing the name
810    of the argument.
811 
812    OPTIONAL is 1 if the argument is optional.  0 otherwise.
813    VARARG is 1 if the argument is a vararg.  0 otherwise.  */
814 
815 #define PKL_AST_FUNC_TYPE_ARG_TYPE(AST) ((AST)->fun_type_arg.type)
816 #define PKL_AST_FUNC_TYPE_ARG_NAME(AST) ((AST)->fun_type_arg.name)
817 #define PKL_AST_FUNC_TYPE_ARG_OPTIONAL(AST) ((AST)->fun_type_arg.optional)
818 #define PKL_AST_FUNC_TYPE_ARG_VARARG(AST) ((AST)->fun_type_arg.vararg)
819 
820 struct pkl_ast_func_type_arg
821 {
822   struct pkl_ast_common common;
823   union pkl_ast_node *type;
824   union pkl_ast_node *name;
825   int optional;
826   int vararg;
827 };
828 
829 pkl_ast_node pkl_ast_make_func_type_arg (pkl_ast ast,
830                                          pkl_ast_node type, pkl_ast_node name);
831 
832 /* PKL_AST_TYPE nodes represent type expressions.
833 
834    If NAME is not NULL, then this specific type instance has a given
835    name, which is encoded in a PKL_AST_IDENTIFIER node.
836 
837    CODE contains the kind of type, as defined in the pkl_ast_type_code
838    enumeration above.
839 
840    COMPILED contains an integer identifying the last compiler pass
841    that was run on the node.  This is only used in certain nodes, and
842    is controlled in the pass manager.
843 
844    In integral types, SIGNED is 1 if the type denotes a signed numeric
845    type.  In non-integral types SIGNED is 0.  SIZE is the size in bits
846    of type.
847 
848    In array types, ETYPE is a PKL_AST_TYPE node reflecting the type of
849    the elements stored in the array.  If the array type is bounded by
850    number of elements, then BOUND is an expression that must evaluate
851    to an integer.  If the array type is bounded by size, then BOUND is
852    an expression that must evaluate to an offset.  If the array type
853    is unbounded, then BOUND is NULL.  MAPPER, WRITER, PRINTER,
854    CONSTRUCTOR and BOUNDCLS are used to hold closures, or
855    PVM_NULL. The field LEX_CORRECTED is used by the transl phase, to
856    keep record of array types that have been lexically corrected; this
857    is to avoid processing the same type more than once.
858 
859    In struct types, NELEM is the number of elements in the struct
860    type, NFIELD is the number of fields, and NDECL is the number of
861    declarations.  ELEMS is a chain of elements, which can be
862    PKL_AST_STRUCT_TYPE_FIELD or PKL_AST_DECL nodes, potentially mixed.
863    PINNED_P is 1 if the struct is pinned, 0 otherwise.  MAPPER, WRITER
864    CONSTRUCTOR, PRINTER, COMPARATOR and INTEGRATOR are used to hold
865    closures, or PVM_NULL.  INT_TYPE, if not NULL, is an AST node with
866    an integral type, that defines the nature of this struct type as
867    integral.
868 
869    In offset types, BASE_TYPE is a PKL_AST_TYPE with the base type for
870    the offset's magnitude, and UNIT is either a PKL_AST_IDENTIFIER
871    containing one of few recognized keywords (b, B, Kb, etc) or a
872    PKL_AST_TYPE.
873 
874    In function types, NARG is the number of formal arguments in the
875    function type.  ARGS is a chain of PKL_AST_FUNC_TYPE_ARG nodes.
876    RTYPE is the type of the returned value, or NULL if the function
877    type denotes a void function.  FIRST_OPT_ARG is the first argument
878    (in ARGS) that has an associated initial.  If no argument in ARGS
879    has an associated initial, this is NULL.  VARARG is 1 if the
880    function takes a variable number of arguments.  0 otherwise.
881 
882    When the size of a value of a given type can be determined at
883    compile time, we say that such type is "complete".  Otherwise, we
884    say that the type is "incomplete" and should be completed at
885    run-time.  */
886 
887 #define PKL_AST_TYPE_CODE(AST) ((AST)->type.code)
888 #define PKL_AST_TYPE_NAME(AST) ((AST)->type.name)
889 #define PKL_AST_TYPE_COMPLETE(AST) ((AST)->type.complete)
890 #define PKL_AST_TYPE_COMPILED(AST) ((AST)->type.compiled)
891 #define PKL_AST_TYPE_I_SIZE(AST) ((AST)->type.val.integral.size)
892 #define PKL_AST_TYPE_I_SIGNED_P(AST) ((AST)->type.val.integral.signed_p)
893 #define PKL_AST_TYPE_A_BOUND(AST) ((AST)->type.val.array.bound)
894 #define PKL_AST_TYPE_A_ETYPE(AST) ((AST)->type.val.array.etype)
895 #define PKL_AST_TYPE_A_MAPPER(AST) ((AST)->type.val.array.mapper)
896 #define PKL_AST_TYPE_A_WRITER(AST) ((AST)->type.val.array.writer)
897 #define PKL_AST_TYPE_A_BOUNDER(AST) ((AST)->type.val.array.bounder)
898 #define PKL_AST_TYPE_A_CONSTRUCTOR(AST) ((AST)->type.val.array.constructor)
899 #define PKL_AST_TYPE_A_PRINTER(AST) ((AST)->type.val.array.printer)
900 #define PKL_AST_TYPE_S_NFIELD(AST) ((AST)->type.val.sct.nfield)
901 #define PKL_AST_TYPE_S_NDECL(AST) ((AST)->type.val.sct.ndecl)
902 #define PKL_AST_TYPE_S_NELEM(AST) ((AST)->type.val.sct.nelem)
903 #define PKL_AST_TYPE_S_ELEMS(AST) ((AST)->type.val.sct.elems)
904 #define PKL_AST_TYPE_S_PINNED_P(AST) ((AST)->type.val.sct.pinned_p)
905 #define PKL_AST_TYPE_S_UNION_P(AST) ((AST)->type.val.sct.union_p)
906 #define PKL_AST_TYPE_S_MAPPER(AST) ((AST)->type.val.sct.mapper)
907 #define PKL_AST_TYPE_S_WRITER(AST) ((AST)->type.val.sct.writer)
908 #define PKL_AST_TYPE_S_CONSTRUCTOR(AST) ((AST)->type.val.sct.constructor)
909 #define PKL_AST_TYPE_S_PRINTER(AST) ((AST)->type.val.sct.printer)
910 #define PKL_AST_TYPE_S_COMPARATOR(AST) ((AST)->type.val.sct.comparator)
911 #define PKL_AST_TYPE_S_INTEGRATOR(AST) ((AST)->type.val.sct.integrator)
912 #define PKL_AST_TYPE_S_ITYPE(AST) ((AST)->type.val.sct.itype)
913 #define PKL_AST_TYPE_O_UNIT(AST) ((AST)->type.val.off.unit)
914 #define PKL_AST_TYPE_O_BASE_TYPE(AST) ((AST)->type.val.off.base_type)
915 #define PKL_AST_TYPE_F_RTYPE(AST) ((AST)->type.val.fun.rtype)
916 #define PKL_AST_TYPE_F_NARG(AST) ((AST)->type.val.fun.narg)
917 #define PKL_AST_TYPE_F_ARGS(AST) ((AST)->type.val.fun.args)
918 #define PKL_AST_TYPE_F_VARARG(AST) ((AST)->type.val.fun.vararg)
919 #define PKL_AST_TYPE_F_FIRST_OPT_ARG(AST) ((AST)->type.val.fun.first_opt_arg)
920 
921 #define PKL_AST_TYPE_COMPLETE_UNKNOWN 0
922 #define PKL_AST_TYPE_COMPLETE_YES 1
923 #define PKL_AST_TYPE_COMPLETE_NO 2
924 
925 struct pkl_ast_type
926 {
927   struct pkl_ast_common common;
928 
929   union pkl_ast_node *name;
930   enum pkl_ast_type_code code;
931   int complete;
932   int compiled;
933 
934   union
935   {
936     struct
937     {
938       size_t size;
939       int signed_p;
940     } integral;
941 
942     struct
943     {
944       union pkl_ast_node *bound;
945       union pkl_ast_node *etype;
946       pvm_val mapper;
947       pvm_val writer;
948       pvm_val bounder;
949       pvm_val constructor;
950       pvm_val printer;
951     } array;
952 
953     struct
954     {
955       size_t nelem;
956       size_t nfield;
957       size_t ndecl;
958       union pkl_ast_node *elems;
959       union pkl_ast_node *itype;
960       int pinned_p;
961       int union_p;
962       pvm_val mapper;
963       pvm_val writer;
964       pvm_val constructor;
965       pvm_val comparator;
966       pvm_val integrator;
967       pvm_val printer;
968     } sct;
969 
970     struct
971     {
972       union pkl_ast_node *unit;
973       union pkl_ast_node *base_type;
974     } off;
975 
976     struct
977     {
978       union pkl_ast_node *rtype;
979       int narg;
980       int vararg;
981       union pkl_ast_node *args;
982       union pkl_ast_node *first_opt_arg;
983     } fun;
984 
985   } val;
986 };
987 
988 pkl_ast_node pkl_ast_make_named_type (pkl_ast ast, pkl_ast_node name);
989 
990 pkl_ast_node pkl_ast_make_integral_type (pkl_ast ast, size_t size, int signed_p);
991 
992 pkl_ast_node pkl_ast_make_void_type (pkl_ast ast);
993 
994 pkl_ast_node pkl_ast_make_string_type (pkl_ast ast);
995 
996 pkl_ast_node pkl_ast_make_array_type (pkl_ast ast, pkl_ast_node etype,
997                                       pkl_ast_node bound);
998 
999 pkl_ast_node pkl_ast_make_struct_type (pkl_ast ast, size_t nelem, size_t nfield,
1000                                        size_t ndecl, pkl_ast_node itype,
1001                                        pkl_ast_node elems,
1002                                        int pinned_p, int union_p);
1003 
1004 pkl_ast_node pkl_ast_make_offset_type (pkl_ast ast, pkl_ast_node base_type,
1005                                        pkl_ast_node unit);
1006 
1007 pkl_ast_node pkl_ast_make_function_type (pkl_ast ast, pkl_ast_node rtype,
1008                                          size_t narg, pkl_ast_node args);
1009 
1010 pkl_ast_node pkl_ast_make_any_type (pkl_ast);
1011 
1012 pkl_ast_node pkl_ast_dup_type (pkl_ast_node type);
1013 
1014 int pkl_ast_type_equal_p (pkl_ast_node t1, pkl_ast_node t2);
1015 
1016 int pkl_ast_type_mappable_p (pkl_ast_node type);
1017 
1018 int pkl_ast_type_is_exception (pkl_ast_node type);
1019 
1020 int pkl_ast_type_promoteable_p (pkl_ast_node ft, pkl_ast_node tt,
1021                                 int promote_array_of_any);
1022 
1023 pkl_ast_node pkl_ast_sizeof_type (pkl_ast ast, pkl_ast_node type);
1024 
1025 int pkl_ast_type_is_complete (pkl_ast_node type);
1026 
1027 void pkl_ast_array_type_remove_bounders (pkl_ast_node type);
1028 
1029 void pkl_print_type (FILE *out, pkl_ast_node type, int use_given_name);
1030 
1031 char *pkl_type_str (pkl_ast_node type, int use_given_name);
1032 
1033 pkl_ast_node pkl_struct_type_traverse (pkl_ast_node type, const char *path);
1034 
1035 /* Return an expression that evaluates to an increment step for the
1036    given TYPE.  If the provided type doesn't support the notion of
1037    increment step this function returns NULL.  */
1038 
1039 pkl_ast_node pkl_ast_type_incr_step (pkl_ast ast, pkl_ast_node type);
1040 
1041 /* PKL_AST_DECL nodes represent the declaration of a named entity:
1042    function, type, variable....
1043 
1044    KIND allows to quickly identify the nature of the entity being
1045    declared: PKL_AST_DECL_KIND_VAR, PKL_AST_DECL_KIND_TYPE,
1046    PKL_AST_DECL_KIND_FUNC or PKL_AST_DECL_KIND_UNIT.
1047 
1048    NAME is PKL_AST_IDENTIFIER node containing the name in the
1049    association.
1050 
1051    INITIAL is the initial value of the entity.  The kind of node
1052    depends on what is being declared:
1053    - An expression node for a variable.
1054    - A PKL_AST_TYPE for a type.
1055    - A PKL_AST_FUNC for a function.
1056    - A constant expression node for an unit.
1057 
1058    ORDER is the order of the declaration in its containing
1059    compile-time environment.  It is filled up when the declaration is
1060    registered in an environment.
1061 
1062    SOURCE is a string describing where the declaration comes from.
1063    Usually it will be the name of a source file, or "<stdin>" or
1064    whatever.
1065 
1066    STRUCT_FIELD_P indicates whether this declaration is for a variable
1067    corresponding to a struct field.  */
1068 
1069 #define PKL_AST_DECL_KIND(AST) ((AST)->decl.kind)
1070 #define PKL_AST_DECL_NAME(AST) ((AST)->decl.name)
1071 #define PKL_AST_DECL_TYPE(AST) ((AST)->decl.type)
1072 #define PKL_AST_DECL_INITIAL(AST) ((AST)->decl.initial)
1073 #define PKL_AST_DECL_ORDER(AST) ((AST)->decl.order)
1074 #define PKL_AST_DECL_SOURCE(AST) ((AST)->decl.source)
1075 #define PKL_AST_DECL_STRUCT_FIELD_P(AST) ((AST)->decl.struct_field_p)
1076 #define PKL_AST_DECL_IN_STRUCT_P(AST) ((AST)->decl.in_struct_p)
1077 
1078 #define PKL_AST_DECL_KIND_ANY 0
1079 #define PKL_AST_DECL_KIND_VAR 1
1080 #define PKL_AST_DECL_KIND_TYPE 2
1081 #define PKL_AST_DECL_KIND_FUNC 3
1082 #define PKL_AST_DECL_KIND_UNIT 4
1083 
1084 struct pkl_ast_decl
1085 {
1086   struct pkl_ast_common common;
1087 
1088   int kind;
1089   int struct_field_p;
1090   int in_struct_p;
1091   char *source;
1092   union pkl_ast_node *name;
1093   union pkl_ast_node *initial;
1094   int order;
1095 };
1096 
1097 pkl_ast_node pkl_ast_make_decl (pkl_ast ast, int kind,
1098                                 pkl_ast_node name, pkl_ast_node initial,
1099                                 const char *source);
1100 
1101 /* PKL_AST_OFFSET nodes represent poke object constructions.
1102 
1103    MAGNITUDE is an integer expression.
1104    UNIT can be an IDENTIFIER, an INTEGER or a TYPE.  */
1105 
1106 #define PKL_AST_OFFSET_MAGNITUDE(AST) ((AST)->offset.magnitude)
1107 #define PKL_AST_OFFSET_UNIT(AST) ((AST)->offset.unit)
1108 
1109 #define PKL_AST_OFFSET_UNIT_BITS 1
1110 #define PKL_AST_OFFSET_UNIT_NIBBLES 4
1111 #define PKL_AST_OFFSET_UNIT_BYTES (2 * PKL_AST_OFFSET_UNIT_NIBBLES)
1112 #define PKL_AST_OFFSET_UNIT_KILOBITS (1024 * PKL_AST_OFFSET_UNIT_BITS)
1113 #define PKL_AST_OFFSET_UNIT_KILOBYTES (1024 * PKL_AST_OFFSET_UNIT_BYTES)
1114 #define PKL_AST_OFFSET_UNIT_MEGABITS (1024 * PKL_AST_OFFSET_UNIT_KILOBITS)
1115 #define PKL_AST_OFFSET_UNIT_MEGABYTES (1024 * PKL_AST_OFFSET_UNIT_KILOBYTES)
1116 #define PKL_AST_OFFSET_UNIT_GIGABITS (1024 * PKL_AST_OFFSET_UNIT_MEGABITS)
1117 
1118 struct pkl_ast_offset
1119 {
1120   struct pkl_ast_common common;
1121 
1122   union pkl_ast_node *magnitude;
1123   union pkl_ast_node *unit;
1124 };
1125 
1126 pkl_ast_node pkl_ast_make_offset (pkl_ast ast,
1127                                   pkl_ast_node magnitude,
1128                                   pkl_ast_node unit);
1129 
1130 /* PKL_AST_CAST nodes represent casts at the language level.
1131 
1132    TYPE is the target type in the case.
1133    EXP is the expression whole value should be casted to the targe
1134    type.  */
1135 
1136 #define PKL_AST_CAST_TYPE(AST) ((AST)->cast.type)
1137 #define PKL_AST_CAST_EXP(AST) ((AST)->cast.exp)
1138 
1139 struct pkl_ast_cast
1140 {
1141   struct pkl_ast_common common;
1142 
1143   union pkl_ast_node *type;
1144   union pkl_ast_node *exp;
1145 };
1146 
1147 pkl_ast_node pkl_ast_make_cast (pkl_ast ast,
1148                                 pkl_ast_node type,
1149                                 pkl_ast_node exp);
1150 
1151 /* PKL_AST_ISA nodes represent an application of the ISA operator.
1152 
1153    EXP is the expression whose's type is checked.
1154    TYPE is the type to check for.  */
1155 
1156 #define PKL_AST_ISA_TYPE(AST) ((AST)->isa.type)
1157 #define PKL_AST_ISA_EXP(AST) ((AST)->isa.exp)
1158 
1159 struct pkl_ast_isa
1160 {
1161   struct pkl_ast_common common;
1162 
1163   union pkl_ast_node *type;
1164   union pkl_ast_node *exp;
1165 };
1166 
1167 pkl_ast_node pkl_ast_make_isa (pkl_ast ast,
1168                                pkl_ast_node type,
1169                                pkl_ast_node exp);
1170 
1171 /* PKL_AST_MAP nodes represent maps, i.e. the mapping of some type at
1172    some offset in IO space.
1173 
1174    STRICT_P is 0 if the node represents a non-strict mapping
1175    operation.  Any other value means the operation is a strict
1176    mapping.
1177 
1178    TYPE is the mapped type.
1179 
1180    IOS is an expression that should evaluate to an integer, and
1181    identifies the IOS where the map operation is performed.  If it is
1182    NULL then the map is performed in the current IOS.
1183 
1184    OFFSET is the offset in IO space where the TYPE is mapped.  */
1185 
1186 #define PKL_AST_MAP_STRICT_P(AST) ((AST)->map.strict_p)
1187 #define PKL_AST_MAP_TYPE(AST) ((AST)->map.type)
1188 #define PKL_AST_MAP_IOS(AST) ((AST)->map.ios)
1189 #define PKL_AST_MAP_OFFSET(AST) ((AST)->map.offset)
1190 
1191 struct pkl_ast_map
1192 {
1193   struct pkl_ast_common common;
1194 
1195   int strict_p;
1196   union pkl_ast_node *type;
1197   union pkl_ast_node *offset;
1198   union pkl_ast_node *ios;
1199 };
1200 
1201 pkl_ast_node pkl_ast_make_map (pkl_ast ast,
1202                                int strict_p,
1203                                pkl_ast_node type,
1204                                pkl_ast_node ios,
1205                                pkl_ast_node offset);
1206 
1207 /* PKL_AST_CONS nodes represent value constructors.
1208 
1209    KIND is the kind of constructor.  It should be one of the
1210    PKL_AST_CONS_KIND_* values defined below.
1211 
1212    TYPE is a type.
1213 
1214    For struct constructors, VALUE is a struct value of type TYPE and
1215    must always be present.
1216 
1217    For array constructors, VALUE is either NULL or the value that will
1218    be used as elements of the constructed array.  */
1219 
1220 #define PKL_AST_CONS_KIND(AST) ((AST)->cons.kind)
1221 #define PKL_AST_CONS_TYPE(AST) ((AST)->cons.type)
1222 #define PKL_AST_CONS_VALUE(AST) ((AST)->cons.value)
1223 
1224 #define PKL_AST_CONS_KIND_UNKNOWN 0
1225 #define PKL_AST_CONS_KIND_STRUCT 1
1226 #define PKL_AST_CONS_KIND_ARRAY 2
1227 
1228 struct pkl_ast_cons
1229 {
1230   struct pkl_ast_common common;
1231 
1232   int kind;
1233   union pkl_ast_node *type;
1234   union pkl_ast_node *value;
1235 };
1236 
1237 pkl_ast_node pkl_ast_make_cons (pkl_ast ast, pkl_ast_node type,
1238                                 pkl_ast_node value);
1239 
1240 /* PKL_AST_FUNCALL nodes represent the invocation of a function.
1241 
1242    FUNCTION is a variable with the function being invoked.
1243    ARGS is a chain of PKL_AST_FUNCALL_ARG nodes.
1244 
1245    NARG is the number of arguments in the funcall.  */
1246 
1247 #define PKL_AST_FUNCALL_ARGS(AST) ((AST)->funcall.args)
1248 #define PKL_AST_FUNCALL_FUNCTION(AST) ((AST)->funcall.function)
1249 #define PKL_AST_FUNCALL_NARG(AST) ((AST)->funcall.narg)
1250 
1251 struct pkl_ast_funcall
1252 {
1253   struct pkl_ast_common common;
1254 
1255   int narg;
1256   union pkl_ast_node *function;
1257   union pkl_ast_node *args;
1258 };
1259 
1260 pkl_ast_node pkl_ast_make_funcall (pkl_ast ast,
1261                                    pkl_ast_node function,
1262                                    pkl_ast_node args);
1263 
1264 /* PKL_AST_FUNCALL_ARG nodes represent actual arguments in function
1265    calls.
1266 
1267    EXP is the value passed for the argument.  Note that this can be
1268    NULL for a placeholder for a missing actual to an optional formal
1269    argument.
1270 
1271    NAME, if not NULL, is an IDENTIFIER node with the name of the
1272    argument.
1273 
1274    FIRST_VARARG is 1 if this actual corresponds to the first
1275    vararg.  0 otherwise.  */
1276 
1277 #define PKL_AST_FUNCALL_ARG_EXP(AST) ((AST)->funcall_arg.exp)
1278 #define PKL_AST_FUNCALL_ARG_NAME(AST) ((AST)->funcall_arg.name)
1279 #define PKL_AST_FUNCALL_ARG_FIRST_VARARG(AST) ((AST)->funcall_arg.first_vararg)
1280 
1281 struct pkl_ast_funcall_arg
1282 {
1283   struct pkl_ast_common common;
1284 
1285   union pkl_ast_node *exp;
1286   union pkl_ast_node *name;
1287   int first_vararg;
1288 };
1289 
1290 pkl_ast_node pkl_ast_make_funcall_arg (pkl_ast ast, pkl_ast_node exp,
1291                                        pkl_ast_node name);
1292 
1293 /* PKL_AST_VAR nodes represent variable references.
1294 
1295    NAME is a PKL_AST_IDENTIFIER containing the name used to refer to
1296    the variable.
1297 
1298    DECL is the declaration that declared the variable.
1299 
1300    BACK is the number of compile-time environment frames to traverse
1301    in order to find the frame where the referred variable is declared.
1302 
1303    OVER is the position in the frame, i.e. the variable declaration is
1304    the OVERth variable declaration in the frame.
1305 
1306    IS_RECURSIVE is a boolean indicating whether the variable
1307    references the declaration of the containing function.
1308 
1309    IS_PARENTHESIZED is a boolean indicating whether the variable is
1310    immediately parenthesized, i.e. there were parenthesis around the
1311    variable.  This is used to disable the de-proceduring of function
1312    values.
1313 
1314    FUNCTION is the function immediately enclosing the variable
1315    reference, or NULL.
1316 
1317    FUNCTION_BACK is the lexical depth relative to the immediately
1318    enclosing function.  This field is meaningful only if FUNCTION is
1319    not NULL.  */
1320 
1321 #define PKL_AST_VAR_NAME(AST) ((AST)->var.name)
1322 #define PKL_AST_VAR_DECL(AST) ((AST)->var.decl)
1323 #define PKL_AST_VAR_BACK(AST) ((AST)->var.back)
1324 #define PKL_AST_VAR_OVER(AST) ((AST)->var.over)
1325 #define PKL_AST_VAR_IS_RECURSIVE(AST) ((AST)->var.is_recursive)
1326 #define PKL_AST_VAR_FUNCTION(AST) ((AST)->var.function)
1327 #define PKL_AST_VAR_FUNCTION_BACK(AST) ((AST)->var.function_back)
1328 #define PKL_AST_VAR_IS_PARENTHESIZED(AST) ((AST)->var.is_parenthesized)
1329 
1330 struct pkl_ast_var
1331 {
1332   struct pkl_ast_common common;
1333 
1334   union pkl_ast_node *name;
1335   union pkl_ast_node *decl;
1336   int back;
1337   int over;
1338   int is_recursive;
1339   int is_parenthesized;
1340 
1341   union pkl_ast_node *function;
1342   int function_back;
1343 };
1344 
1345 pkl_ast_node pkl_ast_make_var (pkl_ast ast,
1346                                pkl_ast_node name,
1347                                pkl_ast_node initial,
1348                                int back, int over);
1349 
1350 /* PKL_AST_LAMBDA nodes represent lambda expressions, which evaluate
1351    to a function.
1352 
1353    FUNCTION is a node of type PKL_AST_FUNC.  */
1354 
1355 #define PKL_AST_LAMBDA_FUNCTION(AST) ((AST)->lambda.function)
1356 
1357 struct pkl_ast_lambda
1358 {
1359   struct pkl_ast_common common;
1360 
1361   union pkl_ast_node *function;
1362 };
1363 
1364 pkl_ast_node pkl_ast_make_lambda (pkl_ast ast, pkl_ast_node function);
1365 
1366 /* PKL_AST_INCRDECR nodes represent {pre,post}{increment,decrement}
1367    expressions.
1368 
1369    ORDER is either PKL_AST_ORDER_PRE or PKL_AST_ORDER_POST.
1370    SIGN is either PKL_AST_SIGN_INCR or PKL_AST_SIGN_DECR.
1371    EXP is the expression to which the operator is applied.
1372 
1373    ASS_STMT is an AST node that is used in a transformation phase in
1374    order to hold an assignment statement.  */
1375 
1376 #define PKL_AST_INCRDECR_ORDER(AST) ((AST)->incrdecr.order)
1377 #define PKL_AST_INCRDECR_SIGN(AST) ((AST)->incrdecr.sign)
1378 #define PKL_AST_INCRDECR_EXP(AST) ((AST)->incrdecr.exp)
1379 #define PKL_AST_INCRDECR_ASS_STMT(AST) ((AST)->incrdecr.ass_stmt)
1380 
1381 #define PKL_AST_ORDER_PRE 0
1382 #define PKL_AST_ORDER_POST 1
1383 
1384 #define PKL_AST_SIGN_INCR 0
1385 #define PKL_AST_SIGN_DECR 1
1386 
1387 struct pkl_ast_incrdecr
1388 {
1389   struct pkl_ast_common common;
1390 
1391   int order;
1392   int sign;
1393   union pkl_ast_node *exp;
1394   union pkl_ast_node *ass_stmt;
1395 };
1396 
1397 pkl_ast_node pkl_ast_make_incrdecr (pkl_ast ast,
1398                                     pkl_ast_node exp, int order, int sign);
1399 
1400 
1401 /* PKL_AST_COMPOUND_STMT nodes represent compound statements in the
1402    language.
1403 
1404    STMTS is a possibly empty chain of statements.
1405 
1406    If BUILTIN is not PKL_AST_BUILTIN_NONE, then this compound
1407    statement is a compiler builtin, i.e. specific code will be
1408    generated for this node.  In this case, STMTS should be NULL.  */
1409 
1410 #define PKL_AST_COMP_STMT_STMTS(AST) ((AST)->comp_stmt.stmts)
1411 #define PKL_AST_COMP_STMT_BUILTIN(AST) ((AST)->comp_stmt.builtin)
1412 #define PKL_AST_COMP_STMT_NUMVARS(AST) ((AST)->comp_stmt.numvars)
1413 
1414 #define PKL_AST_BUILTIN_NONE 0
1415 #define PKL_AST_BUILTIN_PRINT 1
1416 #define PKL_AST_BUILTIN_RAND 2
1417 #define PKL_AST_BUILTIN_GET_ENDIAN 3
1418 #define PKL_AST_BUILTIN_SET_ENDIAN 4
1419 #define PKL_AST_BUILTIN_GET_IOS 5
1420 #define PKL_AST_BUILTIN_SET_IOS 6
1421 #define PKL_AST_BUILTIN_OPEN 7
1422 #define PKL_AST_BUILTIN_CLOSE 8
1423 #define PKL_AST_BUILTIN_IOSIZE 9
1424 #define PKL_AST_BUILTIN_GETENV 10
1425 #define PKL_AST_BUILTIN_FORGET 11
1426 #define PKL_AST_BUILTIN_GET_TIME 12
1427 #define PKL_AST_BUILTIN_STRACE 13
1428 #define PKL_AST_BUILTIN_TERM_GET_COLOR 14
1429 #define PKL_AST_BUILTIN_TERM_SET_COLOR 15
1430 #define PKL_AST_BUILTIN_TERM_GET_BGCOLOR 16
1431 #define PKL_AST_BUILTIN_TERM_SET_BGCOLOR 17
1432 #define PKL_AST_BUILTIN_TERM_BEGIN_CLASS 19
1433 #define PKL_AST_BUILTIN_TERM_END_CLASS 20
1434 #define PKL_AST_BUILTIN_TERM_BEGIN_HYPERLINK 21
1435 #define PKL_AST_BUILTIN_TERM_END_HYPERLINK 22
1436 
1437 struct pkl_ast_comp_stmt
1438 {
1439   struct pkl_ast_common common;
1440 
1441   union pkl_ast_node *stmts;
1442   int builtin;
1443   int numvars;
1444 };
1445 
1446 pkl_ast_node pkl_ast_make_comp_stmt (pkl_ast ast, pkl_ast_node stmts);
1447 
1448 pkl_ast_node pkl_ast_make_builtin (pkl_ast ast, int builtin);
1449 
1450 /* PKL_AST_NULL_STMT nodes represent the "null statement".  It can
1451    appear anywhere an statement is expected, but it has no effect.  */
1452 
1453 struct pkl_ast_null_stmt
1454 {
1455   struct pkl_ast_common common;
1456 };
1457 
1458 pkl_ast_node pkl_ast_make_null_stmt (pkl_ast ast);
1459 
1460 /* PKL_AST_ASS_STMT nodes represent assignment statements in the
1461    language.
1462 
1463    LVALUE is the l-value of the assignment.
1464    EXP is the r-value of the assignment.  */
1465 
1466 #define PKL_AST_ASS_STMT_LVALUE(AST) ((AST)->ass_stmt.lvalue)
1467 #define PKL_AST_ASS_STMT_EXP(AST) ((AST)->ass_stmt.exp)
1468 
1469 struct pkl_ast_ass_stmt
1470 {
1471   struct pkl_ast_common common;
1472 
1473   union pkl_ast_node *lvalue;
1474   union pkl_ast_node *exp;
1475 };
1476 
1477 pkl_ast_node pkl_ast_make_ass_stmt (pkl_ast ast,
1478                                     pkl_ast_node lvalue, pkl_ast_node exp);
1479 
1480 /* PKL_AST_IF_STMT nodes represent conditional statements, with an
1481    optional `else' part.
1482 
1483    EXP is the expression that is to be evaluated to decide what branch
1484    to take.
1485 
1486    THEN_STMT is the statement to be executed when EXP holds true.
1487    ELSE_STMT is the statement to be executed when EXP holds false.  */
1488 
1489 #define PKL_AST_IF_STMT_EXP(AST) ((AST)->if_stmt.exp)
1490 #define PKL_AST_IF_STMT_THEN_STMT(AST) ((AST)->if_stmt.then_stmt)
1491 #define PKL_AST_IF_STMT_ELSE_STMT(AST) ((AST)->if_stmt.else_stmt)
1492 
1493 struct pkl_ast_if_stmt
1494 {
1495   struct pkl_ast_common common;
1496 
1497   union pkl_ast_node *exp;
1498   union pkl_ast_node *then_stmt;
1499   union pkl_ast_node *else_stmt;
1500 };
1501 
1502 pkl_ast_node pkl_ast_make_if_stmt (pkl_ast ast,
1503                                    pkl_ast_node exp,
1504                                    pkl_ast_node then_stmt,
1505                                    pkl_ast_node else_stmt);
1506 
1507 int pkl_ast_lvalue_p (pkl_ast_node node);
1508 
1509 /* PKL_AST_LOOP_STMT nodes represent iterative statements.
1510 
1511    KIND is one of the PKL_AST_LOOP_STMT_KIND_* values defined below.
1512    It identifies the kind of loop.
1513 
1514    ITERATOR is a PKL_AST_LOOP_STMT_ITERATOR node, or NULL.
1515 
1516    CONDITION is a expression that should evaluate to a boolean, that
1517    is evaluated at the beginning of the loop.  If it evals to false,
1518    the loop is exited.  This is used in WHILE, FOR, FOR-IN and
1519    FOR-IN-WHERE loops.  In loops with an iterator, the iterator
1520    variable is available in the scope where CONDITION is evaluated,
1521    and CONDITION determines whether BODY is executed in the current
1522    iteration.
1523 
1524    HEAD is a list of PKL_AST_DECL nodes that are processed before
1525    checking the loop condition, or NULL.  This is used in FOR loops.
1526 
1527    TAIL is a list of statements, to be executed at the end of the loop
1528    body.  This is only used in FOR loops.
1529 
1530    BODY is a statement, which is the body of the loop.  */
1531 
1532 #define PKL_AST_LOOP_STMT_ITERATOR(AST) ((AST)->loop_stmt.iterator)
1533 #define PKL_AST_LOOP_STMT_CONDITION(AST) ((AST)->loop_stmt.condition)
1534 #define PKL_AST_LOOP_STMT_BODY(AST) ((AST)->loop_stmt.body)
1535 #define PKL_AST_LOOP_STMT_TAIL(AST) ((AST)->loop_stmt.tail)
1536 #define PKL_AST_LOOP_STMT_HEAD(AST) ((AST)->loop_stmt.head)
1537 #define PKL_AST_LOOP_STMT_KIND(AST) ((AST)->loop_stmt.kind)
1538 
1539 #define PKL_AST_LOOP_STMT_KIND_WHILE  0
1540 #define PKL_AST_LOOP_STMT_KIND_FOR    1
1541 #define PKL_AST_LOOP_STMT_KIND_FOR_IN 2
1542 
1543 struct pkl_ast_loop_stmt
1544 {
1545   struct pkl_ast_common COMMON;
1546 
1547   int kind;
1548   union pkl_ast_node *iterator;
1549   union pkl_ast_node *condition;
1550   union pkl_ast_node *body;
1551   union pkl_ast_node *head;
1552   union pkl_ast_node *tail;
1553 };
1554 
1555 pkl_ast_node pkl_ast_make_loop_stmt (pkl_ast ast,
1556                                      int kind,
1557                                      pkl_ast_node iterator,
1558                                      pkl_ast_node condition,
1559                                      pkl_ast_node head,
1560                                      pkl_ast_node tail,
1561                                      pkl_ast_node body);
1562 
1563 /* PKL_AST_LOOP_STMT_ITERATOR nodes represent an iterator in a loop
1564    statement.
1565 
1566    DECL is a declaration for a variable created in a new lexical
1567    scope.
1568 
1569    CONTAINER is an expression that evaluates to an array.  This is
1570    used in FOR-IN and FOR-IN-HWERE loops.  */
1571 
1572 #define PKL_AST_LOOP_STMT_ITERATOR_DECL(AST) ((AST)->loop_stmt_iterator.decl)
1573 #define PKL_AST_LOOP_STMT_ITERATOR_CONTAINER(AST) ((AST)->loop_stmt_iterator.container)
1574 
1575 struct pkl_ast_loop_stmt_iterator
1576 {
1577   struct pkl_ast_common COMMON;
1578 
1579   union pkl_ast_node *decl;
1580   union pkl_ast_node *container;
1581 };
1582 
1583 pkl_ast_node pkl_ast_make_loop_stmt_iterator (pkl_ast ast,
1584                                               pkl_ast_node decl,
1585                                               pkl_ast_node container);
1586 
1587 /* PKL_AST_RETURN_STMT nodes represent return statements.
1588 
1589    EXP is the expression to return to the caller.
1590 
1591    NFRAMES is the number of lexical frames to pop before returning
1592    from the function.  This is used by the code generator.
1593 
1594    NDROPS is the number of stack elements to drop before returning
1595    from the function.
1596 
1597    FUNCTION is the PKL_AST_FUNCTION containing this return
1598    statement. */
1599 
1600 #define PKL_AST_RETURN_STMT_EXP(AST) ((AST)->return_stmt.exp)
1601 #define PKL_AST_RETURN_STMT_NFRAMES(AST) ((AST)->return_stmt.nframes)
1602 #define PKL_AST_RETURN_STMT_NDROPS(AST) ((AST)->return_stmt.ndrops)
1603 #define PKL_AST_RETURN_STMT_FUNCTION(AST) ((AST)->return_stmt.function)
1604 
1605 struct pkl_ast_return_stmt
1606 {
1607   struct pkl_ast_common common;
1608 
1609   union pkl_ast_node *exp;
1610   union pkl_ast_node *function;
1611   int nframes;
1612   int ndrops;
1613 };
1614 
1615 pkl_ast_node pkl_ast_make_return_stmt (pkl_ast ast, pkl_ast_node exp);
1616 
1617 void pkl_ast_finish_returns (pkl_ast_node function);
1618 
1619 /* PKL_AST_EXP_STMT nodes represent "expression statements".
1620 
1621    EXP is the expression conforming the statement.  */
1622 
1623 #define PKL_AST_EXP_STMT_EXP(AST) ((AST)->exp_stmt.exp)
1624 
1625 struct pkl_ast_exp_stmt
1626 {
1627   struct pkl_ast_common common;
1628   union pkl_ast_node *exp;
1629 };
1630 
1631 pkl_ast_node pkl_ast_make_exp_stmt (pkl_ast ast, pkl_ast_node exp);
1632 
1633 /* PKL_AST_TRY_UNTIL_STMT nodes represent try-until statements.
1634 
1635    CODE is a statement.
1636 
1637    EXP is an expression that should evaluate to a 32-bit signed
1638    integer.  CODE will be executed repeteadly until the given
1639    exception type is catched, or some other exception is raised.  */
1640 
1641 #define PKL_AST_TRY_UNTIL_STMT_CODE(AST) ((AST)->try_until_stmt.code)
1642 #define PKL_AST_TRY_UNTIL_STMT_EXP(AST) ((AST)->try_until_stmt.exp)
1643 
1644 struct pkl_ast_try_until_stmt
1645 {
1646   struct pkl_ast_common common;
1647 
1648   union pkl_ast_node *code;
1649   union pkl_ast_node *exp;
1650 };
1651 
1652 pkl_ast_node pkl_ast_make_try_until_stmt (pkl_ast ast,
1653                                           pkl_ast_node code, pkl_ast_node exp);
1654 
1655 /* PKL_AST_TRY_CATCH_STMT nodes represent try-catch statements, which
1656    are used in order to support exception handlers.
1657 
1658    CODE is a statement that is executed.
1659 
1660    HANDLER is a statement that will be executed in case an exception
1661    is raised while executing CODE.
1662 
1663    TYPE, if specified, is the argument to the catch clause.  The type
1664    of the argument must be an Exception.
1665 
1666    EXP, if specified, is an expression evaluating to an Exception.
1667    Exceptions having any other type won't be catched by the `catch'
1668    clause of the statement.
1669 
1670    Note that TYPE and EXP are mutually exclusive.  */
1671 
1672 #define PKL_AST_TRY_CATCH_STMT_CODE(AST) ((AST)->try_catch_stmt.code)
1673 #define PKL_AST_TRY_CATCH_STMT_HANDLER(AST) ((AST)->try_catch_stmt.handler)
1674 #define PKL_AST_TRY_CATCH_STMT_ARG(AST) ((AST)->try_catch_stmt.arg)
1675 #define PKL_AST_TRY_CATCH_STMT_EXP(AST) ((AST)->try_catch_stmt.exp)
1676 
1677 struct pkl_ast_try_catch_stmt
1678 {
1679   struct pkl_ast_common common;
1680 
1681   union pkl_ast_node *code;
1682   union pkl_ast_node *handler;
1683   union pkl_ast_node *arg;
1684   union pkl_ast_node *exp;
1685 };
1686 
1687 pkl_ast_node pkl_ast_make_try_catch_stmt (pkl_ast ast,
1688                                           pkl_ast_node code, pkl_ast_node handler,
1689                                           pkl_ast_node arg, pkl_ast_node exp);
1690 
1691 /* PKL_AST_PRINT_STMT nodes represent `print' statements.
1692 
1693    ARGS is a chained list of PKL_AST_PRINT_STMT_ARG nodes.  In `print'
1694    statements, this only contains one argument, whose expression
1695    should be of type string.  In `printf' statements, this may contain
1696    zero or more nodes, and the types of their expressions should match
1697    teh format expressed in FMT.
1698 
1699    FMT, if not NULL, is a format string node.
1700 
1701    TYPES is a linked list of type nodes, corresponding to the %-
1702    directives in FMT.  Note that %<class> and %</class> directives
1703    have type PKL_TYPE_VOID.
1704 
1705    NARGS is the number of arguments in ARGS.
1706 
1707    PREFIX, if not NULL, is a C string that should be printed before
1708    the arguments.
1709 
1710    FMT_PROCESSED_P is a boolean indicating whether the format string
1711    has been already processed in this node.  */
1712 
1713 #define PKL_AST_PRINT_STMT_FMT(AST) ((AST)->print_stmt.fmt)
1714 #define PKL_AST_PRINT_STMT_TYPES(AST) ((AST)->print_stmt.types)
1715 #define PKL_AST_PRINT_STMT_ARGS(AST) ((AST)->print_stmt.args)
1716 #define PKL_AST_PRINT_STMT_NARGS(AST) ((AST)->print_stmt.nargs)
1717 #define PKL_AST_PRINT_STMT_PREFIX(AST) ((AST)->print_stmt.prefix)
1718 #define PKL_AST_PRINT_STMT_FMT_PROCESSED_P(AST) ((AST)->print_stmt.fmt_processed_p)
1719 
1720 struct pkl_ast_print_stmt
1721 {
1722   struct pkl_ast_common common;
1723 
1724   int nargs;
1725   char *prefix;
1726   int fmt_processed_p;
1727   union pkl_ast_node *fmt;
1728   union pkl_ast_node *types;
1729   union pkl_ast_node *args;
1730 };
1731 
1732 pkl_ast_node pkl_ast_make_print_stmt (pkl_ast ast,
1733                                       pkl_ast_node fmt, pkl_ast_node args);
1734 
1735 /* PKL_AST_PRINT_STMT_ARG nodes represent expression arguments to
1736    `printf' statements.
1737 
1738    EXP is an expression node evaluating to the value to print.
1739 
1740    BASE is the numeration base to use when printing this argument.
1741 
1742    BEGIN_SC, if not NULL, marks that this argument is a %<class>
1743    directive, and is a NULL-terminated string with the name of the
1744    styling class to begin.
1745 
1746    END_SC, if not NULL, marks that this argument is a %</class>
1747    directive, and is a NULL-terminated string with the name of the
1748    styling class to end.
1749 
1750    SUFFIX, if not NULL, is a C string that should be printed after the
1751    value of EXP, respectively.
1752 
1753    VALUE_P indicates whether the argument shall be printed as a PVM
1754    value or not.
1755 
1756    PRINT_MODE and PRINT_DEPTH specify how the argument shall be
1757    printed if VALUE_P is true.  PRINT_MODE can be one of the
1758    PKL_AST_PRINT_MODE_* constants defined below, while PRINT_DEPTH can
1759    be zero or a positive integer.  */
1760 
1761 #define PKL_AST_PRINT_STMT_ARG_EXP(AST) ((AST)->print_stmt_arg.exp)
1762 #define PKL_AST_PRINT_STMT_ARG_BASE(AST) ((AST)->print_stmt_arg.base)
1763 #define PKL_AST_PRINT_STMT_ARG_SUFFIX(AST) ((AST)->print_stmt_arg.suffix)
1764 #define PKL_AST_PRINT_STMT_ARG_BEGIN_SC(AST) ((AST)->print_stmt_arg.begin_sc)
1765 #define PKL_AST_PRINT_STMT_ARG_END_SC(AST) ((AST)->print_stmt_arg.end_sc)
1766 #define PKL_AST_PRINT_STMT_ARG_VALUE_P(AST) ((AST)->print_stmt_arg.value_p)
1767 #define PKL_AST_PRINT_STMT_ARG_PRINT_MODE(AST) ((AST)->print_stmt_arg.print_mode)
1768 #define PKL_AST_PRINT_STMT_ARG_PRINT_DEPTH(AST) ((AST)->print_stmt_arg.print_depth)
1769 
1770 #define PKL_AST_PRINT_MODE_FLAT 0
1771 #define PKL_AST_PRINT_MODE_TREE 1
1772 
1773 struct pkl_ast_print_stmt_arg
1774 {
1775   struct pkl_ast_common common;
1776 
1777   char *begin_sc;
1778   char *end_sc;
1779   int base;
1780   int value_p;
1781   int print_mode;
1782   int print_depth;
1783   char *suffix;
1784   union pkl_ast_node *exp;
1785 };
1786 
1787 pkl_ast_node pkl_ast_make_print_stmt_arg (pkl_ast ast, pkl_ast_node exp);
1788 
1789 /* PKL_AST_CONTINUE_STMT nodes represent `continue' statements.  Each
1790    continue statement is associated to a loop node.
1791 
1792    ENTITY is the loop node associated with this continue statement.
1793 
1794    NFRAMES is the lexical depth of the continue statement, relative to
1795    the enclosing entity.  */
1796 
1797 #define PKL_AST_CONTINUE_STMT_ENTITY(AST) ((AST)->continue_stmt.entity)
1798 #define PKL_AST_CONTINUE_STMT_NFRAMES(AST) ((AST)->continue_stmt.nframes)
1799 
1800 struct pkl_ast_continue_stmt
1801 {
1802   struct pkl_ast_common common;
1803   union pkl_ast_node *entity;
1804   int nframes;
1805 };
1806 
1807 pkl_ast_node pkl_ast_make_continue_stmt (pkl_ast ast);
1808 
1809 /* PKL_AST_BREAK_STMT nodes represent `break' statements.  Each break
1810    statement is associated to a loop or switch node.
1811 
1812    ENTITY is the loop or switch node associated with this break
1813    statement.
1814 
1815    NFRAMES is the lexical depth of the break statement, relative to
1816    the enclosing entity.  */
1817 
1818 #define PKL_AST_BREAK_STMT_ENTITY(AST) ((AST)->break_stmt.entity)
1819 #define PKL_AST_BREAK_STMT_NFRAMES(AST) ((AST)->break_stmt.nframes)
1820 
1821 struct pkl_ast_break_stmt
1822 {
1823   struct pkl_ast_common common;
1824   union pkl_ast_node *entity;
1825   int nframes;
1826 };
1827 
1828 pkl_ast_node pkl_ast_make_break_stmt (pkl_ast ast);
1829 
1830 void pkl_ast_finish_breaks (pkl_ast_node entity, pkl_ast_node stmt);
1831 
1832 /* PKL_AST_RAISE_STMT nodes represent `raise' statements, which are
1833    used in order to raise exceptions at the program level.
1834 
1835    EXP is an expression that should evaluate to an exception object.
1836    At the present time, this is simply a 32-bit integer.  */
1837 
1838 #define PKL_AST_RAISE_STMT_EXP(AST) ((AST)->raise_stmt.exp)
1839 
1840 struct pkl_ast_raise_stmt
1841 {
1842   struct pkl_ast_common common;
1843   union pkl_ast_node *exp;
1844 };
1845 
1846 pkl_ast_node pkl_ast_make_raise_stmt (pkl_ast ast, pkl_ast_node exp);
1847 
1848 /* Finally, the `pkl_ast_node' type, which represents an AST node of
1849    any type.  */
1850 
1851 union pkl_ast_node
1852 {
1853   struct pkl_ast_common common; /* This field _must_ appear first.  */
1854   struct pkl_ast_program program;
1855   struct pkl_ast_src src;
1856   /* Expressions.  */
1857   struct pkl_ast_exp exp;
1858   struct pkl_ast_cond_exp cond_exp;
1859   struct pkl_ast_integer integer;
1860   struct pkl_ast_string string;
1861   struct pkl_ast_identifier identifier;
1862   struct pkl_ast_array array;
1863   struct pkl_ast_array_initializer array_initializer;
1864   struct pkl_ast_indexer indexer;
1865   struct pkl_ast_trimmer trimmer;
1866   struct pkl_ast_struct sct;
1867   struct pkl_ast_struct_field sct_field;
1868   struct pkl_ast_struct_ref sref;
1869   struct pkl_ast_offset offset;
1870   struct pkl_ast_cast cast;
1871   struct pkl_ast_isa isa;
1872   struct pkl_ast_map map;
1873   struct pkl_ast_cons cons;
1874   struct pkl_ast_funcall funcall;
1875   struct pkl_ast_funcall_arg funcall_arg;
1876   struct pkl_ast_var var;
1877   struct pkl_ast_lambda lambda;
1878   struct pkl_ast_incrdecr incrdecr;
1879   /* Types.  */
1880   struct pkl_ast_type type;
1881   struct pkl_ast_struct_type_field sct_type_elem;
1882   struct pkl_ast_func_type_arg fun_type_arg;
1883   struct pkl_ast_enum enumeration;
1884   struct pkl_ast_enumerator enumerator;
1885   /* Functions.  */
1886   struct pkl_ast_func func;
1887   struct pkl_ast_func_arg func_arg;
1888   /* Declarations.  */
1889   struct pkl_ast_decl decl;
1890   /* Statements.  */
1891   struct pkl_ast_comp_stmt comp_stmt;
1892   struct pkl_ast_ass_stmt ass_stmt;
1893   struct pkl_ast_if_stmt if_stmt;
1894   struct pkl_ast_loop_stmt loop_stmt;
1895   struct pkl_ast_loop_stmt_iterator loop_stmt_iterator;
1896   struct pkl_ast_return_stmt return_stmt;
1897   struct pkl_ast_exp_stmt exp_stmt;
1898   struct pkl_ast_try_catch_stmt try_catch_stmt;
1899   struct pkl_ast_try_until_stmt try_until_stmt;
1900   struct pkl_ast_break_stmt break_stmt;
1901   struct pkl_ast_continue_stmt continue_stmt;
1902   struct pkl_ast_raise_stmt raise_stmt;
1903   struct pkl_ast_print_stmt print_stmt;
1904   struct pkl_ast_print_stmt_arg print_stmt_arg;
1905 };
1906 
1907 static inline pkl_ast_node __attribute__ ((always_inline, warn_unused_result))
ASTREF(pkl_ast_node ast)1908 ASTREF (pkl_ast_node ast)
1909 {
1910   if (ast)
1911     ++ast->common.refcount;
1912   return ast;
1913 }
1914 
1915 static inline pkl_ast_node __attribute__ ((always_inline, warn_unused_result))
ASTDEREF(pkl_ast_node ast)1916 ASTDEREF (pkl_ast_node ast)
1917 {
1918   if (ast)
1919     --ast->common.refcount;
1920   return ast;
1921 }
1922 
1923 /* The `pkl_ast' struct defined below contains a PKL abstract syntax tree.
1924 
1925    AST contains the tree of linked nodes, starting with a
1926    PKL_AST_PROGRAM node.
1927 
1928    `pkl_ast_init' allocates and initializes a new AST and returns a
1929    pointer to it.
1930 
1931    `pkl_ast_free' frees all the memory allocated to store the AST
1932    nodes.
1933 
1934    `pkl_ast_node_free' frees the memory allocated to store a single
1935    node in the AST and its descendants.  This function is used by the
1936    bison parser.  */
1937 
1938 #define HASH_TABLE_SIZE 1008
1939 typedef pkl_ast_node pkl_hash[HASH_TABLE_SIZE];
1940 
1941 struct pkl_ast
1942 {
1943   size_t uid;
1944   pkl_ast_node ast;
1945 
1946   char *buffer;
1947   FILE *file;
1948   char *filename;
1949 };
1950 
1951 pkl_ast pkl_ast_init (void);
1952 
1953 void pkl_ast_free (pkl_ast ast);
1954 
1955 void pkl_ast_node_free (pkl_ast_node ast);
1956 
1957 void pkl_ast_node_free_chain (pkl_ast_node ast);
1958 
1959 /* Reverse the order of elements chained by CHAIN, and return the new
1960    head of the chain (old last element).  */
1961 
1962 pkl_ast_node pkl_ast_reverse (pkl_ast_node ast);
1963 
1964 void pkl_ast_print (FILE *fp, pkl_ast_node ast);
1965 
1966 #endif /* ! PKL_AST_H */
1967