comdlg32/tests: Avoid "misleading indentation" warnings.
[wine.git] / programs / winedbg / expr.c
blob5d9ef30ad130d4e7cf30a31f3f1999a593a0645c
1 /*
2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
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.
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.
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
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
25 #include "debugger.h"
26 #include "expr.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
31 struct expr
33 unsigned int type;
34 union
36 struct
38 dbg_lgint_t value;
39 } s_const;
41 struct
43 dbg_lguint_t value;
44 } u_const;
46 struct
48 const char* str;
49 } string;
51 struct
53 const char* name;
54 } symbol;
56 struct
58 const char* name;
59 } intvar;
61 struct
63 int unop_type;
64 struct expr* exp1;
65 dbg_lgint_t result;
66 } unop;
68 struct
70 int binop_type;
71 struct expr* exp1;
72 struct expr* exp2;
73 dbg_lgint_t result;
74 } binop;
76 struct
78 struct type_expr_t cast_to;
79 struct expr* expr;
80 } cast;
82 struct
84 struct expr* exp1;
85 const char* element_name;
86 } structure;
88 struct
90 const char* funcname;
91 int nargs;
92 struct expr* arg[5];
93 dbg_lguint_t result;
94 } call;
96 } un;
99 #define EXPR_TYPE_S_CONST 0
100 #define EXPR_TYPE_U_CONST 1
101 #define EXPR_TYPE_SYMBOL 2
102 #define EXPR_TYPE_INTVAR 3
103 #define EXPR_TYPE_BINOP 4
104 #define EXPR_TYPE_UNOP 5
105 #define EXPR_TYPE_STRUCT 6
106 #define EXPR_TYPE_PSTRUCT 7
107 #define EXPR_TYPE_CALL 8
108 #define EXPR_TYPE_STRING 9
109 #define EXPR_TYPE_CAST 10
111 static char expr_list[4096];
112 static unsigned int next_expr_free = 0;
114 static struct expr* expr_alloc(void)
116 struct expr* rtn;
118 rtn = (struct expr*)&expr_list[next_expr_free];
120 next_expr_free += sizeof(struct expr);
121 assert(next_expr_free < sizeof(expr_list));
123 return rtn;
126 void expr_free_all(void)
128 next_expr_free = 0;
131 struct expr* expr_alloc_typecast(struct type_expr_t* tet, struct expr* exp)
133 struct expr* ex;
135 ex = expr_alloc();
137 ex->type = EXPR_TYPE_CAST;
138 ex->un.cast.cast_to = *tet;
139 ex->un.cast.expr = exp;
140 return ex;
143 struct expr* expr_alloc_internal_var(const char* name)
145 struct expr* ex;
147 ex = expr_alloc();
149 ex->type = EXPR_TYPE_INTVAR;
150 ex->un.intvar.name = name;
151 return ex;
154 struct expr* expr_alloc_symbol(const char* name)
156 struct expr* ex;
158 ex = expr_alloc();
160 ex->type = EXPR_TYPE_SYMBOL;
161 ex->un.symbol.name = name;
162 return ex;
165 struct expr* expr_alloc_sconstant(dbg_lgint_t value)
167 struct expr* ex;
169 ex = expr_alloc();
171 ex->type = EXPR_TYPE_S_CONST;
172 ex->un.s_const.value = value;
173 return ex;
176 struct expr* expr_alloc_uconstant(dbg_lguint_t value)
178 struct expr* ex;
180 ex = expr_alloc();
182 ex->type = EXPR_TYPE_U_CONST;
183 ex->un.u_const.value = value;
184 return ex;
187 struct expr* expr_alloc_string(const char* str)
189 struct expr* ex;
191 ex = expr_alloc();
193 ex->type = EXPR_TYPE_STRING;
194 ex->un.string.str = str;
195 return ex;
198 struct expr* expr_alloc_binary_op(int op_type, struct expr* exp1, struct expr* exp2)
200 struct expr* ex;
202 ex = expr_alloc();
204 ex->type = EXPR_TYPE_BINOP;
205 ex->un.binop.binop_type = op_type;
206 ex->un.binop.exp1 = exp1;
207 ex->un.binop.exp2 = exp2;
208 return ex;
211 struct expr* expr_alloc_unary_op(int op_type, struct expr* exp1)
213 struct expr* ex;
215 ex = expr_alloc();
217 ex->type = EXPR_TYPE_UNOP;
218 ex->un.unop.unop_type = op_type;
219 ex->un.unop.exp1 = exp1;
220 return ex;
223 struct expr* expr_alloc_struct(struct expr* exp, const char* element)
225 struct expr* ex;
227 ex = expr_alloc();
229 ex->type = EXPR_TYPE_STRUCT;
230 ex->un.structure.exp1 = exp;
231 ex->un.structure.element_name = element;
232 return ex;
235 struct expr* expr_alloc_pstruct(struct expr* exp, const char* element)
237 struct expr* ex;
239 ex = expr_alloc();
241 ex->type = EXPR_TYPE_PSTRUCT;
242 ex->un.structure.exp1 = exp;
243 ex->un.structure.element_name = element;
244 return ex;
247 struct expr* WINAPIV expr_alloc_func_call(const char* funcname, int nargs, ...)
249 struct expr* ex;
250 va_list ap;
251 int i;
253 ex = expr_alloc();
255 ex->type = EXPR_TYPE_CALL;
256 ex->un.call.funcname = funcname;
257 ex->un.call.nargs = nargs;
259 va_start(ap, nargs);
260 for (i = 0; i < nargs; i++)
262 ex->un.call.arg[i] = va_arg(ap, struct expr*);
264 va_end(ap);
265 return ex;
268 /******************************************************************
269 * expr_eval
272 struct dbg_lvalue expr_eval(struct expr* exp)
274 struct dbg_lvalue rtn;
275 int i;
276 struct dbg_lvalue exp1;
277 struct dbg_lvalue exp2;
278 DWORD64 scale1, scale2, scale3;
279 struct dbg_type type1, type2;
280 DWORD tag;
281 const struct dbg_internal_var* div;
283 init_lvalue_in_debugger(&rtn, dbg_itype_none, NULL);
285 switch (exp->type)
287 case EXPR_TYPE_CAST:
288 /* this is really brute force, we simply change the type... without
289 * checking if this is right or not
291 rtn = expr_eval(exp->un.cast.expr);
292 switch (exp->un.cast.cast_to.type)
294 case type_expr_type_id:
295 if (exp->un.cast.cast_to.u.type.id == dbg_itype_none)
297 dbg_printf("Can't cast to unknown type\n");
298 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
300 rtn.type = exp->un.cast.cast_to.u.type;
301 break;
302 case type_expr_udt_class:
303 case type_expr_udt_struct:
304 case type_expr_udt_union:
305 rtn.type = types_find_type(rtn.type.module, exp->un.cast.cast_to.u.name,
306 SymTagUDT);
307 if (rtn.type.id == dbg_itype_none)
309 dbg_printf("Can't cast to UDT %s\n", exp->un.cast.cast_to.u.name);
310 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
312 break;
313 case type_expr_enumeration:
314 rtn.type = types_find_type(rtn.type.module, exp->un.cast.cast_to.u.name,
315 SymTagEnum);
316 if (rtn.type.id == dbg_itype_none)
318 dbg_printf("Can't cast to enumeration %s\n", exp->un.cast.cast_to.u.name);
319 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
321 break;
322 default:
323 dbg_printf("Unsupported cast type %u\n", exp->un.cast.cast_to.type);
324 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
326 for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
328 rtn.type = types_find_pointer(&rtn.type);
329 if (rtn.type.id == dbg_itype_none)
331 dbg_printf("Cannot find pointer type\n");
332 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
335 break;
336 case EXPR_TYPE_STRING:
337 init_lvalue_in_debugger(&rtn, dbg_itype_astring, &exp->un.string.str);
338 break;
339 case EXPR_TYPE_U_CONST:
340 init_lvalue_in_debugger(&rtn, dbg_itype_lguint, &exp->un.u_const.value);
341 break;
342 case EXPR_TYPE_S_CONST:
343 init_lvalue_in_debugger(&rtn, dbg_itype_lgint, &exp->un.s_const.value);
344 break;
345 case EXPR_TYPE_SYMBOL:
346 switch (symbol_get_lvalue(exp->un.symbol.name, -1, &rtn, FALSE))
348 case sglv_found:
349 break;
350 case sglv_unknown:
351 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
352 /* should never be here */
353 case sglv_aborted:
354 RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
355 /* should never be here */
357 break;
358 case EXPR_TYPE_PSTRUCT:
359 exp1 = expr_eval(exp->un.structure.exp1);
360 if (exp1.type.id == dbg_itype_none || !types_array_index(&exp1, 0, &rtn) ||
361 rtn.type.id == dbg_itype_none)
362 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
363 if (!types_udt_find_element(&rtn, exp->un.structure.element_name))
365 dbg_printf("%s\n", exp->un.structure.element_name);
366 RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
368 break;
369 case EXPR_TYPE_STRUCT:
370 exp1 = expr_eval(exp->un.structure.exp1);
371 if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
372 rtn = exp1;
373 if (!types_udt_find_element(&rtn, exp->un.structure.element_name))
375 dbg_printf("%s\n", exp->un.structure.element_name);
376 RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
378 break;
379 case EXPR_TYPE_CALL:
380 #if 0
382 * First, evaluate all of the arguments. If any of them are not
383 * evaluable, then bail.
385 for (i = 0; i < exp->un.call.nargs; i++)
387 exp1 = expr_eval(exp->un.call.arg[i]);
388 if (exp1.type.id == dbg_itype_none)
389 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
390 cexp[i] = types_extract_as_integer(&exp1);
394 * Now look up the address of the function itself.
396 switch (symbol_get_lvalue(exp->un.call.funcname, -1, &rtn, FALSE))
398 case sglv_found:
399 break;
400 case sglv_unknown:
401 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
402 /* should never be here */
403 case sglv_aborted:
404 RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
405 /* should never be here */
408 /* FIXME: NEWDBG NIY */
409 /* Anyway, I wonder how this could work depending on the calling order of
410 * the function (cdecl vs pascal for example)
412 int (*fptr)();
414 fptr = (int (*)()) rtn.addr.off;
415 switch (exp->un.call.nargs)
417 case 0:
418 exp->un.call.result = (*fptr)();
419 break;
420 case 1:
421 exp->un.call.result = (*fptr)(cexp[0]);
422 break;
423 case 2:
424 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
425 break;
426 case 3:
427 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
428 break;
429 case 4:
430 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
431 break;
432 case 5:
433 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
434 break;
436 #else
437 dbg_printf("Function call no longer implemented\n");
438 /* would need to set up a call to this function, and then restore the current
439 * context afterwards...
441 exp->un.call.result = 0;
442 #endif
443 init_lvalue_in_debugger(&rtn, dbg_itype_none, &exp->un.call.result);
444 /* get return type from function signature type */
445 /* FIXME rtn.type.module should be set to function's module... */
446 types_get_info(&rtn.type, TI_GET_TYPE, &rtn.type.id);
447 break;
448 case EXPR_TYPE_INTVAR:
449 if (!(div = dbg_get_internal_var(exp->un.intvar.name)))
450 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
451 init_lvalue_in_debugger(&rtn, div->typeid, div->pval);
452 break;
453 case EXPR_TYPE_BINOP:
454 exp1 = expr_eval(exp->un.binop.exp1);
455 exp2 = expr_eval(exp->un.binop.exp2);
456 if (exp1.type.id == dbg_itype_none || exp2.type.id == dbg_itype_none)
457 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
458 init_lvalue_in_debugger(&rtn, dbg_itype_lgint, &exp->un.binop.result);
459 type1 = exp1.type;
460 type2 = exp2.type;
461 switch (exp->un.binop.binop_type)
463 case EXP_OP_ADD:
464 if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
465 tag != SymTagPointerType ||
466 !types_get_info(&exp1.type, TI_GET_TYPE, &type1.id))
467 type1.id = dbg_itype_none;
468 if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
469 tag != SymTagPointerType ||
470 !types_get_info(&exp2.type, TI_GET_TYPE, &type2.id))
471 type2.id = dbg_itype_none;
472 scale1 = 1;
473 scale2 = 1;
474 if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
475 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
476 if (type1.id != dbg_itype_none)
478 types_get_info(&type1, TI_GET_LENGTH, &scale2);
479 rtn.type = exp1.type;
481 else if (type2.id != dbg_itype_none)
483 types_get_info(&type2, TI_GET_LENGTH, &scale1);
484 rtn.type = exp2.type;
486 exp->un.binop.result = types_extract_as_integer(&exp1) * (dbg_lguint_t)scale1 +
487 (dbg_lguint_t)scale2 * types_extract_as_integer(&exp2);
488 break;
489 case EXP_OP_SUB:
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 scale3 = 1;
501 if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
503 WINE_FIXME("This may fail (if module base address are wrongly calculated)\n");
504 if (memcmp(&type1, &type2, sizeof(struct dbg_type)))
505 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
506 types_get_info(&type1, TI_GET_LENGTH, &scale3);
508 else if (type1.id != dbg_itype_none)
510 types_get_info(&type1, TI_GET_LENGTH, &scale2);
511 rtn.type = exp1.type;
513 else if (type2.id != dbg_itype_none)
515 types_get_info(&type2, TI_GET_LENGTH, &scale1);
516 rtn.type = exp2.type;
518 exp->un.binop.result = (types_extract_as_integer(&exp1) * (dbg_lguint_t)scale1 -
519 types_extract_as_integer(&exp2) * (dbg_lguint_t)scale2) / (dbg_lguint_t)scale3;
520 break;
521 case EXP_OP_SEG:
522 rtn.type.id = dbg_itype_segptr;
523 rtn.type.module = 0;
524 dbg_curr_process->be_cpu->build_addr(dbg_curr_thread->handle, &dbg_context, &rtn.addr,
525 types_extract_as_integer(&exp1), types_extract_as_integer(&exp2));
526 break;
527 case EXP_OP_LOR:
528 exp->un.binop.result = (types_extract_as_integer(&exp1) || types_extract_as_integer(&exp2));
529 break;
530 case EXP_OP_LAND:
531 exp->un.binop.result = (types_extract_as_integer(&exp1) && types_extract_as_integer(&exp2));
532 break;
533 case EXP_OP_OR:
534 exp->un.binop.result = (types_extract_as_integer(&exp1) | types_extract_as_integer(&exp2));
535 break;
536 case EXP_OP_AND:
537 exp->un.binop.result = (types_extract_as_integer(&exp1) & types_extract_as_integer(&exp2));
538 break;
539 case EXP_OP_XOR:
540 exp->un.binop.result = (types_extract_as_integer(&exp1) ^ types_extract_as_integer(&exp2));
541 break;
542 case EXP_OP_EQ:
543 exp->un.binop.result = (types_extract_as_integer(&exp1) == types_extract_as_integer(&exp2));
544 break;
545 case EXP_OP_GT:
546 exp->un.binop.result = (types_extract_as_integer(&exp1) > types_extract_as_integer(&exp2));
547 break;
548 case EXP_OP_LT:
549 exp->un.binop.result = (types_extract_as_integer(&exp1) < types_extract_as_integer(&exp2));
550 break;
551 case EXP_OP_GE:
552 exp->un.binop.result = (types_extract_as_integer(&exp1) >= types_extract_as_integer(&exp2));
553 break;
554 case EXP_OP_LE:
555 exp->un.binop.result = (types_extract_as_integer(&exp1) <= types_extract_as_integer(&exp2));
556 break;
557 case EXP_OP_NE:
558 exp->un.binop.result = (types_extract_as_integer(&exp1) != types_extract_as_integer(&exp2));
559 break;
560 case EXP_OP_SHL:
561 exp->un.binop.result = types_extract_as_integer(&exp1) << types_extract_as_integer(&exp2);
562 break;
563 case EXP_OP_SHR:
564 exp->un.binop.result = types_extract_as_integer(&exp1) >> types_extract_as_integer(&exp2);
565 break;
566 case EXP_OP_MUL:
567 exp->un.binop.result = (types_extract_as_integer(&exp1) * types_extract_as_integer(&exp2));
568 break;
569 case EXP_OP_DIV:
570 if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
571 exp->un.binop.result = (types_extract_as_integer(&exp1) / types_extract_as_integer(&exp2));
572 break;
573 case EXP_OP_REM:
574 if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
575 exp->un.binop.result = (types_extract_as_integer(&exp1) % types_extract_as_integer(&exp2));
576 break;
577 case EXP_OP_ARR:
578 if (!types_array_index(&exp1, types_extract_as_integer(&exp2), &rtn))
579 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
580 break;
581 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
583 break;
584 case EXPR_TYPE_UNOP:
585 exp1 = expr_eval(exp->un.unop.exp1);
586 if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
587 init_lvalue_in_debugger(&rtn, dbg_itype_lgint, &exp->un.unop.result);
588 switch (exp->un.unop.unop_type)
590 case EXP_OP_NEG:
591 exp->un.unop.result = -types_extract_as_integer(&exp1);
592 break;
593 case EXP_OP_NOT:
594 exp->un.unop.result = !types_extract_as_integer(&exp1);
595 break;
596 case EXP_OP_LNOT:
597 exp->un.unop.result = ~types_extract_as_integer(&exp1);
598 break;
599 case EXP_OP_DEREF:
600 if (!types_array_index(&exp1, 0, &rtn))
601 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
602 break;
603 case EXP_OP_FORCE_DEREF:
604 rtn = exp1;
605 if (exp1.in_debuggee)
606 dbg_read_memory(memory_to_linear_addr(&exp1.addr), &rtn.addr.Offset, sizeof(rtn.addr.Offset));
607 break;
608 case EXP_OP_ADDR:
609 /* only do it on linear addresses */
610 if (exp1.addr.Mode != AddrModeFlat)
611 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
612 exp->un.unop.result = (ULONG_PTR)memory_to_linear_addr(&exp1.addr);
613 rtn.type = types_find_pointer(&exp1.type);
614 if (rtn.type.id == dbg_itype_none)
615 RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
616 break;
617 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
619 break;
620 default:
621 WINE_FIXME("Unexpected expression (%d).\n", exp->type);
622 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
623 break;
626 return rtn;
629 BOOL expr_print(const struct expr* exp)
631 int i;
632 struct dbg_type type;
634 switch (exp->type)
636 case EXPR_TYPE_CAST:
637 WINE_FIXME("No longer supported (missing module base)\n");
638 dbg_printf("((");
639 switch (exp->un.cast.cast_to.type)
641 case type_expr_type_id:
642 type.module = 0;
643 type.id = exp->un.cast.cast_to.type;
644 types_print_type(&type, FALSE); break;
645 case type_expr_udt_class:
646 dbg_printf("class %s", exp->un.cast.cast_to.u.name); break;
647 case type_expr_udt_struct:
648 dbg_printf("struct %s", exp->un.cast.cast_to.u.name); break;
649 case type_expr_udt_union:
650 dbg_printf("union %s", exp->un.cast.cast_to.u.name); break;
651 case type_expr_enumeration:
652 dbg_printf("enum %s", exp->un.cast.cast_to.u.name); break;
654 for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
655 dbg_printf("*");
656 dbg_printf(")");
657 expr_print(exp->un.cast.expr);
658 dbg_printf(")");
659 break;
660 case EXPR_TYPE_INTVAR:
661 dbg_printf("$%s", exp->un.intvar.name);
662 break;
663 case EXPR_TYPE_U_CONST:
664 dbg_printf("%I64u", exp->un.u_const.value);
665 break;
666 case EXPR_TYPE_S_CONST:
667 dbg_printf("%I64d", exp->un.s_const.value);
668 break;
669 case EXPR_TYPE_STRING:
670 dbg_printf("\"%s\"", exp->un.string.str);
671 break;
672 case EXPR_TYPE_SYMBOL:
673 dbg_printf("%s" , exp->un.symbol.name);
674 break;
675 case EXPR_TYPE_PSTRUCT:
676 expr_print(exp->un.structure.exp1);
677 dbg_printf("->%s", exp->un.structure.element_name);
678 break;
679 case EXPR_TYPE_STRUCT:
680 expr_print(exp->un.structure.exp1);
681 dbg_printf(".%s", exp->un.structure.element_name);
682 break;
683 case EXPR_TYPE_CALL:
684 dbg_printf("%s(",exp->un.call.funcname);
685 for (i = 0; i < exp->un.call.nargs; i++)
687 expr_print(exp->un.call.arg[i]);
688 if (i != exp->un.call.nargs - 1) dbg_printf(", ");
690 dbg_printf(")");
691 break;
692 case EXPR_TYPE_BINOP:
693 dbg_printf("(");
694 expr_print(exp->un.binop.exp1);
695 switch (exp->un.binop.binop_type)
697 case EXP_OP_ADD: dbg_printf(" + "); break;
698 case EXP_OP_SUB: dbg_printf(" - "); break;
699 case EXP_OP_SEG: dbg_printf(":"); break;
700 case EXP_OP_LOR: dbg_printf(" || "); break;
701 case EXP_OP_LAND: dbg_printf(" && "); break;
702 case EXP_OP_OR: dbg_printf(" | "); break;
703 case EXP_OP_AND: dbg_printf(" & "); break;
704 case EXP_OP_XOR: dbg_printf(" ^ "); break;
705 case EXP_OP_EQ: dbg_printf(" == "); break;
706 case EXP_OP_GT: dbg_printf(" > "); break;
707 case EXP_OP_LT: dbg_printf(" < "); break;
708 case EXP_OP_GE: dbg_printf(" >= "); break;
709 case EXP_OP_LE: dbg_printf(" <= "); break;
710 case EXP_OP_NE: dbg_printf(" != "); break;
711 case EXP_OP_SHL: dbg_printf(" << "); break;
712 case EXP_OP_SHR: dbg_printf(" >> "); break;
713 case EXP_OP_MUL: dbg_printf(" * "); break;
714 case EXP_OP_DIV: dbg_printf(" / "); break;
715 case EXP_OP_REM: dbg_printf(" %% "); break;
716 case EXP_OP_ARR: dbg_printf("["); break;
717 default: break;
719 expr_print(exp->un.binop.exp2);
720 if (exp->un.binop.binop_type == EXP_OP_ARR) dbg_printf("]");
721 dbg_printf(")");
722 break;
723 case EXPR_TYPE_UNOP:
724 switch (exp->un.unop.unop_type)
726 case EXP_OP_NEG: dbg_printf("-"); break;
727 case EXP_OP_NOT: dbg_printf("!"); break;
728 case EXP_OP_LNOT: dbg_printf("~"); break;
729 case EXP_OP_DEREF: dbg_printf("*"); break;
730 case EXP_OP_ADDR: dbg_printf("&"); break;
732 expr_print(exp->un.unop.exp1);
733 break;
734 default:
735 WINE_FIXME("Unexpected expression (%u).\n", exp->type);
736 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
737 break;
740 return TRUE;
743 struct expr* expr_clone(const struct expr* exp, BOOL *local_binding)
745 int i;
746 struct expr* rtn;
748 rtn = HeapAlloc(GetProcessHeap(), 0, sizeof(struct expr));
751 * First copy the contents of the expression itself.
753 *rtn = *exp;
755 switch (exp->type)
757 case EXPR_TYPE_CAST:
758 rtn->un.cast.expr = expr_clone(exp->un.cast.expr, local_binding);
759 break;
760 case EXPR_TYPE_INTVAR:
761 rtn->un.intvar.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.intvar.name) + 1), exp->un.intvar.name);
762 break;
763 case EXPR_TYPE_U_CONST:
764 case EXPR_TYPE_S_CONST:
765 break;
766 case EXPR_TYPE_STRING:
767 rtn->un.string.str = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.string.str) + 1), exp->un.string.str);
768 break;
769 case EXPR_TYPE_SYMBOL:
770 rtn->un.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.symbol.name) + 1), exp->un.symbol.name);
771 if (local_binding && symbol_is_local(exp->un.symbol.name))
772 *local_binding = TRUE;
773 break;
774 case EXPR_TYPE_PSTRUCT:
775 case EXPR_TYPE_STRUCT:
776 rtn->un.structure.exp1 = expr_clone(exp->un.structure.exp1, local_binding);
777 rtn->un.structure.element_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.structure.element_name) + 1), exp->un.structure.element_name);
778 break;
779 case EXPR_TYPE_CALL:
780 for (i = 0; i < exp->un.call.nargs; i++)
782 rtn->un.call.arg[i] = expr_clone(exp->un.call.arg[i], local_binding);
784 rtn->un.call.funcname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.call.funcname) + 1), exp->un.call.funcname);
785 break;
786 case EXPR_TYPE_BINOP:
787 rtn->un.binop.exp1 = expr_clone(exp->un.binop.exp1, local_binding);
788 rtn->un.binop.exp2 = expr_clone(exp->un.binop.exp2, local_binding);
789 break;
790 case EXPR_TYPE_UNOP:
791 rtn->un.unop.exp1 = expr_clone(exp->un.unop.exp1, local_binding);
792 break;
793 default:
794 WINE_FIXME("Unexpected expression (%u).\n", exp->type);
795 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
796 break;
799 return rtn;
804 * Recursively go through an expression tree and free all memory associated
805 * with it.
807 BOOL expr_free(struct expr* exp)
809 int i;
811 switch (exp->type)
813 case EXPR_TYPE_CAST:
814 expr_free(exp->un.cast.expr);
815 break;
816 case EXPR_TYPE_INTVAR:
817 HeapFree(GetProcessHeap(), 0, (char*)exp->un.intvar.name);
818 break;
819 case EXPR_TYPE_U_CONST:
820 case EXPR_TYPE_S_CONST:
821 break;
822 case EXPR_TYPE_STRING:
823 HeapFree(GetProcessHeap(), 0, (char*)exp->un.string.str);
824 break;
825 case EXPR_TYPE_SYMBOL:
826 HeapFree(GetProcessHeap(), 0, (char*)exp->un.symbol.name);
827 break;
828 case EXPR_TYPE_PSTRUCT:
829 case EXPR_TYPE_STRUCT:
830 expr_free(exp->un.structure.exp1);
831 HeapFree(GetProcessHeap(), 0, (char*)exp->un.structure.element_name);
832 break;
833 case EXPR_TYPE_CALL:
834 for (i = 0; i < exp->un.call.nargs; i++)
836 expr_free(exp->un.call.arg[i]);
838 HeapFree(GetProcessHeap(), 0, (char*)exp->un.call.funcname);
839 break;
840 case EXPR_TYPE_BINOP:
841 expr_free(exp->un.binop.exp1);
842 expr_free(exp->un.binop.exp2);
843 break;
844 case EXPR_TYPE_UNOP:
845 expr_free(exp->un.unop.exp1);
846 break;
847 default:
848 WINE_FIXME("Unexpected expression (%u).\n", exp->type);
849 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
850 break;
853 HeapFree(GetProcessHeap(), 0, exp);
854 return TRUE;