as_builder.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2021 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. andreas@angelcode.com
  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_symboltable.h"
  32. #include "as_scriptengine.h"
  33. #include "as_module.h"
  34. #include "as_array.h"
  35. #include "as_scriptcode.h"
  36. #include "as_scriptnode.h"
  37. #include "as_datatype.h"
  38. #include "as_property.h"
  39. BEGIN_AS_NAMESPACE
  40. #ifdef AS_NO_COMPILER
  41. // Forward declare the structure, as it is part of some function signatures used even without the compiler
  42. struct sGlobalVariableDescription;
  43. #endif
  44. #ifndef AS_NO_COMPILER
  45. struct sFunctionDescription
  46. {
  47. asCScriptCode *script;
  48. asCScriptNode *node;
  49. asCString name;
  50. asCObjectType *objType;
  51. asCArray<asCString> paramNames;
  52. int funcId;
  53. bool isExistingShared;
  54. };
  55. struct sGlobalVariableDescription
  56. {
  57. asCScriptCode *script;
  58. asCScriptNode *declaredAtNode;
  59. asCScriptNode *initializationNode;
  60. asCString name;
  61. asCGlobalProperty *property;
  62. asCDataType datatype;
  63. asSNameSpace *ns;
  64. int index;
  65. bool isCompiled;
  66. bool isPureConstant;
  67. bool isEnumValue;
  68. asQWORD constantValue;
  69. };
  70. struct sPropertyInitializer
  71. {
  72. sPropertyInitializer() : declNode(0), initNode(0), file(0) {}
  73. sPropertyInitializer(const asCString &nm, asCScriptNode *decl, asCScriptNode *init, asCScriptCode *f) : name(nm), declNode(decl), initNode(init), file(f) {}
  74. sPropertyInitializer &operator=(const sPropertyInitializer &o) {name = o.name; declNode = o.declNode; initNode = o.initNode; file = o.file; return *this;}
  75. asCString name;
  76. asCScriptNode *declNode;
  77. asCScriptNode *initNode;
  78. asCScriptCode *file;
  79. };
  80. struct sClassDeclaration
  81. {
  82. sClassDeclaration() {script = 0; node = 0; validState = 0; typeInfo = 0; isExistingShared = false; isFinal = false;}
  83. asCScriptCode *script;
  84. asCScriptNode *node;
  85. asCString name;
  86. int validState;
  87. asCTypeInfo *typeInfo;
  88. bool isExistingShared;
  89. bool isFinal;
  90. asCArray<sPropertyInitializer> propInits;
  91. };
  92. struct sFuncDef
  93. {
  94. asCScriptCode *script;
  95. asCScriptNode *node;
  96. asCString name;
  97. int idx;
  98. };
  99. struct sMixinClass
  100. {
  101. asCScriptCode *script;
  102. asCScriptNode *node;
  103. asCString name;
  104. asSNameSpace *ns;
  105. };
  106. #endif // AS_NO_COMPILER
  107. class asCBuilder
  108. {
  109. public:
  110. asCBuilder(asCScriptEngine *engine, asCModule *module);
  111. ~asCBuilder();
  112. // These methods are used by the application interface
  113. int VerifyProperty(asCDataType *dt, const char *decl, asCString &outName, asCDataType &outType, asSNameSpace *ns);
  114. int ParseDataType(const char *datatype, asCDataType *result, asSNameSpace *implicitNamespace, bool isReturnType = false);
  115. int ParseTemplateDecl(const char *decl, asCString *name, asCArray<asCString> &subtypeNames);
  116. int ParseFunctionDeclaration(asCObjectType *type, const char *decl, asCScriptFunction *func, bool isSystemFunction, asCArray<bool> *paramAutoHandles = 0, bool *returnAutoHandle = 0, asSNameSpace *ns = 0, asCScriptNode **outListPattern = 0, asCObjectType **outParentClass = 0);
  117. int ParseVariableDeclaration(const char *decl, asSNameSpace *implicitNamespace, asCString &outName, asSNameSpace *&outNamespace, asCDataType &outDt);
  118. int CheckNameConflict(const char *name, asCScriptNode *node, asCScriptCode *code, asSNameSpace *ns, bool isProperty, bool isVirtualProperty);
  119. int CheckNameConflictMember(asCTypeInfo *type, const char *name, asCScriptNode *node, asCScriptCode *code, bool isProperty, bool isVirtualProperty);
  120. int ValidateVirtualProperty(asCScriptFunction *func);
  121. #ifndef AS_NO_COMPILER
  122. int AddCode(const char *name, const char *code, int codeLength, int lineOffset, int sectionIdx, bool makeCopy);
  123. asCScriptCode *FindOrAddCode(const char *name, const char *code, size_t length);
  124. int Build();
  125. int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asCScriptFunction **outFunc);
  126. int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  127. #endif
  128. protected:
  129. friend class asCModule;
  130. friend class asCParser;
  131. friend class asCScriptFunction;
  132. friend class asCScriptEngine;
  133. void Reset();
  134. void WriteInfo(const asCString &scriptname, const asCString &msg, int r, int c, bool preMessage);
  135. void WriteInfo(const asCString &msg, asCScriptCode *file, asCScriptNode *node);
  136. void WriteError(const asCString &scriptname, const asCString &msg, int r, int c);
  137. void WriteError(const asCString &msg, asCScriptCode *file, asCScriptNode *node);
  138. void WriteWarning(const asCString &scriptname, const asCString &msg, int r, int c);
  139. void WriteWarning(const asCString &msg, asCScriptCode *file, asCScriptNode *node);
  140. bool DoesGlobalPropertyExist(const char *prop, asSNameSpace *ns, asCGlobalProperty **outProp = 0, sGlobalVariableDescription **outDesc = 0, bool *isAppProp = 0);
  141. asCGlobalProperty *GetGlobalProperty(const char *prop, asSNameSpace *ns, bool *isCompiled, bool *isPureConstant, asQWORD *constantValue, bool *isAppProp);
  142. int ValidateDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func);
  143. asCString GetCleanExpressionString(asCScriptNode *n, asCScriptCode *file);
  144. asSNameSpace *GetNameSpaceFromNode(asCScriptNode *node, asCScriptCode *script, asSNameSpace *implicitNs, asCScriptNode **next, asCObjectType **objType = 0);
  145. asSNameSpace *GetNameSpaceByString(const asCString &nsName, asSNameSpace *implicitNs, asCScriptNode *errNode, asCScriptCode *script, asCTypeInfo **scopeType = 0, bool isRequired = true);
  146. asCString GetScopeFromNode(asCScriptNode *n, asCScriptCode *script, asCScriptNode **next = 0);
  147. asCTypeInfo *GetType(const char *type, asSNameSpace *ns, asCObjectType *parentType);
  148. asCObjectType *GetObjectType(const char *type, asSNameSpace *ns);
  149. asCFuncdefType *GetFuncDef(const char *type, asSNameSpace *ns, asCObjectType *parentType);
  150. asCTypeInfo *GetTypeFromTypesKnownByObject(const char *type, asCObjectType *currentType);
  151. asCDataType CreateDataTypeFromNode(asCScriptNode *node, asCScriptCode *file, asSNameSpace *implicitNamespace, bool acceptHandleForScope = false, asCObjectType *currentType = 0, bool reportError = true, bool *isValid = 0);
  152. asCObjectType *GetTemplateInstanceFromNode(asCScriptNode *node, asCScriptCode *file, asCObjectType *templateType, asSNameSpace *implicitNamespace, asCObjectType *currentType, asCScriptNode **next = 0);
  153. asCDataType ModifyDataTypeFromNode(const asCDataType &type, asCScriptNode *node, asCScriptCode *file, asETypeModifiers *inOutFlag, bool *autoHandle);
  154. int numErrors;
  155. int numWarnings;
  156. bool silent;
  157. asCScriptEngine *engine;
  158. asCModule *module;
  159. #ifndef AS_NO_COMPILER
  160. protected:
  161. friend class asCCompiler;
  162. int CheckForConflictsDueToDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func, asCObjectType *objType);
  163. int GetNamespaceAndNameFromNode(asCScriptNode *n, asCScriptCode *script, asSNameSpace *implicitNs, asSNameSpace *&outNs, asCString &outName);
  164. int RegisterMixinClass(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  165. sMixinClass *GetMixinClass(const char *name, asSNameSpace *ns);
  166. void IncludePropertiesFromMixins(sClassDeclaration *decl);
  167. void IncludeMethodsFromMixins(sClassDeclaration *decl);
  168. void AddInterfaceToClass(sClassDeclaration *decl, asCScriptNode *errNode, asCObjectType *intf);
  169. void AddInterfaceFromMixinToClass(sClassDeclaration *decl, asCScriptNode *errNode, sMixinClass *mixin);
  170. int RegisterScriptFunctionFromNode(asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false, asSNameSpace *ns = 0, bool isExistingShared = false, bool isMixin = false);
  171. int RegisterScriptFunction(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, bool isInterface, bool isGlobalFunction, asSNameSpace *ns, bool isExistingShared, bool isMixin, asCString &name, asCDataType &returnType, asCArray<asCString> &parameterNames, asCArray<asCDataType> &parameterTypes, asCArray<asETypeModifiers> &inOutFlags, asCArray<asCString *> &defaultArgs, asSFunctionTraits funcTraits);
  172. int RegisterVirtualProperty(asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false, asSNameSpace *ns = 0, bool isExistingShared = false);
  173. int RegisterImportedFunction(int funcID, asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  174. int RegisterGlobalVar(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  175. int RegisterClass(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  176. int RegisterInterface(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  177. int RegisterEnum(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  178. int RegisterTypedef(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
  179. int RegisterFuncDef(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns, asCObjectType *parent);
  180. asCScriptFunction *RegisterLambda(asCScriptNode *node, asCScriptCode *file, asCScriptFunction *funcDef, const asCString &name, asSNameSpace *ns, bool isShared);
  181. void CompleteFuncDef(sFuncDef *funcDef);
  182. void CompileInterfaces();
  183. void CompileClasses(asUINT originalNumTempl);
  184. void DetermineTypeRelations();
  185. void GetParsedFunctionDetails(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, asCString &name, asCDataType &returnType, asCArray<asCString> &parameterNames, asCArray<asCDataType> &parameterTypes, asCArray<asETypeModifiers> &inOutFlags, asCArray<asCString *> &defaultArgs, asSFunctionTraits &traits, asSNameSpace *implicitNamespace);
  186. bool DoesMethodExist(asCObjectType *objType, int methodId, asUINT *methodIndex = 0);
  187. void AddDefaultConstructor(asCObjectType *objType, asCScriptCode *file);
  188. asCObjectProperty *AddPropertyToClass(sClassDeclaration *c, const asCString &name, const asCDataType &type, bool isPrivate, bool isProtected, bool isInherited, asCScriptCode *file = 0, asCScriptNode *node = 0);
  189. int CreateVirtualFunction(asCScriptFunction *func, int idx);
  190. void ParseScripts();
  191. void RegisterTypesFromScript(asCScriptNode *node, asCScriptCode *script, asSNameSpace *ns);
  192. void RegisterNonTypesFromScript(asCScriptNode *node, asCScriptCode *script, asSNameSpace *ns);
  193. void CompileFunctions();
  194. void CompileGlobalVariables();
  195. int GetEnumValueFromType(asCEnumType *type, const char *name, asCDataType &outDt, asDWORD &outValue);
  196. int GetEnumValue(const char *name, asCDataType &outDt, asDWORD &outValue, asSNameSpace *ns);
  197. bool DoesTypeExist(const asCString &type);
  198. asCObjectProperty *GetObjectProperty(asCDataType &obj, const char *prop);
  199. asCScriptFunction *GetFunctionDescription(int funcId);
  200. void GetFunctionDescriptions(const char *name, asCArray<int> &funcs, asSNameSpace *ns);
  201. void GetObjectMethodDescriptions(const char *name, asCObjectType *objectType, asCArray<int> &methods, bool objIsConst, const asCString &scope = "", asCScriptNode *errNode = 0, asCScriptCode *script = 0);
  202. void EvaluateTemplateInstances(asUINT startIdx, bool keepSilent);
  203. void CleanupEnumValues();
  204. asCArray<asCScriptCode *> scripts;
  205. asCArray<sFunctionDescription *> functions;
  206. asCSymbolTable<sGlobalVariableDescription> globVariables;
  207. asCArray<sClassDeclaration *> classDeclarations;
  208. asCArray<sClassDeclaration *> interfaceDeclarations;
  209. asCArray<sClassDeclaration *> namedTypeDeclarations;
  210. asCArray<sFuncDef *> funcDefs;
  211. asCArray<sMixinClass *> mixinClasses;
  212. // For use with the DoesTypeExists() method
  213. bool hasCachedKnownTypes;
  214. asCMap<asCString, bool> knownTypes;
  215. #endif
  216. };
  217. END_AS_NAMESPACE
  218. #endif