1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
16 *     contributors may be used to endorse or promote products derived from
17 *     this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Portions of this software have been released under the following terms:
31 *
32 * (c) Copyright 1989-1993 OPEN SOFTWARE FOUNDATION, INC.
33 * (c) Copyright 1989-1993 HEWLETT-PACKARD COMPANY
34 * (c) Copyright 1989-1993 DIGITAL EQUIPMENT CORPORATION
35 *
36 * To anyone who acknowledges that this file is provided "AS IS"
37 * without any express or implied warranty:
38 * permission to use, copy, modify, and distribute this file for any
39 * purpose is hereby granted without fee, provided that the above
40 * copyright notices and this notice appears in all source code copies,
41 * and that none of the names of Open Software Foundation, Inc., Hewlett-
42 * Packard Company or Digital Equipment Corporation be used
43 * in advertising or publicity pertaining to distribution of the software
44 * without specific, written prior permission.  Neither Open Software
45 * Foundation, Inc., Hewlett-Packard Company nor Digital
46 * Equipment Corporation makes any representations about the suitability
47 * of this software for any purpose.
48 *
49 * Copyright (c) 2007, Novell, Inc. All rights reserved.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 *
54 * 1.  Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 * 2.  Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in the
58 *     documentation and/or other materials provided with the distribution.
59 * 3.  Neither the name of Novell Inc. nor the names of its contributors
60 *     may be used to endorse or promote products derived from this
61 *     this software without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 *
74 * @APPLE_LICENSE_HEADER_END@
75 */
76
77/*
78**
79**  NAME:
80**
81**      irep.h
82**
83**  FACILITY:
84**
85**      Interface Definition Language (IDL) Compiler
86**
87**  ABSTRACT:
88**
89**  Defines data structures and API's for Intermediate Representation (IR)
90**  of an IDL interface.
91**
92**  %a%private_begin
93**
94**
95**  %a%private_end
96*/
97
98#ifndef IREPH_INCL
99#define IREPH_INCL
100
101typedef unsigned char byte;
102
103/*
104 * Define maximum arguments to an intermediate rep. tuple and the standard
105 * argument numbers of particular types of arguments.
106 */
107#define IR_MAX_ARG      4           /* Maximum args to tuple                  */
108#define IR_ARG_BOUND    0           /* standard arg# for bound kind           */
109#define IR_ARG_EXPR     0           /* standard arg# for expression           */
110#define IR_ARG_INTFC    0           /* standard arg# for interface node ptr   */
111#define IR_ARG_LIMIT    0           /* standard arg# for data limit kind      */
112#define IR_ARG_NAME     0           /* standard arg# for nametable ID         */
113#define IR_ARG_TUP      0           /* standard arg# for ptr to another tuple */
114#define IR_ARG_PFNUM    1           /* standard arg# for param or field num   */
115#define IR_ARG_TYPE     1           /* standard arg# for AST type node ptr    */
116#define IR_ARG_ARM      2           /* standard arg# for arm node ptr         */
117#define IR_ARG_BYT      2           /* standard arg# for byte value           */
118#define IR_ARG_FIELD    2           /* standard arg# for field node ptr       */
119#define IR_ARG_INST     2           /* standard arg# for instance node ptr    */
120#define IR_ARG_INT      2           /* standard arg# for integer value        */
121#define IR_ARG_LABEL    2           /* standard arg# for case label node ptr  */
122#define IR_ARG_PARAM    2           /* standard arg# for parameter node ptr   */
123#define IR_ARG_REP_AS   2           /* standard arg# for rep_as node ptr      */
124#define IR_ARG_TYPE2    2           /* standard arg# for second type node ptr */
125#define IR_ARG_CS_CHAR  2           /* standard arg# for cs_char node ptr     */
126#define IR_ARG_BOUND_XTRA 3         /* standard arg# for xtra bound info      */
127
128/*
129 * Each tuple has an opcode in addition to its arguments.
130 * Define opcode data type and the valid opcodes.
131 * For each opcode the comment indicates the valid arguments and their types.
132 */
133typedef unsigned short IR_opcode_k_t;
134                                    /* arg0         arg1        arg2          */
135#define IR_op_align_k            1  /* --           --          int_val       */
136#define IR_op_allocate_k         2  /* expr         type        expr          */
137#define IR_op_array_bounds_k     3  /* tup          type        int_val       */
138#define IR_op_array_end_k        4  /* --           type        --            */
139#define IR_op_bound_k            5  /* bound_k      int_val     int_val |     */
140                                    /*                          param|field   */
141#define IR_op_call_k             6  /* expr         expr        int_val       */
142#define IR_op_call_param_k       7  /* expr         type        --            */
143#define IR_op_case_k             8  /* expr         --          case_label    */
144#define IR_op_conformant_array_k 9  /* expr         type        instance      */
145#define IR_op_conformant_info_k 10  /* tup          --          --            */
146#define IR_op_context_handle_k  11  /* expr         type        instance      */
147#define IR_op_declare_k         12  /* name         type        [param]       */
148#define IR_op_default_k         13  /* --           type        --            */
149#define IR_op_disc_union_begin_k 14 /*              type        int_val       */
150#define IR_op_disc_union_end_k  15  /*              type        int_val       */
151#define IR_op_fixed_array_k     16  /* expr         type        instance      */
152#define IR_op_flat_array_k      17  /* tup          --          --            */
153#define IR_op_free_k            18  /* expr         type        --            */
154#define IR_op_full_array_end_k  19  /* --           --          --            */
155#define IR_op_full_array_k      20  /* tup          --          --            */
156#define IR_op_full_ptr_k        21  /* expr         type        instance      */
157#define IR_op_limit_k           22  /* limit_k      int_val     int_val |     */
158                                    /*                          param|field   */
159#define IR_op_marshall_k        23  /* expr         type        instance      */
160#define IR_op_noop_k            24  /* --           --          --            */
161#define IR_op_open_array_k      25  /* expr         type        instance      */
162#define IR_op_passed_by_ref_k   26  /* expr         type        instance      */
163#define IR_op_pipe_begin_k      27  /* --           type        type          */
164#define IR_op_pipe_end_k        28  /* --           type        type          */
165#define IR_op_pointee_end_k     29  /* --           type        --            */
166#define IR_op_ref_ptr_k         30  /* expr         type        instance      */
167#define IR_op_represent_as_k    31  /* tup          type        rep_as        */
168#define IR_op_represent_end_k   32  /* tup          type        rep_as        */
169#define IR_op_struct_begin_k    33  /* expr         type        instance      */
170#define IR_op_struct_end_k      34  /* expr         type        instance      */
171#define IR_op_switch_enc_k      35  /* name         type        --            */
172#define IR_op_switch_n_e_k      36  /* expr         type        param|field   */
173#define IR_op_transmit_as_k     37  /* tup          type        type          */
174#define IR_op_transmit_end_k    38  /* tup          type        type          */
175#define IR_op_type_indirect_k   39  /* expr         type        param         */
176#define IR_op_unique_ptr_k      40  /* expr         type        instance      */
177#define IR_op_varying_array_k   41  /* expr         type        instance      */
178#define IR_op_cs_char_k         42  /* tup          type        cs_char       */
179#define IR_op_cs_char_end_k     43  /* tup          type        cs_char       */
180#define IR_op_codeset_shadow_k  44  /*                          [int_val]     */
181#define IR_op_release_shadow_k  45  /*                                        */
182#define IR_op_interface_k       46  /* interface    type        instance      */
183#define IR_op_range_k           47  /* --           int_val     int_val       */
184
185/*
186 * Each tuple has flags in addition to its opcode and arguments.
187 * Define the valid flags and values.
188 */
189typedef unsigned long IR_flags_t;
190#define IR_CLIENT_SIDE      0x0001  /* Client side only tuple */
191#define IR_SERVER_SIDE      0x0002  /* Server side only tuple */
192#define IR_MARSHALL_CODE    0x0004  /* Marshalling only tuple */
193#define IR_UNMARSHALL_CODE  0x0008  /* Unmarshalling only tuple */
194#define IR_ARRAYIFIED_PTR   0x0010  /* Arrayified pointer */
195#define IR_STRING           0x0020  /* [string] array */
196#define IR_CONFORMANT       0x0040  /* Conformant struct */
197#define IR_ENCAPSULATED     0x0080  /* Encapsulated union */
198#define IR_VOID             0x0100  /* on 'case' tuple => empty arm */
199#define IR_BOOLEAN          0x0200  /* on 'case' tuple => boolean case value */
200#define IR_REP_AS           0x0400  /* on 'type indirect' tuple => indirect  */
201                                    /*  tuples hang off rep_as node          */
202                                    /* on 'string_k' tuples => octetsize is  */
203                                    /*  of rep_as type, n.k. at compile time */
204#define IR_CS_CHAR          0x0800  /* on 'type indirect' tuple => indirect  */
205                                    /*  tuples hang off cs_char node         */
206                                    /* on IR_op_*_array_k, IR_op_bound_k,    */
207                                    /*  IR_op_limit_k tuples => base type of */
208                                    /*  array has [cs_char] attribute        */
209                                    /* on IR_op_marshall_k tuple => instance */
210                                    /*  is used as a field attribute for a   */
211                                    /*  non-fixed array of [cs_char] type    */
212#define IR_IN_ONLY          0x1000  /* [in]-only tuple */
213#define IR_OUT_ONLY         0x2000  /* [out]-only tuple */
214
215#define IR_CF_EARLY         0x10000 /* early correlation flag */
216#define IR_CF_SPLIT         0x20000 /* split correlation flag */
217#define IR_CF_IS_IID_IS     0x40000 /* is iis_is correlation flag */
218#define IR_CF_DONT_CHECK    0x80000 /* don't check correlation flag */
219
220/*
221 * IR_bound_k_t defines the kinds of array bounds.
222 */
223typedef enum {
224    IR_bnd_fixed_k,
225    IR_bnd_min_is_k,
226    IR_bnd_max_is_k,
227    IR_bnd_size_is_k,
228    IR_bnd_string_k
229} IR_bound_k_t;
230
231/*
232 * IR_limit_k_t defines the kinds of array data limits.
233 */
234typedef enum {
235    IR_lim_fixed_k,
236    IR_lim_first_is_k,
237    IR_lim_last_is_k,
238    IR_lim_length_is_k,
239    IR_lim_string_k,
240    IR_lim_upper_conf_k
241} IR_limit_k_t;
242
243/*
244 * IR_expr_t defines a data type for 'expressions' that are associated with a
245 * tuple.  Expressions in tuples are not essential to Data Driven Backends and
246 * thus are not yet specified.  IR_expr_t is specified, however, in a way that
247 * should provide flexibility for future implementation of expressions.
248 * The current implementation must always store NULL for IR_expr_t data.
249 */
250typedef struct IR_expr_n_t {
251    long            dummy;
252} IR_expr_n_t;
253#define IR_EXP_FC_NONE		0
254#define IR_EXP_FC_DIV_2	1
255#define IR_EXP_FC_MUL_2	2
256#define IR_EXP_FC_ADD_1	3
257#define IR_EXP_FC_SUB_1	4
258#define IR_EXP_FC_ALIGN_2	5
259#define IR_EXP_FC_ALIGN_4	6
260#define IR_EXP_FC_ALIGN_8	7
261#define IR_EXP_FC_CALLBACK	8
262#define IR_EXP_FC_DIV_4	9
263#define IR_EXP_FC_MUL_4	10
264#define IR_EXP_FC_DIV_8	11
265#define IR_EXP_FC_MUL_8	12
266#define IR_EXP_FC_FIXED	13
267
268typedef IR_expr_n_t *IR_expr_t;
269
270/*
271 * IR_arg_t defines the various possible argument types in tuples.
272 */
273typedef union IR_arg_t {
274    struct AST_type_n_t         *type;
275    struct AST_parameter_n_t    *param;
276    struct AST_field_n_t        *field;
277    struct AST_arm_n_t          *arm;
278    struct AST_instance_n_t     *inst;
279    struct AST_pointer_n_t      *ptr;
280    struct AST_array_n_t        *array;
281    struct AST_case_label_n_t   *label;
282    struct AST_rep_as_n_t       *rep_as;
283    struct AST_cs_char_n_t      *cs_char;
284    struct AST_interface_n_t    *intfc;
285    struct IR_tup_n_t           *tup;
286    NAMETABLE_id_t              name;
287    IR_expr_t                   expr;
288    IR_bound_k_t                bound_k;
289    IR_limit_k_t                limit_k;
290    long                        int_val;
291    byte                        byt_val;
292} IR_arg_t;
293
294/*
295 * IR_tup_n_t is the basic data element of the intermediate representation.
296 * Each IR_tup_n_t is a tuple consisting of an operation-code, flags, and 0 or
297 * more arguments.  The name 'tuple' sometimes shortened to just 'tup'.
298 * Tuples are organized into singly linked lists hung off AST type, parameter,
299 * and operation nodes.
300 */
301typedef struct IR_tup_n_t {
302    struct IR_tup_n_t   *next;
303    IR_arg_t            arg[IR_MAX_ARG];
304    IR_opcode_k_t       opcode;
305    IR_flags_t          flags;
306} IR_tup_n_t;
307
308/*
309 * Macro that evaluates to TRUE if a tuple list has no meaningful data entries.
310 * Macro necessary because IREP generation can insert no-op sentinel entry.
311 *
312 * tup_p = ptr to beginning of list of IREP tuples.
313 */
314#define IR_NO_IREP(tup_p) \
315    ( (tup_p) == NULL || \
316      ((tup_p)->next == NULL && (tup_p)->opcode == IR_op_noop_k) )
317
318/*
319 * Macro to determine if an instance of a type is a string array.
320 * Returns TRUE if type is array and either:
321 *  1) instance address NULL and type has [(v1_)string] attribute
322 *  2) instance address non-NULL and instance has [(v1_)string] attribute
323 */
324#define IR_STRING_ARRAY(type_p, inst_p) \
325(type_p->kind == AST_array_k \
326 && (((inst_p) == NULL && (AST_STRING_SET(type_p) || AST_STRING0_SET(type_p))) \
327     || ((inst_p) != NULL \
328         && (AST_STRING_SET(inst_p) || AST_STRING0_SET(inst_p)))))
329
330/*
331 * Macro to determine if an instance of a type is a stringified pointer.
332 * Returns TRUE if type is pointer and either:
333 *  1) instance address NULL and type has [string] attribute
334 *  2) instance address non-NULL and instance has [string] attribute
335 *     and an array_rep_type.  NOTE: Frontend puts [string] on parameter that
336 *     is toplevel pointer to [string] array but to this code a pointer instance
337 *     is considered stringified only if the pointer itself is stringified
338 *     (not if it is a pointer to a string array).  This can be determined by
339 *     the presence of an array_rep_type.
340 */
341#define IR_STRINGIFIED(type_p, inst_p) \
342    (type_p->kind == AST_pointer_k \
343     && (((inst_p) == NULL && AST_STRING_SET(type_p)) \
344         || ((inst_p) != NULL && AST_STRING_SET(inst_p) \
345             && type_p->type_structure.pointer->pointee_type->array_rep_type \
346                != NULL)))
347
348/*
349 * Macro to determine if an instance of a type is an arrayified pointer.
350 * Returns TRUE if type is pointer and either:
351 *  1) instance of type is a stringified pointer
352 *  2) instance address non-NULL and instance has an upper bound attribute.
353 */
354#define IR_ARRAYIFIED(type_p, inst_p) \
355    (type_p->kind == AST_pointer_k \
356     && (IR_STRINGIFIED(type_p, inst_p) \
357         || ((inst_p) != NULL && (inst_p)->field_attrs != NULL \
358             && ((inst_p)->field_attrs->max_is_vec != NULL \
359                 || (inst_p)->field_attrs->size_is_vec != NULL))))
360
361/********************************************************/
362/*  Data structures and API for maintaining data scope  */
363/********************************************************/
364
365/*
366 * Scope data maintained by this API includes:
367 *  data structure kind (struct, array, pointer, union, etc.)
368 *  type information
369 *  instance information
370 *
371 * Before a parameter's tuples are processed, IR_init_scope should be called.
372 * After completing parameter processing, IR_finish_scope should be called.
373 * By calling IR_process_tup for each tuple processed, you cause a stack of
374 * scope information to be maintained.  Each constructed data type kind causes
375 * a new level of information on the stack, which is popped off the stack when
376 * the end of the type's tuples is processed.  A number of routines and macros
377 * are provided that allow you to query for scope information.
378 * Note: the term 'scope kind' is simply shorthand for 'scope data kind'.
379 */
380
381#define IR_MAX_EXPR     256     /* Maximum size of expression string */
382
383typedef byte IR_scope_k_t;
384
385/*
386 * Type and values for data kind associated with a scope.
387 */
388typedef struct {
389    IR_scope_k_t            scope_k;    /* Scope kind */
390    struct AST_type_n_t     *type_p;    /* Associated AST type node */
391    struct AST_instance_n_t *inst_p;    /* Associated AST instance node */
392} IR_scope_t;
393
394#define IR_SCP_TOPLEVEL 1
395#define IR_SCP_STRUCT   2
396#define IR_SCP_UNION    3
397#define IR_SCP_ARRAY    4
398#define IR_SCP_POINTER  5
399#define IR_SCP_XMIT_AS  6
400#define IR_SCP_REP_AS   7
401#define IR_SCP_PIPE     8
402#define IR_SCP_PASSED_BY_REF 9
403#define IR_SCP_CS_CHAR      10
404
405/*
406 * Type for information associated with a type indirection scope.
407 */
408typedef struct {
409    IR_flags_t              flags;      /* IREP flags */
410    struct AST_type_n_t     *type_p;    /* AST type node address */
411} IR_type_scope_t;
412
413/*
414**  P r i v a t e   D a t a
415**
416**  Data structures private to Intermediate Rep.  May change in future so
417**  external modules should use defined APIs and not access data directly.
418*/
419typedef struct {
420    struct AST_parameter_n_t    *param_p;   /* AST parameter node ptr */
421    struct AST_instance_n_t *saved_inst_p;  /* AST instance node ptr */
422    IR_type_scope_t     *type_s_a;          /* Stack of type scope info for */
423                                            /*  indirect type references */
424    IR_scope_t          *scope_a;           /* Stack of data scope kinds */
425    int                 type_stack_size;    /* Size of type_s_a stack */
426    int                 scope_stack_size;   /* Size of scope_a stack */
427    int                 type_scope;         /* Current type scoping level */
428    int                 scope;              /* Current data scoping level */
429    boolean             in_flat_rep;        /* T => processing flattened array*/
430                                            /*  tuples prior to base type     */
431} IR_scope_ctx_t;
432
433/*
434**  I R _ c u r _ s c o p e
435**  I R _ p a r e n t _ s c o p e
436**
437**  Returns current/parent scope data kind.
438*/
439#define IR_cur_scope(ctx_p) (ctx_p)->scope_a[(ctx_p)->scope].scope_k
440#define IR_parent_scope(ctx_p) (((ctx_p)->scope == 0) ? \
441    IR_SCP_TOPLEVEL : (ctx_p)->scope_a[(ctx_p)->scope-1].scope_k)
442
443/*
444**  I R _ c u r _ t y p e
445**  I R _ p a r e n t _ t y p e
446**
447**  Returns current/parent type on the scope stack.
448**  Contrast with the indirection stack macros below.
449*/
450#define IR_cur_type(ctx_p) (ctx_p)->scope_a[(ctx_p)->scope].type_p
451#define IR_parent_type(ctx_p) (((ctx_p)->scope == 0) ? \
452    NULL : (ctx_p)->scope_a[(ctx_p)->scope-1].type_p)
453
454/*
455**  I R _ c u r _ i n s t
456**
457**  Returns current instance on the scope stack.
458*/
459#define IR_cur_inst(ctx_p) (ctx_p)->scope_a[(ctx_p)->scope].inst_p
460
461/*
462**  I R _ c u r _ i _ t y p e
463**  I R _ p a r e n t _ i _ t y p e
464**
465**  Returns current/parent type on the type indirection stack.  This stack is
466**  contains a subset of the types contained on the scope stack; it contains
467**  only those types that are accessed via an IR_op_type_indirect_k tuple.
468*/
469#define IR_cur_i_type(ctx_p) (ctx_p)->type_s_a[(ctx_p)->type_scope].type_p
470#define IR_parent_i_type(ctx_p) (((ctx_p)->type_scope == 0) ? \
471    NULL : (ctx_p)->type_s_a[(ctx_p)->type_scope-1].type_p)
472
473/*
474**  I R _ i n _ *
475**
476**  Returns immediate {struct | union | array | pointer} nesting level.
477**  Examples:
478**      If processing pointer within struct, IR_in_struct(ctx) = 0.
479**      If processing  scalar within struct, IR_in_struct(ctx) = 1.
480**      If processing  struct within struct, IR_in_struct(ctx) = 2.
481*/
482#define IR_in_struct(ctx_p)     IR_in_scope(ctx_p, IR_SCP_STRUCT)
483#define IR_in_union(ctx_p)      IR_in_scope(ctx_p, IR_SCP_UNION)
484#define IR_in_array(ctx_p)      IR_in_scope(ctx_p, IR_SCP_ARRAY)
485#define IR_in_pointer(ctx_p)    IR_in_scope(ctx_p, IR_SCP_POINTER)
486#define IR_in_xmit_as(ctx_p)    IR_in_scope(ctx_p, IR_SCP_XMIT_AS)
487#define IR_in_rep_as(ctx_p)     IR_in_scope(ctx_p, IR_SCP_REP_AS)
488#define IR_in_cs_char(ctx_p)    IR_in_scope(ctx_p, IR_SCP_CS_CHAR)
489#define IR_in_pipe(ctx_p)       IR_in_scope(ctx_p, IR_SCP_PIPE)
490
491extern int IR_in_scope(                 /* Returns data kind nesting level */
492    IR_scope_ctx_t      *ctx_p,         /* [in] Scope context */
493    IR_scope_k_t        scope           /* [in] Scope data kind */
494);
495
496/*
497**  I R _ u n d e r _ *
498**
499**  Returns TRUE if in or under a scope of the specified kind.
500**  Example: If processing pointer within struct,
501**      IR_under_pointer(ctx)           = TRUE
502**      IR_under_struct(ctx)            = TRUE
503**      IR_under_ptr_under_struct(ctx)  = TRUE
504**      IR_under_array(ctx)             = FALSE
505*/
506#define IR_under_struct(ctx_p)  IR_under_scope(ctx_p, IR_SCP_STRUCT)
507#define IR_under_union(ctx_p)   IR_under_scope(ctx_p, IR_SCP_UNION)
508#define IR_under_array(ctx_p)   IR_under_scope(ctx_p, IR_SCP_ARRAY)
509#define IR_under_pointer(ctx_p) IR_under_scope(ctx_p, IR_SCP_POINTER)
510#define IR_under_xmit_as(ctx_p) IR_under_scope(ctx_p, IR_SCP_XMIT_AS)
511#define IR_under_rep_as(ctx_p)  IR_under_scope(ctx_p, IR_SCP_REP_AS)
512#define IR_under_cs_char(ctx_p) IR_under_scope(ctx_p, IR_SCP_CS_CHAR)
513#define IR_under_pipe(ctx_p)    IR_under_scope(ctx_p, IR_SCP_PIPE)
514
515#define IR_under_ptr_under_struct(ctx_p) \
516    IR_under_scope_under_scope(ctx_p, IR_SCP_POINTER, IR_SCP_STRUCT)
517
518extern boolean IR_under_scope(          /* Returns TRUE if under scope kind */
519    IR_scope_ctx_t      *ctx_p,         /* [in] Scope context */
520    IR_scope_k_t        scope           /* [in] Scope data kind */
521);
522
523extern boolean IR_under_scope_under_scope(  /* Ret. TRUE if under scope kinds */
524    IR_scope_ctx_t      *ctx_p,         /* [in] Scope context */
525    IR_scope_k_t        cscope,         /* [in] Child scope data kind */
526    IR_scope_k_t        pscope          /* [in] Parent scope data kind */
527);
528
529/*
530**  I R _ u n d e r _ t y p e
531**
532**  Returns nesting level of a type given by the specified address.
533**  Useful for detecting self-pointing data structures.
534*/
535extern int IR_under_type(               /* Returns type nesting level */
536    IR_scope_ctx_t      *ctx_p,         /* [in] Scope context */
537    AST_type_n_t        *type_p         /* [in] Ptr to AST type node */
538);
539
540/*
541**  I R _ i n i t _ s c o p e
542**
543**  Allocates and initializes a scope context and returns its address.
544*/
545extern IR_scope_ctx_t *IR_init_scope(   /* Returns ptr to new scope context */
546    struct AST_parameter_n_t *param_p   /* [in] Ptr to AST parameter node */
547);
548
549/*
550**  I R _ f i n i s h _ s c o p e
551**
552**  Cleans up a scope context.
553*/
554extern void IR_finish_scope(
555    IR_scope_ctx_t      *ctx_p          /* [in] Scope context */
556);
557
558/*
559**  I R _ p r o c e s s _ t u p
560**
561**  Processes tuple and maintains scoping data.
562*/
563extern void IR_process_tup(
564    IR_scope_ctx_t      *ctx_p,         /* [in] Scope context */
565    IR_tup_n_t          *tup_p          /* [in] Irep tuple */
566);
567
568/*
569**  I R _ f i e l d _ e x p r
570**
571**  Constructs a field expression for a toplevel or nested field reference.
572*/
573extern STRTAB_str_t IR_field_expr(      /* Returns field expression */
574    IR_scope_ctx_t      *ctx_p,         /* [in] Scope context */
575    struct AST_field_n_t *field_p       /* [in] Ptr to AST field node */
576);
577
578/**************************/
579/*  Public IREP routines  */
580/**************************/
581
582extern boolean IR_gen_irep(         /* Returns TRUE on success */
583    boolean             *cmd_opt,   /* [in] array of cmd option flags */
584    void                **cmd_val,  /* [in] array of cmd option values */
585    struct AST_interface_n_t *int_p /* [in] ptr to interface node */
586);
587
588#endif
589