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
|
// 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 "Puma/CRecord.h"
#include "Puma/CFunctionInfo.h"
#include "Puma/CTemplateInfo.h"
#include "Puma/CTemplateInstance.h"
using namespace Puma;
#include "MatchName.h"
#include "MatchType.h"
#include "MatchTemplateArg.h"
void MatchName::MRegComp::setup (const DString &str) {
if (_reg_comp)
delete _reg_comp;
const char *sig = str.c_str();
char *buffer = new char[3 + strlen (sig) * 2];
char *p = buffer;
char ch;
*p++ = '^';
while (*sig) {
ch = *sig++;
switch (ch) {
case '%':
*p++ = '.';
*p++ = '*';
break;
default:
*p++ = ch;
}
}
*p++ = '$';
*p++ = 0;
_reg_comp = new RegComp (buffer);
delete[] buffer;
}
bool MatchName::MRegComp::matches (const DString &name) const {
assert (_reg_comp);
return _reg_comp->match (name.c_str ());
}
// this is used if a match name component is '%' => always true
bool MatchName::MTrue::matches (const DString &name) const {
return true;
}
// efficient string comparison
bool MatchName::MStrComp::matches (const DString &name) const {
return _str == name;
}
void MatchName::Name::make_matcher () {
// TODO: make more different Matcher variants (prefix/suffix)
bool only = true; // only '%' characters in the match string
bool none = true; // no '%' character
const char *str = _str.c_str ();
while (*str != '\0') {
if (*str == '%')
none = false;
else
only = false;
str++;
}
if (only)
_matcher = new MTrue;
else if (none)
_matcher = new MStrComp (_str);
else
_matcher = new MRegComp (_str);
}
static CScopeInfo *nscope(CObjectInfo *obj) {
// template instances are stored in a special scope, better take the template
if (obj->TemplateInstance ())
obj = obj->TemplateInstance ()->Template ();
CRecord *crec = obj->ClassScope ();
CScopeInfo *qual = obj->QualifiedScope ();
CScopeInfo *scope = obj->Scope ();
return crec ? (CScopeInfo*)crec : (qual ? qual : (scope->GlobalScope () ? 0 :
scope));
}
MatchName::~MatchName () {
for (int s = 0; s < scopes (); s++)
if (_scope_template_args[s])
delete _scope_template_args[s];
if (_name_template_args)
delete _name_template_args;
}
MatchName &MatchName::operator = (const MatchName ©) {
_scopes = copy._scopes;
for (int i = 0; i < copy.scopes (); i++)
if (copy._scope_template_args[i])
_scope_template_args.push_back (
new MatchTemplateArgList (*copy._scope_template_args[i]));
else
_scope_template_args.push_back (0);
_name = copy._name;
if (copy._name_template_args)
_name_template_args = new MatchTemplateArgList (*copy._name_template_args);
else
_name_template_args = 0;
_oper = copy._oper;
_conv_type = copy._conv_type;
return *this;
}
void MatchName::print (ostream &out) const {
for (int s = 0; s < scopes (); s++) {
out << scope (s);
if (_scope_template_args[s])
out << *_scope_template_args[s];
out << "::";
}
if (!_conv_type.is_undefined ())
out << "operator " << _conv_type;
else
out << name ();
if (_name_template_args)
out << *_name_template_args;
}
bool MatchName::scope_matches (CScopeInfo *scope, int pos) {
assert (scopes () > 0);
// check for last scope
if (pos == -1)
pos = scopes () - 1;
Name &name = _scopes[pos];
MatchTemplateArgList *mtal = _scope_template_args[pos];
pos--;
static DString dots ("...");
if (name.str () == dots) {
// if '...' is the start of the qualified name match expression => match
if (pos == -1)
return true;
// handle ellipses (brute force :-( )
CScopeInfo *curr_scope = scope;
while (curr_scope) {
if (scope_matches (curr_scope, pos))
return true;
curr_scope = nscope (curr_scope);
}
return false;
}
// if the argument scope is the global scope it is a mismatch
if (!scope)
return false;
// check template arguments of the scope
if (mtal) {
CTemplateInstance *instance = scope->TemplateInstance ();
if (!instance || !mtal->matches (instance))
return false;
// the scope is the template, not the instance!
assert (instance->Template ());
scope = instance->Template ();
}
// compare the name pattern
if (!name.matches (scope->Name ()))
return false;
CScopeInfo *next_scope = nscope (scope);
// check the next scope
if (pos >= 0)
return scope_matches (next_scope, pos);
// no scope to check left, 'scope' must be defined in the global scope
if (next_scope)
return false;
// everything ok
return true;
}
bool MatchName::scope_matches (vector<Name> &scopes,
vector<MatchTemplateArgList*> &scope_template_args,
int match_pos, int sig_pos) {
assert (this->scopes () > 0 && this->scopes () > match_pos);
Name &name = _scopes[match_pos];
MatchTemplateArgList *mtal = _scope_template_args[match_pos];
match_pos--;
static DString dots ("...");
if (name.str () == dots) {
// if '...' is the start of the qualified name match expression => match
if (match_pos == -1)
return true;
// handle ellipses (brute force :-( )
while (sig_pos >= 0) {
if (scope_matches (scopes, scope_template_args, match_pos, sig_pos))
return true;
sig_pos--;
}
return false;
}
// if the argument scope is the global scope it is a mismatch
if (sig_pos == -1)
return false;
// check template arguments of the scope
if (mtal) {
MatchTemplateArgList *sig_mtal = scope_template_args[sig_pos];
if (!sig_mtal || !mtal->matches(*sig_mtal))
return false;
}
// compare the name pattern
if (!name.matches (scopes[sig_pos].str ()))
return false;
sig_pos--;
// check the next scope
if (match_pos >= 0)
return scope_matches (scopes, scope_template_args, match_pos, sig_pos);
// no scope to check left, 'scope' must be defined in the global scope
if (sig_pos != -1)
return false;
// everything ok
return true;
}
bool MatchName::oper_matches (CFunctionInfo *obj) {
static DString any ("%");
// '%' as a function name also matches operators
if (!_name.str ().empty ())
return _name.str () == any;
// only operator match expressions are considered
if (_oper == OP_UNDEFINED)
return false;
// any operator
if (_oper == OP_ANY)
return true;
// check the operator name
static DString obstr[] = {
DString ("operator +"), DString ("operator -"), DString ("operator *"),
DString ("operator /"), DString ("operator %"), DString ("operator ^"),
DString ("operator &"), DString ("operator |"), DString ("operator ~"),
DString ("operator !"), DString ("operator <"), DString ("operator >"),
DString ("operator =="), DString ("operator >="), DString ("operator <="),
DString ("operator !="), DString ("operator &&"), DString ("operator ||"),
DString ("operator <<"), DString ("operator >>"), DString ("operator --"),
DString ("operator ++"), DString ("operator ="), DString ("operator ,"),
DString ("operator +="), DString ("operator -="), DString ("operator *="),
DString ("operator /="), DString ("operator %="),
DString ("operator &="), DString ("operator |="), DString ("operator <<="),
DString ("operator >>="), DString ("operator ^="),
DString ("operator ->*"), DString ("operator ->"), DString ("operator new"),
DString ("operator delete"), DString ("operator new[]"),
DString ("operator delete[]"), DString ("operator ()"),
DString ("operator []")
};
return obj->Name () == obstr[((int)_oper) - 1];
}
bool MatchName::oper_matches (Operator oper) {
static DString any ("%");
// '%' as a function name also matches operators
if (!_name.str ().empty ())
return _name.str () == any;
// only operator signatures are considered
if (_oper == OP_UNDEFINED)
return false;
// any operator
if (_oper == OP_ANY)
return true;
// return true, iff the operator is the same
return _oper == oper;
}
bool MatchName::conv_matches (CFunctionInfo *obj) {
static DString any ("%");
// '%' as a function name also matches operators
if (!_name.str ().empty ())
return _name.str () == any;
// any operator is ok
if (_oper == OP_ANY)
return true;
// check the conversion type of the function (if it is a conversion function)
CTypeInfo *ct = obj->ConversionType ();
return ct && !_conv_type.is_undefined () && _conv_type.matches (ct);
}
bool MatchName::conv_matches (MatchTypeRef type) {
static DString any ("%");
// '%' as a function name also matches operators
if (!_name.str ().empty ())
return _name.str () == any;
// any operator is ok
if (_oper == OP_ANY)
return true;
// check the conversion type of the function (if it is a conversion function)
return !type.is_undefined () && !_conv_type.is_undefined () &&
_conv_type.matches (type);
}
bool MatchName::name_matches (CObjectInfo *obj) {
if (_name_template_args) {
CTemplateInstance *instance = obj->TemplateInstance ();
if (!instance || !_name_template_args->matches (instance))
return false;
}
return _name.matches (obj->Name ());
}
bool MatchName::name_matches (Name &name,
MatchTemplateArgList *name_template_args) {
if (_name_template_args) {
if (!name_template_args ||
!_name_template_args->matches (*name_template_args))
return false;
}
return _name.matches (name.str ());
}
bool MatchName::matches (CObjectInfo *obj) {
// if the match name contains a scope check it first
CScopeInfo *obj_scope = nscope (obj);
if (scopes () == 0 && obj_scope)
return false;
if (scopes () > 0 && !scope_matches (obj_scope))
return false;
CFunctionInfo *func = obj->FunctionInfo ();
if (func && func->isOperator ()) {
// special treatment for operators
return oper_matches (func);
}
else if (func && func->isConversion ()) {
// special treatment for conversion functions
return conv_matches (func);
}
else if (_oper == OP_UNDEFINED && _conv_type.is_undefined ()) {
// normal name
return name_matches (obj);
}
else
return false;
}
bool MatchName::matches (MatchName &matched_name) {
// if the matched name is not defined in the global scope and the match
// name has no scopes, it is no match
if (scopes () == 0 && matched_name.scopes () > 0)
return false;
// if the match name has a scope, compare it
if (scopes () > 0 && !scope_matches (matched_name._scopes,
matched_name._scope_template_args,
scopes () - 1, matched_name.scopes () - 1))
return false;
if (matched_name.oper () != OP_UNDEFINED) {
// this an operator name match expression
return oper_matches (matched_name.oper ());
}
else if (!matched_name.conv_type ().is_undefined ()) {
// this is a conversion operator match expression
return conv_matches (matched_name.conv_type ());
}
else if (_oper == OP_UNDEFINED && _conv_type.is_undefined ()) {
// normal name
return name_matches (matched_name._name, matched_name._name_template_args);
}
else
return false;
}
// check whether the name will match only a single entity
bool MatchName::is_trivial () const {
// the name should not contain any wildcards
if (strstr (_name.str ().c_str(), "%") != 0 ||
strstr (_name.str ().c_str(), "...") != 0)
return false;
// the name should not have template parameters, because
// this could be type pattern
if (_name_template_args)
return false;
// now check all scopes for the same conditions
for (int i = 0; i < scopes (); i++) {
const char *scope_name = scope (i).c_str();
if (strstr (scope_name, "%") != 0 ||
strstr (scope_name, "...") != 0)
return false;
if (_scope_template_args[i])
return false;
}
return true;
}
|