setVpx accept now all VPX using ; as delimiter
[ryzomcore.git] / ryzom / tools / pd_parser / parse_node.h
blob9a4371234d6410bcf96c7fc5bbb14c2573f0f229
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef RY_PD_PARSE_NODE_H
18 #define RY_PD_PARSE_NODE_H
20 #include "tokenizer.h"
21 #include "cpp_output.h"
22 #include "templatizer.h"
24 extern CTokenizer Tokenizer;
26 extern std::string getFunctionPrefix;
27 extern std::string setFunctionPrefix;
28 extern std::string newFunction;
29 extern std::string deleteFunction;
31 extern std::string indexVariable;
32 extern std::string valueVariable;
33 extern std::string keyVariable;
34 extern std::string objectVariable;
36 extern std::string staticCreateFunction;
37 extern std::string staticRemoveFunction;
38 extern std::string staticSetUserFactoryFunction;
39 extern std::string staticLoadFunction;
40 extern std::string staticUnloadFunction;
41 extern std::string staticGetFunction;
43 extern std::string initFunction;
44 extern std::string destroyFunction;
45 extern std::string registerFunction;
46 extern std::string registerAttributesFunction;
47 extern std::string unregisterFunction;
48 extern std::string unregisterAttributesFunction;
49 extern std::string fetchFunction;
50 extern std::string setParentFunction;
51 extern std::string setUnnotifiedParentFunction;
52 extern std::string getTableFunction;
53 extern std::string unlinkFunction;
55 extern std::string staticInitFactoryFunction;
56 extern std::string staticFactoryFunction;
57 extern std::string staticFetchFunction;
58 extern std::string staticInitFunction;
60 extern std::string logStartFunction;
61 extern std::string logStopFunction;
64 class CFileNode;
65 class CDbNode;
66 class CTypeNode;
67 class CClassNode;
68 class CIndexNode;
69 class CEnumNode;
70 class CDimensionNode;
71 class CLogMsgNode;
74 class CParseNode
76 public:
78 CParseNode() : Parent(NULL), FileNode(NULL), DbNode(NULL), Env(NULL) {}
80 virtual ~CParseNode()
82 uint i;
83 for (i=0; i<Nodes.size(); ++i)
84 delete Nodes[i];
87 CParseNode* Parent;
88 std::vector<CParseNode*> Nodes;
89 CTokenizer::CToken StartToken;
90 std::string Name;
91 std::string Description;
95 ///
96 virtual bool prolog() { return true; }
97 virtual bool epilog() { return true; }
99 ///
100 bool execute()
102 if (!prolog())
103 return false;
105 uint i;
106 for (i=0; i<Nodes.size(); ++i)
107 if (!Nodes[i]->execute())
108 return false;
110 return epilog();
115 /// Issue error
116 void error(const std::string &errMsg, const char *errType = "semantic")
118 Tokenizer.error(StartToken, errType, errMsg.c_str() );
122 CParseNode* getNode(const std::string &name)
124 uint i;
125 for (i=0; i<Nodes.size(); ++i)
126 if (Nodes[i]->Name == name)
127 return Nodes[i];
128 return NULL;
132 void getFileLine(uint &line, uint &col, std::string &file)
134 Tokenizer.getFileLine(StartToken, line, col, file);
138 CFileNode* getFileNode();
139 CDbNode* getDbNode();
141 CFileNode* FileNode;
142 CDbNode* DbNode;
144 CCppOutput& hOutput();
145 CCppOutput& cppOutput();
146 CCppOutput& inlineOutput();
148 CTypeNode* getTypeNode(const std::string &name, bool genError = true);
149 CEnumNode* getEnumNode(const std::string &name, bool genError = true);
150 CDimensionNode* getDimensionNode(const std::string &name, bool genError = true);
151 CIndexNode* getIndexNode(const std::string &name, bool genError = true);
152 CClassNode* getClassNode(const std::string &name, bool genError = true);
154 CTemplatizerEnv* Env;
155 template<typename T>
156 void setEnv(const std::string& var, const T& val)
158 nlassert(Env != NULL);
159 Env->set(var, val);
161 void define(const std::string& var)
163 nlassert(Env != NULL);
164 Env->set(var, 1);
166 void define(bool isdef, const std::string& var)
168 nlassert(Env != NULL);
169 if (isdef)
170 Env->set(var, 1);
178 class CDbNode : public CParseNode
180 public:
181 CDbNode() : DbXml(false), DbSummary(false) {}
184 bool addTypeNode(const std::string &name, const std::string &displayName = std::string(""), const std::string &defaultValue = std::string(""));
187 std::vector<CFileNode*> FileNodes;
188 std::vector<CTypeNode*> TypeNodes;
189 std::vector<CClassNode*> ClassNodes;
190 std::vector<CLogMsgNode*> LogNodes;
191 CCppOutput DbXml;
192 CCppOutput DbHpp;
193 CCppOutput DbHppInline;
194 CCppOutput DbCpp;
196 CCppOutput DbSummary;
198 std::string MainFile;
200 std::string Pch;
202 std::vector<std::string> xmlDescription;
203 CFunctionGenerator initDb;
204 CFunctionGenerator readyDb;
205 CFunctionGenerator updateDb;
206 CFunctionGenerator releaseDb;
207 CFunctionGenerator logChatDb;
208 CFunctionGenerator logTellDb;
210 std::set<std::string> Implemented;
213 virtual bool prolog();
214 virtual bool epilog();
216 void pass1();
217 void pass2();
218 void pass3();
219 void pass4();
221 void buildClassOrder(std::vector<CClassNode*>& classesOrder, std::vector<CFileNode*>& filesOrder);
223 void generateClassesDeclaration();
224 void generateIncludes(std::vector<CFileNode*>& filesOrder);
226 void generateClassesContent(std::vector<CClassNode*>& classesOrder);
228 void generateLogContent();
230 std::string getDbFile() { return MainFile.empty() ? Name : MainFile; }
232 // get file path from this file
233 std::string getFileNoExtPath(const std::string& file);
239 class CIncludeNode;
240 class CFileNode : public CParseNode
242 public:
244 CFileNode() : SeparatedFlag(false), IncludeStandard(false), IncludePDSLib(false), IncludeDbFile(false), Generate(true) { }
246 std::string IncludeAs;
248 bool SeparatedFlag;
250 CCppOutput Hpp;
251 CCppOutput HppInline;
252 CCppOutput Cpp;
254 bool IncludeStandard;
255 bool IncludePDSLib;
256 bool IncludeDbFile;
257 std::vector<CIncludeNode*> IncludeNodes;
259 bool Generate;
261 std::set<CFileNode*> Dependencies;
263 void checkDependencies(std::set<CFileNode*> &beingChecked,
264 std::set<CFileNode*> &checkedFiles,
265 std::vector<CFileNode*> &filesOrder);
268 virtual bool prolog();
269 virtual bool epilog();
272 virtual bool generateProlog();
273 virtual bool generateEpilog();
276 void writeFile();
278 // get file path from this file
279 std::string getFileNoExtPath(const std::string& file);
285 class CIncludeNode : public CParseNode
287 public:
290 virtual bool prolog();
296 class CUsePchNode : public CParseNode
298 public:
301 virtual bool prolog();
307 class CCppCodeNode : public CParseNode
309 public:
311 std::string RawCode;
314 virtual bool prolog();
320 class CTypeNode : public CParseNode
322 public:
323 CTypeNode() :
324 ToCppType(NULL),
325 ToStorageType(NULL),
326 ExternFlag(false),
327 InternFlag(false),
328 Id(0)
332 std::string CppType;
333 std::string StorageType;
334 std::string DisplayName;
336 CParseNode* ToCppType;
337 CParseNode* ToStorageType;
339 bool ExternFlag;
340 bool InternFlag;
342 std::string Temp;
344 uint32 Size;
345 uint Id;
347 std::string DefaultValue;
349 virtual bool isEnum() { return false; }
350 virtual bool isDimension() { return false; }
351 virtual bool isIndex() { return false; }
353 virtual std::string checkCode(const std::string& var) { return ""; }
355 std::string storageToCpp()
357 if (ToCppType != NULL)
358 return "__pds_cnv_type_"+NLMISC::toString(Id)+"_s2c";
359 else
360 return "("+CppType+")";
363 std::string cppToStorage()
365 if (ToStorageType != NULL)
366 return "__pds_cnv_type_"+NLMISC::toString(Id)+"_c2s";
367 else
368 return "("+StorageType+")";
373 std::string castToCpp(const std::string& var)
375 if (CppType != StorageType)
377 return storageToCpp()+"("+var+")";
379 else
381 return var;
385 std::string castToStorage(const std::string& var)
387 if (CppType != StorageType)
389 return cppToStorage()+"("+var+")";
391 else
393 return var;
397 std::string getCppType()
399 if (CppType == "CEntityId")
400 return "NLMISC::CEntityId";
401 else if (CppType == "CSheetId")
402 return "NLMISC::CSheetId";
403 else
404 return CppType;
410 std::string castToPDS(const std::string& var)
412 if (isEnum())
414 return "(uint32)"+var;
416 else if (ExternFlag)
418 return "("+StorageType+")"+var;
420 else
422 //return "("+getName()+")";
423 return castToStorage(var);
427 std::string castFromUser(const std::string& var)
429 return "("+getName()+")"+var;
434 virtual std::string getName() { return DisplayName.empty() ? Name : DisplayName; }
436 std::string getDefaultValue()
438 if (DefaultValue.empty())
440 if (CppType == "CEntityId") return "NLMISC::CEntityId::Unknown";
441 if (CppType == "CSheetId") return "NLMISC::CSheetId::Unknown";
442 return castFromUser("0");
444 else
446 return DefaultValue;
452 virtual std::string getPrintfFmt()
454 if (CppType == "CEntityId" || CppType == "CSheetId")
455 return "%s";
456 if (CppType == "double" || CppType == "float")
457 return "%f";
458 if (CppType == "sint64")
459 return "%\"NL_I64\"d";
460 if (CppType == "uint64")
461 return "%\"NL_I64\"u";
462 if (CppType == "sint32" || CppType == "sint16" || CppType == "sint8")
463 return "%d";
464 if (CppType == "uint32" || CppType == "uint16" || CppType == "uint8")
465 return "%u";
466 if (CppType == "char" || CppType == "ucchar")
467 return "%c";
468 if (CppType == "bool")
469 return "%s";
470 return "%d";
473 virtual std::string getPrintfVal(const std::string& var)
475 if (CppType == "CEntityId" || CppType == "CSheetId")
476 return var+".toString()";
477 if (CppType == "double" || CppType == "float" ||
478 CppType == "sint64" || CppType == "uint64" ||
479 CppType == "sint32" || CppType == "sint16" || CppType == "sint8" ||
480 CppType == "uint32" || CppType == "uint16" || CppType == "uint8" ||
481 CppType == "char" || CppType == "ucchar")
482 return var;
483 if (CppType == "bool")
484 return "("+var+" ? \"true\" : \"false\")";
485 return "(uint32)"+var;
490 virtual bool prolog();
493 virtual bool generateContent();
495 virtual void generateApplyCode(CClassGenerator::SMethodId& method, const std::string& token, const std::string& value)
497 method.add("__pdr.pop("+token+", "+value+");");
500 virtual void generateStoreCode(CClassGenerator::SMethodId& method, const std::string& token, const std::string& value)
502 method.add("__pdr.push("+token+", "+value+");");
515 class CDeclarationNode;
516 class CCallContext;
518 enum TDeclarationType
520 SimpleType,
521 SimpleClass,
522 ForwardRef,
523 BackRef,
524 ArrayType,
525 ArrayClass,
526 ArrayRef,
530 struct CColumn
532 std::string Name;
533 uint32 ByteSize;
534 TDeclarationType Type;
535 std::string TypeStr;
536 uint TypeId;
539 class CClassNode : public CParseNode
541 public:
543 CClassNode() :
544 IsBackReferenced(false),
545 HasInheritance(false),
546 IsInSet(false),
547 IsInArray(false),
548 IsInArrayRef(false),
549 MappedFlag(false),
550 DerivatedFlag(false),
551 HasParent(false),
552 ParentIsHidden(false),
553 ForceReference(false),
554 PDSMapped(false),
555 MapClass(NULL),
556 Columns(-1),
557 Id(0),
558 HasRowAccess(false),
559 HasTableAccess(false),
560 indexUsedInInit(false),
561 indexUsedInDestroy(false),
562 indexUsedInFetch(false),
563 tableAndRowIndicesUsedInFetch(false),
564 indexUsedInRegister(false),
565 indexUsedInUnregister(false),
566 indexUsedInNotifyInit(false),
567 indexUsedInNotifyRelease(false)
571 std::string Inherited;
572 std::string ClassKey;
573 std::string Implements;
574 std::set<CClassNode*> Dependencies;
576 std::vector<std::string> ChildrenClasses;
577 bool IsBackReferenced;
578 bool HasInheritance;
579 bool IsInSet;
580 bool IsInArray;
581 bool IsInArrayRef;
582 bool MappedFlag;
583 bool DerivatedFlag;
584 bool HasParent;
585 bool ParentIsHidden;
586 bool ForceReference;
587 std::string ParentClass;
588 std::string Reserve;
590 bool PDSMapped;
592 CClassNode *MapClass;
594 std::vector<CDeclarationNode*> Init;
595 std::string InitProto;
596 std::string InitCallArgs;
598 sint Columns;
600 uint Id;
602 std::vector<CDeclarationNode*> Attributes;
604 bool HasRowAccess;
605 bool HasTableAccess;
607 std::set<std::string> Friends;
608 std::set<std::string> ForwardFriends;
610 std::set<CClassNode*> Legacy;
613 virtual bool prolog();
614 virtual bool epilog();
616 CDeclarationNode* getDeclarationNode(const std::string &name);
617 CDeclarationNode* getKey();
618 CDeclarationNode* getClassKey();
619 CDeclarationNode* getDeclaration(const std::string& name);
621 bool useEntityId();
623 void checkClassReferences();
624 void fillAttributes();
625 void fillRefs();
626 void computeFriends();
628 void checkDependencies(std::set<CClassNode*> &beingChecked,
629 std::set<CClassNode*> &checkedClasses,
630 std::vector<CClassNode*> &classesOrder);
632 void buildInit();
634 void computeAttributesColumns();
636 std::string getId() { return (HasInheritance ? NLMISC::toString("__BaseTable") : NLMISC::toString(Id)); }
638 std::string getUserCode(const std::string& name);
642 CClassGenerator Gen;
644 CClassGenerator::SMethodId InitId;
645 CClassGenerator::SMethodId DestroyId;
646 CClassGenerator::SMethodId FetchId;
647 CClassGenerator::SMethodId RegisterId;
648 CClassGenerator::SMethodId RegisterAttributesId;
649 CClassGenerator::SMethodId UnregisterId;
650 CClassGenerator::SMethodId UnregisterAttributesId;
651 CClassGenerator::SMethodId SetParentId;
652 CClassGenerator::SMethodId SetUnnotifiedParentId;
653 CClassGenerator::SMethodId NotifyInitId;
654 CClassGenerator::SMethodId NotifyReleaseId;
656 CClassGenerator::SMethodId UserInitId;
657 CClassGenerator::SMethodId UserReleaseId;
660 CClassGenerator::SMethodId ClearId;
661 CClassGenerator::SMethodId StoreId;
662 CClassGenerator::SMethodId ApplyId;
664 bool indexUsedInInit;
665 bool indexUsedInDestroy;
666 bool indexUsedInFetch;
667 bool tableAndRowIndicesUsedInFetch;
668 bool indexUsedInRegister;
669 bool indexUsedInUnregister;
670 bool indexUsedInNotifyInit;
671 bool indexUsedInNotifyRelease;
673 bool generateContent();
674 void generateContentInCall(CCallContext *context);
677 class CDeclarationNode : public CParseNode
679 public:
681 CDeclarationNode() :
682 InitFillFlag(false),
683 WriteTriggerFlag(false),
684 ParentFlag(false),
685 HiddenFlag(false),
686 MirroredFlag(false),
687 ArrayFlag(false),
688 SetFlag(false),
689 IsRef(false),
690 IsType(false),
691 IsKey(false),
692 Id(0),
693 Column(-1),
694 Columns(-1)
698 bool InitFillFlag;
699 bool WriteTriggerFlag;
700 bool ParentFlag;
701 bool HiddenFlag;
702 bool MirroredFlag;
704 std::string ParentClass;
705 std::string ParentField;
707 std::string Type;
709 bool ArrayFlag;
710 std::string ArrayIndex;
711 bool SetFlag;
712 std::string ForwardRefAttribute;
714 bool IsRef;
715 bool IsType;
716 bool IsKey;
718 std::string DefaultValue;
720 struct CUserCode
722 std::string Event;
723 std::string CodeSpecializer;
724 std::string UserCode;
727 std::vector<CUserCode> UserCodes;
729 std::string getUserCode(const std::string &name, const std::string &specialize = std::string(""))
731 uint i;
732 // first look for a specialized code
733 for (i=0; i<UserCodes.size(); ++i)
734 if (UserCodes[i].Event == name && UserCodes[i].CodeSpecializer == specialize)
735 return UserCodes[i].UserCode;
736 // then look for a default code
737 for (i=0; i<UserCodes.size(); ++i)
738 if (UserCodes[i].Event == name && UserCodes[i].CodeSpecializer.empty())
739 return UserCodes[i].UserCode;
740 return "";
743 TDeclarationType DeclarationType;
745 std::string XmlNode;
746 std::vector<CColumn> ColumnList;
748 uint Id;
750 sint Column;
751 sint Columns;
753 CClassNode* ClassNode;
754 CClassNode* getParentClass() { return dynamic_cast<CClassNode*>(Parent); }
757 virtual bool prolog();
758 virtual bool epilog();
761 std::string getFunc() const { return lcFirst(getFunctionPrefix+Name); }
762 std::string setFunc() const { return lcFirst(setFunctionPrefix+Name); }
763 std::string newFunc() const { return lcFirst(newFunction+Name); }
764 std::string deleteFunc() const { return lcFirst(deleteFunction+Name); }
765 std::string unlinkFunc() const { return lcFirst(unlinkFunction+Name); }
766 std::string cppName() const { return "_"+Name; }
767 std::string tokenName() const { return "__Tok"+Name; }
770 std::string displayPrintfPrefix();
771 std::string displayCppCode(std::string replVar = "");
772 std::string toUint64(std::string replVar = "");
775 void generateContent(CCallContext *context = NULL);
777 void generateTypeContent(CCallContext *context = NULL);
778 void generateClassContent(CCallContext *context = NULL);
779 void generateBackRefContent();
780 void generateForwardRefContent();
781 void generateArrayTypeContent(CCallContext *context = NULL);
782 void generateArrayClassContent(CCallContext *context = NULL);
783 void generateArrayRefContent(CCallContext *context = NULL);
784 void generateSetContent(CCallContext *context = NULL);
787 void generateArrayApplyCode();
788 void generateArrayStoreCode();
789 void generateArrayEndCode();
791 void generateClassPtrApplyCode(const std::string& value);
792 void generateClassPtrStoreCode(const std::string& value);
794 std::string getAccessorName(CCallContext *context, const std::string& accessortype, const std::string& sep = "::");
802 class CIndexNode : public CTypeNode
804 public:
806 CIndexNode()
808 CppType = "uint32";
809 StorageType = "uint32";
812 virtual bool isIndex() { return true; }
814 virtual std::string getSizeName() { return Name+"Size"; }
815 virtual uint getSize() = 0;
817 virtual std::string getIndexName(uint32 value) const
819 return NLMISC::toString(value);
822 virtual std::string getToStringCode(const std::string& var) const
824 return "NLMISC::toString("+var+")";
827 virtual std::string getFromStringCode(const std::string& var) const
829 return "atoi("+var+")";
832 virtual std::string checkCode(const std::string& var) { return "nlassert("+var+"<"+getSizeName()+");"; }
839 class CEnumNode : public CIndexNode
841 public:
842 CEnumNode() :
843 CurrentValue(0),
844 MinValue(0),
845 MaxValue(0)
849 uint32 CurrentValue;
851 uint32 MinValue;
852 uint32 MaxValue;
853 std::vector<std::pair<std::string, uint32> > Values;
854 std::string EndRange;
856 virtual bool isEnum() { return true; }
857 virtual std::string getName()
859 std::string trunc = Name.substr(1);
860 return "C"+trunc+"::"+(DisplayName.empty() ? Name : DisplayName);
863 virtual uint getSize() { return MaxValue-MinValue; }
865 bool prolog();
866 bool epilog();
869 virtual bool generateContent();
871 std::string getIndexName(uint32 value) const
873 std::string result;
875 uint i;
876 for (i=0; i<Values.size(); ++i)
877 if (Values[i].second == value)
878 result = Values[i].first;
880 return result;
883 std::string getUnscopedUseSize() const
885 return "___"+Name+"_useSize";
888 std::string getUnknownValue() const
890 return "Unknown";
893 std::string getUseSize() const
895 std::string trunc = Name.substr(1);
896 return "C"+trunc+"::"+getUnscopedUseSize();
899 std::string getSizeName()
901 std::string trunc = Name.substr(1);
902 return "C"+trunc+"::"+getUnscopedUseSize();
905 virtual std::string getToStringCode(const std::string& var) const
907 std::string trunc = Name.substr(1);
908 return "C"+trunc+"::toString("+var+")";
911 virtual std::string getFromStringCode(const std::string& var) const
913 std::string trunc = Name.substr(1);
914 return "C"+trunc+"::fromString("+var+")";
917 virtual void generateApplyCode(CClassGenerator::SMethodId& method, const std::string& token, const std::string& value)
919 method.add("{");
920 method.add("std::string\tvaluename;");
921 method.add("__pdr.pop("+token+", valuename);");
922 method.add(value+" = "+getFromStringCode("valuename")+";");
923 method.add("}");
926 virtual void generateStoreCode(CClassGenerator::SMethodId& method, const std::string& token, const std::string& value)
928 method.add("{");
929 method.add("std::string\tvaluename = "+getToStringCode(value)+";");
930 method.add("__pdr.push("+token+", valuename);");
931 method.add("}");
935 class CEnumSimpleValueNode : public CEnumNode
937 public:
939 std::vector<std::string> Names;
941 bool prolog();
942 bool epilog();
945 class CEnumRangeNode : public CEnumNode
947 public:
949 bool prolog();
950 bool epilog();
959 class CDimensionNode : public CIndexNode
961 public:
963 public:
964 CDimensionNode()
968 virtual bool isDimension() { return true; }
969 std::string getSizeName() { return Name+"Size"; }
971 bool prolog();
972 bool epilog();
974 sint Dimension;
976 virtual uint getSize() { return Dimension; }
979 virtual bool generateContent();
986 class CLogMsgNode : public CParseNode
988 public:
990 CLogMsgNode() : Context(false)
994 bool prolog();
995 bool epilog()
997 return true;
1000 std::vector<std::pair<std::string, std::string> > Params;
1001 std::vector<std::string> Logs;
1003 bool Context;
1005 uint Id;
1007 void generateContent();
1010 class CExtLogTypeNode : public CParseNode
1012 public:
1014 std::string ExtLogType;
1024 class CCallContext
1026 public:
1028 std::vector<CDeclarationNode*> Context;
1030 std::string RootTable;
1031 std::string RootRow;
1032 uint Column;
1034 CCallContext(CDeclarationNode* decl = NULL)
1036 if (decl != NULL)
1037 Context.push_back(decl);
1040 bool hasRootEntityIdKey()
1042 CDeclarationNode* k = getRootCaller()->getClassKey();
1043 if (k != NULL)
1045 return (getRootCaller()->getTypeNode(getRootCaller()->getKey()->Type)->CppType == "CEntityId");
1047 return false;
1051 * Generates the name of the pointed variable
1052 * For instance: PhysicalScoresBase, where PhysicalScores is an array and Base is an attribute in this array
1053 * Actually concats all fields names
1055 std::string getCallString()
1057 uint i;
1058 std::string res;
1059 for (i=0; i<Context.size(); ++i)
1060 res += Context[i]->Name;
1061 return res;
1064 CClassNode* getRootCaller()
1066 return (CClassNode*)Context[0]->Parent;
1069 CDeclarationNode* getRootDeclaration()
1071 return Context[0];
1075 * Generates the access prototype argument list
1076 * Actually only generates indexes for arrays
1078 std::string getDebugCallStringFmt()
1080 uint i, idx=0;
1081 std::string res;
1083 for (i=0; i<Context.size(); ++i)
1085 CDeclarationNode* decl = Context[i];
1087 if (decl->DeclarationType == ArrayClass || decl->DeclarationType == ArrayType)
1089 if (!res.empty())
1090 res += ", ";
1092 CTypeNode* tnd = decl->getTypeNode(decl->ArrayIndex);
1093 res += indexVariable+NLMISC::toString(idx++)+"="+tnd->getPrintfFmt();
1097 return res;
1102 * Generates the access prototype argument list
1103 * Actually only generates indexes for arrays
1105 std::string getDebugCallStringVal()
1107 uint i, idx=0;
1108 std::string res;
1110 for (i=0; i<Context.size(); ++i)
1112 CDeclarationNode* decl = Context[i];
1114 if (decl->DeclarationType == ArrayClass || decl->DeclarationType == ArrayType)
1116 if (!res.empty())
1117 res += ", ";
1119 CTypeNode* tnd = decl->getTypeNode(decl->ArrayIndex);
1120 res += tnd->getPrintfVal(indexVariable+NLMISC::toString(idx++));
1124 return res;
1129 * Generates the access prototype argument list
1130 * Actually only generates indexes for arrays
1132 std::string getCallArgList()
1134 std::string res;
1135 uint i, idx=0;
1137 for (i=0; i<Context.size(); ++i)
1139 CDeclarationNode* decl = Context[i];
1141 if (decl->DeclarationType == ArrayClass || decl->DeclarationType == ArrayType)
1143 if (!res.empty())
1144 res += ", ";
1146 CTypeNode* tnd = decl->getTypeNode(decl->ArrayIndex);
1147 res += tnd->getName()+ " "+indexVariable+NLMISC::toString(idx++);
1150 return res;
1154 * Generates the c++ path to the variable
1155 * For instance: _PhysicalScores[__i0].Base, where PhysicalScores is an array and Base is an attribute in this array
1157 std::string getCallPath()
1159 std::string res;
1160 uint i, idx=0;
1162 for (i=0; i<Context.size(); ++i)
1164 CDeclarationNode* decl = Context[i];
1166 if (!res.empty())
1167 res += ".";
1169 res += decl->cppName();
1171 if (decl->DeclarationType == ArrayClass || decl->DeclarationType == ArrayType)
1173 res += "["+indexVariable+NLMISC::toString(idx++)+"]";
1176 return res;
1180 * Generates the c++ path to the variable
1181 * For instance: _PhysicalScores[__i0].Base, where PhysicalScores is an array and Base is an attribute in this array
1183 std::string getUserCodeContext()
1185 std::string res;
1186 uint i;
1188 if (Context.empty())
1189 return res;
1191 res = Context[0]->getParentClass()->Name;
1193 for (i=0; i<Context.size(); ++i)
1194 res += "."+Context[i]->Name;
1196 return res;
1200 * Generates check code for all types accessors
1202 std::vector<std::string> getCheckCode()
1204 std::vector<std::string> res;
1205 uint i, idx=0;
1207 for (i=0; i<Context.size(); ++i)
1209 CDeclarationNode* decl = Context[i];
1211 if (decl->DeclarationType == ArrayType || decl->DeclarationType == ArrayRef || decl->DeclarationType == ArrayClass)
1213 CIndexNode* ind = decl->getIndexNode(decl->ArrayIndex);
1214 std::string code = ind->checkCode(indexVariable+NLMISC::toString(idx++));
1215 if (!code.empty())
1216 res.push_back(code);
1220 return res;
1224 * Get current context index
1226 uint getContextIndex()
1228 uint i, idx=0;
1230 for (i=0; i<Context.size(); ++i)
1232 CDeclarationNode* decl = Context[i];
1233 if (decl->DeclarationType == ArrayClass || decl->DeclarationType == ArrayType)
1234 idx++;
1236 return idx-1;
1239 CCallContext getSubContext(CDeclarationNode* decl)
1241 CCallContext ctx = *this;
1242 ctx.Context.push_back(decl);
1243 return ctx;
1246 std::string getColumn()
1248 std::string res;
1249 uint i, idx=0;
1251 for (i=0; i<Context.size(); ++i)
1253 CDeclarationNode* decl = Context[i];
1254 if (decl->Column != 0)
1256 if (!res.empty())
1257 res += "+";
1258 res += NLMISC::toString(decl->Column);
1261 if (decl->DeclarationType == ArrayType || decl->DeclarationType == ArrayRef)
1263 if (!res.empty())
1264 res += "+";
1265 res += indexVariable+NLMISC::toString(idx++);
1267 else if (decl->DeclarationType == ArrayClass)
1269 if (!res.empty())
1270 res += "+";
1271 CClassNode* sub = decl->getClassNode(decl->Type);
1272 res += indexVariable+NLMISC::toString(idx++)+"*"+NLMISC::toString(sub->Columns);
1276 if (res.empty())
1277 res = "0";
1279 return res;
1289 // INLINES
1292 inline CFileNode* CParseNode::getFileNode()
1294 if (FileNode != NULL)
1295 return FileNode;
1297 CParseNode* node = this;
1298 CFileNode* fnode = NULL;
1300 while (node != NULL && (fnode = dynamic_cast<CFileNode*>(node)) == NULL)
1301 node = node->Parent;
1303 if (fnode == NULL)
1304 error("Can't find file node", "internal");
1306 FileNode = fnode;
1308 return fnode;
1311 inline CDbNode* CParseNode::getDbNode()
1313 if (DbNode != NULL)
1314 return DbNode;
1316 CParseNode* node = this;
1317 CDbNode* fnode = NULL;
1319 while (node != NULL && (fnode = dynamic_cast<CDbNode*>(node)) == NULL)
1320 node = node->Parent;
1322 if (fnode == NULL)
1323 error("Can't find db node", "internal");
1325 DbNode = fnode;
1327 return fnode;
1330 inline CCppOutput& CParseNode::hOutput()
1332 CFileNode *fnode = getFileNode();
1333 return fnode->Hpp;
1336 inline CCppOutput& CParseNode::cppOutput()
1338 CFileNode *fnode = getFileNode();
1339 return fnode->Cpp;
1342 inline CCppOutput& CParseNode::inlineOutput()
1344 CFileNode *fnode = getFileNode();
1345 return fnode->HppInline;
1349 inline CTypeNode *CParseNode::getTypeNode(const std::string &name, bool genError)
1351 CDbNode* db = getDbNode();
1352 uint i;
1353 for (i=0; i<db->TypeNodes.size(); ++i)
1354 if (db->TypeNodes[i]->Name == name)
1355 return db->TypeNodes[i];
1356 if (genError)
1357 error("Can't find type '"+name+"'");
1358 return NULL;
1361 inline CEnumNode *CParseNode::getEnumNode(const std::string &name, bool genError)
1363 CEnumNode* node = dynamic_cast<CEnumNode*>(getTypeNode(name, genError));
1364 if (node == NULL && genError)
1365 error("Can't find enum '"+name+"'");
1366 return node;
1369 inline CDimensionNode *CParseNode::getDimensionNode(const std::string &name, bool genError)
1371 CDimensionNode* node = dynamic_cast<CDimensionNode*>(getTypeNode(name, genError));
1372 if (node == NULL && genError)
1373 error("Can't find dimension '"+name+"'");
1374 return node;
1377 inline CIndexNode *CParseNode::getIndexNode(const std::string &name, bool genError)
1379 CIndexNode* node = dynamic_cast<CIndexNode*>(getTypeNode(name, genError));
1380 if (node == NULL && genError)
1381 error("Can't find index type '"+name+"' (neither enum nor dimension)");
1382 return node;
1385 inline CClassNode *CParseNode::getClassNode(const std::string &name, bool genError)
1387 CDbNode* db = getDbNode();
1388 uint i;
1389 for (i=0; i<db->ClassNodes.size(); ++i)
1390 if (db->ClassNodes[i]->Name == name)
1391 return db->ClassNodes[i];
1392 if (genError)
1393 error("Can't find class '"+name+"'");
1394 return NULL;
1397 inline bool CDbNode::addTypeNode(const std::string &name, const std::string &displayName, const std::string &defaultValue)
1399 CTypeNode *node = new CTypeNode();
1401 node->Name = name;
1402 node->DisplayName = displayName;
1403 node->CppType = name;
1404 node->StorageType = name;
1406 node->ToCppType = NULL;
1407 node->ToStorageType = NULL;
1409 node->DefaultValue = defaultValue;
1411 node->ExternFlag = false;
1412 node->InternFlag = true;
1413 node->Parent = this;
1415 Nodes.insert(Nodes.begin(), node);
1417 return true;
1421 inline CDeclarationNode* CClassNode::getDeclarationNode(const std::string &name)
1423 CDeclarationNode *node;
1424 if ((node = dynamic_cast<CDeclarationNode*>(getNode(name))) == NULL)
1425 error("declaration '"+name+"' not found");
1426 return node;
1429 inline bool CClassNode::useEntityId()
1431 //return !ClassKey.empty() && getTypeNode(getKey()->Type)->CppType == "CEntityId";
1432 CDeclarationNode* k = getClassKey();
1433 return k!=NULL && getTypeNode(k->Type)->CppType == "CEntityId";
1436 inline CDeclarationNode* CClassNode::getClassKey()
1438 if (ClassKey.empty())
1440 if (Inherited.empty())
1441 return NULL;
1442 CClassNode* p = getClassNode(Inherited);
1443 return p->getClassKey();
1445 else
1447 return getKey();
1452 inline CDeclarationNode* CClassNode::getKey()
1454 CDeclarationNode* key = NULL;
1455 CClassNode* classNode = this;
1456 while (classNode != NULL)
1458 key = dynamic_cast<CDeclarationNode*>(classNode->getNode(classNode->ClassKey));
1459 if (key != NULL)
1460 return key;
1461 classNode = getClassNode(Inherited, false);
1463 error("key declaration '"+ClassKey+"' not found");
1464 return NULL;
1465 //return getDeclarationNode(ClassKey);
1468 inline CDeclarationNode* CClassNode::getDeclaration(const std::string& name)
1470 CDeclarationNode* decl = NULL;
1471 CClassNode* classNode = this;
1472 while (classNode != NULL)
1474 decl = dynamic_cast<CDeclarationNode*>(classNode->getNode(name));
1475 if (decl != NULL)
1476 return decl;
1477 classNode = getClassNode(classNode->Inherited, false);
1479 error("declaration '"+name+"' not found");
1480 return NULL;
1485 #endif