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