1 #ifndef EXPRESSION_H
2 #define EXPRESSION_H
3 
4 /*********************************************************************//**
5 ** Module:  Expression \file expr.h
6 ** Description: This module implements the Expression abstraction.
7 **  Several types of expressions are supported: identifiers,
8 **  literals, operations (arithmetic, logical, array indexing,
9 **  etc.), function calls, and query expressions.  Every expression
10 **  is marked with a type.
11 ** Constants:
12 **  EXPRESSION_NULL     - the null expression
13 **  LITERAL_E       - a real literal with the value 2.7182...
14 **  LITERAL_EMPTY_SET   - a set literal representing the empty set
15 **  LITERAL_INFINITY    - a numeric literal representing infinity
16 **  LITERAL_PI      - a real literal with the value 3.1415...
17 **  LITERAL_ZERO        - an integer literal representing 0
18 **
19 ************************************************************************/
20 
21 /*
22  * This software was developed by U.S. Government employees as part of
23  * their official duties and is not subject to copyright.
24  *
25  * $Log: expr.h,v $
26  * Revision 1.4  1997/01/21 19:17:11  dar
27  * made C++ compatible
28  *
29  * Revision 1.3  1994/11/10  19:20:03  clark
30  * Update to IS
31  *
32  * Revision 1.2  1993/10/15  18:48:24  libes
33  * CADDETC certified
34  *
35  * Revision 1.6  1993/02/16  03:21:31  libes
36  * fixed numerous confusions of type with return type
37  * fixed implicit loop variable type declarations
38  * improved errors
39  *
40  * Revision 1.5  1993/01/19  22:44:17  libes
41  * *** empty log message ***
42  *
43  * Revision 1.4  1992/08/27  23:39:59  libes
44  * *** empty log message ***
45  *
46  * Revision 1.3  1992/08/18  17:12:41  libes
47  * rm'd extraneous error messages
48  *
49  * Revision 1.2  1992/06/08  18:06:24  libes
50  * prettied up interface to print_objects_when_running
51  */
52 
53 /*************/
54 /* constants */
55 /*************/
56 
57 #define EXPRESSION_NULL (Expression)0
58 
59 /*****************/
60 /* packages used */
61 /*****************/
62 
63 #include <sc_export.h>
64 #include <math.h>
65 #include "expbasic.h"   /* get basic definitions */
66 
67 #ifndef MAXINT
68 #define MAXINT (~(1 << 31))
69 #endif
70 
71 /************/
72 /* typedefs */
73 /************/
74 
75 typedef enum {
76     OP_AND,         OP_ANDOR,
77     OP_ARRAY_ELEMENT,   OP_CONCAT,
78     OP_DIV,         OP_DOT,         OP_EQUAL,
79     OP_EXP,         OP_GREATER_EQUAL,   OP_GREATER_THAN,
80     OP_GROUP,
81     OP_IN,          OP_INST_EQUAL,      OP_INST_NOT_EQUAL,
82     OP_LESS_EQUAL,      OP_LESS_THAN,       OP_LIKE,
83     OP_MINUS,       OP_MOD,         OP_NEGATE,
84     OP_NOT,         OP_NOT_EQUAL,       OP_OR,
85     OP_PLUS,        OP_REAL_DIV,        OP_SUBCOMPONENT,
86     OP_TIMES,       OP_XOR,         OP_UNKNOWN,
87     OP_LAST /**< must be last - used only to size tables */
88 } Op_Code;
89 
90 typedef struct Qualified_Attr   Qualified_Attr;
91 typedef struct Expression_ * Expression;
92 typedef Expression      Ary_Expression, One_Of_Expression, Identifier,
93         Literal;
94 typedef struct Query_    *   Query;
95 typedef One_Of_Expression   Function_Call;
96 typedef Ary_Expression      Ternary_Expression, Binary_Expression,
97         Unary_Expression;
98 typedef Literal         Aggregate_Literal, Integer_Literal,
99         Logical_Literal, Real_Literal, String_Literal,
100         Binary_Literal;
101 
102 /****************/
103 /* modules used */
104 /****************/
105 
106 #include "entity.h"
107 #include "type.h"
108 #include "variable.h"
109 #include "alg.h"
110 #include "scope.h"
111 
112 /***************************/
113 /* hidden type definitions */
114 /***************************/
115 
116 /* expression types */
117 
118 struct Qualified_Attr {
119     struct Expression_ * complex;   /**< complex entity instance */
120     Symbol * entity;
121     Symbol * attribute;
122 };
123 
124 struct Op_Subexpression {
125     Op_Code op_code;
126     Expression op1;
127     Expression op2;
128     Expression op3;
129 };
130 
131 struct Query_ {
132     Variable local;
133     Expression aggregate;   /**< set from which to test */
134     Expression expression;  /**< logical expression */
135     struct Scope_ * scope;
136 };
137 
138 struct Funcall {
139     struct Scope_ * function; /**< can also be an entity because entities can be called as functions */
140     Linked_List list;
141 };
142 
143 union expr_union {
144     int integer;
145     double real;
146     char * attribute;   /**< inverse .... for 'attr' */
147     char * binary;
148     int logical;
149     bool boolean;
150     struct Query_ * query;
151     struct Funcall funcall;
152 
153     /* if etype == aggregate, list of expressions */
154     /* if etype == funcall, 1st element of list is funcall or entity */
155     /*  remaining elements are parameters */
156     Linked_List list;   /**< aggregates (bags, lists, sets, arrays) or lists for oneof expressions */
157     Expression expression;  /**< derived value in derive attrs, or
158                              * initializer in local vars, or
159                              * enumeration tags
160                              * or oneof value */
161     struct Scope_ * entity; /**< used by subtype exp, group expr
162                               * and self expr, some funcall's and any
163                               * expr that results in an entity */
164     Variable variable;  /**< attribute reference */
165 };
166 
167 /** The difference between 'type' and 'return_type' is illustrated
168  * by "func(a)".  Here, 'type' is Type_Function while 'return_type'
169  * might be Type_Integer (assuming func returns an integer). */
170 struct Expression_ {
171     Symbol symbol;      /**< contains things like funcall names, string names, binary values, enumeration names */
172     Type type;
173     Type return_type;   /**< type of value returned by expression */
174     struct Op_Subexpression e;
175     union expr_union u;
176 };
177 
178 /** indexed by the op enumeration values */
179 struct EXPop_entry {
180     char * token;       /**< literal token, e.g., "<>" */
181     Type( *resolve ) PROTO( ( Expression, struct Scope_ * ) );
182 };
183 
184 /********************/
185 /* global variables */
186 /********************/
187 
188 extern SC_EXPRESS_EXPORT struct EXPop_entry EXPop_table[OP_LAST];
189 
190 extern SC_EXPRESS_EXPORT Expression  LITERAL_E;
191 extern SC_EXPRESS_EXPORT Expression  LITERAL_INFINITY;
192 extern SC_EXPRESS_EXPORT Expression  LITERAL_PI;
193 extern SC_EXPRESS_EXPORT Expression  LITERAL_ZERO;
194 extern SC_EXPRESS_EXPORT Expression  LITERAL_ONE;
195 
196 extern SC_EXPRESS_EXPORT Error ERROR_bad_qualification;
197 extern SC_EXPRESS_EXPORT Error ERROR_integer_expression_expected;
198 extern SC_EXPRESS_EXPORT Error ERROR_implicit_downcast;
199 extern SC_EXPRESS_EXPORT Error ERROR_ambig_implicit_downcast;
200 
201 extern SC_EXPRESS_EXPORT struct freelist_head EXP_fl;
202 extern SC_EXPRESS_EXPORT struct freelist_head OP_fl;
203 extern SC_EXPRESS_EXPORT struct freelist_head QUERY_fl;
204 extern SC_EXPRESS_EXPORT struct freelist_head QUAL_ATTR_fl;
205 
206 /******************************/
207 /* macro function definitions */
208 /******************************/
209 
210 #define EXP_new()   (struct Expression_ *)MEM_new(&EXP_fl)
211 #define EXP_destroy(x)  MEM_destroy(&EXP_fl,(Freelist *)(Generic)x)
212 #define OP_new()    (struct Op_Subexpression *)MEM_new(&OP_fl)
213 #define OP_destroy(x)   MEM_destroy(&OP_fl,(Freelist *)(Generic)x)
214 #define QUERY_new() (struct Query_ *)MEM_new(&QUERY_fl)
215 #define QUERY_destroy(x) MEM_destroy(&QUERY_fl,(Freelist *)(Generic)x)
216 #define QUAL_ATTR_new() (struct Qualified_Attr *)MEM_new(&QUAL_ATTR_fl)
217 #define QUAL_ATTR_destroy(x) MEM_destroy(&QUAL_ATTR_fl,(Freelist *)(Generic)x)
218 
219 #define EXPget_name(e)          ((e)->symbol.name)
220 #define ENUMget_name(e)         ((e)->symbol.name)
221 
222 #define BIN_EXPget_operator(e)      ARY_EXPget_operator(e)
223 #define BIN_EXPget_first_operand(e) ARY_EXPget_operand(e)
224 
225 #define UN_EXPget_operator(e)       ARY_EXPget_operator(e)
226 #define UN_EXPget_operand(e)        ARY_EXPget_operand(e)
227 
228 #define FCALLput_parameters(expr,parms) ((e)->u.funcall.list = (parms))
229 #define FCALLget_parameters(e)      ((e)->u.funcall.list)
230 #define FCALLput_function(expr,func)    ((e)->u.funcall.function = (func))
231 #define FCALLget_function(e)        ((e)->u.funcall.function)
232 /** assumes the function is not an entity-function! */
233 #define FCALLget_algorithm(e)       ((e)->u.funcall.function->u.function->body)
234 
235 #define INT_LITget_value(e)     ((e)->u.integer)
236 #define LOG_LITget_value(e)     ((e)->u.logical)
237 #define REAL_LITget_value(e)        ((e)->u.real)
238 #define STR_LITget_value(e)     ((e)->symbol.name)
239 #define STR_LITput_value(e,s)       ((e)->symbol.name = (s))
240 #define BIN_LITget_value(e)     ((e)->u.binary)
241 #define AGGR_LITget_value(e)        ((e)->u.list)
242 #define EXPget_type(e)          ((e)->type)
243 #define ARY_EXPget_operand(e)       ((e)->e.op1)
244 #define ARY_EXPget_operator(e)      ((e)->e.op_code)
245 #define BIN_EXPget_operand(e)       ((e)->e.op2)
246 #define TERN_EXPget_second_operand(e)   ((e)->e.op2)
247 #define TERN_EXPget_third_operand(e)    ((e)->e.op3)
248 #define QUERYget_variable(e)        ((e)->u.query->local)
249 #define QUERYget_source(e)      ((e)->u.query->aggregate)
250 #define QUERYget_discriminant(e)    ((e)->u.query->expression)
251 #define ONEOFget_list(e)        ((e)->u.list)
252 
253 /***********************/
254 /* function prototypes */
255 /***********************/
256 
257 extern SC_EXPRESS_EXPORT Expression   EXPcreate PROTO( ( Type ) );
258 extern SC_EXPRESS_EXPORT Expression   EXPcreate_simple PROTO( ( Type ) );
259 extern SC_EXPRESS_EXPORT Expression   EXPcreate_from_symbol PROTO( ( Type, Symbol * ) );
260 extern SC_EXPRESS_EXPORT Expression   UN_EXPcreate PROTO( ( Op_Code, Expression ) );
261 extern SC_EXPRESS_EXPORT Expression   BIN_EXPcreate PROTO( ( Op_Code, Expression, Expression ) );
262 extern SC_EXPRESS_EXPORT Expression   TERN_EXPcreate PROTO( ( Op_Code, Expression, Expression, Expression ) );
263 extern SC_EXPRESS_EXPORT Expression   QUERYcreate PROTO( ( Symbol *, Expression ) );
264 extern SC_EXPRESS_EXPORT void     EXPinitialize PROTO( ( void ) );
265 extern SC_EXPRESS_EXPORT void     EXPcleanup PROTO( ( void ) );
266 extern SC_EXPRESS_EXPORT Type     EXPtype PROTO( ( Expression, struct Scope_ * ) );
267 extern SC_EXPRESS_EXPORT int      EXPget_integer_value PROTO( ( Expression ) );
268 
269 #endif /*EXPRESSION_H*/
270