xref: /trunk/main/idlc/source/astexpression.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_idlc.hxx"
30 
31 #include <idlc/astexpression.hxx>
32 #include <idlc/astconstant.hxx>
33 #include <idlc/astscope.hxx>
34 #include <idlc/errorhandler.hxx>
35 
36 #include <limits.h>
37 #include <float.h>
38 #include <memory> // auto_ptr<>
39 
40 #undef	MAXCHAR
41 #define MAXCHAR         127
42 #undef	MINCHAR
43 #define MINCHAR         -128
44 
45 using namespace ::rtl;
46 
47 AstExpression::AstExpression(ExprComb c, AstExpression *pExpr1, AstExpression *pExpr2)
48 	: m_combOperator(c)
49 	, m_subExpr1(pExpr1)
50 	, m_subExpr2(pExpr2)
51 	, m_exprValue(NULL)
52 	, m_pSymbolicName(NULL)
53 {
54 	fillDefinitionDetails();
55 
56 }
57 
58 AstExpression::AstExpression(sal_Int32 l)
59 	: m_combOperator(EC_none)
60 	, m_subExpr1(NULL)
61 	, m_subExpr2(NULL)
62 	, m_exprValue(NULL)
63 	, m_pSymbolicName(NULL)
64 {
65 	fillDefinitionDetails();
66 
67 	m_exprValue = new AstExprValue();
68 	m_exprValue->et = ET_long;
69 	m_exprValue->u.lval = l;
70 }
71 
72 AstExpression::AstExpression(sal_Int32	l, ExprType et)
73 	: m_combOperator(EC_none)
74 	, m_subExpr1(NULL)
75 	, m_subExpr2(NULL)
76 	, m_exprValue(NULL)
77 	, m_pSymbolicName(NULL)
78 {
79 	fillDefinitionDetails();
80 
81 	m_exprValue = new AstExprValue();
82 	m_exprValue->et = et;
83 	m_exprValue->u.lval = l;
84 }
85 
86 AstExpression::AstExpression(sal_Int64	h)
87 	: m_combOperator(EC_none)
88 	, m_subExpr1(NULL)
89 	, m_subExpr2(NULL)
90 	, m_exprValue(NULL)
91 	, m_pSymbolicName(NULL)
92 {
93 	fillDefinitionDetails();
94 
95 	m_exprValue = new AstExprValue();
96 	m_exprValue->et = ET_hyper;
97 	m_exprValue->u.hval = h;
98 }
99 
100 AstExpression::AstExpression(sal_uInt64	uh)
101 	: m_combOperator(EC_none)
102 	, m_subExpr1(NULL)
103 	, m_subExpr2(NULL)
104 	, m_exprValue(NULL)
105 	, m_pSymbolicName(NULL)
106 {
107 	fillDefinitionDetails();
108 
109 	m_exprValue = new AstExprValue();
110 	m_exprValue->et = ET_uhyper;
111 	m_exprValue->u.uhval = uh;
112 }
113 
114 AstExpression::AstExpression(double	d)
115 	: m_combOperator(EC_none)
116 	, m_subExpr1(NULL)
117 	, m_subExpr2(NULL)
118 	, m_exprValue(NULL)
119 	, m_pSymbolicName(NULL)
120 {
121 	fillDefinitionDetails();
122 
123 	m_exprValue = new AstExprValue();
124 	m_exprValue->et = ET_double;
125 	m_exprValue->u.dval = d;
126 }
127 
128 AstExpression::AstExpression(::rtl::OString* scopedName)
129 	: m_combOperator(EC_symbol)
130 	, m_subExpr1(NULL)
131 	, m_subExpr2(NULL)
132 	, m_exprValue(NULL)
133 	, m_pSymbolicName(scopedName)
134 {
135 	fillDefinitionDetails();
136 }
137 
138 AstExpression::~AstExpression()
139 {
140 	if ( m_exprValue )
141 		delete m_exprValue;
142 	if ( m_subExpr1 )
143 		delete m_subExpr1;
144 	if ( m_subExpr2 )
145 		delete m_subExpr2;
146 	if ( m_pSymbolicName )
147 		delete m_pSymbolicName;
148 }
149 
150 /*
151  * Perform the coercion from the given AstExprValue to the requested
152  * ExprType. Return an AstExprValue if successful, NULL if failed.
153  * must be done for hyper, uhyper
154  */
155 static AstExprValue *
156 coerce_value(AstExprValue *ev, ExprType t)
157 {
158 	if (ev == NULL)
159 		return NULL;
160 
161 	switch (t)
162 	{
163 		case ET_short:
164 			switch (ev->et)
165 			{
166 				case ET_short:
167 					return ev;
168 				case ET_ushort:
169 					if (ev->u.usval > SAL_MAX_INT16)
170 						return NULL;
171 					ev->u.sval = (sal_Int16)ev->u.usval;
172 					ev->et = ET_short;
173 					return ev;
174 				case ET_long:
175 					if (ev->u.lval < SAL_MIN_INT16 || ev->u.lval > SAL_MAX_INT16)
176                         return NULL;
177 					ev->u.sval = (sal_Int16)ev->u.lval;
178 					ev->et = ET_short;
179 					return ev;
180 				case ET_ulong:
181 					if (ev->u.ulval > SAL_MAX_INT16)
182 						return NULL;
183 					ev->u.sval = (sal_Int16)ev->u.ulval;
184 					ev->et = ET_short;
185 					return ev;
186 				case ET_hyper:
187 					if (ev->u.hval < SAL_MIN_INT16 || ev->u.hval > SAL_MAX_INT16)
188 						return NULL;
189 					ev->u.sval = (sal_Int16)ev->u.hval;
190 					ev->et = ET_short;
191 					return ev;
192 				case ET_uhyper:
193 					if (ev->u.uhval > SAL_MAX_INT16)
194 						return NULL;
195 					ev->u.sval = (sal_Int16)ev->u.uhval;
196 					ev->et = ET_short;
197 					return ev;
198 				case ET_boolean:
199 					ev->u.sval = (sal_Int16)ev->u.bval;
200 					ev->et = ET_short;
201 					return ev;
202 				case ET_float:
203 					if (ev->u.fval < SAL_MIN_INT16 || ev->u.fval > SAL_MAX_INT16)
204 						return NULL;
205 					ev->u.sval = (sal_Int16)ev->u.fval;
206 					ev->et = ET_short;
207 					return ev;
208 				case ET_double:
209 					if (ev->u.dval < SAL_MIN_INT16 || ev->u.dval > SAL_MAX_INT16)
210 						return NULL;
211 					ev->u.sval = (sal_Int16)ev->u.dval;
212 					ev->et = ET_short;
213 					return ev;
214 				case ET_byte:
215 					ev->u.sval = (sal_Int16)ev->u.byval;
216 					ev->et = ET_short;
217 					return ev;
218                 default:
219                     OSL_ASSERT(false);
220 					return NULL;
221 			}
222 		case ET_ushort:
223 			switch (ev->et)
224 			{
225 				case ET_short:
226 					if (ev->u.sval < 0)
227 						return NULL;
228 					ev->u.usval = (sal_uInt16)ev->u.sval;
229 					ev->et = ET_ushort;
230 					return ev;
231 				case ET_ushort:
232 					return ev;
233 				case ET_long:
234 					if (ev->u.lval < 0 || ev->u.lval > SAL_MAX_UINT16)
235 						return NULL;
236 					ev->u.usval = (sal_uInt16)ev->u.lval;
237 					ev->et = ET_ushort;
238 					return ev;
239 				case ET_ulong:
240 					if (ev->u.ulval > SAL_MAX_UINT16)
241 						return NULL;
242 					ev->u.usval = (sal_uInt16)ev->u.ulval;
243 					ev->et = ET_ushort;
244 					return ev;
245 				case ET_hyper:
246 					if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT16)
247 						return NULL;
248 					ev->u.usval = (sal_uInt16)ev->u.hval;
249 					ev->et = ET_ushort;
250 					return ev;
251 				case ET_uhyper:
252 					if (ev->u.uhval > SAL_MAX_UINT16)
253 						return NULL;
254 					ev->u.usval = (sal_uInt16)ev->u.uhval;
255 					ev->et = ET_ushort;
256 					return ev;
257 				case ET_boolean:
258 					ev->u.usval = (sal_uInt16)ev->u.bval;
259 					ev->et = ET_short;
260 					return ev;
261 				case ET_float:
262 					if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT16)
263 						return NULL;
264 					ev->u.usval = (sal_uInt16)ev->u.fval;
265 					ev->et = ET_short;
266 					return ev;
267 				case ET_double:
268 					if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT16)
269 						return NULL;
270 					ev->u.usval = (sal_uInt16)ev->u.dval;
271 					ev->et = ET_short;
272 					return ev;
273 				case ET_byte:
274 					ev->u.usval = (sal_uInt16)ev->u.byval;
275 					ev->et = ET_ushort;
276 					return ev;
277                 default:
278                     OSL_ASSERT(false);
279 					return NULL;
280 			}
281 		case ET_long:
282 			switch (ev->et)
283 			{
284 				case ET_short:
285 					ev->u.lval = (sal_Int32)ev->u.sval;
286 					ev->et = ET_long;
287 					return ev;
288 				case ET_ushort:
289 					ev->u.lval = (sal_Int32)ev->u.usval;
290 					ev->et = ET_long;
291 					return ev;
292 				case ET_long:
293 					return ev;
294 				case ET_ulong:
295 					if (ev->u.ulval > SAL_MAX_INT32)
296 						return NULL;
297 					ev->u.lval = (sal_Int32)ev->u.ulval;
298 					ev->et = ET_long;
299 					return ev;
300 				case ET_hyper:
301 					if (ev->u.hval < SAL_MIN_INT32 || ev->u.hval > SAL_MAX_INT32)
302 						return NULL;
303 					ev->u.lval = (sal_Int32)ev->u.hval;
304 					ev->et = ET_long;
305 					return ev;
306 				case ET_uhyper:
307 					if (ev->u.uhval > SAL_MAX_INT32)
308 						return NULL;
309 					ev->u.lval = (sal_Int32)ev->u.uhval;
310 					ev->et = ET_long;
311 					return ev;
312 				case ET_boolean:
313 					ev->u.lval = (sal_Int32)ev->u.bval;
314 					ev->et = ET_long;
315 					return ev;
316 				case ET_float:
317 					if (ev->u.fval < SAL_MIN_INT32 || ev->u.fval > SAL_MAX_INT32)
318 						return NULL;
319 					ev->u.lval = (sal_Int32)ev->u.fval;
320 					ev->et = ET_long;
321 					return ev;
322 				case ET_double:
323 					if (ev->u.dval < SAL_MIN_INT32 || ev->u.dval > SAL_MAX_INT32)
324 						return NULL;
325 					ev->u.lval = (sal_Int32)ev->u.dval;
326 					ev->et = ET_long;
327 					return ev;
328 				case ET_byte:
329 					ev->u.lval = (sal_Int32) ev->u.byval;
330 					ev->et = ET_long;
331 					return ev;
332                 default:
333                     OSL_ASSERT(false);
334 					return NULL;
335 			}
336 		case ET_ulong:
337 			switch (ev->et)
338 			{
339 				case ET_short:
340 					if (ev->u.sval < 0)
341 						return NULL;
342 					ev->u.ulval = (sal_uInt32)ev->u.sval;
343 					ev->et = ET_ulong;
344 					return ev;
345 				case ET_ushort:
346 					ev->u.ulval = (sal_uInt32)ev->u.usval;
347 					ev->et = ET_ulong;
348 					return ev;
349 				case ET_long:
350 					if (ev->u.lval < 0)
351 						return NULL;
352 					ev->u.ulval = (sal_uInt32)ev->u.lval;
353 					ev->et = ET_ulong;
354 					return ev;
355 				case ET_ulong:
356 					return ev;
357 				case ET_hyper:
358 					if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT32)
359 						return NULL;
360 					ev->u.lval = (sal_uInt32)ev->u.hval;
361 					ev->et = ET_ulong;
362 					return ev;
363 				case ET_uhyper:
364 					if (ev->u.uhval > SAL_MAX_UINT32)
365 						return NULL;
366 					ev->u.ulval = (sal_uInt32)ev->u.uhval;
367 					ev->et = ET_ulong;
368 					return ev;
369 				case ET_boolean:
370 					ev->u.ulval = (sal_uInt32)ev->u.bval;
371 					ev->et = ET_ulong;
372 					return ev;
373 				case ET_float:
374 					if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT32)
375 						return NULL;
376 					ev->u.ulval = (sal_uInt32)ev->u.fval;
377 					ev->et = ET_ulong;
378 					return ev;
379 				case ET_double:
380 					if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT32)
381 						return NULL;
382 					ev->u.ulval = (sal_uInt32)ev->u.dval;
383 					ev->et = ET_ulong;
384 					return ev;
385 				case ET_byte:
386 					ev->u.ulval = (sal_uInt32)ev->u.byval;
387 					ev->et = ET_ulong;
388 					return ev;
389                 default:
390                     OSL_ASSERT(false);
391 					return NULL;
392 			}
393 		case ET_hyper:
394 			switch (ev->et)
395 			{
396 				case ET_short:
397 					ev->u.hval = (sal_Int64)ev->u.sval;
398 					ev->et = ET_hyper;
399 					return ev;
400 				case ET_ushort:
401 					ev->u.hval = (sal_Int64)ev->u.usval;
402 					ev->et = ET_hyper;
403 					return ev;
404 				case ET_long:
405 					ev->u.hval = (sal_Int64)ev->u.lval;
406 					ev->et = ET_hyper;
407 					return ev;
408 				case ET_ulong:
409 					ev->u.hval = (sal_Int64)ev->u.ulval;
410 					ev->et = ET_hyper;
411 					return ev;
412 				case ET_hyper:
413 					return ev;
414 				case ET_uhyper:
415 					if (ev->u.uhval > SAL_MAX_INT64)
416 						return NULL;
417 					ev->u.hval = (sal_Int64)ev->u.uhval;
418 					ev->et = ET_long;
419 					return ev;
420 				case ET_boolean:
421 					ev->u.hval = (sal_Int64)ev->u.bval;
422 					ev->et = ET_hyper;
423 					return ev;
424 				case ET_float:
425 					if (ev->u.fval < SAL_MIN_INT64 || ev->u.fval > SAL_MAX_INT64)
426 						return NULL;
427 					ev->u.hval = (sal_Int64)ev->u.fval;
428 					ev->et = ET_hyper;
429 					return ev;
430 				case ET_double:
431 					if (ev->u.dval < SAL_MIN_INT64 || ev->u.dval > SAL_MAX_INT64)
432 						return NULL;
433 					ev->u.hval = (sal_Int64)ev->u.dval;
434 					ev->et = ET_hyper;
435 					return ev;
436 				case ET_byte:
437 					ev->u.hval = (sal_Int64)ev->u.byval;
438 					ev->et = ET_hyper;
439 					return ev;
440                 default:
441                     OSL_ASSERT(false);
442 					return NULL;
443 			}
444 		case ET_uhyper:
445 			switch (ev->et)
446 			{
447 				case ET_short:
448 					if (ev->u.sval < 0)
449 						return NULL;
450 					ev->u.uhval = (sal_uInt64)ev->u.sval;
451 					ev->et = ET_uhyper;
452 					return ev;
453 				case ET_ushort:
454 					ev->u.uhval = (sal_uInt64)ev->u.usval;
455 					ev->et = ET_uhyper;
456 					return ev;
457 				case ET_long:
458 					if (ev->u.lval < 0)
459 						return NULL;
460 					ev->u.uhval = (sal_uInt64)ev->u.lval;
461 					ev->et = ET_uhyper;
462 					return ev;
463 				case ET_ulong:
464 					ev->u.uhval = (sal_uInt64)ev->u.ulval;
465 					ev->et = ET_uhyper;
466 					return ev;
467 				case ET_hyper:
468 					if (ev->u.hval < 0)
469 						return NULL;
470 					ev->u.uhval = (sal_uInt64)ev->u.hval;
471 					ev->et = ET_uhyper;
472 					return ev;
473 				case ET_uhyper:
474 					return ev;
475 				case ET_boolean:
476 					ev->u.uhval = (sal_uInt64)ev->u.bval;
477 					ev->et = ET_uhyper;
478 					return ev;
479 				case ET_float:
480 					if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT64)
481 						return NULL;
482 					ev->u.uhval = (sal_uInt64)ev->u.fval;
483 					ev->et = ET_uhyper;
484 					return ev;
485 				case ET_double:
486 					if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT64)
487 						return NULL;
488 					ev->u.uhval = (sal_uInt64)ev->u.dval;
489 					ev->et = ET_uhyper;
490 					return ev;
491 				case ET_byte:
492 					ev->u.uhval = (sal_uInt64)ev->u.byval;
493 					ev->et = ET_uhyper;
494 					return ev;
495                 default:
496                     OSL_ASSERT(false);
497 					return NULL;
498 			}
499 		case ET_boolean:
500 			switch (ev->et)
501 			{
502 				case ET_short:
503 					ev->u.bval = (ev->u.sval == 0) ? sal_False : sal_True;
504 					ev->et = ET_boolean;
505 					return ev;
506 				case ET_ushort:
507 					ev->u.bval = (ev->u.usval == 0) ? sal_False : sal_True;
508 					ev->et = ET_boolean;
509 					return ev;
510 				case ET_long:
511 					ev->u.bval = (ev->u.lval == 0) ? sal_False : sal_True;
512 					ev->et = ET_boolean;
513 					return ev;
514 				case ET_ulong:
515 					ev->u.bval = (ev->u.ulval == 0) ? sal_False : sal_True;
516 					ev->et = ET_boolean;
517 					return ev;
518 				case ET_hyper:
519 					ev->u.bval = (ev->u.hval == 0) ? sal_False : sal_True;
520 					ev->et = ET_boolean;
521 					return ev;
522 				case ET_uhyper:
523 					ev->u.bval = (ev->u.uhval == 0) ? sal_False : sal_True;
524 					ev->et = ET_boolean;
525 					return ev;
526 				case ET_boolean:
527 					return ev;
528 				case ET_float:
529 					ev->u.bval = (ev->u.fval == 0.0) ? sal_False : sal_True;
530 					ev->et = ET_boolean;
531 					return ev;
532 				case ET_double:
533 					ev->u.bval = (ev->u.dval == 0.0) ? sal_False : sal_True;
534 					ev->et = ET_boolean;
535 					return ev;
536 				case ET_byte:
537 					ev->u.bval = (ev->u.byval == 0) ? sal_False : sal_True;
538 					ev->et = ET_boolean;
539 					return ev;
540                 default:
541                     OSL_ASSERT(false);
542 					return NULL;
543 			}
544 		case ET_float:
545 			switch (ev->et)
546 			{
547 				case ET_short:
548 					ev->u.fval = (float)ev->u.sval;
549 					ev->et = ET_float;
550 					return ev;
551 				case ET_ushort:
552 					ev->u.fval = (float)ev->u.usval;
553 					ev->et = ET_float;
554 					return ev;
555 				case ET_long:
556 					ev->u.fval = (float)ev->u.lval;
557 					ev->et = ET_float;
558 					return ev;
559 				case ET_ulong:
560 					ev->u.fval = (float)ev->u.ulval;
561 					ev->et = ET_float;
562 					return ev;
563 				case ET_hyper:
564 					ev->u.fval = (float)ev->u.hval;
565 					ev->et = ET_float;
566 					return ev;
567 				case ET_uhyper:
568 					if ((float)ev->u.ulval > FLT_MAX)
569 						return NULL;
570 					ev->u.fval = (float)ev->u.ulval;
571 					ev->et = ET_float;
572 					return ev;
573 				case ET_boolean:
574 					ev->u.fval = (ev->u.bval == sal_True) ? 1.0f : 0.0f;
575 					ev->et = ET_float;
576 					return ev;
577 				case ET_float:
578 					return ev;
579 				case ET_double:
580 					if ((float)ev->u.dval > FLT_MAX || (float)ev->u.dval < -FLT_MAX)
581 						return NULL;
582 					ev->u.fval = (float)ev->u.dval;
583 					ev->et = ET_float;
584 					return ev;
585 				case ET_byte:
586 					ev->u.fval = (float)ev->u.byval;
587 					ev->et = ET_float;
588 					return ev;
589                 default:
590                     OSL_ASSERT(false);
591 					return NULL;
592 			}
593 		case ET_double:
594 			switch (ev->et)
595 			{
596 				case ET_short:
597 					ev->u.dval = (double)ev->u.sval;
598 					ev->et = ET_double;
599 					return ev;
600 				case ET_ushort:
601 					ev->u.dval = (double)ev->u.usval;
602 					ev->et = ET_double;
603 					return ev;
604 				case ET_long:
605 					ev->u.dval = (double)ev->u.lval;
606 					ev->et = ET_double;
607 					return ev;
608 				case ET_ulong:
609 					ev->u.dval = (double)ev->u.ulval;
610 					ev->et = ET_double;
611 					return ev;
612 				case ET_hyper:
613 					ev->u.dval = (double)ev->u.hval;
614 					ev->et = ET_double;
615 					return ev;
616 				case ET_uhyper:
617 					if ((double)ev->u.dval > FLT_MAX || (double)ev->u.dval < -FLT_MAX)
618 						return NULL;
619 					ev->u.dval = (double)ev->u.ulval;
620 					ev->et = ET_double;
621 					return ev;
622 				case ET_boolean:
623 					ev->u.dval = (ev->u.bval == sal_True) ? 1.0 : 0.0;
624 					ev->et = ET_double;
625 					return ev;
626 				case ET_float:
627 					ev->u.dval = (double)ev->u.fval;
628 					ev->et = ET_double;
629 					return ev;
630 				case ET_double:
631 					return ev;
632 				case ET_byte:
633 					ev->u.dval = (double)ev->u.byval;
634 					ev->et = ET_double;
635 					return ev;
636                 default:
637                     OSL_ASSERT(false);
638 					return NULL;
639 			}
640 		case ET_byte:
641 			switch (ev->et)
642 			{
643 				case ET_short:
644 					if (ev->u.sval < SAL_MIN_INT8 || ev->u.sval > SAL_MAX_UINT8)
645 						return NULL;
646 					ev->u.byval = (sal_uChar)ev->u.sval;
647 					ev->et = ET_byte;
648 					return ev;
649 				case ET_ushort:
650 					if (ev->u.usval > SAL_MAX_UINT8)
651 						return NULL;
652 					ev->u.byval = (sal_uChar)ev->u.usval;
653 					ev->et = ET_byte;
654 					return ev;
655 				case ET_long:
656 					if (ev->u.lval < SAL_MIN_INT8 || ev->u.lval > SAL_MAX_UINT8)
657 						return NULL;
658 					ev->u.byval = (sal_uChar) ev->u.lval;
659 					ev->et = ET_byte;
660 					return ev;
661 				case ET_ulong:
662 					if (ev->u.ulval > SAL_MAX_UINT8)
663 						return NULL;
664 					ev->u.byval = (sal_uChar) ev->u.ulval;
665 					ev->et = ET_byte;
666 					return ev;
667 				case ET_hyper:
668 					if (ev->u.hval < SAL_MIN_INT8 || ev->u.hval > SAL_MAX_UINT8)
669 						return NULL;
670 					ev->u.byval = (sal_uChar) ev->u.hval;
671 					ev->et = ET_byte;
672 					return ev;
673 				case ET_uhyper:
674 					if (ev->u.uhval > SAL_MAX_UINT8)
675 						return NULL;
676 					ev->u.byval = (sal_uChar) ev->u.uhval;
677 					ev->et = ET_byte;
678 					return ev;
679 				case ET_boolean:
680 					ev->u.byval = (ev->u.bval == sal_False) ? 1 : 0;
681 					ev->et = ET_byte;
682 					return ev;
683 				case ET_float:
684 					if (ev->u.fval < SAL_MIN_INT8 || ev->u.fval > SAL_MAX_UINT8)
685 						return NULL;
686 					ev->u.byval = (sal_uChar) ev->u.fval;
687 					ev->et = ET_byte;
688 					return ev;
689 				case ET_double:
690 					if (ev->u.dval < SAL_MIN_INT8 || ev->u.dval > SAL_MAX_UINT8)
691 						return NULL;
692 					ev->u.byval = (sal_uChar) ev->u.dval;
693 					ev->et = ET_byte;
694 					return ev;
695 				case ET_byte:
696 					return ev;
697                 default:
698                     OSL_ASSERT(false);
699 					return NULL;
700 			}
701         default:
702             OSL_ASSERT(false);
703             return NULL;
704     }
705 }
706 
707 /*
708  * Evaluate the expression with the evaluation kind requested. Supported
709  * evaluation kinds are
710  * - EK_const:		The expression must evaluate to a constant
711  * - EK_positive_int:	The expression must further evaluate to a
712  *			positive integer
713  */
714 static AstExprValue *
715 eval_kind(AstExprValue *ev, EvalKind ek)
716 {
717 	if (ek == EK_const)
718 		return ev;
719 	if (ek == EK_positive_int)
720 		return coerce_value(ev, ET_ulong);
721 
722 	return NULL;
723 }
724 
725 AstExprValue* AstExpression::coerce(ExprType t, sal_Bool bAssign)
726 {
727 	AstExprValue *copy;
728 
729 	/*
730 	 * Is it already of the right type?
731 	 */
732 	if (m_exprValue != NULL && m_exprValue->et == t)
733 		return m_exprValue;
734 	/*
735 	 * OK, must coerce
736 	 *
737 	 * First, evaluate it, then try to coerce result type
738 	 * If already evaluated, return the result
739 	 */
740 	m_exprValue = eval_internal(EK_const);
741 	if (m_exprValue == NULL)
742 		return NULL;
743 
744 	/*
745 	 * Create a copy to contain coercion result
746 	 */
747 	copy = new AstExprValue;
748 
749 	copy->et = m_exprValue->et;
750 	switch (m_exprValue->et)
751 	{
752 		case ET_short:
753 			copy->u.sval = m_exprValue->u.sval;
754 			break;
755 		case ET_ushort:
756 			copy->u.usval = m_exprValue->u.usval;
757 			break;
758 		case ET_long:
759 			copy->u.lval = m_exprValue->u.lval;
760 			break;
761 		case ET_ulong:
762 			copy->u.ulval = m_exprValue->u.ulval;
763 			break;
764 		case ET_hyper:
765 			copy->u.hval = m_exprValue->u.hval;
766 			break;
767 		case ET_uhyper:
768 			copy->u.uhval = m_exprValue->u.uhval;
769 			break;
770 		case ET_boolean:
771 			copy->u.bval = m_exprValue->u.bval;
772 			break;
773 		case ET_float:
774 			copy->u.fval = m_exprValue->u.fval;
775 			break;
776 		case ET_double:
777 			copy->u.dval = m_exprValue->u.dval;
778 			break;
779 	  	case ET_byte:
780 			copy->u.byval = m_exprValue->u.byval;
781 			break;
782         default:
783             OSL_ASSERT(false);
784             break;
785 	}
786 
787 	if (bAssign)
788 	{
789 		m_exprValue = coerce_value(copy, t);
790 		return m_exprValue;
791 	}
792 
793 	return coerce_value(copy, t);
794 }
795 
796 void AstExpression::evaluate(EvalKind ek)
797 {
798 	m_exprValue = eval_internal(ek);
799 	m_exprValue = eval_kind(m_exprValue, ek);
800 }
801 
802 sal_Bool AstExpression::operator==(AstExpression *pExpr)
803 {
804 	if (m_combOperator != pExpr->getCombOperator())
805 		return sal_False;
806 	evaluate(EK_const);
807 	pExpr->evaluate(EK_const);
808 	if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
809 		return sal_False;
810 	if (m_exprValue->et != pExpr->getExprValue()->et)
811 		return sal_False;
812 	switch (m_exprValue->et)
813 	{
814 		case ET_short:
815 			return (m_exprValue->u.sval == pExpr->getExprValue()->u.sval) ? sal_True : sal_False;
816 		case ET_ushort:
817 			return (m_exprValue->u.usval == pExpr->getExprValue()->u.usval) ? sal_True : sal_False;
818 		case ET_long:
819 			return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
820 		case ET_ulong:
821 			return (m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval) ? sal_True : sal_False;
822 		case ET_hyper:
823 			return (m_exprValue->u.hval == pExpr->getExprValue()->u.hval) ? sal_True : sal_False;
824 		case ET_uhyper:
825 			return (m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval) ? sal_True : sal_False;
826 		case ET_float:
827 			return (m_exprValue->u.fval == pExpr->getExprValue()->u.fval) ? sal_True : sal_False;
828 		case ET_double:
829 			return (m_exprValue->u.dval == pExpr->getExprValue()->u.dval) ? sal_True : sal_False;
830 		case ET_byte:
831 			return (m_exprValue->u.byval == pExpr->getExprValue()->u.byval) ? sal_True : sal_False;
832 		case ET_boolean:
833 			return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
834         default:
835             OSL_ASSERT(false);
836 			return sal_False;
837 	}
838 
839 	return sal_False;
840 }
841 
842 sal_Bool AstExpression::compare(AstExpression *pExpr)
843 {
844 	if (m_combOperator != pExpr->getCombOperator())
845 		return sal_False;
846 	evaluate(EK_const);
847 	pExpr->evaluate(EK_const);
848 	if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
849 		return sal_False;
850 	if (m_exprValue->et != pExpr->getExprValue()->et)
851 		return sal_False;
852 	switch (m_exprValue->et)
853 	{
854 		case ET_short:
855 			return (m_exprValue->u.sval == pExpr->getExprValue()->u.sval) ? sal_True : sal_False;
856 		case ET_ushort:
857 			return (m_exprValue->u.usval == pExpr->getExprValue()->u.usval) ? sal_True : sal_False;
858 		case ET_long:
859 			return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
860 		case ET_ulong:
861 			return (m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval) ? sal_True : sal_False;
862 		case ET_hyper:
863 			return (m_exprValue->u.hval == pExpr->getExprValue()->u.hval) ? sal_True : sal_False;
864 		case ET_uhyper:
865 			return (m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval) ? sal_True : sal_False;
866 		case ET_float:
867 			return (m_exprValue->u.fval == pExpr->getExprValue()->u.fval) ? sal_True : sal_False;
868 		case ET_double:
869 			return (m_exprValue->u.dval == pExpr->getExprValue()->u.dval) ? sal_True : sal_False;
870 		case ET_byte:
871 			return (m_exprValue->u.byval == pExpr->getExprValue()->u.byval) ? sal_True : sal_False;
872 		case ET_boolean:
873 			return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
874         default:
875             OSL_ASSERT(false);
876 			return sal_False;
877 	}
878 
879 	return sal_False;
880 }
881 
882 void AstExpression::fillDefinitionDetails()
883 {
884 	m_pScope = idlc()->scopes()->depth() > 0 ? idlc()->scopes()->top() : NULL;
885 	m_lineNo = idlc()->getLineNumber();
886 	m_fileName = idlc()->getFileName();
887 }
888 
889 AstExprValue* AstExpression::eval_internal(EvalKind ek)
890 {
891 	/*
892 	 * Already evaluated?
893 	 */
894 	if ( m_exprValue != NULL )
895 		return eval_kind(m_exprValue, ek);
896 	/*
897 	 * OK, must evaluate operator
898 	 */
899 	switch (m_combOperator)
900 	{
901 		case EC_add:
902 		case EC_minus:
903 		case EC_mul:
904 		case EC_div:
905 		case EC_mod:
906 			m_exprValue = eval_bin_op(ek);
907 			return eval_kind(m_exprValue, ek);
908 		case EC_or:
909 		case EC_xor:
910 		case EC_and:
911 		case EC_left:
912 		case EC_right:
913 			m_exprValue = eval_bit_op(ek);
914 			return eval_kind(m_exprValue, ek);
915 		case EC_u_plus:
916 		case EC_u_minus:
917 		case EC_bit_neg:
918 			m_exprValue = eval_un_op(ek);
919 			return eval_kind(m_exprValue, ek);
920 		case EC_symbol:
921 			m_exprValue = eval_symbol(ek);
922 			return eval_kind(m_exprValue, ek);
923 		case EC_none:
924 			return NULL;
925 	}
926 
927 	return NULL;
928 }
929 
930 AstExprValue* AstExpression::eval_bin_op(EvalKind ek)
931 {
932 	ExprType eType = ET_double;
933 
934 	if ( m_combOperator == EC_mod )
935 		eType = ET_hyper;
936 
937 	if (ek != EK_const && ek != EK_positive_int)
938 		return NULL;
939 	if (m_subExpr1 == NULL || m_subExpr2 == NULL)
940 		return NULL;
941 	m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
942 	if (m_subExpr1->getExprValue() == NULL)
943 		return NULL;
944 	m_subExpr1->setExprValue(m_subExpr1->coerce(eType));
945 	if (m_subExpr1->getExprValue() == NULL)
946 		return NULL;
947 	m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
948 	if (m_subExpr2->getExprValue() == NULL)
949 		return NULL;
950 	m_subExpr2->setExprValue(m_subExpr2->coerce(eType));
951 	if (m_subExpr2->getExprValue() == NULL)
952 		return NULL;
953 
954     std::auto_ptr< AstExprValue > retval(new AstExprValue());
955 	retval->et = eType;
956 
957 	switch (m_combOperator)
958 	{
959 		case EC_mod:
960 			if (m_subExpr2->getExprValue()->u.hval == 0)
961 				return NULL;
962 			retval->u.hval = m_subExpr1->getExprValue()->u.hval % m_subExpr2->getExprValue()->u.hval;
963 			break;
964 		case EC_add:
965 			retval->u.dval = m_subExpr1->getExprValue()->u.dval + m_subExpr2->getExprValue()->u.dval;
966 			break;
967 		case EC_minus:
968 			retval->u.dval = m_subExpr1->getExprValue()->u.dval - m_subExpr2->getExprValue()->u.dval;
969 			break;
970 		case EC_mul:
971 			retval->u.dval = m_subExpr1->getExprValue()->u.dval * m_subExpr2->getExprValue()->u.dval;
972 			break;
973 		case EC_div:
974 			if (m_subExpr2->getExprValue()->u.dval == 0.0)
975                 return NULL;
976 			retval->u.dval = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval;
977 			break;
978 		default:
979 			return NULL;
980 	}
981 
982 	return retval.release();
983 }
984 
985 AstExprValue* AstExpression::eval_bit_op(EvalKind ek)
986 {
987 	if (ek != EK_const && ek != EK_positive_int)
988 		return NULL;
989 	if (m_subExpr1 == NULL || m_subExpr2 == NULL)
990 		return NULL;
991 	m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
992 	if (m_subExpr1->getExprValue() == NULL)
993 		return NULL;
994 	m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
995 	if (m_subExpr1->getExprValue() == NULL)
996 		return NULL;
997 	m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
998 	if (m_subExpr2->getExprValue() == NULL)
999 		return NULL;
1000 	m_subExpr2->setExprValue(m_subExpr2->coerce(ET_long));
1001 	if (m_subExpr2->getExprValue() == NULL)
1002 		return NULL;
1003 
1004     std::auto_ptr< AstExprValue	> retval(new AstExprValue());
1005 	retval->et = ET_long;
1006 
1007 	switch (m_combOperator)
1008 	{
1009 		case EC_or:
1010 			retval->u.lval = m_subExpr1->getExprValue()->u.lval | m_subExpr2->getExprValue()->u.lval;
1011 			break;
1012 		case EC_xor:
1013 			retval->u.lval = m_subExpr1->getExprValue()->u.lval ^ m_subExpr2->getExprValue()->u.lval;
1014 			break;
1015 		case EC_and:
1016 			retval->u.lval = m_subExpr1->getExprValue()->u.lval & m_subExpr2->getExprValue()->u.lval;
1017 			break;
1018 		case EC_left:
1019 			retval->u.lval = m_subExpr1->getExprValue()->u.lval << m_subExpr2->getExprValue()->u.lval;
1020 			break;
1021 		case EC_right:
1022 			retval->u.lval = m_subExpr1->getExprValue()->u.lval >> m_subExpr2->getExprValue()->u.lval;
1023 			break;
1024 		default:
1025 			return NULL;
1026 	}
1027 
1028 	return retval.release();
1029 }
1030 
1031 AstExprValue* AstExpression::eval_un_op(EvalKind ek)
1032 {
1033 	if (m_exprValue != NULL)
1034 		return m_exprValue;
1035 
1036 	if (ek != EK_const && ek != EK_positive_int)
1037 		return NULL;
1038 	if (m_subExpr1 == NULL)
1039 		return NULL;
1040 	m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
1041 	if (m_subExpr1->getExprValue() == NULL)
1042 		return NULL;
1043 	m_subExpr1->setExprValue(m_subExpr1->coerce(ET_double));
1044 	if (m_subExpr1->getExprValue() == NULL)
1045 		return NULL;
1046 
1047     std::auto_ptr< AstExprValue	> retval(new AstExprValue());
1048 	retval->et = ET_double;
1049 
1050 	switch (m_combOperator)
1051 	{
1052 		case EC_u_plus:
1053 			retval->u.lval = m_subExpr1->getExprValue()->u.lval;
1054 			break;
1055 		case EC_u_minus:
1056 			retval->u.lval = -(m_subExpr1->getExprValue()->u.lval);
1057 			break;
1058 		case EC_bit_neg:
1059 			m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
1060 			if (m_subExpr1->getExprValue() == NULL)
1061 				return NULL;
1062 			retval->u.lval = ~m_subExpr1->getExprValue()->u.lval;
1063 			break;
1064 		default:
1065 			return NULL;
1066 	}
1067 
1068 	return retval.release();
1069 }
1070 
1071 AstExprValue* AstExpression::eval_symbol(EvalKind ek)
1072 {
1073 	AstScope		*pScope = 0;
1074 	AstDeclaration	*pDecl;
1075 	AstConstant		*pConst;
1076 
1077 	/*
1078 	 * Is there a symbol stored?
1079 	 */
1080 	if (m_pSymbolicName == NULL)
1081 	{
1082 		idlc()->error()->evalError(this);
1083 		return NULL;
1084 	}
1085 	/*
1086 	 * Get current scope for lookup
1087 	 */
1088 	if (idlc()->scopes()->depth() > 0)
1089 		pScope = idlc()->scopes()->topNonNull();
1090 	if ( !pScope )
1091 	{
1092 		idlc()->error()->lookupError(*m_pSymbolicName);
1093 		return NULL;
1094 	}
1095 	/*
1096 	 * Do lookup
1097 	 */
1098 	pDecl = pScope->lookupByName(*m_pSymbolicName);
1099 	if (pDecl == NULL)
1100 	{
1101 		idlc()->error()->lookupError(*m_pSymbolicName);
1102 		return NULL;
1103 	}
1104 	/*
1105 	 * Is it a constant?
1106 	 */
1107 	if (pDecl->getNodeType() != NT_const &&
1108 		pDecl->getNodeType() != NT_enum_val)
1109 	{
1110 		idlc()->error()->constantExpected(pDecl, *m_pSymbolicName);
1111 		return NULL;
1112 	}
1113     if (!idlc()->error()->checkPublished(pDecl))
1114     {
1115         return NULL;
1116     }
1117 	/*
1118 	 * OK, now evaluate the constant we just got, to produce its value
1119 	 */
1120 	pConst = static_cast< AstConstant* >(pDecl);
1121 	if (pConst == NULL)
1122 		return NULL;
1123 	return pConst->getConstValue()->eval_internal(ek);
1124 }
1125 
1126 OString AstExpression::toString()
1127 {
1128 	OString exprStr;
1129 	if ( m_combOperator == EC_symbol )
1130 		return *m_pSymbolicName;
1131 
1132 	if ( m_exprValue )
1133 	{
1134 		switch (m_exprValue->et)
1135 		{
1136 			case ET_short:
1137 				return OString::valueOf((sal_Int32)m_exprValue->u.sval);
1138 			case ET_ushort:
1139 				return OString::valueOf((sal_Int32)m_exprValue->u.usval);
1140 			case ET_long:
1141 				return OString::valueOf(m_exprValue->u.lval);
1142 			case ET_ulong:
1143 				return OString::valueOf((sal_Int32)m_exprValue->u.ulval);
1144 			case ET_hyper:
1145 				return OString::valueOf(m_exprValue->u.hval);
1146 			case ET_uhyper:
1147 				return OString::valueOf((sal_Int64)m_exprValue->u.uhval);
1148 			case ET_float:
1149 				return OString::valueOf(m_exprValue->u.fval);
1150 			case ET_double:
1151 				return OString::valueOf(m_exprValue->u.dval);
1152 			case ET_byte:
1153 				return OString::valueOf((sal_Int32)m_exprValue->u.byval);
1154 			case ET_boolean:
1155 				if ( m_exprValue->u.lval == 0)
1156 					return OString("FALSE");
1157 				else
1158 					return OString("TRUE");
1159             default:
1160                 OSL_ASSERT(false);
1161 				return OString();
1162 		}
1163 	}
1164 
1165 	switch (m_combOperator)
1166 	{
1167 		case EC_u_plus:
1168 			exprStr += OString("+");
1169 			break;
1170 		case EC_u_minus:
1171 			exprStr += OString("-");
1172 			break;
1173 		case EC_bit_neg:
1174 			exprStr += OString("~");
1175             break;
1176         default:
1177             break;
1178 	}
1179 	if ( m_subExpr1 )
1180 		exprStr += m_subExpr1->toString();
1181 	switch (m_combOperator)
1182 	{
1183 		case EC_add:
1184 			exprStr += OString(" + ");
1185 			break;
1186 		case EC_minus:
1187 			exprStr += OString(" - ");
1188 			break;
1189 		case EC_mul:
1190 			exprStr += OString(" * ");
1191 			break;
1192 		case EC_div:
1193 			exprStr += OString(" / ");
1194 			break;
1195 		case EC_mod:
1196 			exprStr += OString(" % ");
1197 			break;
1198 		case EC_or:
1199 			exprStr += OString(" | ");
1200 			break;
1201 		case EC_xor:
1202 			exprStr += OString(" ^ ");
1203 			break;
1204 		case EC_and:
1205 			exprStr += OString(" & ");
1206 			break;
1207 		case EC_left:
1208 			exprStr += OString(" << ");
1209 			break;
1210 		case EC_right:
1211 			exprStr += OString(" >> ");
1212 			break;
1213         default:
1214             break;
1215 	}
1216 
1217 	if ( m_subExpr2 )
1218 		exprStr += m_subExpr2->toString();
1219 
1220 	return exprStr;
1221 }
1222 
1223 // Convert the type of an AST_Expression to a char *
1224 const sal_Char* SAL_CALL exprTypeToString(ExprType t)
1225 {
1226 	switch (t)
1227 	{
1228 		case ET_short:
1229 			return "short";
1230 		case ET_ushort:
1231 			return "unsigned short";
1232 		case ET_long:
1233 			return "long";
1234 		case ET_ulong:
1235 			return "unsigned long";
1236 		case ET_hyper:
1237 			return "hyper";
1238 		case ET_uhyper:
1239 			return "unsigned hyper";
1240 		case ET_float:
1241 			return "float";
1242 		case ET_double:
1243 			return "double";
1244 		case ET_char:
1245 			return "char";
1246 		case ET_byte:
1247 			return "byte";
1248 		case ET_boolean:
1249 			return "boolean";
1250 		case ET_string:
1251 			return "string";
1252 		case ET_any:
1253 			return "any";
1254 		case ET_type:
1255 			return "type";
1256 		case ET_void:
1257 			return "void";
1258 		case ET_none:
1259 			return "none";
1260 	}
1261 
1262 	return ("unkown");
1263 }
1264