As test runs are not invoked from the option parsing loop anymore, we
[wine] / programs / winedbg / expr.c
1 /*
2  * File expr.c - expression handling for Wine internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "debugger.h"
28 #include "expr.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
32
33 struct expr
34 {
35     unsigned int        type;
36     union
37     {
38         struct
39         {
40             int                 value;
41         } s_const;
42
43         struct
44         {
45             unsigned int        value;
46         } u_const;
47
48         struct
49         {
50             const char*         str;
51         } string;
52
53         struct
54         {
55             const char*         name;
56         } symbol;
57
58         struct
59         {
60             const char*         name;
61         } intvar;
62
63         struct
64         {
65             int                 unop_type;
66             struct expr*        exp1;
67             long int            result;
68         } unop;
69
70         struct
71         {
72             int                 binop_type;
73             struct expr*        exp1;
74             struct expr*        exp2;
75             long int            result;
76         } binop;
77
78         struct
79         {
80             struct type_expr_t  cast_to;
81             struct expr*        expr;
82         } cast;
83
84         struct
85         {
86             struct expr*        exp1;
87             const char*         element_name;
88             long int            result;
89         } structure;
90
91         struct
92         {
93             const char*         funcname;
94             int                 nargs;
95             struct expr*        arg[5];
96             long int            result;
97         } call;
98
99     } un;
100 };
101
102 #define EXPR_TYPE_S_CONST       0
103 #define EXPR_TYPE_U_CONST       1
104 #define EXPR_TYPE_SYMBOL        2
105 #define EXPR_TYPE_INTVAR        3
106 #define EXPR_TYPE_BINOP         4
107 #define EXPR_TYPE_UNOP          5
108 #define EXPR_TYPE_STRUCT        6
109 #define EXPR_TYPE_PSTRUCT       7
110 #define EXPR_TYPE_CALL          8
111 #define EXPR_TYPE_STRING        9
112 #define EXPR_TYPE_CAST          10
113
114 static char expr_list[4096];
115 static unsigned int next_expr_free = 0;
116
117 static struct expr* expr_alloc(void)
118 {
119     struct expr*        rtn;
120
121     rtn = (struct expr*)&expr_list[next_expr_free];
122
123     next_expr_free += sizeof(struct expr);
124     assert(next_expr_free < sizeof(expr_list));
125
126     return rtn;
127 }
128
129 void expr_free_all(void)
130 {
131     next_expr_free = 0;
132 }
133
134 struct expr* expr_alloc_typecast(struct type_expr_t* tet, struct expr* exp)
135 {
136     struct expr*        ex;
137
138     ex = expr_alloc();
139
140     ex->type            = EXPR_TYPE_CAST;
141     ex->un.cast.cast_to = *tet;
142     ex->un.cast.expr    = exp;
143     return ex;
144 }
145
146 struct expr* expr_alloc_internal_var(const char* name)
147 {
148     struct expr* ex;
149
150     ex = expr_alloc();
151
152     ex->type           = EXPR_TYPE_INTVAR;
153     ex->un.intvar.name = name;
154     return ex;
155 }
156
157 struct expr* expr_alloc_symbol(const char* name)
158 {
159     struct expr* ex;
160
161     ex = expr_alloc();
162
163     ex->type           = EXPR_TYPE_SYMBOL;
164     ex->un.symbol.name = name;
165     return ex;
166 }
167
168 struct expr* expr_alloc_sconstant(int value)
169 {
170     struct expr*        ex;
171
172     ex = expr_alloc();
173
174     ex->type             = EXPR_TYPE_S_CONST;
175     ex->un.s_const.value = value;
176     return ex;
177 }
178
179 struct expr* expr_alloc_uconstant(unsigned int value)
180 {
181     struct expr*        ex;
182
183     ex = expr_alloc();
184
185     ex->type             = EXPR_TYPE_U_CONST;
186     ex->un.u_const.value = value;
187     return ex;
188 }
189
190 struct expr* expr_alloc_string(const char* str)
191 {
192     struct expr*        ex;
193     char*               pnt;
194
195     ex = expr_alloc();
196
197     ex->type          = EXPR_TYPE_STRING;
198     ex->un.string.str = str + 1;
199     if ((pnt = strrchr(ex->un.string.str, '"'))) *pnt = '\0';
200     return ex;
201 }
202
203 struct expr* expr_alloc_binary_op(int op_type, struct expr* exp1, struct expr* exp2)
204 {
205     struct expr*        ex;
206
207     ex = expr_alloc();
208
209     ex->type                = EXPR_TYPE_BINOP;
210     ex->un.binop.binop_type = op_type;
211     ex->un.binop.exp1       = exp1;
212     ex->un.binop.exp2       = exp2;
213     return ex;
214 }
215
216 struct expr* expr_alloc_unary_op(int op_type, struct expr* exp1)
217 {
218     struct expr*        ex;
219
220     ex = expr_alloc();
221
222     ex->type              = EXPR_TYPE_UNOP;
223     ex->un.unop.unop_type = op_type;
224     ex->un.unop.exp1      = exp1;
225     return ex;
226 }
227
228 struct expr* expr_alloc_struct(struct expr* exp, const char* element)
229 {
230     struct expr*        ex;
231
232     ex = expr_alloc();
233
234     ex->type                      = EXPR_TYPE_STRUCT;
235     ex->un.structure.exp1         = exp;
236     ex->un.structure.element_name = element;
237     return ex;
238 }
239
240 struct expr* expr_alloc_pstruct(struct expr* exp, const char* element)
241 {
242     struct expr*        ex;
243
244     ex = expr_alloc();
245
246     ex->type                      = EXPR_TYPE_PSTRUCT;
247     ex->un.structure.exp1         = exp;
248     ex->un.structure.element_name = element;
249     return ex;
250 }
251
252 struct expr* expr_alloc_func_call(const char* funcname, int nargs, ...)
253 {
254     struct expr*        ex;
255     va_list             ap;
256     int                 i;
257
258     ex = expr_alloc();
259
260     ex->type             = EXPR_TYPE_CALL;
261     ex->un.call.funcname = funcname;
262     ex->un.call.nargs    = nargs;
263
264     va_start(ap, nargs);
265     for (i = 0; i < nargs; i++)
266     {
267         ex->un.call.arg[i] = va_arg(ap, struct expr*);
268     }
269     va_end(ap);
270     return ex;
271 }
272
273 /******************************************************************
274  *              expr_eval
275  *
276  */
277 struct dbg_lvalue expr_eval(struct expr* exp)
278 {
279     struct dbg_lvalue                   rtn;
280     int                                 i;
281     struct dbg_lvalue                   exp1;
282     struct dbg_lvalue                   exp2;
283     unsigned int                        cexp[5];
284     DWORD                               scale1, scale2, scale3;
285     struct dbg_type                     type1, type2;
286     DWORD                               tag;
287     const struct dbg_internal_var*      div;
288
289     rtn.cookie       = 0;
290     rtn.type.id      = dbg_itype_none;
291     rtn.type.module  = 0;
292     rtn.addr.Mode    = AddrModeFlat;
293     rtn.addr.Offset  = 0;
294     rtn.addr.Segment = 0;
295
296     switch (exp->type)
297     {
298     case EXPR_TYPE_CAST:
299         /* this is really brute force, we simply change the type... without 
300          * checking if this is right or not
301          */
302         rtn = expr_eval(exp->un.cast.expr);
303         switch (exp->un.cast.cast_to.type)
304         {
305         case type_expr_type_id:
306             if (exp->un.cast.cast_to.u.type.id == dbg_itype_none)
307             {
308                 dbg_printf("Can't cast to unknown type\n");
309                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
310             }
311             rtn.type = exp->un.cast.cast_to.u.type;
312             break;
313         case type_expr_udt_class:
314         case type_expr_udt_struct:
315         case type_expr_udt_union:
316             rtn.type = types_find_type((DWORD)memory_to_linear_addr(&rtn.addr),
317                                        exp->un.cast.cast_to.u.name, SymTagUDT);
318             if (rtn.type.id == dbg_itype_none)
319             {
320                 dbg_printf("Can't cast to UDT %s\n", exp->un.cast.cast_to.u.name);
321                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
322             }
323             break;
324         case type_expr_enumeration:
325             rtn.type = types_find_type((DWORD)memory_to_linear_addr(&rtn.addr),
326                                        exp->un.cast.cast_to.u.name, SymTagEnum);
327             if (rtn.type.id == dbg_itype_none)
328             {
329                 dbg_printf("Can't cast to enumeration %s\n", exp->un.cast.cast_to.u.name);
330                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
331             }
332             break;
333         default:
334             dbg_printf("Unsupported cast type %u\n", exp->un.cast.cast_to.type);
335             RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
336         }
337         for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
338         {
339             rtn.type = types_find_pointer(&rtn.type);
340             if (rtn.type.id == dbg_itype_none)
341             {
342                 dbg_printf("Cannot find pointer type\n");
343                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
344             }
345         }
346         break;
347     case EXPR_TYPE_STRING:
348         rtn.cookie      = DLV_HOST;
349         rtn.type.id     = dbg_itype_astring;
350         rtn.type.module = 0;
351         rtn.addr.Offset = (unsigned int)&exp->un.string.str;
352         break;
353     case EXPR_TYPE_U_CONST:
354         rtn.cookie      = DLV_HOST;
355         rtn.type.id     = dbg_itype_unsigned_int;
356         rtn.type.module = 0;
357         rtn.addr.Offset = (unsigned int)&exp->un.u_const.value;
358         break;
359     case EXPR_TYPE_S_CONST:
360         rtn.cookie      = DLV_HOST;
361         rtn.type.id     = dbg_itype_signed_int;
362         rtn.type.module = 0;
363         rtn.addr.Offset = (unsigned int)&exp->un.s_const.value;
364         break;
365     case EXPR_TYPE_SYMBOL:
366         switch (symbol_get_lvalue(exp->un.symbol.name, -1, &rtn, FALSE))
367         {
368         case sglv_found:
369             break;
370         case sglv_unknown:
371             RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
372             /* should never be here */
373         case sglv_aborted:
374             RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
375             /* should never be here */
376         }
377         break;
378     case EXPR_TYPE_PSTRUCT:
379         exp1 = expr_eval(exp->un.structure.exp1);
380         if (exp1.type.id == dbg_itype_none || !types_deref(&exp1, &rtn) ||
381             rtn.type.id == dbg_itype_none)
382             RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
383         if (!types_udt_find_element(&rtn, exp->un.structure.element_name,
384                                     &exp->un.structure.result))
385         {
386             dbg_printf("%s\n", exp->un.structure.element_name);
387             RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
388         }
389         break;
390     case EXPR_TYPE_STRUCT:
391         exp1 = expr_eval(exp->un.structure.exp1);
392         if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
393         rtn = exp1;
394         if (!types_udt_find_element(&rtn, exp->un.structure.element_name,
395                                     &exp->un.structure.result))
396         {
397             dbg_printf("%s\n", exp->un.structure.element_name);
398             RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
399         }
400         break;
401     case EXPR_TYPE_CALL:
402         /*
403          * First, evaluate all of the arguments.  If any of them are not
404          * evaluable, then bail.
405          */
406         for (i = 0; i < exp->un.call.nargs; i++)
407         {
408             exp1 = expr_eval(exp->un.call.arg[i]);
409             if (exp1.type.id == dbg_itype_none)
410                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
411             cexp[i] = types_extract_as_integer(&exp1);
412         }
413
414         /*
415          * Now look up the address of the function itself.
416          */
417         switch (symbol_get_lvalue(exp->un.call.funcname, -1, &rtn, FALSE))
418         {
419         case sglv_found:
420             break;
421         case sglv_unknown:
422             RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
423             /* should never be here */
424         case sglv_aborted:
425             RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
426             /* should never be here */
427         }
428
429 #if 0
430         /* FIXME: NEWDBG NIY */
431         /* Anyway, I wonder how this could work depending on the calling order of
432          * the function (cdecl vs pascal for example)
433          */
434         int             (*fptr)();
435
436         fptr = (int (*)()) rtn.addr.off;
437         switch (exp->un.call.nargs)
438         {
439         case 0:
440             exp->un.call.result = (*fptr)();
441             break;
442         case 1:
443             exp->un.call.result = (*fptr)(cexp[0]);
444             break;
445         case 2:
446             exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
447             break;
448         case 3:
449             exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
450             break;
451         case 4:
452             exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
453             break;
454         case 5:
455             exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
456             break;
457         }
458 #else
459         dbg_printf("Function call no longer implemented\n");
460         /* would need to set up a call to this function, and then restore the current
461          * context afterwards...
462          */
463         exp->un.call.result = 0;
464 #endif
465         rtn.cookie = DLV_HOST;
466         /* get function signature type */
467         types_get_info(&rtn.type, TI_GET_TYPE, &rtn.type);
468         /* and now, return type */
469         types_get_info(&rtn.type, TI_GET_TYPE, &rtn.type);
470         rtn.addr.Offset = (unsigned int)&exp->un.call.result;
471         break;
472     case EXPR_TYPE_INTVAR:
473         rtn.cookie = DLV_HOST;
474         if (!(div = dbg_get_internal_var(exp->un.intvar.name)))
475             RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
476         rtn.type.id     = div->typeid;
477         rtn.type.module = 0;
478         rtn.addr.Offset = (unsigned int)div->pval;
479         break;
480     case EXPR_TYPE_BINOP:
481         rtn.cookie = DLV_HOST;
482         exp1 = expr_eval(exp->un.binop.exp1);
483         exp2 = expr_eval(exp->un.binop.exp2);
484         if (exp1.type.id == dbg_itype_none || exp2.type.id == dbg_itype_none)
485             RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
486         rtn.type.id = dbg_itype_signed_int;
487         rtn.type.module = 0;
488         rtn.addr.Offset = (unsigned int)&exp->un.binop.result;
489         switch (exp->un.binop.binop_type)
490         {
491         case EXP_OP_ADD:
492             if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
493                 tag != SymTagPointerType ||
494                 !types_get_info(&exp1.type, TI_GET_TYPE, &type1))
495                 type1.id = dbg_itype_none;
496             if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
497                 tag != SymTagPointerType ||
498                 !types_get_info(&exp2.type, TI_GET_TYPE, &type2))
499                 type2.id = dbg_itype_none;
500             scale1 = 1;
501             scale2 = 1;
502             if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
503                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
504             if (type1.id != dbg_itype_none)
505             {
506                 types_get_info(&type1, TI_GET_LENGTH, &scale2);
507                 rtn.type = exp1.type;
508             }
509             else if (type2.id != dbg_itype_none)
510             {
511                 types_get_info(&type2, TI_GET_LENGTH, &scale1);
512                 rtn.type = exp2.type;
513             }
514             exp->un.binop.result = (types_extract_as_integer(&exp1) * scale1 +
515                                     scale2 * types_extract_as_integer(&exp2));
516             break;
517         case EXP_OP_SUB:
518             if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
519                 tag != SymTagPointerType ||
520                 !types_get_info(&exp1.type, TI_GET_TYPE, &type1))
521                 type1.id = dbg_itype_none;
522             if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
523                 tag != SymTagPointerType ||
524                 !types_get_info(&exp2.type, TI_GET_TYPE, &type2))
525                 type2.id = dbg_itype_none;
526             scale1 = 1;
527             scale2 = 1;
528             scale3 = 1;
529             if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
530             {
531                 WINE_FIXME("This may fail (if module base address are wrongly calculated)\n");
532                 if (memcmp(&type1, &type2, sizeof(struct dbg_type)))
533                     RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
534                 types_get_info(&type1, TI_GET_LENGTH, &scale3);
535             }
536             else if (type1.id != dbg_itype_none)
537             {
538                 types_get_info(&type1, TI_GET_LENGTH, &scale2);
539                 rtn.type = exp1.type;
540             }
541             else if (type2.id != dbg_itype_none)
542             {
543                 types_get_info(&type2, TI_GET_LENGTH, &scale1);
544                 rtn.type = exp2.type;
545             }
546             exp->un.binop.result = (types_extract_as_integer(&exp1) * scale1 - 
547                                     types_extract_as_integer(&exp2) * scale2) / scale3;
548             break;
549         case EXP_OP_SEG:
550             rtn.type.id = dbg_itype_none;
551             rtn.type.module = 0;
552             rtn.addr.Mode = AddrMode1632;
553             rtn.addr.Segment = types_extract_as_integer(&exp1);
554             rtn.addr.Offset = types_extract_as_integer(&exp2);
555             break;
556         case EXP_OP_LOR:
557             exp->un.binop.result = (types_extract_as_integer(&exp1) || types_extract_as_integer(&exp2));
558             break;
559         case EXP_OP_LAND:
560             exp->un.binop.result = (types_extract_as_integer(&exp1) && types_extract_as_integer(&exp2));
561             break;
562         case EXP_OP_OR:
563             exp->un.binop.result = (types_extract_as_integer(&exp1) | types_extract_as_integer(&exp2));
564             break;
565         case EXP_OP_AND:
566             exp->un.binop.result = (types_extract_as_integer(&exp1) & types_extract_as_integer(&exp2));
567             break;
568         case EXP_OP_XOR:
569             exp->un.binop.result = (types_extract_as_integer(&exp1) ^ types_extract_as_integer(&exp2));
570             break;
571         case EXP_OP_EQ:
572             exp->un.binop.result = (types_extract_as_integer(&exp1) == types_extract_as_integer(&exp2));
573             break;
574         case EXP_OP_GT:
575             exp->un.binop.result = (types_extract_as_integer(&exp1) > types_extract_as_integer(&exp2));
576             break;
577         case EXP_OP_LT:
578             exp->un.binop.result = (types_extract_as_integer(&exp1) < types_extract_as_integer(&exp2));
579             break;
580         case EXP_OP_GE:
581             exp->un.binop.result = (types_extract_as_integer(&exp1) >= types_extract_as_integer(&exp2));
582             break;
583         case EXP_OP_LE:
584             exp->un.binop.result = (types_extract_as_integer(&exp1) <= types_extract_as_integer(&exp2));
585             break;
586         case EXP_OP_NE:
587             exp->un.binop.result = (types_extract_as_integer(&exp1) != types_extract_as_integer(&exp2));
588             break;
589         case EXP_OP_SHL:
590             exp->un.binop.result = ((unsigned long)types_extract_as_integer(&exp1) << types_extract_as_integer(&exp2));
591             break;
592         case EXP_OP_SHR:
593             exp->un.binop.result = ((unsigned long)types_extract_as_integer(&exp1) >> types_extract_as_integer(&exp2));
594             break;
595         case EXP_OP_MUL:
596             exp->un.binop.result = (types_extract_as_integer(&exp1) * types_extract_as_integer(&exp2));
597             break;
598         case EXP_OP_DIV:
599             if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
600             exp->un.binop.result = (types_extract_as_integer(&exp1) / types_extract_as_integer(&exp2));
601             break;
602         case EXP_OP_REM:
603             if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
604             exp->un.binop.result = (types_extract_as_integer(&exp1) % types_extract_as_integer(&exp2));
605             break;
606         case EXP_OP_ARR:
607             if (!types_array_index(&exp1, types_extract_as_integer(&exp2), &rtn))
608                 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
609             break;
610         default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
611         }
612         break;
613     case EXPR_TYPE_UNOP:
614         rtn.cookie = DLV_HOST;
615         exp1 = expr_eval(exp->un.unop.exp1);
616         if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
617         rtn.addr.Offset = (unsigned int)&exp->un.unop.result;
618         rtn.type.id     = dbg_itype_signed_int;
619         rtn.type.module = 0;
620         switch (exp->un.unop.unop_type)
621         {
622         case EXP_OP_NEG:
623             exp->un.unop.result = -types_extract_as_integer(&exp1);
624             break;
625         case EXP_OP_NOT:
626             exp->un.unop.result = !types_extract_as_integer(&exp1);
627             break;
628         case EXP_OP_LNOT:
629             exp->un.unop.result = ~types_extract_as_integer(&exp1);
630             break;
631         case EXP_OP_DEREF:
632             if (!types_deref(&exp1, &rtn))
633                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
634             break;
635         case EXP_OP_FORCE_DEREF:
636             rtn = exp1;
637             if (exp1.cookie == DLV_TARGET)
638                 dbg_read_memory(memory_to_linear_addr(&exp1.addr), &rtn.addr.Offset, sizeof(rtn.addr.Offset));
639             break;
640         case EXP_OP_ADDR:
641             /* only do it on linear addresses */
642             if (exp1.addr.Mode != AddrModeFlat)
643                 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
644             exp->un.unop.result = (unsigned int)memory_to_linear_addr(&exp1.addr);
645             rtn.type = types_find_pointer(&exp1.type);
646             break;
647         default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
648         }
649         break;
650     default:
651         WINE_FIXME("Unexpected expression (%d).\n", exp->type);
652         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
653         break;
654     }
655
656     return rtn;
657 }
658
659 int expr_print(const struct expr* exp)
660 {
661     int                 i;
662     struct dbg_type     type;
663
664     switch (exp->type)
665     {
666     case EXPR_TYPE_CAST:
667         WINE_FIXME("No longer supported (missing module base)\n");
668         dbg_printf("((");
669         switch (exp->un.cast.cast_to.type)
670         {
671         case type_expr_type_id:
672             type.module = 0;
673             type.id = exp->un.cast.cast_to.type;
674             types_print_type(&type, FALSE); break;
675         case type_expr_udt_class:
676             dbg_printf("class %s", exp->un.cast.cast_to.u.name); break;
677         case type_expr_udt_struct:
678             dbg_printf("struct %s", exp->un.cast.cast_to.u.name); break;
679         case type_expr_udt_union:
680             dbg_printf("union %s", exp->un.cast.cast_to.u.name); break;
681         case type_expr_enumeration:
682             dbg_printf("enum %s", exp->un.cast.cast_to.u.name); break;
683         }
684         for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
685             dbg_printf("*");
686         dbg_printf(")");
687         expr_print(exp->un.cast.expr);
688         dbg_printf(")");
689         break;
690     case EXPR_TYPE_INTVAR:
691         dbg_printf("$%s", exp->un.intvar.name);
692         break;
693     case EXPR_TYPE_U_CONST:
694         dbg_printf("%u", exp->un.u_const.value);
695         break;
696     case EXPR_TYPE_S_CONST:
697         dbg_printf("%d", exp->un.s_const.value);
698         break;
699     case EXPR_TYPE_STRING:
700         dbg_printf("\"%s\"", exp->un.string.str);
701         break;
702     case EXPR_TYPE_SYMBOL:
703         dbg_printf("%s" , exp->un.symbol.name);
704         break;
705     case EXPR_TYPE_PSTRUCT:
706         expr_print(exp->un.structure.exp1);
707         dbg_printf("->%s", exp->un.structure.element_name);
708         break;
709     case EXPR_TYPE_STRUCT:
710         expr_print(exp->un.structure.exp1);
711         dbg_printf(".%s", exp->un.structure.element_name);
712         break;
713     case EXPR_TYPE_CALL:
714         dbg_printf("%s(",exp->un.call.funcname);
715         for (i = 0; i < exp->un.call.nargs; i++)
716         {
717             expr_print(exp->un.call.arg[i]);
718             if (i != exp->un.call.nargs - 1) dbg_printf(", ");
719         }
720         dbg_printf(")");
721         break;
722     case EXPR_TYPE_BINOP:
723         dbg_printf("(");
724         expr_print(exp->un.binop.exp1);
725         switch (exp->un.binop.binop_type)
726         {
727         case EXP_OP_ADD:        dbg_printf(" + ");    break;
728         case EXP_OP_SUB:        dbg_printf(" - ");    break;
729         case EXP_OP_SEG:        dbg_printf(":");      break;
730         case EXP_OP_LOR:        dbg_printf(" || ");   break;
731         case EXP_OP_LAND:       dbg_printf(" && ");   break;
732         case EXP_OP_OR:         dbg_printf(" | ");    break;
733         case EXP_OP_AND:        dbg_printf(" & ");    break;
734         case EXP_OP_XOR:        dbg_printf(" ^ ");    break;
735         case EXP_OP_EQ:         dbg_printf(" == ");   break;
736         case EXP_OP_GT:         dbg_printf(" > ");    break;
737         case EXP_OP_LT:         dbg_printf(" < ");    break;
738         case EXP_OP_GE:         dbg_printf(" >= ");   break;
739         case EXP_OP_LE:         dbg_printf(" <= ");   break;
740         case EXP_OP_NE:         dbg_printf(" != ");   break;
741         case EXP_OP_SHL:        dbg_printf(" << ");   break;
742         case EXP_OP_SHR:        dbg_printf(" >> ");   break;
743         case EXP_OP_MUL:        dbg_printf(" * ");    break;
744         case EXP_OP_DIV:        dbg_printf(" / ");    break;
745         case EXP_OP_REM:        dbg_printf(" %% ");   break;
746         case EXP_OP_ARR:        dbg_printf("[");      break;
747         default:                                      break;
748         }
749         expr_print(exp->un.binop.exp2);
750         if (exp->un.binop.binop_type == EXP_OP_ARR) dbg_printf("]");
751         dbg_printf(")");
752         break;
753     case EXPR_TYPE_UNOP:
754         switch (exp->un.unop.unop_type)
755         {
756         case EXP_OP_NEG:        dbg_printf("-");      break;
757         case EXP_OP_NOT:        dbg_printf("!");      break;
758         case EXP_OP_LNOT:       dbg_printf("~");      break;
759         case EXP_OP_DEREF:      dbg_printf("*");      break;
760         case EXP_OP_ADDR:       dbg_printf("&");      break;
761         }
762         expr_print(exp->un.unop.exp1);
763         break;
764     default:
765         WINE_FIXME("Unexpected expression (%u).\n", exp->type);
766         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
767         break;
768     }
769
770     return TRUE;
771 }
772
773 struct expr* expr_clone(const struct expr* exp)
774 {
775     int                 i;
776     struct expr*        rtn;
777
778     rtn = HeapAlloc(GetProcessHeap(), 0, sizeof(struct expr));
779
780     /*
781      * First copy the contents of the expression itself.
782      */
783     *rtn = *exp;
784
785     switch (exp->type)
786     {
787     case EXPR_TYPE_CAST:
788         rtn->un.cast.expr = expr_clone(exp->un.cast.expr);
789         break;
790     case EXPR_TYPE_INTVAR:
791         rtn->un.intvar.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.intvar.name) + 1), exp->un.intvar.name);
792         break;
793     case EXPR_TYPE_U_CONST:
794     case EXPR_TYPE_S_CONST:
795         break;
796     case EXPR_TYPE_STRING:
797         rtn->un.string.str = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.string.str) + 1), exp->un.string.str);
798         break;
799     case EXPR_TYPE_SYMBOL:
800         rtn->un.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.symbol.name) + 1), exp->un.symbol.name);
801         break;
802     case EXPR_TYPE_PSTRUCT:
803     case EXPR_TYPE_STRUCT:
804         rtn->un.structure.exp1 = expr_clone(exp->un.structure.exp1);
805         rtn->un.structure.element_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.structure.element_name) + 1), exp->un.structure.element_name);
806         break;
807     case EXPR_TYPE_CALL:
808         for (i = 0; i < exp->un.call.nargs; i++)
809         {
810             rtn->un.call.arg[i] = expr_clone(exp->un.call.arg[i]);
811         }
812         rtn->un.call.funcname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.call.funcname) + 1), exp->un.call.funcname);
813         break;
814     case EXPR_TYPE_BINOP:
815         rtn->un.binop.exp1 = expr_clone(exp->un.binop.exp1);
816         rtn->un.binop.exp2 = expr_clone(exp->un.binop.exp2);
817         break;
818     case EXPR_TYPE_UNOP:
819         rtn->un.unop.exp1 = expr_clone(exp->un.unop.exp1);
820         break;
821     default:
822         WINE_FIXME("Unexpected expression (%u).\n", exp->type);
823         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
824         break;
825     }
826
827     return rtn;
828 }
829
830
831 /*
832  * Recursively go through an expression tree and free all memory associated
833  * with it.
834  */
835 int expr_free(struct expr* exp)
836 {
837     int i;
838
839     switch (exp->type)
840     {
841     case EXPR_TYPE_CAST:
842         expr_free(exp->un.cast.expr);
843         break;
844     case EXPR_TYPE_INTVAR:
845         HeapFree(GetProcessHeap(), 0, (char*)exp->un.intvar.name);
846         break;
847     case EXPR_TYPE_U_CONST:
848     case EXPR_TYPE_S_CONST:
849         break;
850     case EXPR_TYPE_STRING:
851         HeapFree(GetProcessHeap(), 0, (char*)exp->un.string.str);
852         break;
853     case EXPR_TYPE_SYMBOL:
854         HeapFree(GetProcessHeap(), 0, (char*)exp->un.symbol.name);
855         break;
856     case EXPR_TYPE_PSTRUCT:
857     case EXPR_TYPE_STRUCT:
858         expr_free(exp->un.structure.exp1);
859         HeapFree(GetProcessHeap(), 0, (char*)exp->un.structure.element_name);
860         break;
861     case EXPR_TYPE_CALL:
862         for (i = 0; i < exp->un.call.nargs; i++)
863         {
864             expr_free(exp->un.call.arg[i]);
865         }
866         HeapFree(GetProcessHeap(), 0, (char*)exp->un.call.funcname);
867         break;
868     case EXPR_TYPE_BINOP:
869         expr_free(exp->un.binop.exp1);
870         expr_free(exp->un.binop.exp2);
871         break;
872     case EXPR_TYPE_UNOP:
873         expr_free(exp->un.unop.exp1);
874         break;
875     default:
876         WINE_FIXME("Unexpected expression (%u).\n", exp->type);
877         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
878         break;
879     }
880
881     HeapFree(GetProcessHeap(), 0, exp);
882     return TRUE;
883 }