00001 #include "stdafx.h"
00002 #include "CodeGen.h"
00003 #include "Dumper.h"
00004 #include "MakeVisitor.h"
00005
00006
00007 int h_ind = 1;
00008 int s_ind = 0;
00009
00010 std::string CodeGen::indent( int tabs )
00011 {
00012 return std::string( (unsigned) tabs, '\t');
00013 }
00014
00015
00016 std::string CodeGen::fill( int fill )
00017 {
00018 return std::string( (unsigned) fill, ' ');
00019 }
00020
00021
00026 void CodeGen::dumpFoldGetter( FolderRep* sub, FolderRep *cont)
00027 {
00028 std::string sub_nmsp( sub->getValidNmspc()), fol_nmsp( cont->getValidNmspc()), diff_nmsp( "");
00029 bool is_diff_nmsp = sub_nmsp != fol_nmsp;
00030 if( is_diff_nmsp) diff_nmsp = sub_nmsp;
00031
00032 std::string method_name = FolderRep::subFolderGetterMethodName( sub, diff_nmsp);
00033 std::string retval_folderkind( sub->getLValidName()), folderkind( sub->getLStrictName());
00034
00035 if ( !sub->isToBeEx())
00036 retval_folderkind = "BON::Folder";
00037
00038 if ( Dumper::m_bGenRegular)
00039 {
00040 Method m;
00041
00042 folderGetter( retval_folderkind, folderkind, method_name, sub->isToBeEx(), cont, m);
00043
00044 cont->addMethod( m);
00045 }
00046
00047
00048 if ( Dumper::m_bGenTemplates)
00049 {
00050 Method m;
00051
00052 folderGetterGeneric( retval_folderkind, folderkind, method_name, sub->isToBeEx(), cont, m);
00053
00054 cont->addMethod( m);
00055 }
00056 }
00057
00058
00059 void CodeGen::dumpKindGetter( FCO* fco, FolderRep * cont)
00060 {
00061 std::string fco_nmsp( fco->getValidNmspc()), fol_nmsp( cont->getValidNmspc()), diff_nmsp( "");
00062 bool is_diff_nmsp = fco_nmsp != fol_nmsp;
00063 if( is_diff_nmsp) diff_nmsp = fco_nmsp;
00064
00065 std::string method_name = FolderRep::kindGetterMethodName( fco, diff_nmsp);
00066
00067 std::string retval_kind( fco->getLValidName()), kind( fco->getLStrictName());
00068
00069
00070 if (!fco->isToBeEx())
00071 retval_kind = "BON::FCO";
00072
00073 std::string src, comm;
00074
00075 std::vector<FCO*> r;
00076 fco->getIntDescendants( r);
00077 if ( r.empty())
00078 {
00079 if (!fco->isAbstract())
00080 {
00081 if ( Dumper::m_bGenRegular)
00082 {
00083 Method m;
00084
00085 kindGetter1( retval_kind, kind, method_name, cont, m);
00086
00087 cont->addMethod( m);
00088 }
00089
00090
00091 if ( Dumper::m_bGenTemplates)
00092 {
00093 Method m;
00094
00095 kindGetter1Generic( retval_kind, kind, method_name, cont, m);
00096
00097 cont->addMethod( m);
00098 }
00099 }
00100 }
00101 else
00102 {
00103 r.push_back( fco);
00104
00105 std::vector< std::string > kind_vec;
00106
00107 std::vector<FCO*>::iterator it = r.begin();
00108 for( ; it != r.end(); ++it)
00109 {
00110 if (!(*it)->isAbstract())
00111 {
00112 kind_vec.push_back( (*it)->getLStrictName());
00113 }
00114 }
00115
00116 if ( !kind_vec.empty())
00117 {
00118 if ( Dumper::m_bGenRegular)
00119 {
00120 Method m;
00121
00122 kindGetter2( retval_kind, kind, kind_vec, method_name, cont, m);
00123
00124 cont->addMethod( m);
00125 }
00126
00127
00128 if ( Dumper::m_bGenTemplates)
00129 {
00130 Method m;
00131
00132 kindGetter2Generic( retval_kind, kind, kind_vec, method_name, cont, m);
00133
00134 cont->addMethod( m);
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 }
00158 }
00159
00160
00161
00166 void CodeGen::dumpRoleGetter( FCO* fco, RoleRep * role, ModelRep * cont)
00167 {
00168
00169 std::string method_name;
00170
00171 std::string src, comm;
00172
00173 std::string fco_nmsp( fco->getValidNmspc()), mod_nmsp( cont->getValidNmspc()), diff_nmsp( "");
00174 bool is_diff_nmsp = fco_nmsp != mod_nmsp;
00175 if( is_diff_nmsp) diff_nmsp = fco_nmsp;
00176
00177 std::string l_fco_name = fco->getLName();
00178
00179 std::vector<FCO*> desc;
00180 fco->getIntDescendants( desc);
00181 if ( desc.empty())
00182 {
00183 if ( !fco->isAbstract())
00184 {
00185 method_name = ModelRep::roleGetterMethodName2( fco, role, false, diff_nmsp );
00186 std::string inquire = fco->getLStrictNmspc() + role->getSmartRoleName();
00187 std::string retval_kind = fco->getLValidName();
00188 if ( !fco->isToBeEx())
00189 {
00190 FCO * ext_anc = fco->getExtedAnc();
00191 if ( ext_anc)
00192 retval_kind = ext_anc->getLValidName();
00193 else
00194 retval_kind = "BON::" + Any::KIND_TYPE_STR[fco->getMyKind()];
00195 }
00196
00197 if ( Dumper::m_bGenRegular)
00198 {
00199 Method m;
00200
00201 roleGetter1( retval_kind, inquire, method_name, l_fco_name, cont, m);
00202
00203 cont->addMethod( m);
00204 }
00205
00206
00207 if ( Dumper::m_bGenTemplates)
00208 {
00209 Method tm;
00210
00211 roleGetter1Generic( retval_kind, inquire, method_name, l_fco_name, cont, tm);
00212
00213 cont->addMethod( tm);
00214 }
00215 }
00216 else { }
00217 }
00218 else
00219 {
00220 method_name = ModelRep::roleGetterMethodName2( fco, role, false, diff_nmsp);
00221
00222 desc.push_back( fco);
00223 int count = 0;
00224 std::vector<FCO*>::iterator it = desc.begin();
00225 for( ; it != desc.end(); ++it)
00226 {
00227 if (!(*it)->isAbstract())
00228 ++count;
00229 }
00230
00231
00232
00233
00234
00235 if (count == 0)
00236 {
00237
00238
00239 }
00240 else if ( count == 1)
00241 {
00242 int k;
00243 for( k = 0; k < desc.size() && desc[k]->isAbstract(); ++k) { }
00244 if ( k >= desc.size()) throw("Index out of bound during dumpRoleGetter");
00245
00246 method_name = ModelRep::roleGetterMethodName2( desc[k], role, false, diff_nmsp);
00247
00248 std::string retval_kind = desc[k]->getLValidName();
00249 if( !desc[k]->isToBeEx())
00250 {
00251 FCO * ext_anc = desc[k]->getExtedAnc();
00252 if ( ext_anc)
00253 retval_kind = ext_anc->getLValidName();
00254 else
00255 retval_kind = "BON::" + Any::KIND_TYPE_STR[desc[k]->getMyKind()];
00256 }
00257
00258
00259
00260
00261
00262 std::string inquire = desc[k]->getLStrictNmspc() + (role->getOnlyRoleName().empty()?desc[k]->getName():role->getOnlyRoleName());
00263
00264 if ( Dumper::m_bGenRegular)
00265 {
00266 Method m;
00267
00268 roleGetter2( retval_kind, inquire, method_name, l_fco_name, cont, m);
00269
00270 cont->addMethod( m);
00271 }
00272
00273
00274 if ( Dumper::m_bGenTemplates)
00275 {
00276 Method tm;
00277
00278 roleGetter2Generic( retval_kind, inquire, method_name, l_fco_name, cont, tm);
00279
00280 cont->addMethod( tm);
00281 }
00282 }
00283 else if ( count > 1)
00284 {
00285
00286
00287
00288 method_name = ModelRep::roleGetterMethodName2( fco, role, false, diff_nmsp);
00289
00290 std::string retval_kind = fco->getLValidName();
00291 if ( !fco->isToBeEx())
00292 {
00293 FCO * ext_anc = fco->getExtedAnc();
00294 if ( ext_anc)
00295 retval_kind = ext_anc->getLValidName();
00296 else
00297 retval_kind = "BON::" + Any::KIND_TYPE_STR[fco->getMyKind()];
00298 }
00299
00300 std::string in_case_of_name_conflict_str;
00301
00302
00303
00304 if (role->getOnlyRoleName().empty() && !role->getFCOPtr()->isAbstract())
00305 in_case_of_name_conflict_str = "int dummy";
00306
00307 std::vector< std::string > roles;
00308 for( int k = 0; k < desc.size(); ++k)
00309 {
00310 if ( !desc[k]->isAbstract())
00311 {
00312 roles.push_back( desc[k]->getLStrictNmspc() + desc[k]->getName() + role->getOnlyRoleName());
00313 }
00314 }
00315
00316 if ( Dumper::m_bGenRegular)
00317 {
00318 Method m;
00319
00320 roleGetter3( retval_kind, method_name, l_fco_name, role->getFCOPtr()->getLNmspc() + role->getOnlyRoleName(), roles, in_case_of_name_conflict_str, cont, m);
00321
00322 cont->addMethod( m);
00323 }
00324
00325
00326 if ( Dumper::m_bGenTemplates)
00327 {
00328 Method tm;
00329
00330 roleGetter3Generic( retval_kind, method_name, l_fco_name, role->getFCOPtr()->getLNmspc() + role->getOnlyRoleName(), roles, in_case_of_name_conflict_str, cont, tm);
00331
00332 cont->addMethod( tm);
00333 }
00334
00335
00336
00337
00338 for(int k = 0; k < desc.size(); ++k)
00339 {
00340 if ( !desc[k]->isAbstract())
00341 {
00342 method_name = ModelRep::roleGetterMethodName3( desc[k], role, false, diff_nmsp);
00343
00344
00345 std::string retval_kind = desc[k]->getLValidName();
00346 if ( !desc[k]->isToBeEx())
00347 {
00348 FCO * ext_anc = desc[k]->getExtedAnc();
00349 if ( ext_anc)
00350 retval_kind = ext_anc->getLValidName();
00351 else
00352 retval_kind = "BON::" + Any::KIND_TYPE_STR[ desc[k]->getMyKind()];
00353 }
00354
00355 if ( Dumper::m_bGenRegular)
00356 {
00357 Method m;
00358
00359 roleGetter4( retval_kind, method_name, fco->getName(), desc[k]->getName(), role->getOnlyRoleName(), fco->getLStrictNmspc(), cont, m);
00360
00361 cont->addMethod( m);
00362 }
00363
00364
00365 if ( Dumper::m_bGenTemplates)
00366 {
00367 Method tm;
00368
00369 roleGetter4Generic( retval_kind, method_name, fco->getName(), desc[k]->getName(), role->getOnlyRoleName(), fco->getLStrictNmspc(), cont, tm);
00370
00371 cont->addMethod( tm);
00372 }
00373 }
00374 }
00375 }
00376 }
00377 }
00378
00379
00384 Method CodeGen::dumpRefGetter( ReferenceRep * cont, FCO * fco, const std::string& common_kind)
00385 {
00386 Method m;
00387 std::string mmm, retval_kind;
00388 if (fco)
00389 {
00390 retval_kind = fco->getLValidName();
00391
00392 if ( !fco->isToBeEx())
00393 {
00394 FCO * ext_anc = fco->getExtedAnc();
00395 if ( ext_anc)
00396 retval_kind = ext_anc->getLValidName();
00397 else
00398 retval_kind = "BON::" + Any::KIND_TYPE_STR[fco->getMyKind()];
00399 }
00400 }
00401 else if ( !common_kind.empty())
00402 retval_kind = common_kind;
00403 else
00404 retval_kind = "BON::FCO";
00405
00406 mmm = indent(s_ind + 0) + "{\n";
00407 mmm += indent(s_ind + 1) + "BON::FCO r = BON::ReferenceImpl::getReferred();\n";
00408 mmm += indent(s_ind + 1) + "return " + retval_kind + "(r);\n";
00409 mmm += indent(s_ind + 0) + "}\n\n\n";
00410
00411 m.m_returnValue = retval_kind;
00412 m.m_signature = cont->refGetterTemplate(fco) + "()";
00413 m.m_implementation = mmm;
00414 m.m_container = cont;
00415 m.m_comment = "";
00416
00417 return m;
00418 }
00419
00420 std::string CodeGen::folderKidsTemplate()
00421 {
00422 std::string mmm;
00423 mmm = indent(s_ind + 1) + "// then its children\n";
00424 mmm += indent(s_ind + 1) + "std::set<BON::Folder> subfolders = BON::FolderImpl::getChildFolders();\n";
00425 mmm += indent(s_ind + 1) + "for( std::set<BON::Folder>::const_iterator it = subfolders.begin(); it != subfolders.end(); ++it)\n";
00426 mmm += indent(s_ind + 1) + "{\n";
00427 mmm += indent(s_ind + 2) + "(*it)->accept( pVisitor);\n";
00428 mmm += indent(s_ind + 1) + "}\n\n";
00429
00430 mmm += indent(s_ind + 1) + "std::set<BON::FCO> children = BON::FolderImpl::getRootFCOs();\n";
00431 mmm += indent(s_ind + 1) + "for( std::set<BON::FCO>::const_iterator it = children.begin(); it != children.end(); ++it)\n";
00432 mmm += indent(s_ind + 1) + "{\n";
00433 mmm += indent(s_ind + 2) + "(*it)->accept( pVisitor);\n";
00434 mmm += indent(s_ind + 1) + "}\n";
00435
00436 return mmm;
00437 }
00438
00439 std::string CodeGen::modelKidsTemplate()
00440 {
00441 std::string mmm;
00442 mmm = indent(s_ind + 1) + "// then its children\n";
00443 mmm += indent(s_ind + 1) + "std::set<BON::FCO> children = ModelImpl::getChildFCOs();\n";
00444 mmm += indent(s_ind + 1) + "for( std::set<BON::FCO>::const_iterator it = children.begin(); it != children.end(); ++it)\n";
00445 mmm += indent(s_ind + 1) + "{\n";
00446 mmm += indent(s_ind + 2) + "(*it)->accept( pVisitor);\n";
00447 mmm += indent(s_ind + 1) + "}\n";
00448
00449 return mmm;
00450 }
00451
00452
00453 Method CodeGen::acceptMethod( Any * any, bool pWithTraversalOfKids, bool pSpecialized, bool pRetValBool)
00454 {
00455 std::string kd = any->getMyKindStr();
00456 std::string nm = any->getValidName();
00457 std::string tp = any->getLValidName();
00458 ClassAndNamespace can = ClassAndNamespace::makeIt( any);
00459
00460
00461 std::string sp_val = any->getStrictNmspc();
00462 Any::convertToValidName( sp_val);
00463
00464 Method m;
00465 std::string mmm;
00466
00467
00468 mmm = indent(s_ind + 0) + "{\n";
00469 if( pSpecialized)
00470 {
00471 mmm += indent(s_ind + 1) + "// visit the " + tp + "\n";
00472
00473 if( pRetValBool)
00474 {
00475 mmm += indent(s_ind + 1) + "bool rv = pVisitor->visit" + can.infoName() + "( " + can.exactType() + "( this));\n";
00476 }
00477 else
00478 {
00479 mmm += indent(s_ind + 1) + "pVisitor->visit" + can.infoName() + "( " + can.exactType() + "( this));\n";
00480 }
00481
00482 if( pWithTraversalOfKids)
00483 {
00484 if( any->getMyKind() == Any::MODEL)
00485 {
00486 mmm += "\n" + modelKidsTemplate();
00487 }
00488 else if( any->getMyKind() == Any::FOLDER)
00489 {
00490 mmm += "\n" + folderKidsTemplate();
00491 }
00492 }
00493
00494 if( pRetValBool)
00495 {
00496 mmm += indent(s_ind + 1) + "return rv;\n";
00497 }
00498 } else
00499 {
00500 mmm += indent(s_ind + 1) + "// visit the " + kd + "\n";
00501 mmm += indent(s_ind + 1) + "pVisitor->visit" + kd + "( BON::" + kd + "( this));\n\n";
00502
00503 if( pWithTraversalOfKids)
00504 {
00505 if( any->getMyKind() == Any::MODEL)
00506 {
00507 mmm += modelKidsTemplate();
00508 }
00509 else if( any->getMyKind() == Any::FOLDER)
00510 {
00511 mmm += folderKidsTemplate();
00512 }
00513 }
00514 }
00515 mmm += indent(s_ind + 0) + "}\n\n\n";
00516
00517
00518 m.m_virtual = true;
00519 if( pSpecialized)
00520 {
00521 m.m_returnValue = pRetValBool ? "bool":"void";
00522 m.m_signature = "accept( " + global_vars.m_namespace_name + Any::NamespaceDelimiter_str + MakeVisitor::getVisitorName( Dumper::getInstance()->getValidName()) + " *pVisitor)";
00523 }
00524 else
00525 {
00526 m.m_returnValue = "void";
00527 m.m_signature = "accept( BON::Visitor *pVisitor)";
00528 }
00529 m.m_implementation = mmm;
00530 m.m_container = any;
00531 m.m_comment = "";
00532
00533
00534 return m;
00535 }
00536
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596