Synopsis - Cross-Reference
File: /Synopsis/Parsers/IDL/idlexpr.h1// -*- c++ -*- 2// Package : omniidl 3// idlexpr.h Created on: 1999/10/18 4// Author : Duncan Grisby (dpg1) 5// 6// Copyright (C) 1999 AT&T Laboratories Cambridge 7// 8// This file is part of omniidl. 9// 10// omniidl is free software; you can redistribute it and/or modify it 11// under the terms of the GNU General Public License as published by 12// the Free Software Foundation; either version 2 of the License, or 13// (at your option) any later version. 14// 15// This program 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 GNU 18// General Public License for more details. 19// 20// You should have received a copy of the GNU General Public License 21// along with this program; if not, write to the Free Software 22// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23// 02111-1307, USA. 24// 25// Description: 26// 27// Expression tree and evaluator 28 29// $Id: idlexpr.h,v 1.7.2.1 2003/03/23 21:01:45 dgrisby Exp $ 30// $Log: idlexpr.h,v $ 31// Revision 1.7.2.1 2003/03/23 21:01:45 dgrisby 32// Start of omniORB 4.1.x development branch. 33// 34// Revision 1.4.2.4 2001/08/29 11:54:20 dpg1 35// Clean up const handling in IDL compiler. 36// 37// Revision 1.4.2.3 2001/03/13 10:32:12 dpg1 38// Fixed point support. 39// 40// Revision 1.4.2.2 2000/10/27 16:31:09 dpg1 41// Clean up of omniidl dependencies and types, from omni3_develop. 42// 43// Revision 1.4.2.1 2000/07/17 10:36:03 sll 44// Merged from omni3_develop the diff between omni3_0_0_pre3 and omni3_0_0. 45// 46// Revision 1.5 2000/07/13 15:25:53 dpg1 47// Merge from omni3_develop for 3.0 release. 48// 49// Revision 1.2 1999/11/02 17:07:26 dpg1 50// Changes to compile on Solaris. 51// 52// Revision 1.1 1999/10/27 14:05:56 dpg1 53// *** empty log message *** 54// 55 56#ifndef _idlexpr_h_ 57#define _idlexpr_h_ 58 59#include <idlutil.h> 60#include <idlscope.h> 61#include <idlfixed.h> 62 63class Enumerator; 64class Enum; 65class Const; 66 67 68struct IdlLongVal { 69 IdlLongVal(IDL_ULong a) : negative(0), u(a) {} 70 IdlLongVal(IDL_Long a) : negative(0), s(a) { if (a<0) negative=1; } 71 72 IDL_Boolean negative; 73 union { 74 IDL_ULong u; 75 IDL_Long s; 76 }; 77}; 78 79#ifdef HAS_LongLong 80struct IdlLongLongVal { 81 IdlLongLongVal(IDL_ULongLong a) : negative(0), u(a) {} 82 IdlLongLongVal(IDL_LongLong a) : negative(0), s(a) { if (a<0) negative=1; } 83 84 IDL_Boolean negative; 85 union { 86 IDL_ULongLong u; 87 IDL_LongLong s; 88 }; 89}; 90#endif 91 92class IdlExpr { 93public: 94 IdlExpr(const char* file, int line) : file_(idl_strdup(file)), line_(line) {} 95 virtual ~IdlExpr() { delete [] file_; } 96 97 // 98 // Virtual functions overridded by derived expression types 99 // 100 101 virtual IdlLongVal evalAsLongV(); 102#ifdef HAS_LongLong 103 virtual IdlLongLongVal evalAsLongLongV(); 104#endif 105 virtual IDL_Float evalAsFloat(); 106 virtual IDL_Double evalAsDouble(); 107 virtual IDL_Boolean evalAsBoolean(); 108 virtual IDL_Char evalAsChar(); 109 virtual const char* evalAsString(); 110 virtual Enumerator* evalAsEnumerator(const Enum* target); 111#ifdef HAS_LongDouble 112 virtual IDL_LongDouble evalAsLongDouble(); 113#endif 114 virtual IDL_WChar evalAsWChar(); 115 virtual const IDL_WChar* evalAsWString(); 116 virtual IDL_Fixed* evalAsFixed(); 117 118 // 119 // Functions to convert an integer represented as a signed/unsigned 120 // union to an IDL integer type 121 // 122 123 IDL_Short evalAsShort(); 124 IDL_Long evalAsLong(); 125 IDL_UShort evalAsUShort(); 126 IDL_ULong evalAsULong(); 127 IDL_Octet evalAsOctet(); 128#ifdef HAS_LongLong 129 IDL_LongLong evalAsLongLong(); 130 IDL_ULongLong evalAsULongLong(); 131#endif 132 133 inline const char* file() { return file_; } 134 inline int line() { return line_; } 135 136 virtual const char* errText() = 0; 137 138 static IdlExpr* scopedNameToExpr(const char* file, int line, ScopedName* sn); 139 140private: 141 char* file_; 142 int line_; 143}; 144 145 146// Dummy expression class used as a place-holder after an error 147class DummyExpr : public IdlExpr { 148public: 149 DummyExpr(const char* file, int line) : IdlExpr(file, line) {} 150 virtual ~DummyExpr() {} 151 152 IdlLongVal evalAsLongV() { return IdlLongVal (IDL_ULong(1)); } 153#ifdef HAS_LongLong 154 IdlLongLongVal evalAsLongLongV() { return IdlLongLongVal(IDL_ULongLong(1));} 155#endif 156 157 IDL_Float evalAsFloat() { return 1.0; } 158 IDL_Double evalAsDouble() { return 1.0; } 159 IDL_Boolean evalAsBoolean() { return 0; } 160 IDL_Char evalAsChar() { return '!'; } 161 const char* evalAsString() { return "!"; } 162 Enumerator* evalAsEnumerator(const Enum* target) { return 0; } 163#ifdef HAS_LongDouble 164 IDL_LongDouble evalAsLongDouble() { return 1.0; } 165#endif 166 IDL_WChar evalAsWChar() { return '!'; } 167 const IDL_WChar* evalAsWString(); 168 IDL_Fixed* evalAsFixed() { return new IDL_Fixed("1"); } 169 170 const char* errText() { return "dummy"; } 171}; 172 173 174// Literals 175 176class IntegerExpr : public IdlExpr { 177public: 178 IntegerExpr(const char* file, int line, IdlIntLiteral v) 179 : IdlExpr(file, line), value_(v) { } 180 ~IntegerExpr() {} 181 182 IdlLongVal evalAsLongV(); 183#ifdef HAS_LongLong 184 IdlLongLongVal evalAsLongLongV(); 185#endif 186 const char* errText() { return "integer literal"; } 187private: 188 IdlIntLiteral value_; 189}; 190 191class StringExpr : public IdlExpr { 192public: 193 StringExpr(const char* file, int line, const char* v) 194 : IdlExpr(file, line), value_(idl_strdup(v)) { } 195 ~StringExpr() { delete [] value_; } 196 197 const char* evalAsString(); 198 const char* errText() { return "string literal"; } 199private: 200 char* value_; 201}; 202 203class WStringExpr : public IdlExpr { 204public: 205 WStringExpr(const char* file, int line, const IDL_WChar* v) 206 : IdlExpr(file, line), value_(idl_wstrdup(v)) {} 207 ~WStringExpr() { delete [] value_; } 208 209 const IDL_WChar* evalAsWString(); 210 const char* errText() { return "wide string literal"; } 211private: 212 IDL_WChar* value_; 213}; 214 215class CharExpr : public IdlExpr { 216public: 217 CharExpr(const char* file, int line, IDL_Char v) 218 : IdlExpr(file, line), value_(v) { } 219 ~CharExpr() {} 220 221 IDL_Char evalAsChar(); 222 const char* errText() { return "character literal"; } 223private: 224 IDL_Char value_; 225}; 226 227class WCharExpr : public IdlExpr { 228public: 229 WCharExpr(const char* file, int line, IDL_WChar v) 230 : IdlExpr(file, line), value_(v) {} 231 ~WCharExpr() {} 232 233 IDL_WChar evalAsWChar(); 234 const char* errText() { return "wide character literal"; } 235private: 236 IDL_WChar value_; 237}; 238 239class FixedExpr : public IdlExpr { 240public: 241 FixedExpr(const char* file, int line, IDL_Fixed* v) 242 : IdlExpr(file, line), value_(v) {} 243 ~FixedExpr() {} 244 245 IDL_Fixed* evalAsFixed(); 246 const char* errText() { return "fixed point literal"; } 247private: 248 IDL_Fixed* value_; 249}; 250 251class FloatExpr : public IdlExpr { 252public: 253 FloatExpr(const char* file, int line, IdlFloatLiteral v) 254 : IdlExpr(file, line), value_(v) { } 255 ~FloatExpr() {} 256 257 IDL_Float evalAsFloat(); 258 IDL_Double evalAsDouble(); 259#ifdef HAS_LongDouble 260 IDL_LongDouble evalAsLongDouble(); 261#endif 262 const char* errText() { return "floating point literal"; } 263private: 264 IdlFloatLiteral value_; 265}; 266 267class BooleanExpr : public IdlExpr { 268public: 269 BooleanExpr(const char* file, int line, IDL_Boolean v) 270 : IdlExpr(file, line), value_(v) { } 271 ~BooleanExpr() {} 272 273 IDL_Boolean evalAsBoolean(); 274 const char* errText() { return "boolean literal"; } 275private: 276 IDL_Boolean value_; 277}; 278 279// Enumerator referred to by scoped name 280class EnumExpr : public IdlExpr { 281public: 282 EnumExpr(const char* file, int line, Enumerator* e, ScopedName* sn) 283 : IdlExpr(file, line), value_(e), scopedName_(sn) {} 284 ~EnumExpr() {} 285 286 Enumerator* evalAsEnumerator(const Enum* target); 287 const char* errText() { return "enumerator"; } 288private: 289 Enumerator* value_; 290 ScopedName* scopedName_; 291}; 292 293// Constant referred to by scoped name 294class ConstExpr : public IdlExpr { 295public: 296 ConstExpr(const char* file, int line, Const* c, ScopedName* sn) 297 : IdlExpr(file, line), c_(c), scopedName_(sn) {} 298 ~ConstExpr() {} 299 300 IdlLongVal evalAsLongV(); 301#ifdef HAS_LongLong 302 IdlLongLongVal evalAsLongLongV(); 303#endif 304 IDL_Float evalAsFloat(); 305 IDL_Double evalAsDouble(); 306 IDL_Boolean evalAsBoolean(); 307 IDL_Char evalAsChar(); 308 IDL_Octet evalAsOctet(); 309 const char* evalAsString(); 310 Enumerator* evalAsEnumerator(const Enum* target); 311#ifdef HAS_LongDouble 312 IDL_LongDouble evalAsLongDouble(); 313#endif 314 IDL_WChar evalAsWChar(); 315 const IDL_WChar* evalAsWString(); 316 IDL_Fixed* evalAsFixed(); 317 318 const char* errText() { return "constant"; } 319private: 320 Const* c_; 321 ScopedName* scopedName_; 322}; 323 324 325 326// Expressions 327 328#ifdef HAS_LongLong 329#define EXPR_INT_CONVERSION_FUNCTIONS \ 330 IdlLongVal evalAsLongV(); \ 331 IdlLongLongVal evalAsLongLongV(); 332#else 333#define EXPR_INT_CONVERSION_FUNCTIONS \ 334 IdlLongVal evalAsLongV(); 335#endif 336 337#ifdef HAS_LongDouble 338#define EXPR_FLOAT_CONVERSION_FUNCTIONS \ 339 IDL_Float evalAsFloat(); \ 340 IDL_Double evalAsDouble(); \ 341 IDL_LongDouble evalAsLongDouble(); 342#else 343#define EXPR_FLOAT_CONVERSION_FUNCTIONS \ 344 IDL_Float evalAsFloat(); \ 345 IDL_Double evalAsDouble(); 346#endif 347 348#define EXPR_FIXED_CONVERSION_FUNCTIONS \ 349 IDL_Fixed* evalAsFixed(); 350 351#define EXPR_CONVERSION_FUNCTIONS \ 352 EXPR_INT_CONVERSION_FUNCTIONS \ 353 EXPR_FLOAT_CONVERSION_FUNCTIONS \ 354 EXPR_FIXED_CONVERSION_FUNCTIONS 355 356 357#define EXPR_INT_BINARY_CLASS(cls, str) \ 358class cls : public IdlExpr { \ 359public: \ 360 cls(const char* file, int line, IdlExpr* a, IdlExpr* b) \ 361 : IdlExpr(file, line), a_(a), b_(b) { } \ 362 ~cls() { delete a_; delete b_; } \ 363 EXPR_INT_CONVERSION_FUNCTIONS \ 364 const char* errText() { return "result of " str " operation"; } \ 365private: \ 366 IdlExpr* a_; \ 367 IdlExpr* b_; \ 368}; 369 370#define EXPR_BINARY_CLASS(cls, str) \ 371class cls : public IdlExpr { \ 372public: \ 373 cls(const char* file, int line, IdlExpr* a, IdlExpr* b) \ 374 : IdlExpr(file, line), a_(a), b_(b) { } \ 375 ~cls() { delete a_; delete b_; } \ 376 EXPR_CONVERSION_FUNCTIONS \ 377 const char* errText() { return "result of " str " operation"; } \ 378private: \ 379 IdlExpr* a_; \ 380 IdlExpr* b_; \ 381}; 382 383EXPR_INT_BINARY_CLASS(OrExpr, "or") 384EXPR_INT_BINARY_CLASS(XorExpr, "exclusive or") 385EXPR_INT_BINARY_CLASS(AndExpr, "and") 386EXPR_INT_BINARY_CLASS(RShiftExpr, "right shift") 387EXPR_INT_BINARY_CLASS(LShiftExpr, "left shift") 388EXPR_INT_BINARY_CLASS(ModExpr, "remainder") 389 390EXPR_BINARY_CLASS(AddExpr, "add") 391EXPR_BINARY_CLASS(SubExpr, "subtract") 392EXPR_BINARY_CLASS(MultExpr, "multiply") 393EXPR_BINARY_CLASS(DivExpr, "divide") 394 395 396class InvertExpr : public IdlExpr { 397public: 398 InvertExpr(const char* file, int line, IdlExpr* e) 399 : IdlExpr(file, line), e_(e) { } 400 ~InvertExpr() { delete e_; } 401 EXPR_INT_CONVERSION_FUNCTIONS 402 const char* errText() { return "result of unary invert operator"; } 403private: 404 IdlExpr* e_; 405}; 406 407class MinusExpr : public IdlExpr { 408public: 409 MinusExpr(const char* file, int line, IdlExpr* e) 410 : IdlExpr(file, line), e_(e) { } 411 ~MinusExpr() { delete e_; } 412 EXPR_CONVERSION_FUNCTIONS 413 const char* errText() { return "result of unary negate operator"; } 414private: 415 IdlExpr* e_; 416}; 417 418class PlusExpr : public IdlExpr { 419public: 420 PlusExpr(const char* file, int line, IdlExpr* e) 421 : IdlExpr(file, line), e_(e) { } 422 ~PlusExpr() { delete e_; } 423 EXPR_CONVERSION_FUNCTIONS 424 const char* errText() { return "result of unary plus operator"; } 425private: 426 IdlExpr* e_; 427}; 428 429 430#endif // _idlexpr_h_