4 * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved. See the
5 * COPYRIGHT file at the base of the distribution.
11 _AllocExpDebug(token_t *tok, const char *file, int line)
13 fprintf(stderr, "allocate from %s:%d\n", file, line);
14 return(_AllocExp(tok));
18 _AllocExp(token_t *tok)
20 Exp *exp = zalloc(sizeof(Exp));
21 exp->ex_Run = RunUnresolvedExp;
22 exp->ex_Run64 = Run64DefaultExp;
23 exp->ex_Gen = GenUnresolvedExp;
24 exp->ex_Visibility = SCOPE_ALL_VISIBLE | 0x80000000;
25 exp->ex_TmpOffset = -1;
27 LexInitRef(&exp->ex_LexRef, tok);
36 while ((exp = next) != NULL) {
37 RelStrTable(&exp->ex_Id);
40 exp->ex_Lhs = NULL; /* safety */
41 exp->ex_Rhs = NULL; /* safety */
45 LexDoneRef(&exp->ex_LexRef);
46 zfree(exp, sizeof(Exp));
51 * Wrap an expression (used for things like typeof()). The
52 * passed expression may be NULL.
55 WrapExp(token_t *tok, Exp *oexp, int t)
57 Exp *exp = AllocExp(NULL);
62 LexDupRef(&oexp->ex_LexRef, &exp->ex_LexRef);
63 exp->ex_Flags |= EXF_UNARY;
64 exp->ex_ArgId = oexp->ex_ArgId;
65 oexp->ex_ArgId = NULL;
67 LexInitRef(&exp->ex_LexRef, tok);
73 * Invert the sense of a boolean expression
76 ExpToNotExp(Exp *oexp)
78 Exp *exp = AllocExp(NULL);
80 LexDupRef(&oexp->ex_LexRef, &exp->ex_LexRef);
83 exp->ex_Token = TOK_NOT;
84 exp->ex_Type = &BoolType;
85 exp->ex_Flags |= EXF_UNARY | EXF_PARSE_TYPE;
86 exp->ex_Flags |= oexp->ex_Flags & EXF_DUPEXP;
87 exp->ex_ArgId = oexp->ex_ArgId;
88 oexp->ex_ArgId = NULL;
96 ExpToCastExp(Exp *oexp, Type *type)
98 if (oexp->ex_Type != type) {
99 Exp *exp = AllocExp(NULL);
101 LexDupRef(&oexp->ex_LexRef, &exp->ex_LexRef);
103 exp->ex_Token = TOK_CAST;
105 exp->ex_Flags |= EXF_UNARY | EXF_PARSE_TYPE;
106 exp->ex_Flags |= oexp->ex_Flags & EXF_DUPEXP;
107 exp->ex_ArgId = oexp->ex_ArgId;
108 oexp->ex_ArgId = NULL;
115 * Convert a list of expressions linked by ex_Next to a compound expression.
116 * Note that a degenerate TOK_VOIDEXP converts to an empty compound
117 * expression (just as () was originally parsed as TOK_VOIDEXP).
119 * Since ex_Next is used for other purposes, such as the resolver's
120 * method call conversion code, we have to return a consolidated
121 * TOK_COMPOUND expression node whos Lhs bases a linked list of
122 * expressions representing the compound expression.
124 * Compound expressions need a type hint to be evaluated properly (handled
127 * Note that we do not promote ex_ArgId to the compound element here. This
128 * function is designed to create a compound expression out of a linked list,
129 * not really to convert a non-compound expression to a compound expression
130 * (though it does that too).
133 ExpToCompoundExp(Exp *oexp, int t)
135 Exp *exp = AllocExp(NULL);
137 LexDupRef(&oexp->ex_LexRef, &exp->ex_LexRef);
138 if (oexp->ex_Token == TOK_VOIDEXP && oexp->ex_Next == NULL)
143 exp->ex_Flags |= EXF_COMPOUND;
144 exp->ex_Flags |= oexp->ex_Flags & EXF_DUPEXP;
150 ExpToBracketedExp(Exp *oexp, int t)
152 Exp *exp = AllocExp(NULL);
154 LexDupRef(&oexp->ex_LexRef, &exp->ex_LexRef);
157 exp->ex_Flags |= EXF_COMPOUND;
158 exp->ex_Flags |= oexp->ex_Flags & EXF_DUPEXP;
164 SetDupExp(SemGroup *sg, Exp *exp)
166 if (exp && (exp->ex_Flags & EXF_DUPEXP) == 0) {
167 exp->ex_Flags |= EXF_DUPEXP;
169 SetDupExp(sg, exp->ex_Lhs);
171 SetDupExp(sg, exp->ex_Rhs);
173 SetDupExp(sg, exp->ex_Next);
179 * DupExp() - duplicate an expression
181 * This occurs during the superclass/subclass merge, before we
182 * resolve any expression trees.
185 DupExp(SemGroup *sg, Exp *oexp)
190 exp = AllocExp(NULL);
191 LexDupRef(&oexp->ex_LexRef, &exp->ex_LexRef);
192 dassert_exp(exp, exp->ex_Decl == NULL);
194 switch(oexp->ex_Flags & (EXF_COMPOUND|EXF_UNARY|EXF_BINARY)) {
196 dassert_exp(oexp, oexp->ex_Rhs == NULL);
201 pscan = &exp->ex_Lhs;
202 for (oscan = oexp->ex_Lhs;
204 oscan = oscan->ex_Next) {
205 *pscan = DupExp(sg, oscan);
206 pscan = &(*pscan)->ex_Next;
212 * Duplicate a unary expression. We do NOT duplicate
213 * alias substitutions, that creates havoc.
215 * WARNING: Casts may store the original typeof
216 * expression in ex_Rhs.
218 dassert_exp(oexp, oexp->ex_Lhs);
219 if (oexp->ex_Flags & EXF_ALIAS)
222 exp->ex_Lhs = DupExp(sg, oexp->ex_Lhs);
224 if (oexp->ex_Token == TOK_CAST)
225 exp->ex_Rhs = DupExp(sg, oexp->ex_Rhs);
227 dassert_exp(oexp, oexp->ex_Rhs == NULL);
230 dassert_exp(oexp, oexp->ex_Lhs);
231 dassert_exp(oexp, oexp->ex_Rhs);
232 exp->ex_Lhs = DupExp(sg, oexp->ex_Lhs);
233 exp->ex_Rhs = DupExp(sg, oexp->ex_Rhs);
236 if (oexp->ex_Flags & (EXF_COMPOUND |
239 dassert_exp(oexp, 0);
241 dassert_exp(oexp, oexp->ex_Lhs == NULL);
242 dassert_exp(oexp, oexp->ex_Rhs == NULL);
245 exp->ex_Prec = oexp->ex_Prec;
246 exp->ex_Flags = oexp->ex_Flags;
247 exp->ex_Flags2 = oexp->ex_Flags2;
249 exp->ex_Flags &= ~(EXF_DUPEXP | EXF_RESOLVED | EXF_RESOLVING |
253 * NOTE! Special case! ex_Decl is only non-NULL when we do
254 * a TypeToQualType() and have to dup an expression
255 * post-resolve. sg will be NULL in this case.
257 * XXX DupExp optimization invalidates this test, don't assert.
258 * XXX add EXF_PARSE_DECL
261 dassert_exp(exp, oexp->ex_Decl == NULL || sg == NULL);
263 exp->ex_Decl = oexp->ex_Decl;
266 exp->ex_Id = StrTableDup(oexp->ex_Id);
268 exp->ex_ArgId = StrTableDup(oexp->ex_ArgId);
269 exp->ex_Token = oexp->ex_Token;
272 * The type may be left over from the original resolution of
273 * the expression prior to duplication. We can inherit some
274 * of these, but otherwise leave ex_Type NULL and re-resolve
277 if ((oexp->ex_Flags & (EXF_PARSE_TYPE | EXF_RET_TYPE)) &&
279 exp->ex_Type = DupType(NULL, oexp->ex_Type);
282 if (exp->ex_Token == TOK_INLINE_CALL) {
284 DupStmt(sg, NULL, oexp->ex_AuxStmt);
286 exp->ex_AuxExp = oexp->ex_AuxExp; /* union */
295 ExpPrintError(Exp *exp, int type)
297 LexPrintRef(&exp->ex_LexRef, type);