as_builder.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_builder.h
  25. //
  26. // This is the class that manages the compilation of the scripts
  27. //
  28. #ifndef AS_BUILDER_H
  29. #define AS_BUILDER_H
  30. #include "as_config.h"
  31. #include "as_scriptengine.h"
  32. #include "as_module.h"
  33. #include "as_array.h"
  34. #include "as_scriptcode.h"
  35. #include "as_scriptnode.h"
  36. #include "as_datatype.h"
  37. #include "as_property.h"
  38. BEGIN_AS_NAMESPACE
  39. struct sExplicitSignature
  40. {
  41. sExplicitSignature(int argCount = 0) : argTypes(argCount), argModifiers(argCount), argNames(argCount), defaultArgs(argCount) {}
  42. asCDataType returnType;
  43. asCArray<asCDataType> argTypes;
  44. asCArray<asETypeModifiers> argModifiers;
  45. asCArray<asCString> argNames;
  46. asCArray<asCString *> defaultArgs;
  47. };
  48. struct sFunctionDescription
  49. {
  50. asCScriptCode *script;
  51. asCScriptNode *node;
  52. asCString name;
  53. asCObjectType *objType;
  54. sExplicitSignature *explicitSignature;
  55. int funcId;
  56. };
  57. struct sGlobalVariableDescription
  58. {
  59. asCScriptCode *script;
  60. asCScriptNode *idNode;
  61. asCScriptNode *nextNode;
  62. asCString name;
  63. asCGlobalProperty *property;
  64. asCDataType datatype;
  65. int index;
  66. bool isCompiled;
  67. bool isPureConstant;
  68. bool isEnumValue;
  69. asQWORD constantValue;
  70. };
  71. struct sClassDeclaration
  72. {
  73. sClassDeclaration() {script = 0; node = 0; validState = 0; objType = 0; isExistingShared = false; isFinal = false;}
  74. asCScriptCode *script;
  75. asCScriptNode *node;
  76. asCString name;
  77. int validState;
  78. asCObjectType *objType;
  79. bool isExistingShared;
  80. bool isFinal;
  81. };
  82. struct sFuncDef
  83. {
  84. asCScriptCode *script;
  85. asCScriptNode *node;
  86. asCString name;
  87. int idx;
  88. };
  89. class asCCompiler;
  90. class asCBuilder
  91. {
  92. public:
  93. asCBuilder(asCScriptEngine *engine, asCModule *module);
  94. ~asCBuilder();
  95. int VerifyProperty(asCDataType *dt, const char *decl, asCString &outName, asCDataType &outType);
  96. int ParseDataType(const char *datatype, asCDataType *result);
  97. int ParseTemplateDecl(const char *decl, asCString *name, asCString *subtypeName);
  98. int ParseFunctionDeclaration(asCObjectType *type, const char *decl, asCScriptFunction *func, bool isSystemFunction, asCArray<bool> *paramAutoHandles = 0, bool *returnAutoHandle = 0);
  99. int ParseVariableDeclaration(const char *decl, asCObjectProperty *var);
  100. int AddCode(const char *name, const char *code, int codeLength, int lineOffset, int sectionIdx, bool makeCopy);
  101. int Build();
  102. int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asCScriptFunction **outFunc);
  103. int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  104. void WriteInfo(const char *scriptname, const char *msg, int r, int c, bool preMessage);
  105. void WriteError(const char *scriptname, const char *msg, int r, int c);
  106. void WriteWarning(const char *scriptname, const char *msg, int r, int c);
  107. int CheckNameConflict(const char *name, asCScriptNode *node, asCScriptCode *code);
  108. int CheckNameConflictMember(asCObjectType *type, const char *name, asCScriptNode *node, asCScriptCode *code, bool isProperty);
  109. protected:
  110. friend class asCCompiler;
  111. friend class asCModule;
  112. friend class asCParser;
  113. asCObjectProperty *GetObjectProperty(asCDataType &obj, const char *prop);
  114. asCGlobalProperty *GetGlobalProperty(const char *prop, bool *isCompiled, bool *isPureConstant, asQWORD *constantValue, bool *isAppProp);
  115. asCScriptFunction *GetFunctionDescription(int funcId);
  116. void GetFunctionDescriptions(const char *name, asCArray<int> &funcs);
  117. void GetObjectMethodDescriptions(const char *name, asCObjectType *objectType, asCArray<int> &methods, bool objIsConst, const asCString &scope = "");
  118. int RegisterScriptFunction(int funcID, asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false);
  119. int RegisterScriptFunctionWithSignature(int funcID, asCScriptNode *node, asCScriptCode *file, asCString &name, sExplicitSignature *signature, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false, bool isPrivate = false, bool isConst = false, bool isFinal = false, bool isOverride = false, bool treatAsProperty = false);
  120. int RegisterVirtualProperty(asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false);
  121. int RegisterImportedFunction(int funcID, asCScriptNode *node, asCScriptCode *file);
  122. int RegisterGlobalVar(asCScriptNode *node, asCScriptCode *file);
  123. int RegisterClass(asCScriptNode *node, asCScriptCode *file);
  124. int RegisterInterface(asCScriptNode *node, asCScriptCode *file);
  125. int RegisterEnum(asCScriptNode *node, asCScriptCode *file);
  126. int RegisterTypedef(asCScriptNode *node, asCScriptCode *file);
  127. int RegisterFuncDef(asCScriptNode *node, asCScriptCode *file);
  128. void CompleteFuncDef(sFuncDef *funcDef);
  129. void CompileClasses();
  130. void GetParsedFunctionDetails(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, asCString &name, asCDataType &returnType, asCArray<asCDataType> &parameterTypes, asCArray<asETypeModifiers> &inOutFlags, asCArray<asCString *> &defaultArgs, bool &isConstMethod, bool &isConstructor, bool &isDestructor, bool &isPrivate, bool &isOverride, bool &isFinal);
  131. int ValidateDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func);
  132. bool DoesMethodExist(asCObjectType *objType, int methodId, asUINT *methodIndex = 0);
  133. void AddDefaultConstructor(asCObjectType *objType, asCScriptCode *file);
  134. asCObjectProperty *AddPropertyToClass(sClassDeclaration *c, const asCString &name, const asCDataType &type, bool isPrivate, asCScriptCode *file = 0, asCScriptNode *node = 0);
  135. int CreateVirtualFunction(asCScriptFunction *func, int idx);
  136. asCObjectType *GetObjectType(const char *type);
  137. asCScriptFunction *GetFuncDef(const char *type);
  138. int GetEnumValueFromObjectType(asCObjectType *objType, const char *name, asCDataType &outDt, asDWORD &outValue);
  139. int GetEnumValue(const char *name, asCDataType &outDt, asDWORD &outValue);
  140. asCString GetCleanExpressionString(asCScriptNode *n, asCScriptCode *file);
  141. void ParseScripts();
  142. void CompileFunctions();
  143. void CompileGlobalVariables();
  144. struct preMessage_t
  145. {
  146. bool isSet;
  147. asCString message;
  148. int r;
  149. int c;
  150. } preMessage;
  151. int numErrors;
  152. int numWarnings;
  153. asCArray<asCScriptCode *> scripts;
  154. asCArray<sFunctionDescription *> functions;
  155. asCArray<sGlobalVariableDescription *> globVariables;
  156. asCArray<sClassDeclaration *> classDeclarations;
  157. asCArray<sClassDeclaration *> interfaceDeclarations;
  158. asCArray<sClassDeclaration *> namedTypeDeclarations;
  159. asCArray<sFuncDef *> funcDefs;
  160. asCScriptEngine *engine;
  161. asCModule *module;
  162. asCDataType CreateDataTypeFromNode(asCScriptNode *node, asCScriptCode *file, bool acceptHandleForScope = false, asCObjectType *templateType = 0);
  163. asCDataType ModifyDataTypeFromNode(const asCDataType &type, asCScriptNode *node, asCScriptCode *file, asETypeModifiers *inOutFlag, bool *autoHandle);
  164. };
  165. END_AS_NAMESPACE
  166. #endif