1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003 The 'ac++' developers (see aspectc.org)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
#include <string>
using std::string;
#include <sstream>
using std::ostringstream;
#include "Puma/CRecord.h"
#include "Puma/CEnumInfo.h"
using namespace Puma;
#include "MatchTypeInfos.h"
// comparison functions
// is it the same kind of type?
bool MatchType::operator ==(const MatchType &other) const {
// by default two types are equal if they have the same ID
return type () == other.type ();
}
MTUndefined *MTUndefined::clone () const {
return new MTUndefined(*this);
}
void MTUndefined::print (ostream &out, const char *inner, bool prefix) const {
out << "<undefined>";
if (inner)
out << " " << inner;
}
bool MTUndefined::matches (CTypeInfo *ctype) const {
// conversion operator might have an undefined result type
return ctype->isUndefined ();
}
bool MTUndefined::matches (const MatchType &type) const {
// conversion operator might have an undefined result type
return type == *this; // true if both object types are equal
}
// print the string representation of this qualified type
void MTQual::print_qual (ostream &out) const {
if (_const) out << "const";
if (_const && _volatile) out << " ";
if (_volatile) out << "volatile";
}
// perform a type match
// if a type is qualified as const/volatile in a match expression, it does
// only match types which are qualified like this. However, according to
// the AspectC++ semantics a type is also matched if it is const/volatile,
// but the match expression does not require this qualifier.
bool MTQual::qual_matches (CTypeQualified *qtype) const {
if (!qtype && is_qualified ())
return false;
if (qtype) {
if (_const && !qtype->isConst ())
return false;
if (_volatile && !qtype->isVolatile ())
return false;
}
return true;
}
bool MTQual::qual_matches (const MTQual *qual) const {
if (!qual && is_qualified ())
return false;
if (qual) {
if (_const && !qual->is_const ())
return false;
if (_volatile && !qual->is_volatile ())
return false;
}
return true;
}
// print the string representation of this primitive type
// inner might be a name, prefix == true means that the name has prefix
void MTPrim::print (ostream &out, const char *inner, bool prefix) const {
if (is_qualified ()) {
print_qual (out);
out << " ";
}
out << type ();
if (inner)
out << " " << inner;
}
MTNamed *MTNamed::clone () const {
return new MTNamed (*this);
}
// print the string representation of this names type
// inner might be a name, prefix == true means that the name has prefix
void MTNamed::print (ostream &out, const char *inner, bool prefix) const {
if (is_qualified ()) {
print_qual (out);
out << " ";
}
out << _name;
if (inner)
out << " " << inner;
}
bool MTNamed::matches (CTypeInfo *ctype) const {
CTypeQualified *qtype = ctype->TypeQualified ();
if (qtype)
ctype = qtype->BaseType ();
CTypeRecord *rctype = ctype->TypeRecord ();
if (rctype)
return _name.matches (rctype->Record ()) && qual_matches (qtype);
CTypeEnum *ectype = ctype->TypeEnum ();
return ectype && _name.matches (ectype->EnumInfo ()) && qual_matches (qtype);
}
bool MTNamed::matches (const MatchType &type) const {
if (type != *this)
return false;
MTNamed &matched_type = (MTNamed&)type;
return _name.matches (matched_type._name) &&
qual_matches (matched_type.qualified ());
}
MTPointer *MTPointer::clone () const {
return new MTPointer (*this);
}
// print the string represenation of this pointer type
// inner might be a name, prefix == true means that the name has prefix
void MTPointer::print (ostream &out, const char *inner, bool prefix) const {
ostringstream declarator;
declarator << "*";
if (is_qualified ()) {
declarator << " ";
print_qual (declarator);
declarator << " ";
}
if (inner)
declarator << inner;
if (base ())
base ()->print (out, declarator.str ().c_str (), true);
else
out << "<any> " << declarator.str ();
}
bool MTPointer::matches (CTypeInfo *ctype) const {
CTypeQualified *qtype = ctype->TypeQualified ();
CTypePointer *pctype = (qtype ? qtype->BaseType () : ctype)->TypePointer ();
return pctype && base ()->matches (pctype->BaseType ()) &&
qual_matches (qtype);
}
bool MTPointer::matches (const MatchType &type) const {
if (type != *this)
return false;
MTPointer &matched_type = (MTPointer&)type;
return base ()->matches (*matched_type.base ()) &&
qual_matches (matched_type.qualified ());
}
MTMembPointer *MTMembPointer::clone () const {
return new MTMembPointer (*this);
}
// print the string represenation of this pointer type
// inner might be a name, prefix == true means that the name has prefix
void MTMembPointer::print (ostream &out, const char *inner, bool prefix) const {
ostringstream declarator;
declarator << _memb_ptr_scope << "*";
if (is_qualified ()) {
declarator << " ";
print_qual (declarator);
declarator << " ";
}
if (inner)
declarator << inner;
if (base ())
base ()->print (out, declarator.str ().c_str (), true);
else
out << "<any> " << declarator.str ();
}
bool MTMembPointer::matches (CTypeInfo *ctype) const {
CTypeQualified *qtype = ctype->TypeQualified ();
CTypeMemberPointer *mpctype =
(qtype ? qtype->BaseType () : ctype)->TypeMemberPointer ();
return mpctype && base ()->matches (mpctype->BaseType ()) &&
qual_matches (qtype) &&
_memb_ptr_scope.scope_matches (mpctype->Record ());
}
bool MTMembPointer::matches (const MatchType &type) const {
if (type != *this)
return false;
MTMembPointer &matched_type = (MTMembPointer&)type;
return base ()->matches (*matched_type.base ()) &&
qual_matches (matched_type.qualified ()) &&
_memb_ptr_scope.matches (matched_type._memb_ptr_scope);
}
MTReference *MTReference::clone () const {
return new MTReference (*this);
}
// print the string represenation of this reference type
// inner might be a name, prefix == true means that the name has prefix
void MTReference::print (ostream &out, const char *inner, bool prefix) const {
ostringstream declarator;
declarator << "&";
if (inner)
declarator << inner;
if (base ())
base ()->print (out, declarator.str ().c_str (), true);
else
out << "<any> " << declarator.str ();
}
bool MTReference::matches (CTypeInfo *ctype) const {
return ctype->TypeAddress () && base ()->matches (ctype->BaseType ());
}
bool MTReference::matches (const MatchType &type) const {
return type == *this && base ()->matches (*type.base ());
}
//MTFunction::~MTFunction () {
// for (int a = 0; a < args (); a++)
// delete arg (a);
//}
MTFunction *MTFunction::clone () const {
return new MTFunction (*this);
}
// print the string represenation of this function type
// inner might be a name, prefix == true means that the name has prefix
void MTFunction::print (ostream &out, const char *inner, bool prefix) const {
ostringstream declarator;
if (inner)
declarator << (prefix ? "(" : "") << inner << (prefix ? ")" : "");
declarator << "(";
int a = 0;
while (a < args ()) {
if (a > 0) declarator << ",";
arg (a).type_info ()->print (declarator);
a++;
}
if (_varargs) declarator << (a > 0 ? "," : "") << "...";
declarator << ")";
if (is_qualified ()) {
declarator << " ";
print_qual (declarator);
declarator << " ";
}
if (base ())
base ()->print (out, declarator.str ().c_str (), false);
else
out << "<any> " << declarator.str ();
}
// perform a type match
bool MTFunction::matches (CTypeInfo *ctype) const {
// check if it's a function type
CTypeFunction *cftype = ctype->TypeFunction ();
if (!cftype)
return false;
// check the type qualifier
if (!qual_matches (cftype))
return false;
// check the argument types
int cftype_args = (int)cftype->ArgTypes ()->Entries ();
if ((!_varargs && args () != cftype_args) || (args () > cftype_args))
return false;
for (int a = 0; a < args (); a++)
if (!arg (a).matches (cftype->ArgTypes ()->Entry ((unsigned)a)))
return false;
// finally check the result type
return base ()->matches (cftype->ReturnType ());
}
bool MTFunction::matches (const MatchType &type) const {
// check if it's a function type
if (type != *this)
return false;
const MTFunction &matched_type = (MTFunction&)type;
// check the type qualifier
if (!qual_matches (matched_type.qualified ()))
return false;
// check the argument types
int matched_args = matched_type.args ();
// TODO: what about matched functions with varargs?
if ((!_varargs && args () != matched_args) || (args () > matched_args))
return false;
for (int a = 0; a < args (); a++)
if (!arg (a).matches (matched_type.arg (a)))
return false;
// finally check the result type
return base ()->matches (*matched_type.base ());
}
// adjust the argument types according to �8.3.5.2 and �8.3.5.3 of ISO C++
void MTFunction::adjust_args (bool &f, bool &a, bool &q, bool &v) {
// adjust all argument types according to ISO C++
for (int i = 0; i < args (); i++) {
MatchType *curr = _arg_types[i].type_info ();
// recursively adjust the argument
curr->adjust_args (f, a , q, v);
// type `function returning T' is adjusted to be
// `pointer to function returning T'
if (_arg_types[i].is_function ()) {
f = true;
_arg_types[i].to_pointer ();
continue;
}
// type `array of T' is adjusted to be `pointer to T'
if (_arg_types[i].is_array ()) {
a = true;
MatchTypeRef converted_type = _arg_types[i].base ();
converted_type.to_pointer();
_arg_types[i] = converted_type;
continue;
}
// any cv-qualifier modifying a parameter is deleted
if (_arg_types[i].is_qualifiable () &&
_arg_types[i].qualifiers ().is_qualified ()) {
q = true;
_arg_types[i].qualifiers ().unqualify ();
}
}
// �8.3.5.2 the parameter list `(void)' is equivalent to
// the empty parameter list
if (args () == 1 && _arg_types[0].is_void ()) {
v = true;
_arg_types.clear ();
}
}
MTArray *MTArray::clone () const {
return new MTArray (*this);
}
// print the string represenation of this array type
// inner might be a name, prefix == true means that the name has prefix
void MTArray::print (ostream &out, const char *inner, bool prefix) const {
ostringstream declarator;
if (inner)
declarator << (prefix ? "(" : "") << inner << (prefix ? ")" : "");
declarator << "[";
if (_any_size)
declarator << "%";
else
declarator << _dim;
declarator << "]";
if (base ())
base ()->print (out, declarator.str ().c_str (), false);
else
out << "<any> " << declarator.str ();
}
// perform a type match
bool MTArray::matches (CTypeInfo *ctype) const {
CTypeArray *actype = ctype->TypeArray ();
return actype && base ()->matches (ctype->BaseType ()) &&
(_any_size || actype->Dimension () == (long)_dim);
}
bool MTArray::matches (const MatchType &type) const {
if (type != *this)
return false;
MTArray &matched_type = (MTArray&)type;
return base ()->matches (*matched_type.base ()) &&
(_any_size || _dim == matched_type._dim);
}
MTAny *MTAny::clone () const {
return new MTAny (*this);
}
// perform a type match
bool MTAny::matches (CTypeInfo *ctype) const {
return qual_matches (ctype->TypeQualified ());
}
bool MTAny::matches (const MatchType &type) const {
return qual_matches (type.qualified ());
}
MTBool *MTBool::clone () const {
return new MTBool (*this);
}
// perform a type match
bool MTBool::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_bool () && qual_matches (ctype->TypeQualified ());
}
bool MTBool::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTSignedChar *MTSignedChar::clone () const {
return new MTSignedChar (*this);
}
// perform a type match
bool MTSignedChar::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_signed_char () && qual_matches (ctype->TypeQualified ());
}
bool MTSignedChar::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTUnsignedChar *MTUnsignedChar::clone () const {
return new MTUnsignedChar (*this);
}
// perform a type match
bool MTUnsignedChar::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_unsigned_char () && qual_matches (ctype->TypeQualified ());
}
bool MTUnsignedChar::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTChar *MTChar::clone () const {
return new MTChar (*this);
}
// perform a type match
bool MTChar::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_char () && qual_matches (ctype->TypeQualified ());
}
bool MTChar::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTUnsignedShort *MTUnsignedShort::clone () const {
return new MTUnsignedShort (*this);
}
// perform a type match
bool MTUnsignedShort::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_unsigned_short () && qual_matches (ctype->TypeQualified ());
}
bool MTUnsignedShort::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTShort *MTShort::clone () const {
return new MTShort (*this);
}
// perform a type match
bool MTShort::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_short () && qual_matches (ctype->TypeQualified ());
}
bool MTShort::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTUnsignedInt *MTUnsignedInt::clone () const {
return new MTUnsignedInt (*this);
}
// perform a type match
bool MTUnsignedInt::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_unsigned_int () && qual_matches (ctype->TypeQualified ());
}
bool MTUnsignedInt::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTInt *MTInt::clone () const {
return new MTInt (*this);
}
// perform a type match
bool MTInt::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_int () && qual_matches (ctype->TypeQualified ());
}
bool MTInt::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTWCharT *MTWCharT::clone () const {
return new MTWCharT (*this);
}
// perform a type match
bool MTWCharT::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_wchar_t () && qual_matches (ctype->TypeQualified ());
}
bool MTWCharT::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTUnsignedLong *MTUnsignedLong::clone () const {
return new MTUnsignedLong (*this);
}
// perform a type match
bool MTUnsignedLong::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_unsigned_long () && qual_matches (ctype->TypeQualified ());
}
bool MTUnsignedLong::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTLong *MTLong::clone () const {
return new MTLong (*this);
}
// perform a type match
bool MTLong::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_long () && qual_matches (ctype->TypeQualified ());
}
bool MTLong::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTUnsignedLongLong *MTUnsignedLongLong::clone () const {
return new MTUnsignedLongLong (*this);
}
// perform a type match
bool MTUnsignedLongLong::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_unsigned_long_long () && qual_matches (ctype->TypeQualified ());
}
bool MTUnsignedLongLong::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTLongLong *MTLongLong::clone () const {
return new MTLongLong (*this);
}
// perform a type match
bool MTLongLong::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_long_long () && qual_matches (ctype->TypeQualified ());
}
bool MTLongLong::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTFloat *MTFloat::clone () const {
return new MTFloat (*this);
}
// perform a type match
bool MTFloat::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_float () && qual_matches (ctype->TypeQualified ());
}
bool MTFloat::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTDouble *MTDouble::clone () const {
return new MTDouble (*this);
}
// perform a type match
bool MTDouble::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_double () && qual_matches (ctype->TypeQualified ());
}
bool MTDouble::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTLongDouble *MTLongDouble::clone () const {
return new MTLongDouble (*this);
}
// perform a type match
bool MTLongDouble::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_long_double () && qual_matches (ctype->TypeQualified ());
}
bool MTLongDouble::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
MTVoid *MTVoid::clone () const {
return new MTVoid (*this);
}
// perform a type match
bool MTVoid::matches (CTypeInfo *ctype) const {
return ctype->UnqualType ()->is_void () && qual_matches (ctype->TypeQualified ());
}
bool MTVoid::matches (const MatchType &type) const {
return type == *this && qual_matches (type.qualified ());
}
// type ids
MatchType::TID MTUndefined::_tid = "<undefined>";
MatchType::TID MTUndefined::type () const { return _tid; }
MatchType::TID MTDeclarator::_tid = "<declarator>";
MatchType::TID MTDeclarator::type () const { return _tid; }
MatchType::TID MTReference::_tid = "&";
MatchType::TID MTReference::type () const { return _tid; }
MatchType::TID MTPointer::_tid = "*";
MatchType::TID MTPointer::type () const { return _tid; }
MatchType::TID MTMembPointer::_tid = "::*";
MatchType::TID MTMembPointer::type () const { return _tid; }
MatchType::TID MTFunction::_tid = "()";
MatchType::TID MTFunction::type () const { return _tid; }
MatchType::TID MTArray::_tid = "[]";
MatchType::TID MTArray::type () const { return _tid; }
MatchType::TID MTAny::_tid = "%";
MatchType::TID MTAny::type () const { return _tid; }
MatchType::TID MTNamed::_tid = "<named>";
MatchType::TID MTNamed::type () const { return _tid; }
MatchType::TID MTBool::_tid = "bool";
MatchType::TID MTBool::type () const { return _tid; }
MatchType::TID MTSignedChar::_tid = "signed char";
MatchType::TID MTSignedChar::type () const { return _tid; }
MatchType::TID MTUnsignedChar::_tid = "unsigned char";
MatchType::TID MTUnsignedChar::type () const { return _tid; }
MatchType::TID MTChar::_tid = "char";
MatchType::TID MTChar::type () const { return _tid; }
MatchType::TID MTUnsignedShort::_tid = "unsigned short";
MatchType::TID MTUnsignedShort::type () const { return _tid; }
MatchType::TID MTShort::_tid = "short";
MatchType::TID MTShort::type () const { return _tid; }
MatchType::TID MTUnsignedInt::_tid = "unsigned int";
MatchType::TID MTUnsignedInt::type () const { return _tid; }
MatchType::TID MTInt::_tid = "int";
MatchType::TID MTInt::type () const { return _tid; }
MatchType::TID MTWCharT::_tid = "wchar_t";
MatchType::TID MTWCharT::type () const { return _tid; }
MatchType::TID MTUnsignedLong::_tid = "unsigned long";
MatchType::TID MTUnsignedLong::type () const { return _tid; }
MatchType::TID MTLong::_tid = "long";
MatchType::TID MTLong::type () const { return _tid; }
MatchType::TID MTUnsignedLongLong::_tid = "unsigned long long";
MatchType::TID MTUnsignedLongLong::type () const { return _tid; }
MatchType::TID MTLongLong::_tid = "long long";
MatchType::TID MTLongLong::type () const { return _tid; }
MatchType::TID MTFloat::_tid = "float";
MatchType::TID MTFloat::type () const { return _tid; }
MatchType::TID MTDouble::_tid = "double";
MatchType::TID MTDouble::type () const { return _tid; }
MatchType::TID MTLongDouble::_tid = "long double";
MatchType::TID MTLongDouble::type () const { return _tid; }
MatchType::TID MTVoid::_tid = "void";
MatchType::TID MTVoid::type () const { return _tid; }
|