1 /******************************** -*- C -*- ****************************
2  *
3  *	Semantic Tree information declarations.
4  *
5  *
6  ***********************************************************************/
7 
8 /***********************************************************************
9  *
10  * Copyright 1988,89,90,91,92,94,95,99,2000,2001,2002,2003,2006
11  * Free Software Foundation, Inc.
12  * Written by Steve Byrne.
13  *
14  * This file is part of GNU Smalltalk.
15  *
16  * GNU Smalltalk is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the Free
18  * Software Foundation; either version 2, or (at your option) any later
19  * version.
20  *
21  * Linking GNU Smalltalk statically or dynamically with other modules is
22  * making a combined work based on GNU Smalltalk.  Thus, the terms and
23  * conditions of the GNU General Public License cover the whole
24  * combination.
25  *
26  * In addition, as a special exception, the Free Software Foundation
27  * give you permission to combine GNU Smalltalk with free software
28  * programs or libraries that are released under the GNU LGPL and with
29  * independent programs running under the GNU Smalltalk virtual machine.
30  *
31  * You may copy and distribute such a system following the terms of the
32  * GNU GPL for GNU Smalltalk and the licenses of the other code
33  * concerned, provided that you include the source code of that other
34  * code when and as the GNU GPL requires distribution of source code.
35  *
36  * Note that people who make modified versions of GNU Smalltalk are not
37  * obligated to grant this special exception for their modified
38  * versions; it is their choice whether to do so.  The GNU General
39  * Public License gives permission to release a modified version without
40  * this exception; this exception also makes it possible to release a
41  * modified version which carries forward this exception.
42  *
43  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
46  * more details.
47  *
48  * You should have received a copy of the GNU General Public License along with
49  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
50  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
51  *
52  ***********************************************************************/
53 
54 
55 
56 #ifndef GST_TREE_H
57 #define GST_TREE_H
58 
59 /* These are the possible types of parse-tree nodes */
60 typedef enum
61 {
62   TREE_METHOD_NODE,
63   TREE_UNARY_EXPR,
64   TREE_BINARY_EXPR,
65   TREE_KEYWORD_EXPR,
66   TREE_VARIABLE_NODE,
67   TREE_ATTRIBUTE_LIST,
68   TREE_KEYWORD_LIST,
69   TREE_VAR_DECL_LIST,
70   TREE_VAR_ASSIGN_LIST,
71   TREE_STATEMENT_LIST,
72   TREE_RETURN_EXPR,
73   TREE_ASSIGN_EXPR,
74   TREE_CONST_EXPR,
75   TREE_SYMBOL_NODE,
76   TREE_ARRAY_ELT_LIST,
77   TREE_BLOCK_NODE,
78   TREE_CASCADE_EXPR,
79   TREE_MESSAGE_LIST,
80   TREE_ARRAY_CONSTRUCTOR,
81 
82   TREE_FIRST = TREE_METHOD_NODE,
83   TREE_LAST = TREE_ARRAY_CONSTRUCTOR
84 }
85 node_type;
86 
87 /* A structure holding a constant for objects having byte-sized
88    indexed instance variables (ByteArrays and LargeIntegers).  */
89 typedef struct byte_object
90 {
91   OOP class;
92   int size;
93   gst_uchar body[1];
94 }
95  *byte_object;
96 
97 /* A forward declaration.  */
98 typedef struct tree_node *tree_node;
99 
100 #include "gst-parse.h"
101 
102 /* A generic kind of parse-tree node that stores a list of nodes.  In
103    particular, NEXTADDR points to the last NEXT pointer in the list so
104    that tail adds are easier.  These nodes are also used for variables
105    by storing their name in the NAME member and by setting an
106    appropriate node type like TREE_VARIABLE_NODE (when the variable is
107    an argument or the receiver of a message) or TREE_VAR_DECL_LIST (when
108    the nodes is a list of arguments or temporaries).  */
109 typedef struct list_node
110 {
111   const char *name;
112   tree_node value;
113   tree_node next;
114   tree_node *nextAddr;
115 }
116 list_node;
117 
118 /* A parse-tree node for a message send.  EXPRESSION is a list_node
119    containing the arguments.  The same data structure is also used for
120    assignments (TREE_ASSIGN_EXPR) and in this case RECEIVER is the list
121    of assigned-to variables, SELECTOR is dummy and EXPRESSION is the
122    assigned value.  */
123 typedef struct expr_node
124 {
125   tree_node receiver;
126   OOP selector;
127   tree_node expression;
128 }
129 expr_node;
130 
131 /* The different kinds of constants that can be stored in a
132    const_node.  */
133 typedef enum
134 {
135   CONST_BYTE_OBJECT,
136   CONST_INT,
137   CONST_CHAR,
138   CONST_FLOATD,
139   CONST_FLOATE,
140   CONST_FLOATQ,
141   CONST_STRING,
142   CONST_OOP,
143   CONST_BINDING,
144   CONST_DEFERRED_BINDING,
145   CONST_ARRAY
146 }
147 const_type;
148 
149 /* A parse-tree node holding a constant.  CONSTTYPE identifies which
150    kind of constant is stored, the VAL union can include an intptr_t, a
151    double, a string (char *), an OOP (typically a Symbol, Association
152    or ScaledDecimal), an array (stored as a list_node) or a
153    byte_object struct (for ByteArrays and LargeIntegers).  */
154 typedef struct const_node
155 {
156   const_type constType;
157   union
158   {
159     intptr_t iVal;
160     long double fVal;
161     const char *sVal;
162     OOP oopVal;
163     tree_node aVal;
164     byte_object boVal;
165   }
166   val;
167 }
168 const_node;
169 
170 /* A parse-tree node defining a method.  SELECTOREXPR is an expr_node
171    with a nil receiver, holding the selector for the method and
172    a list_node (of type TREE_VAR_DECL_LIST) for the arguments.  The
173    method's temporaries and statements are also held in list_nodes
174    (respectively, of course, TEMPORARIES and STATEMENTS).  The final
175    field is the ending position of the method in the current stream,
176    or -1 if the stream is not file-based.  */
177 typedef struct method_node
178 {
179   tree_node selectorExpr;
180   tree_node temporaries;
181   tree_node attributes;
182   tree_node statements;
183   int64_t endPos;
184   mst_Boolean isOldSyntax;
185 }
186 method_node;
187 
188 /* A parse-tree node defining a block.  Not having a name, blocks
189    hold arguments in a simple list_node as well.  */
190 typedef struct block_node
191 {
192   tree_node arguments;
193   tree_node temporaries;
194   tree_node statements;
195 }
196 block_node;
197 
198 
199 /* A generic parse-tree node has a field marking the kind of
200    node (NODETYPE) and a union holding the five different
201    kinds of node (list_nodes, expr_nodes, const_node,
202    method_nodes and block_nodes).  */
203 struct tree_node
204 {
205   node_type nodeType;
206   YYLTYPE location;
207   union
208   {
209     list_node nvList;
210     expr_node nvExpr;
211     const_node nvConst;
212     method_node nvMethod;
213     block_node nvBlock;
214   }
215   nodeVal;
216 };
217 
218 #define v_block		nodeVal.nvBlock
219 #define v_list		nodeVal.nvList
220 #define v_expr		nodeVal.nvExpr
221 #define v_const		nodeVal.nvConst
222 #define v_method	nodeVal.nvMethod
223 
224 
225 /* Create a method_node with the given fields (see description under
226    struct method_node).  TEMPORARIES can possibly be NULL.  If the
227    method has any attributes associated with it, then they are in
228    ATTRIBUTES.  */
229 extern tree_node _gst_make_method (YYLTYPE *startLocation,
230                                    YYLTYPE *endLocation,
231 				   tree_node selectorExpr,
232 				   tree_node temporaries,
233 				   tree_node attributes,
234 				   tree_node statements,
235 				   int isOldSyntax)
236   ATTRIBUTE_HIDDEN;
237 
238 /* Create an expr_node to be passed to _gst_make_method for a unary
239    selector, or representing a send of a unary message
240    UNARYSELECTOREXPR to the object identified by RECEIVER.  */
241 extern tree_node _gst_make_unary_expr (YYLTYPE *location,
242 				       tree_node receiver,
243 				       const char *unarySelectorExpr)
244   ATTRIBUTE_HIDDEN;
245 
246 /* Create an expr_node to be passed to _gst_make_method for
247    a binary selector, or representing a send of a binary message
248    BINARYOP to the object identified by RECEIVER, with the given
249    ARGUMENT.  */
250 extern tree_node _gst_make_binary_expr (YYLTYPE *location,
251 				        tree_node receiver,
252 					const char *binaryOp,
253 					tree_node argument)
254   ATTRIBUTE_HIDDEN;
255 
256 /* Create an expr_node to be passed to _gst_make_method for a keyword
257    selector, or representing a send of a keyword message identified by
258    KEYWORDMESSAGE to the object identified by RECEIVER.  The selector
259    is split across the different list_nodes making up KEYWORDMESSAGE
260    and joined at compilation time.  */
261 extern tree_node _gst_make_keyword_expr (YYLTYPE *location,
262 				  	 tree_node receiver,
263 					 tree_node keywordMessage)
264   ATTRIBUTE_HIDDEN;
265 
266 /* Create a list_node that represents a variable called NAME (it does
267    not matter if it is a global, local, or instance variable, or an
268    argument).  */
269 extern tree_node _gst_make_variable (YYLTYPE *location,
270 				     const char *name)
271   ATTRIBUTE_HIDDEN;
272 
273 /* Create a list_node that represents a part of a keyword selector,
274    KEYWORD, together with the corresponding argument EXPRESSION.  */
275 extern tree_node _gst_make_keyword_list (YYLTYPE *location,
276 				         const char *keyword,
277 					 tree_node expression)
278   ATTRIBUTE_HIDDEN;
279 
280 /* Given a constant node, create an ATTRIBUTE_LIST node.  */
281 extern tree_node _gst_make_attribute_list (YYLTYPE *location,
282 					   tree_node constant)
283   ATTRIBUTE_HIDDEN;
284 
285 /* Given a variable tree node, convert it to a variable list tree node
286    with a NULL next link.  */
287 extern tree_node _gst_make_variable_list (YYLTYPE *location,
288 				  	  tree_node variable)
289   ATTRIBUTE_HIDDEN;
290 
291 /* Given a variable tree node, convert it to an assignment list tree node
292    with a NULL next link.  */
293 extern tree_node _gst_make_assignment_list (YYLTYPE *location,
294 				  	    tree_node variable)
295   ATTRIBUTE_HIDDEN;
296 
297 /* Create an expr_node of type TREE_ASSIGN_EXPR.  */
298 extern tree_node _gst_make_assign (YYLTYPE *location,
299 				   tree_node variables,
300 				   tree_node expression)
301   ATTRIBUTE_HIDDEN;
302 
303 /* Create an expr_node of type TREE_RETURN_EXPR where the returned
304    EXPRESSION is stored in the RECEIVER field of the node.  */
305 extern tree_node _gst_make_return (YYLTYPE *location,
306 				   tree_node expression)
307   ATTRIBUTE_HIDDEN;
308 
309 /* Create a const_node storing an intptr_t, IVAL.  */
310 extern tree_node _gst_make_int_constant (YYLTYPE *location,
311 				  	 intptr_t ival)
312   ATTRIBUTE_HIDDEN;
313 
314 /* Create a const_node storing a double, FVAL.  The type (FloatD,
315    FloatE, FloatQ) is given by TYPE.  */
316 extern tree_node _gst_make_float_constant (YYLTYPE *location,
317 				  	   long double fval,
318 					   int type)
319   ATTRIBUTE_HIDDEN;
320 
321 /* Create a const_node storing a char, IVAL.  */
322 extern tree_node _gst_make_char_constant (YYLTYPE *location,
323 				  	  int ival)
324   ATTRIBUTE_HIDDEN;
325 
326 /* Create a const_node storing a symbol, pointed to by the
327    SYMBOLNODE's NAME member.  The symbol is interned and the
328    const_node is created with its OOP.  */
329 extern tree_node _gst_make_symbol_constant (YYLTYPE *location,
330 				  	    tree_node symbolNode)
331   ATTRIBUTE_HIDDEN;
332 
333 /* Create a const_node storing a string, pointed to by SVAL.  */
334 extern tree_node _gst_make_string_constant (YYLTYPE *location,
335 				  	    const char *sval)
336   ATTRIBUTE_HIDDEN;
337 
338 /* Create a const_node storing a deferred variable binding, whose key
339    is the variable VARNODE.  */
340 extern tree_node _gst_make_deferred_binding_constant (YYLTYPE *location,
341 						      tree_node varNode)
342   ATTRIBUTE_HIDDEN;
343 
344 /* Create a const_node for an array whose elements are
345    described by the nodes in the list, AVAL.  */
346 extern tree_node _gst_make_array_constant (YYLTYPE *location,
347 				  	   tree_node aval)
348   ATTRIBUTE_HIDDEN;
349 
350 /* Create a const_node for the byte_object BOVAL (a LargeInteger).  */
351 extern tree_node _gst_make_byte_object_constant (YYLTYPE *location,
352 				  	         byte_object boval)
353   ATTRIBUTE_HIDDEN;
354 
355 /* Create a const_node for a ByteArray object, creating a
356    byteObjectConst out of the single elements which are stored in AVAL
357    as a list_node.  That is, this method converts from CONST_ARRAY
358    format to byteObjectConst format and answer the resulting
359    const_node.  */
360 extern tree_node _gst_make_byte_array_constant (YYLTYPE *location,
361 				  	        tree_node aval)
362   ATTRIBUTE_HIDDEN;
363 
364 /* Create a const_node for an object, OVAL, which is typically a
365    Class or ScaledDecimal.  */
366 extern tree_node _gst_make_oop_constant (YYLTYPE *location,
367 				  	 OOP oval)
368   ATTRIBUTE_HIDDEN;
369 
370 /* Create an TREE_ARRAY_CONSTRUCTOR node, that is a const_node whose
371    aVal does not contain other constants, but rather statements to be
372    evaluated at run-time and whose results are to put each in an
373    element of the array.  */
374 extern tree_node _gst_make_array_constructor (YYLTYPE *location,
375 				  	      tree_node statements)
376   ATTRIBUTE_HIDDEN;
377 
378 /* Resolve the variable binding to an association and create a
379    const_node of CONST_OOP type.  */
380 extern tree_node _gst_make_binding_constant (YYLTYPE *location,
381 				  	     tree_node variables)
382   ATTRIBUTE_HIDDEN;
383 
384 /* Create a TREE_SYMBOL_NODE describing an identifier (variable,
385    unary/binary selector or symbol constant, it doesn't patter) pointed
386    to by IDENT.  */
387 extern tree_node _gst_intern_ident (YYLTYPE *location,
388 				    const char *ident)
389   ATTRIBUTE_HIDDEN;
390 
391 /* Create an element of an array constant, which is a list type object.
392    Return the element with the next field NILed out and pointing to
393    the first element, ELT.  */
394 extern tree_node _gst_make_array_elt (YYLTYPE *location,
395 				      tree_node elt)
396   ATTRIBUTE_HIDDEN;
397 
398 /* Creates a block tree node with the given ARGUMENTS, TEMPORARIES and
399    STATEMENTS.  */
400 extern tree_node _gst_make_block (YYLTYPE *location,
401 				  tree_node arguments,
402 				  tree_node temporaries,
403 				  tree_node statements)
404   ATTRIBUTE_HIDDEN;
405 
406 /* Creates a node for holding a list of cascaded messages (basically an
407    expr_node that isn't using its symbol.  MESSAGEEXPR is the expression
408    invoke first as it computes the receiver.  Then the remaining
409    CASCADEDMESSAGES are sent to that same receiver.  */
410 extern tree_node _gst_make_cascaded_message (YYLTYPE *location,
411 				   	     tree_node messageExpr,
412 					     tree_node cascadedMessages)
413   ATTRIBUTE_HIDDEN;
414 
415 /* Create a node of type TREE_STATEMENT_LIST, where the first node is
416    EXPRESSION.  */
417 extern tree_node _gst_make_statement_list (YYLTYPE *location,
418 					   tree_node expression)
419   ATTRIBUTE_HIDDEN;
420 
421 /* Create a TREE_MESSAGE_LIST which is used as the second parameter to
422    _gst_make_cascaded_message -- that is, it represents the sends after
423    the first.  */
424 extern tree_node _gst_make_message_list (YYLTYPE *location,
425 				   	 tree_node messageElt)
426   ATTRIBUTE_HIDDEN;
427 
428 /* Adds node N2 onto a list of nodes headed by N1.  N1 contains the
429    address of the last NEXT field in the chain, so storing N2 into
430    there indirectly and then making that NEXT field point to N2's NEXT
431    field works properly.  */
432 extern tree_node _gst_add_node (tree_node n1,
433 				tree_node n2)
434   ATTRIBUTE_HIDDEN;
435 
436 /* Free the objects on the compilation obstack.  */
437 extern void _gst_free_tree ()
438   ATTRIBUTE_HIDDEN;
439 
440 /* Print the NODE with LEVEL spaces of indentation.  */
441 extern void _gst_print_tree (tree_node node,
442 			     int level)
443   ATTRIBUTE_HIDDEN;
444 
445 
446 #endif /* GST_TREE_H */
447