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 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
|
// This file is part of PUMA.
// Copyright (C) 1999-2003 The PUMA developer team.
//
// 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 <iostream>
using std::cout;
using std::endl;
#include <set>
using std::set;
#include "Puma/CTree.h"
#include "Puma/CObjectInfo.h"
#include "Puma/CFileInfo.h"
#include "Puma/CEnumInfo.h"
#include "Puma/ErrorStream.h"
#include "Puma/TokenStream.h"
#include "Puma/TokenProvider.h"
#include "Puma/CCSyntax.h"
#include "Puma/CCParser.h"
#include "Puma/PreprocessorParser.h"
#include "Puma/PreMacroManager.h"
#include "Puma/CPrintVisitor.h"
#include "Introducer.h"
#include "ACUnit.h"
#include "Plan.h"
#include "CodeWeaver.h"
#include "LineDirectiveMgr.h"
#include "IncludeGraph.h"
#include "Naming.h"
#include "AspectInfo.h"
#include "OrderInfo.h"
#include "IntroductionInfo.h"
#include "ModelBuilder.h"
#ifdef ACMODEL
#include "Utils.h"
#else
#include "JoinPointLoc.h"
#endif
#include "PointCutContext.h"
#include "PointCutExpr.h"
#include "IntroductionUnit.h"
#include "TransformInfo.h"
// Destructor: release all allocated resources
Introducer::~Introducer () {
for (list<TokenProvider*>::iterator i = _token_providers.begin ();
i != _token_providers.end (); ++i)
delete *i;
for (list<TokenStream*>::iterator i = _token_streams.begin ();
i != _token_streams.end (); ++i)
delete *i;
}
// called when a new class/union/struct/aspect is created, current scope
// is the global scope
void Introducer::class_before (CT_ClassDef *cd) {
enter ();
assert (cd);
CClassInfo *ci = (CClassInfo*)cd->Object ()->DefObject ();
CProject &project = *ci->SemDB ()->Project ();
ErrorStream &err = project.err ();
const Unit *primary = ci->SourceInfo ()->FileInfo ()->Primary ();
// create the weaving plan for this class
JPP_Class *jp_plan = create_plan (ci);
if (!jp_plan) {
// this class is not the target of an introduction
leave ();
return;
}
// determine the units that should be included in front of the class
set<const Unit*> units;
for (int i = 0; i < jp_plan->otherIntros (); i++) {
#ifdef ACMODEL
ACM_ClassSlice *cs = jp_plan->otherIntro (i)->get_slice ();
if (cs->get_kind () == CST_NORMAL ||
(cs->get_kind () == CST_DEP_NORMAL &&
cs->get_prot () != SP_UNKNOWN)) {
units.insert (&TI_ClassSlice::of (*cs)->slice_unit ());
}
#else
JPL_ClassSlice *cs = jp_plan->otherIntro (i)->introduced ();
if (cs->slice_type () == JPL_ClassSlice::CS_NORMAL ||
(cs->slice_type () == JPL_ClassSlice::CS_OLD_OTHER &&
cs->prot () != CProtection::PROT_NONE)) {
units.insert (&TI_ClassSlice::of (*cs)->slice_unit ());
}
#endif
}
for (int i = 0; i < jp_plan->baseIntros (); i++) {
#ifdef ACMODEL
ACM_Introduction *ii = jp_plan->baseIntro (i);
units.insert (&TI_ClassSlice::of (*ii->get_slice ())->slice_unit ());
#else
JPL_Introduction *ii = jp_plan->baseIntro (i);
units.insert (&TI_ClassSlice::of (*ii->introduced ())->slice_unit ());
#endif
}
const Unit *this_unit = (Unit*)cd->token ()->belonging_to ();
// TODO: this_unit might be a macro unit!
// cout << "included units for " << ci->QualName () << " in "
// << this_unit->name () << " :" << endl;
for (set<const Unit*>::iterator iter = units.begin ();
iter != units.end (); ++iter) {
const Unit *slice_unit = *iter;
if (slice_unit == primary) {
if (this_unit != primary)
err << sev_error << cd->token ()->location ()
<< "affected by aspect in '" << slice_unit->name ()
<< "'. Move the aspect into a separate aspect header." << endMessage;
}
else if (_ig.includes (slice_unit, this_unit)) {
err << sev_warning << cd->token ()->location ()
<< "can't include '" << slice_unit->name ()
<< "' to avoid include cycle" << endMessage;
}
else {
// cout << "new edge from " << this_unit->name () << " to "
// << slice_unit->name () << endl;
_ig.add_edge (this_unit, slice_unit);
// handling of nested classes -> search the outermost class
CClassInfo *inscls = ci;
while (inscls->Parent ()->ClassInfo ())
inscls = inscls->Parent ()->ClassInfo ();
// namespace should be closed and re-opened
ostringstream includes;
_code_weaver.close_namespace (includes, inscls);
// inspos should be infront of the class
Token *inspos = (Token*)inscls->Tree ()->token ();
Filename incname = project.getInclString (slice_unit->name ());
includes << endl << "#ifndef ";
Naming::guard (includes, (FileUnit*)slice_unit);
includes << endl << "#define ";
Naming::guard (includes, (FileUnit*)slice_unit);
includes << endl;
includes << "#include \"" << incname << "\"" << endl;
includes << "#endif" << endl;
// re-open the namespace
_code_weaver.open_namespace (includes, inscls);
string inc (includes.str ());
_code_weaver.paste (_code_weaver.weave_pos (inspos, WeavePos::WP_BEFORE),
inc);
if (_included_aspect_headers.find (slice_unit) ==
_included_aspect_headers.end ()) {
_included_aspect_headers.insert (slice_unit);
// prepare a new C preprocessor
TokenStream stream; // linearize tokens from several files
stream.push ((Unit*)slice_unit);
PreprocessorParser cpp (&project.err (), &project.unitManager (),
_cpp->locals ());
PreMacroManager *old_mm = cpp.macroManager ();
cpp.macroManager (_cpp->macroManager ());
cpp.stream (&stream);
cpp.configure (project.config ());
TokenProvider provider (cpp); // look ahead token buffer
CTree *tree = _parser.syntax ().run (provider, &CCSyntax::trans_unit);
// TODO: tree should later be freed!
_ah_trees.push_back (tree);
// print all error messages
_parser.builder ().errors (err);
// cout << "--------" << endl;
// CPrintVisitor printer;
// cout << "Printing syntax tree..." << endl;
// printer.print (tree, cout);
cpp.macroManager (old_mm);
}
// else
// cout << "'" << slice_unit->name () << "' not included again!" << endl;
}
}
// cd->add_intro (tree);
leave ();
}
// called when a new class/union/struct/aspect is created
void Introducer::class_begin (CT_ClassDef *cd) {
enter ();
assert (cd);
leave ();
}
// parse code that shall be introduced
CTree *Introducer::parse (list<Unit*> units, CTree *(CCSyntax::*rule)(),
const char *expected_id, ErrorStream &err) {
// only parse if there is something to parse
if (units.size () == 0)
return 0;
// create a token stream and push all units onto it
TokenStream *stream = new TokenStream;
_token_streams.push_back (stream);
for (list<Unit*>::reverse_iterator ui = units.rbegin ();
ui != units.rend (); ++ui)
stream->push (*ui);
// parse it
TokenProvider *provider = new TokenProvider (*stream); // look ahead buffer
_token_providers.push_back (provider);
CTree *tree = _parser.syntax ().run (*provider, rule);
// print all error messages => does that really work?
_parser.builder ().errors (err);
// handle errors
if (!tree || tree->NodeName () != expected_id) {
// TODO: say more here
err << sev_error << "introduction cannot be parsed" << endMessage;
}
return tree;
}
// called when a new class/union/struct/aspect definition ends
// (still in the class scope)
void Introducer::class_end (CT_ClassDef *cd) {
enter ();
assert (cd);
CClassInfo *ci = (CClassInfo*)cd->Object ()->DefObject ();
ErrorStream &err = ci->SemDB ()->Project ()->err ();
// manipulate the code (will be effective after commit),
// paste declaration before "}" of the class definition
Token *inspos = cd->Members ()->end_token ();
const WeavePos &pos = _code_weaver.weave_pos (inspos, WeavePos::WP_BEFORE);
// first check with the plan if there are intros for this class
JPP_Class *jp_plan = plan_lookup (ci);
if (!jp_plan) {
insert_introspection_code (cd);
leave ();
return;
}
// collect the intros
list<Unit*> units;
LineDirectiveMgr &lmgr = _code_weaver.line_directive_mgr ();
jp_plan->gen_intros (units, err, ci, lmgr);
// parse the intros
CTree *tree = parse (units, &CCSyntax::member_spec, CT_MembList::NodeId (), err);
if (tree) {
// move the introduced members into the normal member list
CT_MembList *memb_list = (CT_MembList*)tree;
for (int e = 0; e < memb_list->Entries (); e++) {
CTree *entry = memb_list->Entry (e);
cd->Members ()->InsertSon (cd->Members ()->Sons () - 1, entry);
}
delete memb_list;
// paste the generated code and add the units to the manipulator's unit mgr.
for (list<Unit*>::iterator ui = units.begin ();
ui != units.end (); ++ui)
_code_weaver.paste (pos, *ui);
}
else {
// delete all intro units
for (list<Unit*>::iterator ui = units.begin (); ui != units.end (); ++ui)
delete *ui;
}
insert_introspection_code (cd);
leave ();
}
// insert introspection code
// * at the end of class definitions, after AspectC++ introductions
void Introducer::insert_introspection_code (CT_ClassDef *cd) {
if (!_conf.introspection())
return;
CClassInfo *ci = (CClassInfo*)cd->Object ()->DefObject ();
ErrorStream &err = ci->SemDB ()->Project ()->err ();
// return if this class is not an introduction target
if (!_jpm.is_valid_model_class (ci) || !_jpm.is_intro_target (ci))
return;
// manipulate the code (will be effective after commit),
// paste declaration before "}" of the class definition
Token *inspos = cd->Members ()->end_token ();
const WeavePos &pos = _code_weaver.weave_pos (inspos, WeavePos::WP_BEFORE);
list<Unit*> units;
ACUnit *unit = new ACUnit (err);
unit->name ("<typeinfo>");
*unit << "public:" << endl;
// generate __AttrTypes => a list with all attribute types
*unit << " typedef ";
unsigned e = 0;
for (unsigned a = 0; a < ci->Attributes (); a++) {
if (!is_attribute (ci->Attribute (a)))
continue;
*unit << "AC::TL< ";
// TODO: Better check if there is really an ambiguity - not only for "stat"
// if (ci->Attribute (a)->TypeInfo ()->Record () &&
// strcmp (ci->Attribute (a)->TypeInfo ()->Record ()->Name (), "stat") == 0)
// *unit << "struct ";
ci->Attribute (a)->TypeInfo ()->TypeText (*unit, "", true, true, true);
*unit << ",";
e++;
}
*unit << "AC::TLE ";
while (e > 0) {
*unit << " > ";
e--;
}
*unit << "__AttrTypes;" << endl;
// generate __attr_name => the name of attribute i
*unit << " const char *__attr_name (unsigned i) const {";
if (ci->Attributes () == 0) {
*unit << " return 0; }";
}
else {
*unit << endl << " static const char *names[] = { ";
bool first = true;
for (unsigned a = 0; a < ci->Attributes (); a++) {
if (!is_attribute (ci->Attribute (a)))
continue;
if (!first) *unit << ", ";
*unit << "\"" << ci->Attribute (a)->Name () << "\"";
first = false;
}
*unit << " }; return names[i];" << endl
<< " }";
}
*unit << endl;
// generate __attr => the untyped pointer to attribute i
*unit << " void *__attr (unsigned __i) const {";
if (ci->Attributes () == 0) {
*unit << " return 0; }";
}
else {
*unit << endl
<< " switch (__i) { ";
for (unsigned a = 0; a < ci->Attributes (); a++) {
if (!is_attribute (ci->Attribute (a)))
continue;
*unit << "case " << a << ": return (void*)&" << ci->Attribute (a)->Name () << "; ";
}
*unit << "default: return 0; }" << endl
<< " }";
}
*unit << endu;
// if (ci->Name()) cout << "Class: " << ci->Name() << endl;
// cout << "Unit:" << endl << *unit << endl;
units.push_back (unit);
CTree *tree = parse (units, &CCSyntax::member_spec, CT_MembList::NodeId (), err);
if (tree) {
// move the introduced members into the normal member list
CT_MembList *memb_list = (CT_MembList*)tree;
for (int e = 0; e < memb_list->Entries (); e++) {
CTree *entry = memb_list->Entry (e);
cd->Members ()->InsertSon (cd->Members ()->Sons () - 1, entry);
}
_code_weaver.paste (pos, unit);
delete memb_list;
}
else {
delete unit;
}
// paste a #line directive
LineDirectiveMgr &lmgr = _code_weaver.line_directive_mgr ();
ACUnit *dunit = new ACUnit (err);
lmgr.directive (*dunit, (Unit*)inspos->belonging_to (), inspos);
*dunit << endu;
if (dunit->empty ()) delete dunit; else _code_weaver.paste (pos, dunit);
}
// checks if an attribute that us returned by the parser is an attribute
// in the sense of the AspectC++ introspection mechnism
bool Introducer::is_attribute (CAttributeInfo *obj) {
if (obj->isAnonymous () ||
obj->isStatic () ||
obj->EnumeratorInfo () ||
// TODO: temporary hack - attributes that have an anonymous type
// shall no be ignored in the type list!
(obj->TypeInfo () &&
(obj->TypeInfo ()->isBitField() ||
(obj->TypeInfo ()->Record () && obj->TypeInfo ()->Record ()->isAnonymous ()) ||
(obj->TypeInfo ()->EnumInfo () && obj->TypeInfo ()->EnumInfo ()->isAnonymous ()))))
return false;
return true;
}
// called after the parser tried to parse a base clause
void Introducer::base_clause_end (CT_ClassDef *cd, Token *open) {
enter ();
assert (cd);
CClassInfo *ci = (CClassInfo*)cd->Object ()->DefObject ();
ErrorStream &err = ci->SemDB ()->Project ()->err ();
// first check with the plan if there are intros for this class
JPP_Class *jp_plan = plan_lookup (ci);
if (!jp_plan) {
leave ();
return;
}
// create a unit with the code that shall be introduced
LineDirectiveMgr &lmgr = _code_weaver.line_directive_mgr ();
// collect the intros
list<Unit*> units;
jp_plan->gen_base_intros (units, err, ci, lmgr);
// parse the introduced code as a base clause
CTree *tree = parse (units, &CCSyntax::base_clause, CT_BaseSpecList::NodeId (),
err);
if (tree) {
cd->BaseIntros (tree);
// manipulate the code (will be effective after commit),
// paste declaration before "{" of the class definition
Token *inspos = open;
const WeavePos &pos = _code_weaver.weave_pos (inspos, WeavePos::WP_BEFORE);
// paste the generated code and add the unit to the manipulator's unit mgr.
if (cd->BaseClasses () != 0) {
// get the first introduced base class unit
Unit *first_unit = units.front ();
// delete the ":" token at the beginning of the unit
first_unit->remove ((ListElement*)first_unit->first ());
// insert "," instead
ACUnit comma (err);
comma << "," << endu;
first_unit->move_before ((ListElement*)first_unit->first (), comma);
}
// now paste all units
for (list<Unit*>::iterator ui = units.begin ();
ui != units.end (); ++ui)
_code_weaver.paste (pos, *ui);
// paste a #line directive
ACUnit *dunit = new ACUnit (err);
lmgr.directive (*dunit, (Unit*)inspos->belonging_to (), inspos);
*dunit << endu;
if (dunit->empty ()) delete dunit; else _code_weaver.paste (pos, dunit);
}
else {
// delete all base into units
for (list<Unit*>::iterator ui = units.begin (); ui != units.end (); ++ui)
delete *ui;
}
leave ();
}
// called after the program has been parsed completely
void Introducer::trans_unit_end (CT_Program *pr) {
// ignore this translation unit, if it is a nested parser run
if (_intro_level != 0)
return;
enter ();
CFileInfo *fi = pr->Scope ()->FileInfo ();
assert (fi);
CSemDatabase &db = *fi->SemDB ();
CProject &project = *db.Project ();
ErrorStream &err = project.err ();
const Unit *primary = fi->Primary ();
LineDirectiveMgr &lmgr = _code_weaver.line_directive_mgr ();
// ... some unit for formatting
ACUnit ws (err); ws << " " << endu;
// loop until there are no more entries in the target map
while (!_targets.empty ()) {
// get the information from the first entry and delete it
TargetMap::iterator i = _targets.begin ();
CClassInfo *ci = i->first;
#ifdef ACMODEL
ACM_Class &jp_loc = *i->second;
#else
JPL_Class &jp_loc = *i->second;
#endif
JPP_Class &jp_plan = *(JPP_Class*)jp_loc.plan();
_targets.erase (i);
// create units with the code that shall be introduced
list<Unit*> intros;
jp_plan.gen_intros (intros, err, ci, lmgr, true);
// ignore this class if there are no non-inline intros for it
if (intros.size () == 0)
continue;
CSourceInfo *si = ci->SourceInfo ();
bool in_header = (strcmp (si->FileName (),
si->FileInfo ()->Primary ()->name ()) != 0);
if (in_header) {
// check if there is a link-once code element in the class
CObjectInfo *loo = link_once_object (ci);
// TODO: for now loo has to != 0, later we can exploit the proj. repo
if (!loo) {
err << sev_warning << TransformInfo::location (jp_loc)
<< "cannot introduce non-inline function or static attribute"
#ifdef ACMODEL
<< " into \"class " << signature (jp_loc).c_str()
#else
<< " into \"class " << jp_loc.signature ()
#endif
<< "\". It has to contain link-once code." << endMessage;
for (list<Unit*>::iterator i = intros.begin (); i != intros.end (); ++i)
delete *i;
continue;
}
// continue silently if this is only a declaration
if (loo->Scope () == ci) {
for (list<Unit*>::iterator i = intros.begin (); i != intros.end (); ++i)
delete *i;
continue;
}
}
// parse the introduced code
CTree *tree = parse (intros, &CCSyntax::decl_seq,
Builder::Container::NodeId (), err);
if (!tree) {
for (list<Unit*>::iterator i = intros.begin (); i != intros.end (); ++i)
delete *i;
leave ();
return;
}
// move the introduced members into the normal program node
Builder::Container *decls = (Builder::Container*)tree;
for (int e = 0; e < decls->Entries (); e++) {
CTree *entry = decls->Entry (e);
pr->AddSon (entry);
}
delete decls;
// paste the generated code and add the unit to the manipulator's unit mgr.
for (list<Unit*>::iterator i = intros.begin (); i != intros.end (); ++i)
_code_weaver.paste_end (*i);
// determine the units that should be included in front of the intros
set<const Unit*> units;
for (int i = 0; i < jp_plan.otherIntros (); i++) {
#ifdef ACMODEL
ACM_ClassSlice *cs = jp_plan.otherIntro (i)->get_slice ();
if (cs->get_kind () == CST_NORMAL) {
#else
JPL_ClassSlice *cs = jp_plan.otherIntro (i)->introduced ();
if (cs->slice_type () == JPL_ClassSlice::CS_NORMAL) {
#endif
CObjectInfo *obj = TI_ClassSlice::of (*cs)->assoc_obj ();
assert (obj && obj->Tree ());
assert (obj->Tree ()->NodeName () == CT_ClassSliceDecl::NodeId ());
ACSliceInfo *acsi = obj->SemDB ()->SliceInfo (obj)->definition ();
for (int m = 0; m < acsi->members (); m++)
units.insert (acsi->member_unit (m));
}
#ifdef ACMODEL
else if (cs->get_kind () == CST_DEP_NORMAL &&
cs->get_prot () == SP_UNKNOWN) {
#else
else if (cs->slice_type () == JPL_ClassSlice::CS_OLD_OTHER &&
cs->prot () == CProtection::PROT_NONE) {
#endif
units.insert (&TI_ClassSlice::of (*cs)->slice_unit ());
}
}
// parse the aspect headers that are needed by this intro
for (set<const Unit*>::iterator iter = units.begin ();
iter != units.end (); ++iter) {
const Unit *slice_unit = *iter;
if (slice_unit != primary) {
// cout << "new edge from " << primary->name () << " to "
// << slice_unit->name () << endl;
_ig.add_edge (primary, slice_unit);
// generate a unit with the include
ostringstream includes;
Filename incname = project.getInclString (slice_unit->name ());
includes << endl << "#ifndef ";
Naming::guard (includes, (FileUnit*)slice_unit);
includes << endl << "#define ";
Naming::guard (includes, (FileUnit*)slice_unit);
includes << endl;
includes << "#include \"" << incname << "\"" << endl;
includes << "#endif" << endl;
string inc (includes.str ());
_code_weaver.paste_end (inc);
if (_included_aspect_headers.find (slice_unit) ==
_included_aspect_headers.end ()) {
_included_aspect_headers.insert (slice_unit);
// prepare a new C preprocessor
TokenStream stream; // linearize tokens from several files
stream.push ((Unit*)slice_unit);
PreprocessorParser cpp (&project.err (), &project.unitManager (),
_cpp->locals ());
PreMacroManager *old_mm = cpp.macroManager ();
cpp.macroManager (_cpp->macroManager ());
cpp.stream (&stream);
cpp.configure (project.config ());
TokenProvider provider (cpp); // look ahead token buffer
CTree *tree = _parser.syntax ().run (provider, &CCSyntax::trans_unit);
// TODO: tree should later be freed!
_ah_trees.push_back (tree);
// print all error messages
_parser.builder ().errors (err);
cpp.macroManager (old_mm);
}
// else
// cout << "'" << slice_unit->name () << "' not included again!" << endl;
}
}
}
leave ();
}
// manage the intro nesting level and the _cpp pointer
void Introducer::enter () {
if (_intro_level == 0)
_cpp = &(PreprocessorParser&)_parser.syntax ().provider ()->source ();
_intro_level++;
}
void Introducer::leave () {
_intro_level--;
if (_intro_level == 0)
_cpp = 0;
}
// create the weaving plan for a given class
JPP_Class *Introducer::create_plan (CClassInfo *ci) {
// return if this class is not an introduction target
if (!_jpm.is_intro_target (ci->DefObject ()))
return 0;
// try to register this class (might be a newly introduced class)
#ifdef ACMODEL
ACM_Class *jpl = 0;
ACAspectInfo *ai = ci->SemDB()->AspectInfo (ci->DefObject ());
if (ai)
jpl = _jpm.register_aspect (ai);
else
jpl = _jpm.register_class (ci);
#else
JPL_Class *jpl = _jpm.register_class (ci);
#endif
// return if this is either not a valid model class or no valid intro target
#ifdef ACMODEL
if (!jpl || !jpl->get_intro_target ())
#else
if (!jpl || !jpl->intro_target ())
#endif
return 0;
// Plan object pointer
JPP_Class *jpp = 0;
// iterate through all introduction advice in the plan
PointCutContext context (_jpm);
const list<IntroductionInfo*> &intros = _plan.introduction_infos ();
for (list<IntroductionInfo*>::const_iterator i = intros.begin ();
i != intros.end (); ++i) {
IntroductionInfo *intro = *i;
// TODO: consider stand-alone advice here as well in the future (C-mode)
// something like ... if (!intro->is_activated ()) continue;
context.concrete_aspect (intro->aspect ());
Binding binding; // binding and condition not used for intros
Condition condition;
if (intro->expr ()->evaluate (*jpl, context, binding, condition)) {
jpp = _plan.consider (jpl, &intro->intro ());
}
}
if (jpp) {
// ordering
_plan.order (jpl);
// check the plan
_plan.check (jpl);
// remember the class info and join point location
_targets.insert (TargetMap::value_type (ci, jpl));
}
return jpp;
}
JPP_Class *Introducer::plan_lookup (CClassInfo *ci) {
TargetMap::iterator i = _targets.find (ci);
if (i != _targets.end ())
return (JPP_Class*)i->second->plan ();
return 0;
}
CObjectInfo *Introducer::link_once_object (CClassInfo *ci) {
for (unsigned i = 0; i < ci->Functions (); i++) {
CFunctionInfo *fi = ci->Function (i)->DefObject ();
// skip template functions and built-in functions
// they don't need link-once code
if (fi->isBuiltin () || fi->isTemplate () || is_intro (fi)) {
continue;
}
// if a member function is undefined it is link-once code!
if (!fi->isDefined ()) {
return fi;
}
// if the function is defined, outside the class scope, and is not inline,
// we found the implementation
if (fi->Scope () != ci && !fi->isInline ()) {
return fi;
}
}
for (unsigned i = 0; i < ci->Attributes (); i++) {
CAttributeInfo *ai = ci->Attribute (i)->DefObject ();
// ignore introduced attributes
if (is_intro (ai))
continue;
// if the scope is outside the class, we or definition
if (ai->Scope () != ci) {
return ai;
}
// initialized, we can us this object
if (ai->isStatic () && !ai->Init ()) {
return ai;
}
}
return 0;
}
bool Introducer::is_intro (CObjectInfo *obj) {
Unit *unit = obj->SourceInfo ()->SrcUnit ();
return (IntroductionUnit::cast (unit) != 0);
}
|