8a6cf4b1eb8fac979d86efa72b180a59ab1a071d
[binutils-gdb.git] / gdb / expression.h
1 /* Definitions for expressions stored in reversed prefix form, for GDB.
2 Copyright 1986, 1989, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #if !defined (EXPRESSION_H)
21 #define EXPRESSION_H 1
22
23 #ifdef __STDC__
24 struct block; /* Forward declaration for prototypes */
25 #endif
26
27 /* Definitions for saved C expressions. */
28
29 /* An expression is represented as a vector of union exp_element's.
30 Each exp_element is an opcode, except that some opcodes cause
31 the following exp_element to be treated as a long or double constant
32 or as a variable. The opcodes are obeyed, using a stack for temporaries.
33 The value is left on the temporary stack at the end. */
34
35 /* When it is necessary to include a string,
36 it can occupy as many exp_elements as it needs.
37 We find the length of the string using strlen,
38 divide to find out how many exp_elements are used up,
39 and skip that many. Strings, like numbers, are indicated
40 by the preceding opcode. */
41
42 enum exp_opcode
43 {
44 /* Used when it's necessary to pass an opcode which will be ignored,
45 or to catch uninitialized values. */
46 OP_NULL,
47
48 /* BINOP_... operate on two values computed by following subexpressions,
49 replacing them by one result value. They take no immediate arguments. */
50 BINOP_ADD, /* + */
51 BINOP_SUB, /* - */
52 BINOP_MUL, /* * */
53 BINOP_DIV, /* / */
54 BINOP_REM, /* % */
55 BINOP_LSH, /* << */
56 BINOP_RSH, /* >> */
57 BINOP_LOGICAL_AND, /* && */
58 BINOP_LOGICAL_OR, /* || */
59 BINOP_BITWISE_AND, /* & */
60 BINOP_BITWISE_IOR, /* | */
61 BINOP_BITWISE_XOR, /* ^ */
62 BINOP_EQUAL, /* == */
63 BINOP_NOTEQUAL, /* != */
64 BINOP_LESS, /* < */
65 BINOP_GTR, /* > */
66 BINOP_LEQ, /* <= */
67 BINOP_GEQ, /* >= */
68 BINOP_REPEAT, /* @ */
69 BINOP_ASSIGN, /* = */
70 BINOP_COMMA, /* , */
71 BINOP_SUBSCRIPT, /* x[y] */
72 BINOP_EXP, /* Exponentiation */
73
74 /* C++. */
75 BINOP_MIN, /* <? */
76 BINOP_MAX, /* >? */
77 BINOP_SCOPE, /* :: */
78
79 /* STRUCTOP_MEMBER is used for pointer-to-member constructs.
80 X . * Y translates into X STRUCTOP_MEMBER Y. */
81 STRUCTOP_MEMBER,
82 /* STRUCTOP_MPTR is used for pointer-to-member constructs
83 when X is a pointer instead of an aggregate. */
84 STRUCTOP_MPTR,
85 /* end of C++. */
86
87 /* For Modula-2 integer division DIV */
88 BINOP_INTDIV,
89
90 BINOP_ASSIGN_MODIFY, /* +=, -=, *=, and so on.
91 The following exp_element is another opcode,
92 a BINOP_, saying how to modify.
93 Then comes another BINOP_ASSIGN_MODIFY,
94 making three exp_elements in total. */
95
96 /* Modula-2 standard (binary) procedures*/
97 BINOP_VAL,
98 BINOP_INCL,
99 BINOP_EXCL,
100
101 /* This must be the highest BINOP_ value, for expprint.c. */
102 BINOP_END,
103
104 /* Operates on three values computed by following subexpressions. */
105 TERNOP_COND, /* ?: */
106
107 /* Multidimensional subscript operator, such as Modula-2 x[a,b,...].
108 The dimensionality is encoded in the operator, like the number of
109 function arguments in OP_FUNCALL, I.E. <OP><dimension><OP>.
110 The value of the first following subexpression is subscripted
111 by each of the next following subexpressions, one per dimension. */
112
113 MULTI_SUBSCRIPT,
114
115 /* The OP_... series take immediate following arguments.
116 After the arguments come another OP_... (the same one)
117 so that the grouping can be recognized from the end. */
118
119 /* OP_LONG is followed by a type pointer in the next exp_element
120 and the long constant value in the following exp_element.
121 Then comes another OP_LONG.
122 Thus, the operation occupies four exp_elements. */
123
124 OP_LONG,
125 /* OP_DOUBLE is similar but takes a double constant instead of a long one. */
126 OP_DOUBLE,
127 /* OP_VAR_VALUE takes one struct symbol * in the following exp_element,
128 followed by another OP_VAR_VALUE, making three exp_elements. */
129 OP_VAR_VALUE,
130 /* OP_LAST is followed by an integer in the next exp_element.
131 The integer is zero for the last value printed,
132 or it is the absolute number of a history element.
133 With another OP_LAST at the end, this makes three exp_elements. */
134 OP_LAST,
135 /* OP_REGISTER is followed by an integer in the next exp_element.
136 This is the number of a register to fetch (as an int).
137 With another OP_REGISTER at the end, this makes three exp_elements. */
138 OP_REGISTER,
139 /* OP_INTERNALVAR is followed by an internalvar ptr in the next exp_element.
140 With another OP_INTERNALVAR at the end, this makes three exp_elements. */
141 OP_INTERNALVAR,
142 /* OP_FUNCALL is followed by an integer in the next exp_element.
143 The integer is the number of args to the function call.
144 That many plus one values from following subexpressions
145 are used, the first one being the function.
146 The integer is followed by a repeat of OP_FUNCALL,
147 making three exp_elements. */
148 OP_FUNCALL,
149 /* OP_STRING represents a string constant.
150 Its format is the same as that of a STRUCTOP, but the string
151 data is just made into a string constant when the operation
152 is executed. */
153 OP_STRING,
154
155 /* UNOP_CAST is followed by a type pointer in the next exp_element.
156 With another UNOP_CAST at the end, this makes three exp_elements.
157 It casts the value of the following subexpression. */
158 UNOP_CAST,
159 /* UNOP_MEMVAL is followed by a type pointer in the next exp_element
160 With another UNOP_MEMVAL at the end, this makes three exp_elements.
161 It casts the contents of the word addressed by the value of the
162 following subexpression. */
163 UNOP_MEMVAL,
164 /* UNOP_... operate on one value from a following subexpression
165 and replace it with a result. They take no immediate arguments. */
166 UNOP_NEG, /* Unary - */
167 UNOP_LOGICAL_NOT, /* Unary ! */
168 UNOP_COMPLEMENT, /* Unary ~ */
169 UNOP_IND, /* Unary * */
170 UNOP_ADDR, /* Unary & */
171 UNOP_PREINCREMENT, /* ++ before an expression */
172 UNOP_POSTINCREMENT, /* ++ after an expression */
173 UNOP_PREDECREMENT, /* -- before an expression */
174 UNOP_POSTDECREMENT, /* -- after an expression */
175 UNOP_SIZEOF, /* Unary sizeof (followed by expression) */
176
177 UNOP_PLUS, /* Unary plus */
178
179 UNOP_CAP, /* Modula-2 standard (unary) procedures */
180 UNOP_CHR,
181 UNOP_ORD,
182 UNOP_ABS,
183 UNOP_FLOAT,
184 UNOP_HIGH,
185 UNOP_MAX,
186 UNOP_MIN,
187 UNOP_ODD,
188 UNOP_TRUNC,
189
190 OP_BOOL, /* Modula-2 builtin BOOLEAN type */
191 OP_M2_STRING, /* Modula-2 string constants */
192
193 /* STRUCTOP_... operate on a value from a following subexpression
194 by extracting a structure component specified by a string
195 that appears in the following exp_elements (as many as needed).
196 STRUCTOP_STRUCT is used for "." and STRUCTOP_PTR for "->".
197 They differ only in the error message given in case the value is
198 not suitable or the structure component specified is not found.
199
200 The length of the string follows in the next exp_element,
201 (after the string), followed by another STRUCTOP_... code. */
202 STRUCTOP_STRUCT,
203 STRUCTOP_PTR,
204
205 /* C++ */
206 /* OP_THIS is just a placeholder for the class instance variable.
207 It just comes in a tight (OP_THIS, OP_THIS) pair. */
208 OP_THIS,
209
210 /* OP_SCOPE surrounds a type name and a field name. The type
211 name is encoded as one element, but the field name stays as
212 a string, which, of course, is variable length. */
213 OP_SCOPE,
214
215 /* OP_TYPE is for parsing types, and used with the "ptype" command
216 so we can look up types that are qualified by scope, either with
217 the GDB "::" operator, or the Modula-2 '.' operator. */
218 OP_TYPE
219 };
220
221 union exp_element
222 {
223 enum exp_opcode opcode;
224 struct symbol *symbol;
225 LONGEST longconst;
226 double doubleconst;
227 char string;
228 struct type *type;
229 struct internalvar *internalvar;
230 };
231
232 struct expression
233 {
234 const struct language_defn *language_defn; /* language it was entered in */
235 int nelts;
236 union exp_element elts[1];
237 };
238
239 /* From parse.c */
240
241 extern struct expression *
242 parse_expression PARAMS ((char *));
243
244 extern struct expression *
245 parse_exp_1 PARAMS ((char **, struct block *, int));
246
247 /* The innermost context required by the stack and register variables
248 we've encountered so far. To use this, set it to NULL, then call
249 parse_<whatever>, then look at it. */
250 extern struct block *innermost_block;
251
252 /* From expprint.c */
253
254 extern void
255 print_expression PARAMS ((struct expression *, FILE *));
256
257 extern char *
258 op_string PARAMS ((enum exp_opcode));
259
260 /* To enable dumping of all parsed expressions in a human readable
261 form, define DEBUG_EXPRESSIONS. This is a compile time constant
262 at the moment, since it's not clear that this feature is important
263 enough to include by default. */
264
265 #ifdef DEBUG_EXPRESSIONS
266 extern void
267 dump_expression PARAMS ((struct expression *, FILE *, char *));
268 #define DUMP_EXPRESSION(exp,file,note) dump_expression ((exp), (file), (note))
269 #else
270 #define DUMP_EXPRESSION(exp,file,note) /* Null expansion */
271 #endif /* DEBUG_EXPRESSIONS */
272
273 #endif /* !defined (EXPRESSION_H) */