PageRenderTime 98ms CodeModel.GetById 43ms RepoModel.GetById 1ms app.codeStats 1ms

/structural/supporting_matl_misc_and_old/support/fortranPrototypeGenerator/fullReader/laFortRead.cpp

https://bitbucket.org/slawton/windturbinemdo-stevebitb
C++ | 5964 lines | 4694 code | 722 blank | 548 comment | 825 complexity | 2b14b7f8e548822d865fde3b5630f5fb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. #include <fstream>
  2. #include <ostream>
  3. #include <iostream>
  4. #include <vector>
  5. #include <list>
  6. #include <string>
  7. #include <cctype>
  8. #include <cstdlib>
  9. #include <sstream>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include "eMsg.h"
  14. using namespace std;
  15. #define LINE_SIZE 1024
  16. // this is a list of all the globals
  17. // objList<fortranType> fTypes;
  18. // objList<cppType_basic> bTypes;
  19. // objList<cppType_template> tTypes;
  20. // list<fortranFunction> fortranFunctionList;
  21. // objList<mapText> textMaps;
  22. // objList<cppFunctionName> cppFuncs;
  23. // cppType* voidType;
  24. // sourceFile fortranSource;
  25. // sourceFile cppPrototype;
  26. // sourceFile cppDefinition;
  27. // commandSource_recording recorder;
  28. char* copyLine(const char* ln)
  29. {
  30. unsigned long L=0;
  31. if(ln==NULL)
  32. throw eMsg("fortranFunction: the input array is NULL");
  33. while(ln[L]!=0)
  34. L++;
  35. L++;
  36. char* retVal=new char[L];
  37. for(unsigned long i=0;i<L;i++)
  38. retVal[i]=ln[i];
  39. return retVal;
  40. }
  41. bool strEqual(const char* lhs, const char* rhs)
  42. {
  43. if(lhs==NULL || rhs==NULL)
  44. {
  45. if(lhs==rhs)
  46. return true;
  47. return false;
  48. }
  49. unsigned long i=0;
  50. while(lhs[i]!=0 && rhs!=0)
  51. {
  52. if(lhs[i]!=rhs[i])
  53. return false;
  54. i++;
  55. }
  56. if(rhs[i]!=lhs[i])
  57. return false;
  58. return true;
  59. }
  60. bool strFilterEqual(const char* fltr, const char* test)
  61. {
  62. unsigned long at=0;
  63. while(fltr[at]!=0 && test[at]!=0)
  64. {
  65. if(fltr[at]!='*' && fltr[at]!=test[at])
  66. return false;
  67. at++;
  68. }
  69. if(fltr[at]==0)
  70. return true;
  71. return false;
  72. }
  73. class wordList
  74. {
  75. private:
  76. unsigned long sz;
  77. vector<unsigned long> start;
  78. vector<unsigned long> end;
  79. char* line;
  80. public:
  81. wordList& setLine(const char* line_in)
  82. {
  83. if(line_in==NULL)
  84. {
  85. sz=0;
  86. return *this;
  87. }
  88. unsigned long at=0;
  89. sz=0;
  90. while(line_in[at]!=0)
  91. {
  92. while(line_in[at]!=0 && isspace(line_in[at]))
  93. at++;
  94. if(line_in[at]!=0)
  95. sz++;
  96. while(line_in[at]!=0 && !isspace(line_in[at]))
  97. at++;
  98. }
  99. start.resize(sz);
  100. end.resize(sz);
  101. if(line!=NULL)
  102. delete[] line;
  103. line=new char[at+1];
  104. at=0;
  105. unsigned long atWrd=0;
  106. while(line_in[at]!=0)
  107. {
  108. while(line_in[at]!=0 && isspace(line_in[at]))
  109. {
  110. line[at]=line_in[at];
  111. at++;
  112. }
  113. if(line_in[at]!=0)
  114. start[atWrd]=at;
  115. while(line_in[at]!=0 && !isspace(line_in[at]))
  116. {
  117. end[atWrd]=at;
  118. line[at]=line_in[at];
  119. at++;
  120. }
  121. atWrd++;
  122. }
  123. line[at]=0;
  124. return *this;
  125. }
  126. wordList(const char* line_in=NULL) : sz(0) , start(0) , end(0) , line(NULL)
  127. {
  128. setLine(line_in);
  129. }
  130. ~wordList()
  131. {
  132. if(line!=NULL)
  133. delete[] line;
  134. }
  135. wordList& getWord(unsigned long at, char* word, unsigned long bfSz)
  136. {
  137. if(at>=sz)
  138. throw eMsg("wordList: The argument does not exist");
  139. unsigned long wSz=end[at]-start[at]+1;
  140. if(wSz>bfSz+1)
  141. throw eMsg("wordList: the buffer provided is not big enough");
  142. unsigned long i;
  143. for(i=0;i<wSz;i++)
  144. word[i]=line[start[at]+i];
  145. word[i]=0;
  146. return *this;
  147. }
  148. unsigned long size()
  149. {
  150. return sz;
  151. }
  152. unsigned long getStart(unsigned long at)
  153. {
  154. if(at>=sz)
  155. throw eMsg("wordList: the requested start does not exist");
  156. return start[at];
  157. }
  158. unsigned long getEnd(unsigned long at)
  159. {
  160. if(at>=sz)
  161. throw eMsg("wordList: the requested end does not exist");
  162. return end[at];
  163. }
  164. };
  165. class cppType
  166. {
  167. protected:
  168. static int typeID;
  169. int myID;
  170. friend class cppType_template;
  171. virtual void addMyID(list<int>& idList) const
  172. {
  173. idList.push_back(myID);
  174. }
  175. public:
  176. cppType()
  177. {
  178. myID=typeID++;
  179. }
  180. cppType(const cppType& rhs)
  181. {
  182. this->myID=rhs.myID;
  183. }
  184. virtual ~cppType()
  185. {}
  186. // virtual const char* getName()=0;
  187. virtual void printType(ostream& out)=0;
  188. virtual void printDescription(ostream& out)=0;
  189. virtual cppType* copyType()=0;
  190. bool operator==(const cppType& rhs)
  191. {
  192. list<int> lhsType;
  193. list<int> rhsType;
  194. this->addMyID(lhsType);
  195. rhs.addMyID(rhsType);
  196. unsigned long tpCount=lhsType.size();
  197. if(tpCount!=rhsType.size())
  198. return false;
  199. list<int>::iterator itLhs=lhsType.begin();
  200. list<int>::iterator itRhs=rhsType.begin();
  201. for(unsigned long i=0;i<tpCount;i++)
  202. {
  203. if(*itLhs!=*itRhs)
  204. return false;
  205. itLhs++;
  206. itRhs++;
  207. }
  208. return true;
  209. }
  210. bool operator!=(const cppType& rhs)
  211. {
  212. return !(*this==rhs);
  213. }
  214. };
  215. int cppType::typeID=0;
  216. class cppType_basic : public cppType
  217. {
  218. private:
  219. char* name;
  220. char* description;
  221. public:
  222. cppType_basic& setName(const char* name_in)
  223. {
  224. unsigned long i=0;
  225. while(name_in[i]!=0)
  226. i++;
  227. i++;
  228. if(name!=NULL)
  229. delete[] name;
  230. name=new char[i];
  231. if(name==NULL)
  232. throw eMsg("cppType_basic::setName(): Failed to allocate memory");
  233. for(unsigned long j=0;j<i;j++)
  234. name[j]=name_in[j];
  235. return *this;
  236. }
  237. cppType_basic& setDescription(const char* desc_in)
  238. {
  239. unsigned long i=0;
  240. while(desc_in[i]!=0)
  241. i++;
  242. i++;
  243. if(description!=NULL)
  244. delete[] description;
  245. description=new char[i];
  246. if(description==NULL)
  247. throw eMsg("cppType_basic::setDescription(): Failed to allocate memory");
  248. for(unsigned long j=0;j<i;j++)
  249. description[j]=desc_in[j];
  250. return *this;
  251. }
  252. cppType_basic() : name(NULL) , description(NULL)
  253. {}
  254. cppType_basic(const char* name_in, const char* desc_in) : name(NULL) , description(NULL)
  255. {
  256. setName(name_in);
  257. setDescription(desc_in);
  258. }
  259. cppType_basic(const cppType_basic& rhs) : cppType(rhs) , name(NULL) , description(NULL)
  260. {
  261. if(rhs.name!=NULL)
  262. this->setName(rhs.name);
  263. if(rhs.description)
  264. this->setDescription(rhs.description);
  265. }
  266. virtual ~cppType_basic()
  267. {
  268. if(name!=NULL)
  269. delete[] name;
  270. if(description!=NULL)
  271. delete[] description;
  272. }
  273. virtual void printType(ostream& out)
  274. {
  275. if(name==NULL)
  276. return;
  277. out << name;
  278. }
  279. virtual void printDescription(ostream& out)
  280. {
  281. if(description==NULL)
  282. return;
  283. out << name << ": " << description;
  284. }
  285. virtual cppType* copyType()
  286. {
  287. return new cppType_basic(*this);
  288. }
  289. const char* getName()
  290. {
  291. return name;
  292. }
  293. };
  294. class cppType_template : public cppType_basic
  295. {
  296. private:
  297. vector<bool> isType;
  298. vector<cppType*> args;
  299. vector<int> vals;
  300. vector<bool> canDelete;
  301. void addMyID(list<int>& idList) const
  302. {
  303. this->cppType::addMyID(idList);
  304. for(unsigned long i=0;i<args.size();i++)
  305. {
  306. if(isType[i])
  307. {
  308. if(args[i]==NULL)
  309. throw eMsg("cppType_template: template parameter has not been set");
  310. args[i]->addMyID(idList);
  311. }
  312. else
  313. {
  314. idList.push_back(vals[i]);
  315. }
  316. }
  317. }
  318. public:
  319. cppType_template(const char* name_in,const char* desc_in,unsigned long size_in) : cppType_basic(name_in,desc_in) , isType(size_in,true) , args(size_in,NULL) , vals(size_in,0) , canDelete(size_in,false)
  320. {}
  321. cppType_template(unsigned long size_in=0) : isType(size_in,true) , args(size_in,NULL) , vals(size_in,0) , canDelete(size_in,false)
  322. {}
  323. cppType_template(const cppType_template& rhs) : cppType_basic(rhs) , isType(rhs.isType) , args(rhs.args) , vals(rhs.vals) , canDelete(rhs.isType.size(),false)
  324. {}
  325. virtual ~cppType_template()
  326. {
  327. for(unsigned long i=0;i<args.size();i++)
  328. {
  329. if(canDelete[i] && args[i]!=NULL)
  330. delete args[i];
  331. }
  332. }
  333. cppType_template& resize(unsigned long size_in=0)
  334. {
  335. isType.resize(size_in,true);
  336. args.resize(size_in,NULL);
  337. vals.resize(size_in,0);
  338. canDelete.resize(size_in,false);
  339. return *this;
  340. }
  341. unsigned long size()
  342. {
  343. return isType.size();
  344. }
  345. cppType_template& clearTypes()
  346. {
  347. for(unsigned long i=0;i<isType.size();i++)
  348. {
  349. if(isType[i])
  350. args[i]=NULL;
  351. }
  352. return *this;
  353. }
  354. cppType_template& setArgType(unsigned long at, cppType* arg, bool canDelete_in)
  355. {
  356. if(at>=isType.size())
  357. throw eMsg("cppType_template: The argument does not exist");
  358. if(args[at]!=NULL && args[at]!=arg && canDelete[at])
  359. delete args[at];
  360. isType[at]=true;
  361. args[at]=arg;
  362. canDelete[at]=canDelete_in;
  363. return *this;
  364. }
  365. cppType_template& setArgType(unsigned long at, int val)
  366. {
  367. if(at>=isType.size())
  368. throw eMsg("cppType_template: The argument does not exist");
  369. isType[at]=true;
  370. vals[at]=val;
  371. return *this;
  372. }
  373. virtual void printType(ostream& out)
  374. {
  375. this->cppType_basic::printType(out);
  376. out << '<';
  377. for(unsigned long i=0;i<isType.size();i++)
  378. {
  379. if(i>0)
  380. out << ',';
  381. out << ' ';
  382. if(isType[i] && args[i]!=NULL)
  383. args[i]->printType(out);
  384. else if(!isType[i])
  385. out << vals[i];
  386. else
  387. throw eMsg("cppType_template: the template type has not been set");
  388. out << ' ';
  389. }
  390. out << '>';
  391. }
  392. virtual void printDescription(ostream& out)
  393. {
  394. this->cppType_basic::printDescription(out);
  395. }
  396. virtual cppType* copyType()
  397. {
  398. return new cppType_template(*this);
  399. }
  400. };
  401. class fortranType
  402. {
  403. private:
  404. vector<vector<const char*> > fTypeName;
  405. cppType* cType;
  406. public:
  407. fortranType(unsigned long sz_in) : fTypeName(sz_in,vector<const char*>(0,NULL))
  408. {}
  409. fortranType& setSize(unsigned long sz_in)
  410. {
  411. fTypeName.resize(sz_in);
  412. return *this;
  413. }
  414. fortranType& setTypeSize(unsigned long typeAt, unsigned long wordCnt)
  415. {
  416. if(typeAt>=fTypeName.size())
  417. throw eMsg("fortranType: the requested type does not exist");
  418. fTypeName[typeAt].resize(wordCnt);
  419. return *this;
  420. }
  421. fortranType& setText(unsigned long typeAt,unsigned long at, const char* wrd)
  422. {
  423. if(typeAt>=fTypeName.size())
  424. throw eMsg("fortranType: the requested type does not exist");
  425. if(at>=fTypeName[typeAt].size())
  426. throw eMsg("fortranType: the word in the type does not exist");
  427. fTypeName[typeAt][at]=wrd;
  428. return *this;
  429. }
  430. fortranType& setType(cppType* cType_in)
  431. {
  432. if(cType_in==NULL)
  433. throw eMsg("fortranType: the type is NULL");
  434. cType=cType_in;
  435. return *this;
  436. }
  437. bool isType(wordList& line,unsigned long& end)
  438. {
  439. char word[LINE_SIZE];
  440. for(unsigned long i=0;i<fTypeName.size();i++)
  441. {
  442. bool fail=false;
  443. unsigned long j;
  444. for(j=0; j<fTypeName[i].size();j++)
  445. {
  446. line.getWord(j,word,LINE_SIZE);
  447. if(!strEqual(word,fTypeName[i][j]))
  448. fail=true;
  449. else
  450. end=line.getEnd(j);
  451. }
  452. if(!fail)
  453. {
  454. return true;
  455. }
  456. }
  457. return false;
  458. }
  459. bool isType(wordList& line)
  460. {
  461. unsigned long end;
  462. return this->isType(line,end);
  463. }
  464. cppType* getType()
  465. {
  466. return cType;
  467. }
  468. };
  469. template<class oType>
  470. class objList
  471. {
  472. private:
  473. list<oType*> typeObjs;
  474. bool own;
  475. vector<oType*> vct;
  476. bool vctGood;
  477. public:
  478. objList(bool own_in=true) : own(own_in) , vctGood(false)
  479. {}
  480. ~objList()
  481. {
  482. if(own)
  483. {
  484. for(typename list<oType*>::iterator it=typeObjs.begin();it!=typeObjs.end();it++)
  485. delete *it;
  486. }
  487. }
  488. unsigned long size()
  489. {
  490. return typeObjs.size();
  491. }
  492. objList& addObj(oType* newType)
  493. {
  494. if(newType==NULL)
  495. throw eMsg("objList: the new type is NULL");
  496. typeObjs.push_back(newType);
  497. vctGood=false;
  498. return *this;
  499. }
  500. oType* getObj(unsigned long at)
  501. {
  502. if(at>=typeObjs.size())
  503. throw eMsg("objList: The requested type does not exist");
  504. if(!vctGood)
  505. {
  506. vct.resize(typeObjs.size(),NULL);
  507. typename list<oType*>::iterator it=typeObjs.begin();
  508. for(unsigned long i=0;i<typeObjs.size();i++)
  509. {
  510. vct[i]=*it;
  511. it++;
  512. }
  513. vctGood=true;
  514. }
  515. return vct[at];
  516. }
  517. objList& deleteBack(unsigned long cnt=1)
  518. {
  519. if(cnt>typeObjs.size())
  520. cnt=typeObjs.size();
  521. for(unsigned long i=0;i<cnt;i++)
  522. {
  523. if(own)
  524. delete typeObjs.back();
  525. typeObjs.pop_back();
  526. }
  527. return *this;
  528. }
  529. };
  530. objList<fortranType> fTypes;
  531. objList<cppType_basic> bTypes;
  532. objList<cppType_template> tTypes;
  533. cppType* voidType;
  534. cppType* readType(wordList& line, unsigned long& end)
  535. {
  536. for(unsigned long i=0;i<fTypes.size();i++)
  537. {
  538. fortranType* fTp=fTypes.getObj(i);
  539. if(fTp->isType(line,end))
  540. {
  541. end++;
  542. return fTp->getType();
  543. }
  544. }
  545. return NULL;
  546. }
  547. cppType* readType(wordList& line)
  548. {
  549. unsigned long end;
  550. return readType(line,end);
  551. }
  552. class variable
  553. {
  554. private:
  555. char* name;
  556. cppType* type;
  557. bool deleteType;
  558. unsigned long pointerLevel;
  559. bool isReference;
  560. public:
  561. variable& setName(const char* name_in)
  562. {
  563. if(name!=NULL)
  564. delete[] name;
  565. if(name_in==NULL)
  566. {
  567. name=NULL;
  568. return *this;
  569. }
  570. unsigned long L=0;
  571. while(name_in[L]!=0)
  572. L++;
  573. L++;
  574. name=new char[L];
  575. if(name==NULL)
  576. throw eMsg("variable: Failed to allocate memory");
  577. for(unsigned long i=0;i<L;i++)
  578. name[i]=name_in[i];
  579. return *this;
  580. }
  581. variable& setType(cppType* type_in,bool deleteType_in=false)
  582. {
  583. if(type!=NULL && type!=type_in && deleteType)
  584. delete type;
  585. type=type_in;
  586. deleteType=deleteType_in;
  587. return *this;
  588. }
  589. variable& setPointerLevel(unsigned long lvl_in)
  590. {
  591. pointerLevel=lvl_in;
  592. return *this;
  593. }
  594. variable& setReference(bool isRef_in)
  595. {
  596. isReference=isRef_in;
  597. return *this;
  598. }
  599. variable(cppType* type_in=NULL,const char* name_in=NULL,long pntrLevel=0,bool isReference_in=false,bool deleteType_in=false) : name(NULL) , type(type_in) , deleteType(false) , pointerLevel(pntrLevel) , isReference(isReference_in)
  600. {
  601. setType(type_in,deleteType_in);
  602. setName(name_in);
  603. }
  604. variable(const variable& asg) : name(NULL) , type(NULL) , deleteType(false) , pointerLevel(0) , isReference(false)
  605. {
  606. *this=asg;
  607. }
  608. ~variable()
  609. {
  610. if(name!=NULL)
  611. delete[] name;
  612. if(deleteType && type!=NULL)
  613. delete type;
  614. }
  615. variable& operator=(const variable& asg)
  616. {
  617. if(name!=NULL)
  618. delete[] name;
  619. if(asg.name==NULL)
  620. this->name=NULL;
  621. else
  622. {
  623. this->name=copyLine(asg.name);
  624. if(this->name==NULL)
  625. throw eMsg("variable::operator=(): Failed to allocate memory");
  626. }
  627. if(this->deleteType && this->type!=NULL)
  628. delete this->type;
  629. if(asg.deleteType)
  630. {
  631. if(asg.type==NULL)
  632. {
  633. this->type=NULL;
  634. this->deleteType=false;
  635. }
  636. else
  637. {
  638. this->type=asg.type->copyType();
  639. if(this->type==NULL)
  640. throw eMsg("variable::operator=(): Failed to allocate memory");
  641. this->deleteType=asg.deleteType;
  642. }
  643. }
  644. else
  645. {
  646. this->deleteType=false;
  647. this->type=asg.type;
  648. }
  649. this->pointerLevel=asg.pointerLevel;
  650. this->isReference=asg.isReference;
  651. return *this;
  652. }
  653. variable& printName(ostream& out)
  654. {
  655. if(name==NULL)
  656. throw eMsg("variable: Name has not been set");
  657. out << name;
  658. return *this;
  659. }
  660. variable& printDecl(ostream& out)
  661. {
  662. if(type==NULL)
  663. throw eMsg("variable: type has not been set");
  664. type->printType(out);
  665. for(unsigned long i=0;i<pointerLevel;i++)
  666. out << '*';
  667. if(isReference)
  668. out << '&';
  669. out << ' ';
  670. this->printName(out);
  671. return *this;
  672. }
  673. cppType* getType()
  674. {
  675. return type;
  676. }
  677. const char* getName()
  678. {
  679. return name;
  680. }
  681. };
  682. struct comment
  683. {
  684. char* line;
  685. bool tagged;
  686. comment() : line(NULL)
  687. {}
  688. ~comment()
  689. {
  690. if(line!=NULL)
  691. delete[] line;
  692. }
  693. comment(const comment& asg)
  694. {
  695. if(asg.line!=NULL)
  696. {
  697. unsigned long i=0;
  698. while(asg.line[i]!=0)
  699. i++;
  700. this->line=new char[i+1];
  701. if(this->line==NULL)
  702. throw eMsg("comment: Failed to allocate memory");
  703. i=0;
  704. while(asg.line[i]!=0)
  705. {
  706. this->line[i]=asg.line[i];
  707. i++;
  708. }
  709. this->line[i]=0;
  710. }
  711. else
  712. {
  713. this->line=NULL;
  714. }
  715. this->tagged=asg.tagged;
  716. }
  717. comment& operator=(const comment& asg)
  718. {
  719. if(this->line!=NULL)
  720. delete[] this->line;
  721. if(asg.line!=NULL)
  722. {
  723. unsigned long i=0;
  724. while(asg.line[i]!=0)
  725. i++;
  726. this->line=new char[i+1];
  727. if(this->line==NULL)
  728. throw eMsg("comment: Failed to allocate memory");
  729. i=0;
  730. while(asg.line[i]!=0)
  731. {
  732. this->line[i]=asg.line[i];
  733. i++;
  734. }
  735. }
  736. else
  737. {
  738. this->line=NULL;
  739. }
  740. this->tagged=asg.tagged;
  741. }
  742. };
  743. class nameSource
  744. {
  745. protected:
  746. vector<bool> sourceIsSunk;
  747. void setSourceSize(unsigned long sz_in)
  748. {
  749. sourceIsSunk.resize(sz_in,false);
  750. }
  751. public:
  752. nameSource() : sourceIsSunk(0,false)
  753. {}
  754. virtual ~nameSource()
  755. {}
  756. virtual unsigned long sourceSize()
  757. {
  758. return sourceIsSunk.size();
  759. }
  760. virtual const char* getSourceName(unsigned long at)=0;
  761. virtual const char* getSourceDescription()=0;
  762. virtual const char* getSourceTermDescription(unsigned long at)=0;
  763. virtual bool isSunk(unsigned long at)
  764. {
  765. if(at>=sourceIsSunk.size())
  766. throw eMsg("nameSource::isSunk(): The source does not exist");
  767. return sourceIsSunk[at];
  768. }
  769. virtual void setSunk(unsigned long at,bool isSunk=true)
  770. {
  771. if(at>=sourceIsSunk.size())
  772. throw eMsg("nameSource::setSunk(): The source does not exist");
  773. sourceIsSunk[at]=isSunk;
  774. }
  775. };
  776. class nameSink
  777. {
  778. protected:
  779. vector<nameSource*> sources;
  780. vector<unsigned long> atSources;
  781. void setSinkSize(unsigned long sz)
  782. {
  783. sources.resize(sz);
  784. atSources.resize(sz);
  785. }
  786. public:
  787. nameSink()
  788. {}
  789. virtual ~nameSink()
  790. {}
  791. unsigned long sinkSize()
  792. {
  793. return sources.size();
  794. }
  795. void setSource(unsigned long atSnk,nameSource* src,unsigned long atSrc)
  796. {
  797. if(atSnk>=sources.size())
  798. throw eMsg("nameSink: The sink does not exist");
  799. sources[atSnk]=src;
  800. atSources[atSnk]=atSrc;
  801. }
  802. bool sinkSet(unsigned long at)
  803. {
  804. if(at>=sources.size())
  805. throw eMsg("nameSink: The sink does not exist");
  806. if(sources[at]!=NULL)
  807. return true;
  808. return false;
  809. }
  810. virtual const char* getSinkDescription()=0;
  811. virtual const char* getSinkTermDescription(unsigned long at)=0;
  812. };
  813. class declaration
  814. {
  815. public:
  816. virtual void printDecl(ostream& out)=0;
  817. };
  818. class code
  819. {
  820. public:
  821. code()
  822. {}
  823. virtual ~code()
  824. {}
  825. virtual void writeCode(ostream& out)=0;
  826. };
  827. class function
  828. {
  829. public:
  830. virtual const char* getFunctionName()=0;
  831. virtual unsigned long getFunctionArgumentCount()=0;
  832. // virtual const char* getArgName(unsigned long at)=0;
  833. virtual const char* getArgumentDescription(unsigned long at)=0;
  834. };
  835. class nameMap : virtual public nameSource , virtual public nameSink
  836. {
  837. public:
  838. nameMap()
  839. {}
  840. virtual ~nameMap()
  841. {}
  842. };
  843. class mappingCode : public nameMap , public code
  844. {
  845. public:
  846. mappingCode()
  847. {}
  848. virtual ~mappingCode()
  849. {}
  850. };
  851. enum txtTYPE
  852. {
  853. ttINPUT,
  854. ttOUTPUT,
  855. ttTYPE,
  856. ttCUSTOM
  857. };
  858. class mapText
  859. {
  860. private:
  861. char* txt;
  862. unsigned long inCnt;
  863. unsigned long outCnt;
  864. unsigned long typeCnt;
  865. unsigned long custCnt;
  866. char* desc;
  867. char** inDesc;
  868. char** outDesc;
  869. char** tpDesc;
  870. char** cstDesc;
  871. void clearText()
  872. {
  873. if(txt!=NULL)
  874. delete[] txt;
  875. txt=NULL;
  876. if(desc!=NULL)
  877. delete[] desc;
  878. desc=NULL;
  879. if(inDesc!=NULL)
  880. {
  881. for(unsigned long i=0;i<inCnt;i++)
  882. {
  883. if(inDesc[i])
  884. delete[] inDesc[i];
  885. }
  886. delete[] inDesc;
  887. inDesc=NULL;
  888. }
  889. inCnt=0;
  890. if(outDesc!=NULL)
  891. {
  892. for(unsigned long i=0;i<outCnt;i++)
  893. {
  894. if(outDesc[i])
  895. delete[] outDesc[i];
  896. }
  897. delete[] outDesc;
  898. outDesc=NULL;
  899. }
  900. outCnt=0;
  901. if(tpDesc!=NULL)
  902. {
  903. for(unsigned long i=0;i<typeCnt;i++)
  904. {
  905. if(tpDesc[i])
  906. delete[] tpDesc[i];
  907. }
  908. delete[] tpDesc;
  909. tpDesc=NULL;
  910. }
  911. typeCnt=0;
  912. if(cstDesc!=NULL)
  913. {
  914. for(unsigned long i=0;i<custCnt;i++)
  915. {
  916. if(cstDesc[i])
  917. delete[] cstDesc[i];
  918. }
  919. delete[] cstDesc;
  920. cstDesc=NULL;
  921. }
  922. custCnt=0;
  923. }
  924. public:
  925. bool setText(const char* text)
  926. {
  927. clearText();
  928. if(text==NULL)
  929. {
  930. txt=NULL;
  931. return false;
  932. }
  933. else
  934. {
  935. txt=copyLine(text);
  936. if(txt==NULL)
  937. throw eMsg("mapText: Failed to allocate text");
  938. }
  939. unsigned long at=0;
  940. inCnt=0;
  941. outCnt=0;
  942. typeCnt=0;
  943. custCnt=0;
  944. while(txt[at]!=0)
  945. {
  946. if(txt[at]=='@')
  947. {
  948. char number[24];
  949. at++;
  950. if(txt[at]!='(')
  951. return false;
  952. at++;
  953. txtTYPE tt=ttINPUT;
  954. if(txt[at]=='o')
  955. tt=ttOUTPUT;
  956. else if(txt[at]=='i')
  957. tt=ttINPUT;
  958. else if(txt[at]=='t')
  959. tt=ttTYPE;
  960. else if(txt[at]=='c')
  961. tt=ttCUSTOM;
  962. else
  963. return false;
  964. at++;
  965. unsigned long i=0;
  966. while(txt[at]!=')' && txt[at]!=0)
  967. {
  968. if(!isdigit(txt[at]))
  969. return false;
  970. number[i++]=txt[at++];
  971. }
  972. number[i]=0;
  973. if(txt[at]==0)
  974. return false;
  975. if(txt[at]!=')')
  976. return false;
  977. unsigned long atTerm=atoi(number);
  978. switch(tt)
  979. {
  980. case ttOUTPUT:
  981. if(outCnt<(atTerm+1))
  982. outCnt=atTerm+1;
  983. break;
  984. case ttINPUT:
  985. if(inCnt<(atTerm+1))
  986. inCnt=atTerm+1;
  987. break;
  988. case ttTYPE:
  989. if(typeCnt<(atTerm+1))
  990. typeCnt=atTerm+1;
  991. break;
  992. case ttCUSTOM:
  993. if(custCnt<(atTerm+1))
  994. custCnt=atTerm+1;
  995. break;
  996. default:
  997. throw eMsg("mapText: An unexpected error occured");
  998. }
  999. }
  1000. at++;
  1001. }
  1002. if(inCnt>0)
  1003. {
  1004. inDesc=new char*[inCnt];
  1005. if(inDesc==NULL)
  1006. throw eMsg("mapText::setText(): Failed to allocate memory");
  1007. for(unsigned long i=0;i<inCnt;i++)
  1008. inDesc[i]=NULL;
  1009. }
  1010. if(outCnt>0)
  1011. {
  1012. outDesc=new char*[outCnt];
  1013. if(outDesc==NULL)
  1014. throw eMsg("mapText::setText(): Failed to allocate memory");
  1015. for(unsigned long i=0;i<outCnt;i++)
  1016. outDesc[i]=NULL;
  1017. }
  1018. if(typeCnt>0)
  1019. {
  1020. tpDesc=new char*[typeCnt];
  1021. if(tpDesc==NULL)
  1022. throw eMsg("mapText::setText(): Failed to allocate memory");
  1023. for(unsigned long i=0;i<typeCnt;i++)
  1024. tpDesc[i]=NULL;
  1025. }
  1026. if(custCnt>0)
  1027. {
  1028. cstDesc=new char*[custCnt];
  1029. if(cstDesc==NULL)
  1030. throw eMsg("mapText::setText(): Failed to allocate memory");
  1031. for(unsigned long i=0;i<custCnt;i++)
  1032. cstDesc[i]=NULL;
  1033. }
  1034. return true;
  1035. };
  1036. mapText(const char* txt_in=NULL) : txt(NULL) , inCnt(0) , outCnt(0) , typeCnt(0) , custCnt(0) , desc(NULL) , inDesc(NULL) , outDesc(NULL) , tpDesc(NULL) , cstDesc(NULL)
  1037. {
  1038. if(txt_in!=NULL)
  1039. {
  1040. if(!setText(txt_in))
  1041. throw eMsg("mapText::mapText(): Cannot create mapText with malformed text");
  1042. }
  1043. }
  1044. ~mapText()
  1045. {
  1046. clearText();
  1047. }
  1048. mapText& setDescription(const char* dsc_in)
  1049. {
  1050. if(desc!=NULL)
  1051. delete[] desc;
  1052. if(dsc_in==NULL)
  1053. desc=NULL;
  1054. else
  1055. {
  1056. desc=copyLine(dsc_in);
  1057. if(desc==NULL)
  1058. throw eMsg("mapText::setDescription(): Failed to allocate memory");
  1059. }
  1060. return *this;
  1061. }
  1062. mapText& setInputDescription(unsigned long at, const char* dsc_in)
  1063. {
  1064. if(at>=inCnt)
  1065. throw eMsg("mapText::setInputDescription(): The input description does not exist");
  1066. if(inDesc==NULL)
  1067. throw eMsg("mapText::setInputDescription(): The container for the input description has not been set");
  1068. if(inDesc[at]!=NULL)
  1069. delete[] inDesc[at];
  1070. if(dsc_in==NULL)
  1071. inDesc[at]=NULL;
  1072. else
  1073. {
  1074. inDesc[at]=copyLine(dsc_in);
  1075. if(inDesc[at]==NULL)
  1076. throw eMsg("mapText::setInputDescription(): Failed to allocate memory");
  1077. }
  1078. return *this;
  1079. }
  1080. mapText& setOutputDescription(unsigned long at, const char* dsc_in)
  1081. {
  1082. if(at>=outCnt)
  1083. throw eMsg("mapText::setOutputDescription(): The input description does not exist");
  1084. if(outDesc==NULL)
  1085. throw eMsg("mapText::setOutputDescription(): The container for the input description has not been set");
  1086. if(outDesc[at]!=NULL)
  1087. delete[] outDesc[at];
  1088. if(dsc_in==NULL)
  1089. outDesc[at]=NULL;
  1090. else
  1091. {
  1092. outDesc[at]=copyLine(dsc_in);
  1093. if(outDesc[at]==NULL)
  1094. throw eMsg("mapText::setOutputDescription(): Failed to allocate memory");
  1095. }
  1096. return *this;
  1097. }
  1098. mapText& setTypeDescription(unsigned long at, const char* dsc_in)
  1099. {
  1100. if(at>=typeCnt)
  1101. throw eMsg("mapText::setTypeDescription(): The input description does not exist");
  1102. if(tpDesc==NULL)
  1103. throw eMsg("mapText::setTypeDescription(): The container for the input description has not been set");
  1104. if(tpDesc[at]!=NULL)
  1105. delete[] tpDesc[at];
  1106. if(dsc_in==NULL)
  1107. tpDesc[at]=NULL;
  1108. else
  1109. {
  1110. tpDesc[at]=copyLine(dsc_in);
  1111. if(tpDesc[at]==NULL)
  1112. throw eMsg("mapText::setTypeDescription(): Failed to allocate memory");
  1113. }
  1114. return *this;
  1115. }
  1116. mapText& setCustomDescription(unsigned long at, const char* dsc_in)
  1117. {
  1118. if(at>=custCnt)
  1119. throw eMsg("mapText::setCustomDescription(): The input description does not exist");
  1120. if(cstDesc==NULL)
  1121. throw eMsg("mapText::setCustomDescription(): The container for the input description has not been set");
  1122. if(cstDesc[at]!=NULL)
  1123. delete[] cstDesc[at];
  1124. if(dsc_in==NULL)
  1125. cstDesc[at]=NULL;
  1126. else
  1127. {
  1128. cstDesc[at]=copyLine(dsc_in);
  1129. if(cstDesc[at]==NULL)
  1130. throw eMsg("mapText::setCustomDescription(): Failed to allocate memory");
  1131. }
  1132. return *this;
  1133. }
  1134. const char* getTxt() const
  1135. {
  1136. return txt;
  1137. }
  1138. unsigned long getInCnt() const
  1139. {
  1140. return inCnt;
  1141. }
  1142. unsigned long getOutCnt() const
  1143. {
  1144. return outCnt;
  1145. }
  1146. unsigned long getTypeCnt() const
  1147. {
  1148. return typeCnt;
  1149. }
  1150. unsigned long getCustCnt() const
  1151. {
  1152. return custCnt;
  1153. }
  1154. const char* getDescription() const
  1155. {
  1156. return desc;
  1157. }
  1158. const char* getInputDescription(unsigned long at) const
  1159. {
  1160. if(at>=inCnt)
  1161. throw eMsg("mapText::getInputDescription(): The requested description does not exist");
  1162. return inDesc[at];
  1163. }
  1164. const char* getOutputDescription(unsigned long at) const
  1165. {
  1166. if(at>=outCnt)
  1167. throw eMsg("mapText::getOutputDescription(): The description does not exist");
  1168. return outDesc[at];
  1169. }
  1170. const char* getTypeDescription(unsigned long at) const
  1171. {
  1172. if(at>=typeCnt)
  1173. throw eMsg("mapText::getTypeDescription(): The description does not exist");
  1174. return tpDesc[at];
  1175. }
  1176. const char* getCustomDescription(unsigned long at) const
  1177. {
  1178. if(at>=custCnt)
  1179. throw eMsg("mapText::getCustomDescription(): The description does not exist");
  1180. return cstDesc[at];
  1181. }
  1182. };
  1183. objList<mapText> textMaps;
  1184. class nameMap_text : public mappingCode
  1185. {
  1186. private:
  1187. const mapText* mpTxt;
  1188. const char* txt;
  1189. unsigned long outCnt;
  1190. unsigned long typeCnt;
  1191. unsigned long custCnt;
  1192. vector<cppType*> types;
  1193. vector<bool> canDelete;
  1194. vector<char*> outNames;
  1195. vector<char*> custNames;
  1196. public:
  1197. nameMap_text& setText(const mapText& mpTxt_in)
  1198. {
  1199. mpTxt=&mpTxt_in;
  1200. this->txt=mpTxt->getTxt();
  1201. this->outCnt=mpTxt->getOutCnt();
  1202. this->typeCnt=mpTxt->getTypeCnt();
  1203. this->custCnt=mpTxt->getCustCnt();
  1204. types.resize(typeCnt,NULL);
  1205. canDelete.resize(typeCnt,false);
  1206. outNames.resize(outCnt,NULL);
  1207. custNames.resize(custCnt,NULL);
  1208. this->setSinkSize(mpTxt->getInCnt());
  1209. this->setSourceSize(outCnt);
  1210. return *this;
  1211. }
  1212. nameMap_text() : mpTxt(NULL) , txt(NULL) , outCnt(0) , typeCnt(0) , custCnt(0) , types(0,NULL) , canDelete(0,false) , outNames(0,NULL) , custNames(0,NULL)
  1213. {}
  1214. nameMap_text(const mapText& mpTxt_in) : mpTxt(NULL) , txt(NULL) , outCnt(0) , typeCnt(0) , custCnt(0) , types(0,NULL) , canDelete(0,false) , outNames(0,NULL) , custNames(0,NULL)
  1215. {
  1216. this->setText(mpTxt_in);
  1217. }
  1218. ~nameMap_text()
  1219. {
  1220. for(unsigned long i=0;i<types.size();i++)
  1221. {
  1222. if(types[i]!=NULL && canDelete[i])
  1223. delete types[i];
  1224. }
  1225. for(unsigned long i=0;i<outNames.size();i++)
  1226. {
  1227. if(outNames[i]!=NULL)
  1228. delete[] outNames[i];
  1229. }
  1230. for(unsigned long i=0;i<custNames.size();i++)
  1231. {
  1232. if(custNames[i]!=NULL)
  1233. delete[] custNames[i];
  1234. }
  1235. }
  1236. nameMap_text& setOutName(unsigned long at,const char* name)
  1237. {
  1238. if(at>=outNames.size())
  1239. throw eMsg("nameMap_text: The nameMap does not have those outputs");
  1240. if(outNames[at]!=NULL)
  1241. delete[] outNames[at];
  1242. outNames[at]=NULL;
  1243. if(name==NULL)
  1244. return *this;
  1245. outNames[at]=copyLine(name);
  1246. if(outNames[at]==NULL)
  1247. throw eMsg("nameMap_text: failed to allocate memory");
  1248. return *this;
  1249. }
  1250. const char* getOutName(unsigned long at)
  1251. {
  1252. if(at>=outNames.size())
  1253. throw eMsg("nameMap_text: The output does not exist");
  1254. return outNames[at];
  1255. }
  1256. unsigned long outNameCount()
  1257. {
  1258. return outNames.size();
  1259. }
  1260. nameMap_text& setCustName(unsigned long at,const char* name)
  1261. {
  1262. if(at>=custNames.size())
  1263. throw eMsg("nameMap_text: The nameMap does not have those customs");
  1264. if(custNames[at]!=NULL)
  1265. delete[] custNames[at];
  1266. custNames[at]=NULL;
  1267. if(name==NULL)
  1268. return *this;
  1269. custNames[at]=copyLine(name);
  1270. if(custNames[at]==NULL)
  1271. throw eMsg("nameMap_text: failed to allocate memory");
  1272. return *this;
  1273. }
  1274. const char* getCustName(unsigned long at)
  1275. {
  1276. if(at>=custNames.size())
  1277. throw eMsg("nameMap_text: The custom does not exist");
  1278. return custNames[at];
  1279. }
  1280. unsigned long custNameCount()
  1281. {
  1282. return custNames.size();
  1283. }
  1284. nameMap_text& setCppType(unsigned long at, cppType* type_in,bool canDelete_in=false)
  1285. {
  1286. if(at>=types.size())
  1287. throw eMsg("nameMap_text: The type does not exist");
  1288. if(types[at]!=NULL && types[at]!=type_in && canDelete[at])
  1289. delete types[at];
  1290. types[at]=type_in;
  1291. canDelete[at]=canDelete_in;
  1292. return *this;
  1293. }
  1294. cppType* getCppType(unsigned long at)
  1295. {
  1296. if(at>=types.size())
  1297. throw eMsg("nameMap_text: The type does not exist");
  1298. return types[at];
  1299. }
  1300. unsigned long getTypeCount()
  1301. {
  1302. return types.size();
  1303. }
  1304. void writeCode(ostream& out)
  1305. {
  1306. if(txt==NULL)
  1307. return;
  1308. unsigned long at=0;
  1309. while(txt[at]!=0)
  1310. {
  1311. if(txt[at]=='@')
  1312. {
  1313. char number[24];
  1314. at++;
  1315. if(txt[at]!='(')
  1316. throw eMsg("nameMap_text: malformed text");
  1317. at++;
  1318. txtTYPE tt=ttINPUT;
  1319. if(txt[at]=='o')
  1320. tt=ttOUTPUT;
  1321. else if(txt[at]=='i')
  1322. tt=ttINPUT;
  1323. else if(txt[at]=='t')
  1324. tt=ttTYPE;
  1325. else if(txt[at]=='c')
  1326. tt=ttCUSTOM;
  1327. else
  1328. throw eMsg("nameMap_text: The text is malformed");
  1329. at++;
  1330. unsigned long i=0;
  1331. while(txt[at]!=')' && txt[at]!=0)
  1332. {
  1333. if(!isdigit(txt[at]))
  1334. throw eMsg("nameMap_text: malformed text");
  1335. number[i++]=txt[at++];
  1336. }
  1337. if(txt[at]==0)
  1338. throw eMsg("nameMap_text: malformed text");
  1339. if(txt[at]!=')')
  1340. throw eMsg("nameMap_text: malformed text");
  1341. number[i]=0;
  1342. unsigned long atTerm=atoi(number);
  1343. switch(tt)
  1344. {
  1345. case ttOUTPUT:
  1346. if(atTerm>=outCnt)
  1347. throw eMsg("nameMap_text: specified an output that does not exist");
  1348. if(outNames[atTerm]==NULL)
  1349. throw eMsg("nameMap_text: not all the outputs have been specified");
  1350. out << outNames[atTerm];
  1351. break;
  1352. case ttINPUT:
  1353. if(atTerm>=this->sinkSize())
  1354. throw eMsg("nameMap_text: specified and input that does not exist");
  1355. if(this->sources[atTerm]==NULL)
  1356. throw eMsg("nameMap_text: the input data has not been specified");
  1357. out << this->sources[atTerm]->getSourceName(this->atSources[atTerm]);
  1358. break;
  1359. case ttTYPE:
  1360. if(atTerm>=typeCnt)
  1361. throw eMsg("nameMap_text: The requested type does not exist");
  1362. if(types[atTerm]==NULL)
  1363. throw eMsg("nameMap_text: The requested type has not been specified");
  1364. types[atTerm]->printType(out);
  1365. break;
  1366. case ttCUSTOM:
  1367. if(atTerm>=custCnt)
  1368. throw eMsg("nameMap_text: The requested custom does not exist");
  1369. if(custNames[atTerm]==NULL)
  1370. throw eMsg("nameMap_text: The requested custom has not been specified");
  1371. out << custNames[atTerm];
  1372. break;
  1373. default:
  1374. throw eMsg("nameMap_text: There was an error trying to fill in a custom item");
  1375. }
  1376. }
  1377. else
  1378. {
  1379. out << txt[at];
  1380. }
  1381. at++;
  1382. }
  1383. out << endl;
  1384. }
  1385. const char* getSinkDescription()
  1386. {
  1387. if(mpTxt==NULL)
  1388. throw eMsg("nameMap_text::getSinkDescription(): The text does not exist");
  1389. return mpTxt->getDescription();
  1390. }
  1391. const char* getSinkTermDescription(unsigned long at)
  1392. {
  1393. if(mpTxt==NULL)
  1394. throw eMsg("nameMap_text::getSinkTermDescription(): The text does not exist");
  1395. return mpTxt->getInputDescription(at);
  1396. }
  1397. const char* getSourceName(unsigned long at)
  1398. {
  1399. if(at>=outCnt)
  1400. throw eMsg("nameMap_text::getSourceName(): The source name does not exist");
  1401. return outNames[at];
  1402. }
  1403. const char* getSourceDescription()
  1404. {
  1405. if(mpTxt==NULL)
  1406. throw eMsg("nameMap_text::getSourceDescription(): The text has not been set");
  1407. return mpTxt->getDescription();
  1408. }
  1409. const char* getSourceTermDescription(unsigned long at)
  1410. {
  1411. if(mpTxt==NULL)
  1412. throw eMsg("nameMap_text::getSourceTermDescription(): The text source has not been set");
  1413. return mpTxt->getOutputDescription(at);
  1414. }
  1415. };
  1416. class returnSink : public mappingCode
  1417. {
  1418. public:
  1419. returnSink()
  1420. {
  1421. this->setSourceSize(0);
  1422. this->setSinkSize(1);
  1423. }
  1424. const char* getSinkDescription()
  1425. {
  1426. return "Returns a varible";
  1427. }
  1428. const char* getSinkTermDescription(unsigned long at)
  1429. {
  1430. return "The variable to be sunk";
  1431. }
  1432. const char* getSourceName(unsigned long at)
  1433. {
  1434. throw eMsg("returnSink::getSourceName(): This object is not meant to return any source information");
  1435. }
  1436. const char* getSourceDescription()
  1437. {
  1438. return "This is an empty source that merely returns a variable";
  1439. }
  1440. const char* getSourceTermDescription(unsigned long at)
  1441. {
  1442. throw eMsg("returnSink::getSourceTermDescription(): This object has not sources");
  1443. }
  1444. void writeCode(ostream& out)
  1445. {
  1446. out << "\treturn " << this->sources[0]->getSourceName(this->atSources[0]) << ';';
  1447. }
  1448. };
  1449. class returnValue : public mappingCode
  1450. {
  1451. private:
  1452. char* value;
  1453. public:
  1454. returnValue(const char* val=NULL) : value(NULL)
  1455. {
  1456. this->setSourceSize(0);
  1457. this->setSinkSize(0);
  1458. if(val!=NULL)
  1459. {
  1460. value=copyLine(val);
  1461. if(value==NULL)
  1462. throw eMsg("returnValue::returnValue(): Failed to allocate memory");
  1463. }
  1464. }
  1465. ~returnValue()
  1466. {
  1467. if(value!=NULL)
  1468. delete[] value;
  1469. }
  1470. returnValue& setValue(const char* val)
  1471. {
  1472. if(value!=NULL)
  1473. delete[] value;
  1474. if(val==NULL)
  1475. {
  1476. value=NULL;
  1477. return *this;
  1478. }
  1479. value=copyLine(val);
  1480. if(value==NULL)
  1481. throw eMsg("returnValue::setValue(): Failed to allocate memory");
  1482. return *this;
  1483. }
  1484. const char* getSinkDescription()
  1485. {
  1486. return "This is an empty sink that returns a value";
  1487. }
  1488. const char* getSinkTermDescription(unsigned long at)
  1489. {
  1490. throw eMsg("returnValue::getSinkTermDescription(): This is an empty sink");
  1491. }
  1492. const char* getSourceName(unsigned long at)
  1493. {
  1494. throw eMsg("returnValue::getSourceName(): This is an empty source");
  1495. }
  1496. const char* getSourceDescription()
  1497. {
  1498. return "This is an empty source that return a value";
  1499. }
  1500. const char* getSourceTermDescription(unsigned long at)
  1501. {
  1502. throw eMsg("returnValue::getSourceTermDescription(): This is an empty source");
  1503. }
  1504. void writeCode(ostream& out)
  1505. {
  1506. if(value==NULL)
  1507. throw eMsg("returnValue::writeCode(): The value has not been set");
  1508. out << "\treturn " << value << ';';
  1509. }
  1510. };
  1511. class callingCode : public mappingCode
  1512. {
  1513. protected:
  1514. function* func;
  1515. bool canDelete;
  1516. unsigned long inExtra;
  1517. void printCall(ostream& out)
  1518. {
  1519. if(func==NULL)
  1520. throw eMsg("callingCode::printCall(): The calling code does not have a function to call");
  1521. out << func->getFunctionName() << "( ";
  1522. bool isFirst=true;
  1523. for(unsigned long i=0;i<func->getFunctionArgumentCount();i++)
  1524. {
  1525. if(!isFirst)
  1526. out << " , ";
  1527. out << this->sources[i]->getSourceName(this->atSources[i]);
  1528. isFirst=false;
  1529. }
  1530. out << " )";
  1531. }
  1532. callingCode(unsigned long outCnt,function* func_in=NULL,bool canDelete_in=false, unsigned long inExtra_in=0) : func(func_in) , canDelete(canDelete_in) , inExtra(inExtra_in)
  1533. {
  1534. if(func_in!=NULL)
  1535. this->setSinkSize(func_in->getFunctionArgumentCount());
  1536. this->setSourceSize(outCnt);
  1537. }
  1538. public:
  1539. callingCode(function* func_in=NULL,bool canDelete_in=false) : func(func_in) , canDelete(canDelete_in) , inExtra(0)
  1540. {
  1541. if(func_in!=NULL)
  1542. this->setSinkSize(func_in->getFunctionArgumentCount());
  1543. this->setSourceSize(0);
  1544. }
  1545. virtual ~callingCode()
  1546. {
  1547. if(func!=NULL && canDelete)
  1548. delete func;
  1549. }
  1550. virtual void setFunction(function* func_in,bool canDelete_in=false)
  1551. {
  1552. if(func!=NULL && func!=func_in && canDelete)
  1553. delete func;
  1554. func=func_in;
  1555. canDelete=canDelete_in;
  1556. if(func!=NULL)
  1557. this->setSinkSize(func->getFunctionArgumentCount()+inExtra);
  1558. }
  1559. virtual void writeCode(ostream& out)
  1560. {
  1561. out << '\t';
  1562. this->printCall(out);
  1563. out << ';' << endl;
  1564. }
  1565. virtual const char* getSourceName(unsigned long at)
  1566. {
  1567. throw eMsg("callingCode::getSourceName(): This code provides no sources");
  1568. }
  1569. virtual const char* getSourceDescription()
  1570. {
  1571. return "This calls a function, no new variables are created";
  1572. }
  1573. virtual const char* getSourceTermDescription(unsigned long at)
  1574. {
  1575. throw eMsg("callingCode::getSourceDescription(): This code provides no sources");
  1576. }
  1577. virtual const char* getSinkDescription()
  1578. {
  1579. return "This calls a function where each argument is an input to this code segment";
  1580. }
  1581. virtual const char* getSinkTermDescription(unsigned long at)
  1582. {
  1583. if(func==NULL)
  1584. throw eMsg("callingCode::getSinkTermDescription(): The function has not been set");
  1585. if(at>=this->sinkSize())
  1586. throw eMsg("callingCode::getSinkTermDescription(): The sink does not exist");
  1587. return func->getArgumentDescription(at);
  1588. }
  1589. };
  1590. class callingCode_return : public callingCode
  1591. {
  1592. public:
  1593. callingCode_return(function* func_in=NULL,bool canDelete_in=false) : callingCode(func_in,canDelete_in)
  1594. {}
  1595. void writeCode(ostream& out)
  1596. {
  1597. out << "\treturn ";
  1598. this->printCall(out);
  1599. out << ';' << endl;
  1600. }
  1601. const char* getSinkDescription()
  1602. {
  1603. return "return a function call where each argument is an input to this code segment";
  1604. }
  1605. const char* getSourceDescription()
  1606. {
  1607. return "This calls a function, no new variables are created because it returns";
  1608. }
  1609. };
  1610. class callingCode_assignNew : public callingCode
  1611. {
  1612. private:
  1613. cppType* retType;
  1614. char* retVar;
  1615. bool deleteRet;
  1616. public:
  1617. callingCode_assignNew(function* func_in=NULL,bool canDelete_in=false, cppType* retType_in=NULL, const char* retVar_in=NULL, bool deleteRet_in=false) : callingCode(1,func_in,canDelete_in) , retType(retType_in) , retVar(NULL) , deleteRet(deleteRet_in)
  1618. {
  1619. if(retVar_in!=NULL)
  1620. {
  1621. retVar=copyLine(retVar_in);
  1622. if(retVar==NULL)
  1623. throw eMsg("callingCode_assignNew::callingCode_assignNew(): Failed to allocate memory");
  1624. }
  1625. }
  1626. ~callingCode_assignNew()
  1627. {
  1628. if(retType!=NULL && deleteRet)
  1629. delete retType;
  1630. if(retVar!=NULL)
  1631. delete[] retVar;
  1632. }
  1633. callingCode_assignNew& setVariable(cppType* retType_in,const char* retVar_in,bool deleteRet=false)
  1634. {
  1635. if(retType!=NULL && deleteRet)
  1636. delete retType;
  1637. if(retVar!=NULL)
  1638. delete[] retVar;
  1639. retType=retType_in;
  1640. if(retVar_in==NULL)
  1641. {
  1642. retVar=NULL;
  1643. }
  1644. else
  1645. {
  1646. retVar=copyLine(retVar_in);
  1647. if(retVar==NULL)
  1648. throw eMsg("callingCode_assignNew::setVariable(): Failed to allocate memory");
  1649. }
  1650. return *this;
  1651. }
  1652. void writeCode(ostream& out)
  1653. {
  1654. if(retType==NULL || retVar==NULL)
  1655. throw eMsg("callingCode_assignNew::writeCode(): Not all the inputs have been specified");
  1656. out << '\t';
  1657. retType->printType(out);
  1658. out << ' ' << retVar << '=';
  1659. this->printCall(out);
  1660. out << ';' << endl;
  1661. }
  1662. const char* getSinkDescription()
  1663. {
  1664. return "return a function call where each argument is an input to this code segment";
  1665. }
  1666. const char* getSourceName(unsigned long at)
  1667. {
  1668. if(at!=0)
  1669. throw eMsg("callingCode_assignNew::getSourceName(): The output does not exist");
  1670. if(retVar==NULL)
  1671. throw eMsg("callingCode_assignNew::getSourceName(): The output has not been specified");
  1672. return retVar;
  1673. }
  1674. const char* getSourceDescription()
  1675. {
  1676. return "The calls a function, the return value is assigned to this code segments output variable";
  1677. }
  1678. const char* getSourceTermDescription(unsigned long at)
  1679. {
  1680. if(at!=0)
  1681. throw eMsg("callingCode_assignNew::getSourceTermDescription(): The output does not exist");
  1682. return "The variable return value is assigned to";
  1683. }
  1684. };
  1685. class callingCode_assignOld : public callingCode
  1686. {
  1687. public:
  1688. callingCode_assignOld(function* func_in=NULL,bool canDelete_in=false) : callingCode(0,func_in,canDelete_in,1)
  1689. {}
  1690. void writeCode(ostream& out)
  1691. {
  1692. unsigned long atSink=this->sinkSize()-1;
  1693. out << '\t' << this->sources[atSink]->getSourceName(this->atSources[atSink]) << '=';
  1694. this->printCall(out);
  1695. out << ';' << endl;
  1696. }
  1697. const char* getSinkDescription()
  1698. {
  1699. return "return a function call where each argument is an input to this code segment, the return value is assigned to an old variable";
  1700. }
  1701. const char* getSourceDescription()
  1702. {
  1703. return "This calls a function, no new variables are created because it returns";
  1704. }
  1705. const char* getSinkTermDescription(unsigned long at)
  1706. {
  1707. if(at>=this->sinkSize())
  1708. throw eMsg("callingCode_assignOld::getSinkTermDescription(): THe sink does not exist");
  1709. if(at<(this->sinkSize()-1))
  1710. return callingCode::getSinkTermDescription(at);
  1711. return "The variable where the return value is assigned";
  1712. }
  1713. };
  1714. class cppFunctionCode : public declaration
  1715. {
  1716. public:
  1717. virtual void writeDefinition(ostream& out)=0;
  1718. };
  1719. class cppFunctionName;
  1720. class cppFunctionInstance;
  1721. class cppFunctionName
  1722. {
  1723. private:
  1724. char* name;
  1725. list<cppFunctionInstance*> funcList;
  1726. bool isEqual(cppFunctionInstance* rhs,cppFunctionInstance* lhs);
  1727. public:
  1728. cppFunctionName(const char* fName=NULL);
  1729. virtual ~cppFunctionName();
  1730. const char* getName();
  1731. bool isUnique(cppFunctionInstance* fnc_test);
  1732. unsigned long getFunctionCount();
  1733. cppFunctionInstance* getFunctionInstance(unsigned long at);
  1734. cppFunctionInstance* createFunctionInstance();
  1735. void deleteLastInstance();
  1736. };
  1737. class cppFunctionInstance : public cppFunctionCode , public nameSource
  1738. {
  1739. private:
  1740. cppFunctionName* funcNameSource;
  1741. list<mappingCode*> mapCodePointers;
  1742. list<bool> delMapCode;
  1743. callingCode* myCall;
  1744. bool deleteCall;
  1745. list<code*> defnCode;
  1746. list<nameSource*> sources;
  1747. list<nameSink*> sinks;
  1748. vector<variable*> args;
  1749. vector<bool> deleteVar;
  1750. vector<char*> argDesc;
  1751. vector<bool> sunk;
  1752. cppType* retType;
  1753. bool callAdded;
  1754. bool retCalled;
  1755. bool areSinksClosed();
  1756. void printFunctionDecl(ostream& out);
  1757. public:
  1758. cppFunctionInstance(cppFunctionName* funcNameSource_in);
  1759. ~cppFunctionInstance();
  1760. unsigned long getArgumentCount();
  1761. cppFunctionInstance& setArgumentCount(unsigned long argCnt);
  1762. cppFunctionInstance& addArgument(unsigned long at, variable* arg,const char* desc,bool canDelete_in=false);
  1763. cppFunctionInstance& defineReturn(cppType* retType_in);
  1764. variable* getVariable(unsigned long at);
  1765. bool checkSinks();
  1766. bool canWrite();
  1767. bool canAdd();
  1768. bool canAddCall();
  1769. cppFunctionInstance& addMap(mappingCode* mpCd_in,bool retCall=false,bool canDelete_in=false);
  1770. cppFunctionInstance& addCall(callingCode* clCd_in,bool retCall=false,bool canDelete_in=false);
  1771. unsigned long sourceObjectCount();
  1772. unsigned long sinkObjectCount();
  1773. nameSource* getSource(unsigned long at);
  1774. nameSink* getSink(); // returns the most recently added sink
  1775. nameSink* getSink(unsigned long at);
  1776. // this is inherited from nameSource
  1777. unsigned long sourceSize();
  1778. const char* getSourceName(unsigned long at);
  1779. const char* getSourceDescription();
  1780. const char* getSourceTermDescription(unsigned long at);
  1781. bool isSunk(unsigned long at);
  1782. void setSunk(unsigned long at,bool isSunk=true);
  1783. void writeDefinition(ostream& out);
  1784. void printDecl(ostream& out);
  1785. };
  1786. bool cppFunctionName::isEqual(cppFunctionInstance* rhs,cppFunctionInstance* lhs)
  1787. {
  1788. if(rhs->getArgumentCount()!=lhs->getArgumentCount())
  1789. return false;
  1790. for(unsigned long i=0;i<rhs->getArgumentCount();i++)
  1791. {
  1792. if((*(rhs->getVariable(i)->getType()))!=(*(lhs->getVariable(i)->getType())))
  1793. return false;
  1794. }
  1795. return true;
  1796. }
  1797. cppFunctionName::cppFunctionName(const char* fName) : name(NULL)
  1798. {
  1799. if(fName!=NULL)
  1800. {
  1801. name=copyLine(fName);
  1802. if(name==NULL)
  1803. throw eMsg("cppFunctionName: Failed to allocate memory");
  1804. }
  1805. }
  1806. cppFunctionName::~cppFunctionName()
  1807. {
  1808. if(name!=NULL)
  1809. delete[] name;
  1810. for(list<cppFunctionInstance*>::iterator it=funcList.begin();it!=funcList.end();it++)
  1811. {
  1812. if((*it)!=NULL)
  1813. delete *it;
  1814. }
  1815. }
  1816. const char* cppFunctionName::getName()
  1817. {
  1818. if(name==NULL)
  1819. throw eMsg("cppFunctionName: The function name has not been specified");
  1820. return name;
  1821. }
  1822. bool cppFunctionName::isUnique(cppFunctionInstance* fnc_test)
  1823. {
  1824. for(list<cppFunctionInstance*>::iterator it=funcList.begin();it!=funcList.end();it++)
  1825. {
  1826. if(fnc_test!=*it)
  1827. {
  1828. if(isEqual(fnc_test,*it))
  1829. return false;
  1830. }
  1831. }
  1832. return true;
  1833. }
  1834. unsigned long cppFunctionName::getFunctionCount()
  1835. {
  1836. return funcList.size();
  1837. }
  1838. cppFunctionInstance* cppFunctionName::getFunctionInstance(unsigned long at)
  1839. {
  1840. if(at>=funcList.size())
  1841. throw eMsg("cppFunctionName::getFunctionInstance(): The function instance does not exist");
  1842. list<cppFunctionInstance*>::iterator it=funcList.begin();
  1843. for(unsigned long i=0;i<at;i++)
  1844. it++;
  1845. return *it;
  1846. }
  1847. cppFunctionInstance* cppFunctionName::createFunctionInstance()
  1848. {
  1849. funcList.push_back(new cppFunctionInstance(this));
  1850. if(funcList.back()==NULL)
  1851. throw eMsg("cppFunctionName::createFunctionInstance(): Failed to allocate memory");
  1852. return funcList.back();
  1853. }
  1854. void cppFunctionName::deleteLastInstance()
  1855. {
  1856. if(funcList.size()==0)
  1857. return;
  1858. delete funcList.back();
  1859. funcList.pop_back();
  1860. }
  1861. bool cppFunctionInstance::areSinksClosed()
  1862. {
  1863. if(sinks.size()>0)
  1864. {
  1865. nameSink* lstSnk=sinks.back();
  1866. for(unsigned long i=0;i<lstSnk->sinkSize();i++)
  1867. {
  1868. if(!lstSnk->sinkSet(i))
  1869. return false;
  1870. }
  1871. }
  1872. return true;
  1873. }
  1874. void cppFunctionInstance::printFunctionDecl(ostream& out)
  1875. {
  1876. if(retType==NULL || funcNameSource==NULL)
  1877. throw eMsg("cppFunctionInstance::printFunctionDecl(): Not all the inputs have been set");
  1878. if(!funcNameSource->isUnique(this))
  1879. throw eMsg("cppFunctionInstance::printFunctionDecl(): This function instance is not unique");
  1880. for(unsigned long i=0;i<args.size();i++)
  1881. {
  1882. if(args[i]==NULL)
  1883. throw eMsg("cppFunctionInstance::printFunctionDecl(): Not all the inputs have been set");
  1884. }
  1885. retType->printType(out);
  1886. out << ' ';
  1887. out << funcNameSource->getName();
  1888. out << "( ";
  1889. bool isFirst=true;
  1890. for(unsigned long i=0;i<args.size();i++)
  1891. {
  1892. if(!isFirst)
  1893. out << " , ";
  1894. args[i]->printDecl(out);
  1895. isFirst=false;
  1896. }
  1897. out << " )";
  1898. }
  1899. cppFunctionInstance::cppFunctionInstance(cppFunctionName* funcNameSource_in) : funcNameSource(funcNameSource_in) , callAdded(false) , retCalled(false)
  1900. {
  1901. sources.push_back(this);
  1902. }
  1903. cppFunctionInstance::~cppFunctionInstance()
  1904. {
  1905. for(unsigned long i=0;i<argDesc.size();i++)
  1906. {
  1907. if(argDesc[i]!=NULL)
  1908. delete[] argDesc[i];
  1909. }
  1910. for(unsigned long i=0;i<args.size();i++)
  1911. {
  1912. if(args[i]!=NULL && deleteVar[i])
  1913. delete args[i];
  1914. }
  1915. typename list<mappingCode*>::iterator itPntr=mapCodePointers.begin();
  1916. for(typename list<bool>::iterator itDel=delMapCode.begin();itDel!=delMapCode.end();itDel++)
  1917. {
  1918. if((*itPntr)!=NULL && (*itDel))
  1919. delete (*itPntr);
  1920. itPntr++;
  1921. }
  1922. }
  1923. unsigned long cppFunctionInstance::getArgumentCount()
  1924. {
  1925. return args.size();
  1926. }
  1927. cppFunctionInstance& cppFunctionInstance::setArgumentCount(unsigned long argCnt)
  1928. {
  1929. for(unsigned long i=argCnt;i<argDesc.size();i++)
  1930. {
  1931. if(deleteVar[i])
  1932. delete args[i];
  1933. delete[] argDesc[i];
  1934. argDesc[i]=NULL;
  1935. }
  1936. args.resize(argCnt,NULL);
  1937. argDesc.resize(argCnt,NULL);
  1938. deleteVar.resize(argCnt,false);
  1939. sunk.resize(argCnt,false);
  1940. return *this;
  1941. }
  1942. cppFunctionInstance& cppFunctionInstance::addArgument(unsigned long at, variable* arg,const char* desc,bool canDelete_in)
  1943. {
  1944. if(at>=args.size())
  1945. throw eMsg("cppFunctionInstance::addArgument(): The argument does not exist");
  1946. if(args[at]!=NULL && args[at]!=arg && deleteVar[at])
  1947. delete args[at];
  1948. args[at]=arg;
  1949. deleteVar[at]=canDelete_in;
  1950. if(argDesc[at]!=NULL)
  1951. delete[] argDesc[at];
  1952. argDesc[at]=copyLine(desc);
  1953. if(argDesc[at]==NULL)
  1954. throw eMsg("cppFunctionInstance::addArgument(): Failed to allocate memory");
  1955. return *this;
  1956. }
  1957. cppFunctionInstance& cppFunctionInstance::defineReturn(cppType* retType_in)
  1958. {
  1959. if(retType_in==NULL)
  1960. throw eMsg("The return type is not valid");
  1961. retType=retType_in;
  1962. return *this;
  1963. }
  1964. variable* cppFunctionInstance::getVariable(unsigned long at)
  1965. {
  1966. if(at>=args.size())
  1967. throw eMsg("The requested variable does not exist");
  1968. return args[at];
  1969. }
  1970. bool cppFunctionInstance::checkSinks()
  1971. {
  1972. return areSinksClosed();
  1973. }
  1974. bool cppFunctionInstance::canWrite()
  1975. {
  1976. if(retType==NULL)
  1977. return false;
  1978. if(retCalled)
  1979. return funcNameSource->isUnique(this);
  1980. if((*retType)!=(*voidType))
  1981. return false;
  1982. if(!checkSinks())
  1983. return false;
  1984. if(!callAdded)
  1985. return false;
  1986. return funcNameSource->isUnique(this);
  1987. }
  1988. bool cppFunctionInstance::canAdd()
  1989. {
  1990. return (retType==voidType || !retCalled);
  1991. }
  1992. bool cppFunctionInstance::canAddCall()
  1993. {
  1994. return !callAdded;
  1995. }
  1996. cppFunctionInstance& cppFunctionInstance::addMap(mappingCode* mpCd_in,bool retCall,bool canDelete_in)
  1997. {
  1998. if(retCalled)
  1999. throw eMsg("cppFunctionInstance: Cannot add more code to a function that is complete");
  2000. if(!areSinksClosed())
  2001. throw eMsg("cppFunctionInstance: Cannot add another sink until all the sinks are closed");
  2002. if(retCall)
  2003. {
  2004. if(retType==voidType || retType==NULL)
  2005. throw eMsg("cppFunctionInstance: Cannot have a return statement in a function returning void");
  2006. if(!callAdded)
  2007. throw eMsg("cppFunctionInstance: Cannot call return without the fortran function being called");
  2008. retCalled=true;
  2009. }
  2010. mapCodePointers.push_back(mpCd_in);
  2011. delMapCode.push_back(canDelete_in);
  2012. defnCode.push_back(mpCd_in);
  2013. sources.push_back(mpCd_in);
  2014. sinks.push_back(mpCd_in);
  2015. return *this;
  2016. }
  2017. cppFunctionInstance& cppFunctionInstance::addCall(callingCode* clCd_in,bool retCall,bool canDelete_in)
  2018. {
  2019. if(retCalled)
  2020. throw eMsg("cppFunctionInstance: The function has returned cannot add more code");
  2021. if(callAdded)
  2022. throw eMsg("cppFunctionInstance: Cannot have more than one instance of fortran function calls");
  2023. if(retCall)
  2024. {
  2025. if(retTy

Large files files are truncated, but you can click here to view the full file