programs: Remove unreachable break after return/break. Found by Smatch.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
194     ex = expr_alloc();
195
196     ex->type          = EXPR_TYPE_STRING;
197     ex->un.string.str = str;
198     return ex;
199 }
200
201 struct expr* expr_alloc_binary_op(int op_type, struct expr* exp1, struct expr* exp2)
202 {
203     struct expr*        ex;
204
205     ex = expr_alloc();
206
207     ex->type                = EXPR_TYPE_BINOP;
208     ex->un.binop.binop_type = op_type;
209     ex->un.binop.exp1       = exp1;
210     ex->un.binop.exp2       = exp2;
211     return ex;
212 }
213
214 struct expr* expr_alloc_unary_op(int op_type, struct expr* exp1)
215 {
216     struct expr*        ex;
217
218     ex = expr_alloc();
219
220     ex->type              = EXPR_TYPE_UNOP;
221     ex->un.unop.unop_type = op_type;
222     ex->un.unop.exp1      = exp1;
223     return ex;
224 }
225
226 struct expr* expr_alloc_struct(struct expr* exp, const char* element)
227 {
228     struct expr*        ex;
229
230     ex = expr_alloc();
231
232     ex->type                      = EXPR_TYPE_STRUCT;
233     ex->un.structure.exp1         = exp;
234     ex->un.structure.element_name = element;
235     return ex;
236 }
237
238 struct expr* expr_alloc_pstruct(struct expr* exp, const char* element)
239 {
240     struct expr*        ex;
241
242     ex = expr_alloc();
243
244     ex->type                      = EXPR_TYPE_PSTRUCT;
245     ex->un.structure.exp1         = exp;
246     ex->un.structure.element_name = element;
247     return ex;
248 }
249
250 struct expr* expr_alloc_func_call(const char* funcname, int nargs, ...)
251 {
252     struct expr*        ex;
253     va_list             ap;
254     int                 i;
255
256     ex = expr_alloc();
257
258     ex->type             = EXPR_TYPE_CALL;
259     ex->un.call.funcname = funcname;
260     ex->un.call.nargs    = nargs;
261
262     va_start(ap, nargs);
263     for (i = 0; i < nargs; i++)
264     {
265         ex->un.call.arg[i] = va_arg(ap, struct expr*);
266     }
267     va_end(ap);
268     return ex;
269 }
270
271 /******************************************************************
272  *              expr_eval
273  *
274  */
275 struct dbg_lvalue expr_eval(struct expr* exp)
276 {
277     struct dbg_lvalue                   rtn;
278     int                                 i;
279     struct dbg_lvalue                   exp1;
280     struct dbg_lvalue                   exp2;
281     unsigned int                        cexp[5];
282     DWORD64                             scale1, scale2, scale3;
283     struct dbg_type                     type1, type2;
284     DWORD                               tag;
285     const struct dbg_internal_var*      div;
286
287     rtn.cookie       = 0;
288     rtn.type.id      = dbg_itype_none;
289     rtn.type.module  = 0;
290     rtn.addr.Mode    = AddrModeFlat;
291     rtn.addr.Offset  = 0;
292     rtn.addr.Segment = 0;
293
294     switch (exp->type)
295     {
296     case EXPR_TYPE_CAST:
297         /* this is really brute force, we simply change the type... without 
298          * checking if this is right or not
299          */
300         rtn = expr_eval(exp->un.cast.expr);
301         switch (exp->un.cast.cast_to.type)
302         {
303         case type_expr_type_id:
304             if (exp->un.cast.cast_to.u.type.id == dbg_itype_none)
305             {
306                 dbg_printf("Can't cast to unknown type\n");
307                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
308             }
309             rtn.type = exp->un.cast.cast_to.u.type;
310             break;
311         case type_expr_udt_class:
312         case type_expr_udt_struct:
313         case type_expr_udt_union:
314             rtn.type = types_find_type(rtn.type.module, exp->un.cast.cast_to.u.name,
315                                        SymTagUDT);
316             if (rtn.type.id == dbg_itype_none)
317             {
318                 dbg_printf("Can't cast to UDT %s\n", exp->un.cast.cast_to.u.name);
319                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
320             }
321             break;
322         case type_expr_enumeration:
323             rtn.type = types_find_type(rtn.type.module, exp->un.cast.cast_to.u.name,
324                                        SymTagEnum);
325             if (rtn.type.id == dbg_itype_none)
326             {
327                 dbg_printf("Can't cast to enumeration %s\n", exp->un.cast.cast_to.u.name);
328                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
329             }
330             break;
331         default:
332             dbg_printf("Unsupported cast type %u\n", exp->un.cast.cast_to.type);
333             RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
334         }
335         for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
336         {
337             rtn.type = types_find_pointer(&rtn.type);
338             if (rtn.type.id == dbg_itype_none)
339             {
340                 dbg_printf("Cannot find pointer type\n");
341                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
342             }
343         }
344         break;
345     case EXPR_TYPE_STRING:
346         rtn.cookie      = DLV_HOST;
347         rtn.type.id     = dbg_itype_astring;
348         rtn.type.module = 0;
349         rtn.addr.Offset = (unsigned int)&exp->un.string.str;
350         break;
351     case EXPR_TYPE_U_CONST:
352         rtn.cookie      = DLV_HOST;
353         rtn.type.id     = dbg_itype_unsigned_int;
354         rtn.type.module = 0;
355         rtn.addr.Offset = (unsigned int)&exp->un.u_const.value;
356         break;
357     case EXPR_TYPE_S_CONST:
358         rtn.cookie      = DLV_HOST;
359         rtn.type.id     = dbg_itype_signed_int;
360         rtn.type.module = 0;
361         rtn.addr.Offset = (unsigned int)&exp->un.s_const.value;
362         break;
363     case EXPR_TYPE_SYMBOL:
364         switch (symbol_get_lvalue(exp->un.symbol.name, -1, &rtn, FALSE))
365         {
366         case sglv_found:
367             break;
368         case sglv_unknown:
369             RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
370             /* should never be here */
371         case sglv_aborted:
372             RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
373             /* should never be here */
374         }
375         break;
376     case EXPR_TYPE_PSTRUCT:
377         exp1 = expr_eval(exp->un.structure.exp1);
378         if (exp1.type.id == dbg_itype_none || !types_deref(&exp1, &rtn) ||
379             rtn.type.id == dbg_itype_none)
380             RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
381         if (!types_udt_find_element(&rtn, exp->un.structure.element_name,
382                                     &exp->un.structure.result))
383         {
384             dbg_printf("%s\n", exp->un.structure.element_name);
385             RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
386         }
387         break;
388     case EXPR_TYPE_STRUCT:
389         exp1 = expr_eval(exp->un.structure.exp1);
390         if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
391         rtn = exp1;
392         if (!types_udt_find_element(&rtn, exp->un.structure.element_name,
393                                     &exp->un.structure.result))
394         {
395             dbg_printf("%s\n", exp->un.structure.element_name);
396             RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
397         }
398         break;
399     case EXPR_TYPE_CALL:
400         /*
401          * First, evaluate all of the arguments.  If any of them are not
402          * evaluable, then bail.
403          */
404         for (i = 0; i < exp->un.call.nargs; i++)
405         {
406             exp1 = expr_eval(exp->un.call.arg[i]);
407             if (exp1.type.id == dbg_itype_none)
408                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
409             cexp[i] = types_extract_as_integer(&exp1);
410         }
411
412         /*
413          * Now look up the address of the function itself.
414          */
415         switch (symbol_get_lvalue(exp->un.call.funcname, -1, &rtn, FALSE))
416         {
417         case sglv_found:
418             break;
419         case sglv_unknown:
420             RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
421             /* should never be here */
422         case sglv_aborted:
423             RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
424             /* should never be here */
425         }
426
427 #if 0
428         /* FIXME: NEWDBG NIY */
429         /* Anyway, I wonder how this could work depending on the calling order of
430          * the function (cdecl vs pascal for example)
431          */
432         int             (*fptr)();
433
434         fptr = (int (*)()) rtn.addr.off;
435         switch (exp->un.call.nargs)
436         {
437         case 0:
438             exp->un.call.result = (*fptr)();
439             break;
440         case 1:
441             exp->un.call.result = (*fptr)(cexp[0]);
442             break;
443         case 2:
444             exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
445             break;
446         case 3:
447             exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
448             break;
449         case 4:
450             exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
451             break;
452         case 5:
453             exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
454             break;
455         }
456 #else
457         dbg_printf("Function call no longer implemented\n");
458         /* would need to set up a call to this function, and then restore the current
459          * context afterwards...
460          */
461         exp->un.call.result = 0;
462 #endif
463         rtn.cookie = DLV_HOST;
464         /* get return type from function signature tupe */
465         types_get_info(&rtn.type, TI_GET_TYPE, &rtn.type.id);
466         rtn.addr.Offset = (unsigned int)&exp->un.call.result;
467         break;
468     case EXPR_TYPE_INTVAR:
469         rtn.cookie = DLV_HOST;
470         if (!(div = dbg_get_internal_var(exp->un.intvar.name)))
471             RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
472         rtn.type.id     = div->typeid;
473         rtn.type.module = 0;
474         rtn.addr.Offset = (unsigned int)div->pval;
475         break;
476     case EXPR_TYPE_BINOP:
477         rtn.cookie = DLV_HOST;
478         exp1 = expr_eval(exp->un.binop.exp1);
479         exp2 = expr_eval(exp->un.binop.exp2);
480         if (exp1.type.id == dbg_itype_none || exp2.type.id == dbg_itype_none)
481             RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
482         rtn.type.id = dbg_itype_signed_int;
483         rtn.type.module = 0;
484         rtn.addr.Offset = (unsigned int)&exp->un.binop.result;
485         type1 = exp1.type;
486         type2 = exp2.type;
487         switch (exp->un.binop.binop_type)
488         {
489         case EXP_OP_ADD:
490             if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
491                 tag != SymTagPointerType ||
492                 !types_get_info(&exp1.type, TI_GET_TYPE, &type1.id))
493                 type1.id = dbg_itype_none;
494             if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
495                 tag != SymTagPointerType ||
496                 !types_get_info(&exp2.type, TI_GET_TYPE, &type2.id))
497                 type2.id = dbg_itype_none;
498             scale1 = 1;
499             scale2 = 1;
500             if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
501                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
502             if (type1.id != dbg_itype_none)
503             {
504                 types_get_info(&type1, TI_GET_LENGTH, &scale2);
505                 rtn.type = exp1.type;
506             }
507             else if (type2.id != dbg_itype_none)
508             {
509                 types_get_info(&type2, TI_GET_LENGTH, &scale1);
510                 rtn.type = exp2.type;
511             }
512             exp->un.binop.result = types_extract_as_integer(&exp1) * (DWORD)scale1 +
513                 (DWORD)scale2 * types_extract_as_integer(&exp2);
514             break;
515         case EXP_OP_SUB:
516             if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
517                 tag != SymTagPointerType ||
518                 !types_get_info(&exp1.type, TI_GET_TYPE, &type1.id))
519                 type1.id = dbg_itype_none;
520             if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
521                 tag != SymTagPointerType ||
522                 !types_get_info(&exp2.type, TI_GET_TYPE, &type2.id))
523                 type2.id = dbg_itype_none;
524             scale1 = 1;
525             scale2 = 1;
526             scale3 = 1;
527             if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
528             {
529                 WINE_FIXME("This may fail (if module base address are wrongly calculated)\n");
530                 if (memcmp(&type1, &type2, sizeof(struct dbg_type)))
531                     RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
532                 types_get_info(&type1, TI_GET_LENGTH, &scale3);
533             }
534             else if (type1.id != dbg_itype_none)
535             {
536                 types_get_info(&type1, TI_GET_LENGTH, &scale2);
537                 rtn.type = exp1.type;
538             }
539             else if (type2.id != dbg_itype_none)
540             {
541                 types_get_info(&type2, TI_GET_LENGTH, &scale1);
542                 rtn.type = exp2.type;
543             }
544             exp->un.binop.result = (types_extract_as_integer(&exp1) * (DWORD)scale1 - 
545                                     types_extract_as_integer(&exp2) * (DWORD)scale2) / (DWORD)scale3;
546             break;
547         case EXP_OP_SEG:
548             rtn.type.id = dbg_itype_segptr;
549             rtn.type.module = 0;
550             be_cpu->build_addr(dbg_curr_thread->handle, &dbg_context, &rtn.addr,
551                                types_extract_as_integer(&exp1), types_extract_as_integer(&exp2));
552             break;
553         case EXP_OP_LOR:
554             exp->un.binop.result = (types_extract_as_integer(&exp1) || types_extract_as_integer(&exp2));
555             break;
556         case EXP_OP_LAND:
557             exp->un.binop.result = (types_extract_as_integer(&exp1) && types_extract_as_integer(&exp2));
558             break;
559         case EXP_OP_OR:
560             exp->un.binop.result = (types_extract_as_integer(&exp1) | types_extract_as_integer(&exp2));
561             break;
562         case EXP_OP_AND:
563             exp->un.binop.result = (types_extract_as_integer(&exp1) & types_extract_as_integer(&exp2));
564             break;
565         case EXP_OP_XOR:
566             exp->un.binop.result = (types_extract_as_integer(&exp1) ^ types_extract_as_integer(&exp2));
567             break;
568         case EXP_OP_EQ:
569             exp->un.binop.result = (types_extract_as_integer(&exp1) == types_extract_as_integer(&exp2));
570             break;
571         case EXP_OP_GT:
572             exp->un.binop.result = (types_extract_as_integer(&exp1) > types_extract_as_integer(&exp2));
573             break;
574         case EXP_OP_LT:
575             exp->un.binop.result = (types_extract_as_integer(&exp1) < types_extract_as_integer(&exp2));
576             break;
577         case EXP_OP_GE:
578             exp->un.binop.result = (types_extract_as_integer(&exp1) >= types_extract_as_integer(&exp2));
579             break;
580         case EXP_OP_LE:
581             exp->un.binop.result = (types_extract_as_integer(&exp1) <= types_extract_as_integer(&exp2));
582             break;
583         case EXP_OP_NE:
584             exp->un.binop.result = (types_extract_as_integer(&exp1) != types_extract_as_integer(&exp2));
585             break;
586         case EXP_OP_SHL:
587             exp->un.binop.result = ((unsigned long)types_extract_as_integer(&exp1) << types_extract_as_integer(&exp2));
588             break;
589         case EXP_OP_SHR:
590             exp->un.binop.result = ((unsigned long)types_extract_as_integer(&exp1) >> types_extract_as_integer(&exp2));
591             break;
592         case EXP_OP_MUL:
593             exp->un.binop.result = (types_extract_as_integer(&exp1) * types_extract_as_integer(&exp2));
594             break;
595         case EXP_OP_DIV:
596             if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
597             exp->un.binop.result = (types_extract_as_integer(&exp1) / types_extract_as_integer(&exp2));
598             break;
599         case EXP_OP_REM:
600             if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
601             exp->un.binop.result = (types_extract_as_integer(&exp1) % types_extract_as_integer(&exp2));
602             break;
603         case EXP_OP_ARR:
604             if (!types_array_index(&exp1, types_extract_as_integer(&exp2), &rtn))
605                 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
606             break;
607         default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
608         }
609         break;
610     case EXPR_TYPE_UNOP:
611         rtn.cookie = DLV_HOST;
612         exp1 = expr_eval(exp->un.unop.exp1);
613         if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
614         rtn.addr.Offset = (unsigned int)&exp->un.unop.result;
615         rtn.type.id     = dbg_itype_signed_int;
616         rtn.type.module = 0;
617         switch (exp->un.unop.unop_type)
618         {
619         case EXP_OP_NEG:
620             exp->un.unop.result = -types_extract_as_integer(&exp1);
621             break;
622         case EXP_OP_NOT:
623             exp->un.unop.result = !types_extract_as_integer(&exp1);
624             break;
625         case EXP_OP_LNOT:
626             exp->un.unop.result = ~types_extract_as_integer(&exp1);
627             break;
628         case EXP_OP_DEREF:
629             if (!types_deref(&exp1, &rtn))
630                 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
631             break;
632         case EXP_OP_FORCE_DEREF:
633             rtn = exp1;
634             if (exp1.cookie == DLV_TARGET)
635                 dbg_read_memory(memory_to_linear_addr(&exp1.addr), &rtn.addr.Offset, sizeof(rtn.addr.Offset));
636             break;
637         case EXP_OP_ADDR:
638             /* only do it on linear addresses */
639             if (exp1.addr.Mode != AddrModeFlat)
640                 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
641             exp->un.unop.result = (unsigned int)memory_to_linear_addr(&exp1.addr);
642             rtn.type = types_find_pointer(&exp1.type);
643             if (rtn.type.id == dbg_itype_none)
644                 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
645             break;
646         default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
647         }
648         break;
649     default:
650         WINE_FIXME("Unexpected expression (%d).\n", exp->type);
651         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
652         break;
653     }
654
655     return rtn;
656 }
657
658 int expr_print(const struct expr* exp)
659 {
660     int                 i;
661     struct dbg_type     type;
662
663     switch (exp->type)
664     {
665     case EXPR_TYPE_CAST:
666         WINE_FIXME("No longer supported (missing module base)\n");
667         dbg_printf("((");
668         switch (exp->un.cast.cast_to.type)
669         {
670         case type_expr_type_id:
671             type.module = 0;
672             type.id = exp->un.cast.cast_to.type;
673             types_print_type(&type, FALSE); break;
674         case type_expr_udt_class:
675             dbg_printf("class %s", exp->un.cast.cast_to.u.name); break;
676         case type_expr_udt_struct:
677             dbg_printf("struct %s", exp->un.cast.cast_to.u.name); break;
678         case type_expr_udt_union:
679             dbg_printf("union %s", exp->un.cast.cast_to.u.name); break;
680         case type_expr_enumeration:
681             dbg_printf("enum %s", exp->un.cast.cast_to.u.name); break;
682         }
683         for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
684             dbg_printf("*");
685         dbg_printf(")");
686         expr_print(exp->un.cast.expr);
687         dbg_printf(")");
688         break;
689     case EXPR_TYPE_INTVAR:
690         dbg_printf("$%s", exp->un.intvar.name);
691         break;
692     case EXPR_TYPE_U_CONST:
693         dbg_printf("%u", exp->un.u_const.value);
694         break;
695     case EXPR_TYPE_S_CONST:
696         dbg_printf("%d", exp->un.s_const.value);
697         break;
698     case EXPR_TYPE_STRING:
699         dbg_printf("\"%s\"", exp->un.string.str);
700         break;
701     case EXPR_TYPE_SYMBOL:
702         dbg_printf("%s" , exp->un.symbol.name);
703         break;
704     case EXPR_TYPE_PSTRUCT:
705         expr_print(exp->un.structure.exp1);
706         dbg_printf("->%s", exp->un.structure.element_name);
707         break;
708     case EXPR_TYPE_STRUCT:
709         expr_print(exp->un.structure.exp1);
710         dbg_printf(".%s", exp->un.structure.element_name);
711         break;
712     case EXPR_TYPE_CALL:
713         dbg_printf("%s(",exp->un.call.funcname);
714         for (i = 0; i < exp->un.call.nargs; i++)
715         {
716             expr_print(exp->un.call.arg[i]);
717             if (i != exp->un.call.nargs - 1) dbg_printf(", ");
718         }
719         dbg_printf(")");
720         break;
721     case EXPR_TYPE_BINOP:
722         dbg_printf("(");
723         expr_print(exp->un.binop.exp1);
724         switch (exp->un.binop.binop_type)
725         {
726         case EXP_OP_ADD:        dbg_printf(" + ");    break;
727         case EXP_OP_SUB:        dbg_printf(" - ");    break;
728         case EXP_OP_SEG:        dbg_printf(":");      break;
729         case EXP_OP_LOR:        dbg_printf(" || ");   break;
730         case EXP_OP_LAND:       dbg_printf(" && ");   break;
731         case EXP_OP_OR:         dbg_printf(" | ");    break;
732         case EXP_OP_AND:        dbg_printf(" & ");    break;
733         case EXP_OP_XOR:        dbg_printf(" ^ ");    break;
734         case EXP_OP_EQ:         dbg_printf(" == ");   break;
735         case EXP_OP_GT:         dbg_printf(" > ");    break;
736         case EXP_OP_LT:         dbg_printf(" < ");    break;
737         case EXP_OP_GE:         dbg_printf(" >= ");   break;
738         case EXP_OP_LE:         dbg_printf(" <= ");   break;
739         case EXP_OP_NE:         dbg_printf(" != ");   break;
740         case EXP_OP_SHL:        dbg_printf(" << ");   break;
741         case EXP_OP_SHR:        dbg_printf(" >> ");   break;
742         case EXP_OP_MUL:        dbg_printf(" * ");    break;
743         case EXP_OP_DIV:        dbg_printf(" / ");    break;
744         case EXP_OP_REM:        dbg_printf(" %% ");   break;
745         case EXP_OP_ARR:        dbg_printf("[");      break;
746         default:                                      break;
747         }
748         expr_print(exp->un.binop.exp2);
749         if (exp->un.binop.binop_type == EXP_OP_ARR) dbg_printf("]");
750         dbg_printf(")");
751         break;
752     case EXPR_TYPE_UNOP:
753         switch (exp->un.unop.unop_type)
754         {
755         case EXP_OP_NEG:        dbg_printf("-");      break;
756         case EXP_OP_NOT:        dbg_printf("!");      break;
757         case EXP_OP_LNOT:       dbg_printf("~");      break;
758         case EXP_OP_DEREF:      dbg_printf("*");      break;
759         case EXP_OP_ADDR:       dbg_printf("&");      break;
760         }
761         expr_print(exp->un.unop.exp1);
762         break;
763     default:
764         WINE_FIXME("Unexpected expression (%u).\n", exp->type);
765         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
766         break;
767     }
768
769     return TRUE;
770 }
771
772 struct expr* expr_clone(const struct expr* exp, BOOL *local_binding)
773 {
774     int                 i;
775     struct expr*        rtn;
776
777     rtn = HeapAlloc(GetProcessHeap(), 0, sizeof(struct expr));
778
779     /*
780      * First copy the contents of the expression itself.
781      */
782     *rtn = *exp;
783
784     switch (exp->type)
785     {
786     case EXPR_TYPE_CAST:
787         rtn->un.cast.expr = expr_clone(exp->un.cast.expr, local_binding);
788         break;
789     case EXPR_TYPE_INTVAR:
790         rtn->un.intvar.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.intvar.name) + 1), exp->un.intvar.name);
791         break;
792     case EXPR_TYPE_U_CONST:
793     case EXPR_TYPE_S_CONST:
794         break;
795     case EXPR_TYPE_STRING:
796         rtn->un.string.str = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.string.str) + 1), exp->un.string.str);
797         break;
798     case EXPR_TYPE_SYMBOL:
799         rtn->un.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.symbol.name) + 1), exp->un.symbol.name);
800         if (local_binding && symbol_is_local(exp->un.symbol.name))
801             *local_binding = TRUE;
802         break;
803     case EXPR_TYPE_PSTRUCT:
804     case EXPR_TYPE_STRUCT:
805         rtn->un.structure.exp1 = expr_clone(exp->un.structure.exp1, local_binding);
806         rtn->un.structure.element_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.structure.element_name) + 1), exp->un.structure.element_name);
807         break;
808     case EXPR_TYPE_CALL:
809         for (i = 0; i < exp->un.call.nargs; i++)
810         {
811             rtn->un.call.arg[i] = expr_clone(exp->un.call.arg[i], local_binding);
812         }
813         rtn->un.call.funcname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.call.funcname) + 1), exp->un.call.funcname);
814         break;
815     case EXPR_TYPE_BINOP:
816         rtn->un.binop.exp1 = expr_clone(exp->un.binop.exp1, local_binding);
817         rtn->un.binop.exp2 = expr_clone(exp->un.binop.exp2, local_binding);
818         break;
819     case EXPR_TYPE_UNOP:
820         rtn->un.unop.exp1 = expr_clone(exp->un.unop.exp1, local_binding);
821         break;
822     default:
823         WINE_FIXME("Unexpected expression (%u).\n", exp->type);
824         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
825         break;
826     }
827
828     return rtn;
829 }
830
831
832 /*
833  * Recursively go through an expression tree and free all memory associated
834  * with it.
835  */
836 int expr_free(struct expr* exp)
837 {
838     int i;
839
840     switch (exp->type)
841     {
842     case EXPR_TYPE_CAST:
843         expr_free(exp->un.cast.expr);
844         break;
845     case EXPR_TYPE_INTVAR:
846         HeapFree(GetProcessHeap(), 0, (char*)exp->un.intvar.name);
847         break;
848     case EXPR_TYPE_U_CONST:
849     case EXPR_TYPE_S_CONST:
850         break;
851     case EXPR_TYPE_STRING:
852         HeapFree(GetProcessHeap(), 0, (char*)exp->un.string.str);
853         break;
854     case EXPR_TYPE_SYMBOL:
855         HeapFree(GetProcessHeap(), 0, (char*)exp->un.symbol.name);
856         break;
857     case EXPR_TYPE_PSTRUCT:
858     case EXPR_TYPE_STRUCT:
859         expr_free(exp->un.structure.exp1);
860         HeapFree(GetProcessHeap(), 0, (char*)exp->un.structure.element_name);
861         break;
862     case EXPR_TYPE_CALL:
863         for (i = 0; i < exp->un.call.nargs; i++)
864         {
865             expr_free(exp->un.call.arg[i]);
866         }
867         HeapFree(GetProcessHeap(), 0, (char*)exp->un.call.funcname);
868         break;
869     case EXPR_TYPE_BINOP:
870         expr_free(exp->un.binop.exp1);
871         expr_free(exp->un.binop.exp2);
872         break;
873     case EXPR_TYPE_UNOP:
874         expr_free(exp->un.unop.exp1);
875         break;
876     default:
877         WINE_FIXME("Unexpected expression (%u).\n", exp->type);
878         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
879         break;
880     }
881
882     HeapFree(GetProcessHeap(), 0, exp);
883     return TRUE;
884 }