as_builder.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 sFunctionDescription
  40. {
  41. asCScriptCode *script;
  42. asCScriptNode *node;
  43. asCString name;
  44. asCObjectType *objType;
  45. int funcId;
  46. };
  47. struct sGlobalVariableDescription
  48. {
  49. asCScriptCode *script;
  50. asCScriptNode *idNode;
  51. asCScriptNode *nextNode;
  52. asCString name;
  53. asCGlobalProperty *property;
  54. asCDataType datatype;
  55. int index;
  56. bool isCompiled;
  57. bool isPureConstant;
  58. bool isEnumValue;
  59. asQWORD constantValue;
  60. };
  61. struct sClassDeclaration
  62. {
  63. asCScriptCode *script;
  64. asCScriptNode *node;
  65. asCString name;
  66. int validState;
  67. asCObjectType *objType;
  68. };
  69. struct sFuncDef
  70. {
  71. asCScriptCode *script;
  72. asCScriptNode *node;
  73. asCString name;
  74. int idx;
  75. };
  76. class asCCompiler;
  77. class asCBuilder
  78. {
  79. public:
  80. asCBuilder(asCScriptEngine *engine, asCModule *module);
  81. ~asCBuilder();
  82. int VerifyProperty(asCDataType *dt, const char *decl, asCString &outName, asCDataType &outType);
  83. int ParseDataType(const char *datatype, asCDataType *result);
  84. int ParseTemplateDecl(const char *decl, asCString *name, asCString *subtypeName);
  85. int ParseFunctionDeclaration(asCObjectType *type, const char *decl, asCScriptFunction *func, bool isSystemFunction, asCArray<bool> *paramAutoHandles = 0, bool *returnAutoHandle = 0);
  86. int ParseVariableDeclaration(const char *decl, asCObjectProperty *var);
  87. int AddCode(const char *name, const char *code, int codeLength, int lineOffset, int sectionIdx, bool makeCopy);
  88. int Build();
  89. int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asCScriptFunction **outFunc);
  90. int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  91. void WriteInfo(const char *scriptname, const char *msg, int r, int c, bool preMessage);
  92. void WriteError(const char *scriptname, const char *msg, int r, int c);
  93. void WriteWarning(const char *scriptname, const char *msg, int r, int c);
  94. int CheckNameConflict(const char *name, asCScriptNode *node, asCScriptCode *code);
  95. int CheckNameConflictMember(asCObjectType *type, const char *name, asCScriptNode *node, asCScriptCode *code, bool isProperty);
  96. protected:
  97. friend class asCCompiler;
  98. friend class asCModule;
  99. friend class asCParser;
  100. asCObjectProperty *GetObjectProperty(asCDataType &obj, const char *prop);
  101. asCGlobalProperty *GetGlobalProperty(const char *prop, bool *isCompiled, bool *isPureConstant, asQWORD *constantValue);
  102. asCScriptFunction *GetFunctionDescription(int funcID);
  103. void GetFunctionDescriptions(const char *name, asCArray<int> &funcs);
  104. void GetObjectMethodDescriptions(const char *name, asCObjectType *objectType, asCArray<int> &methods, bool objIsConst, const asCString &scope = "");
  105. int RegisterScriptFunction(int funcID, asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false);
  106. int RegisterImportedFunction(int funcID, asCScriptNode *node, asCScriptCode *file);
  107. int RegisterGlobalVar(asCScriptNode *node, asCScriptCode *file);
  108. int RegisterClass(asCScriptNode *node, asCScriptCode *file);
  109. int RegisterInterface(asCScriptNode *node, asCScriptCode *file);
  110. int RegisterEnum(asCScriptNode *node, asCScriptCode *file);
  111. int RegisterTypedef(asCScriptNode *node, asCScriptCode *file);
  112. int RegisterFuncDef(asCScriptNode *node, asCScriptCode *file);
  113. void CompleteFuncDef(sFuncDef *funcDef);
  114. void CompileClasses();
  115. 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);
  116. int ValidateDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func);
  117. bool DoesMethodExist(asCObjectType *objType, int methodId);
  118. void AddDefaultConstructor(asCObjectType *objType, asCScriptCode *file);
  119. asCObjectProperty *AddPropertyToClass(sClassDeclaration *c, const asCString &name, const asCDataType &type, bool isPrivate, asCScriptCode *file = 0, asCScriptNode *node = 0);
  120. int CreateVirtualFunction(asCScriptFunction *func, int idx);
  121. asCObjectType *GetObjectType(const char *type);
  122. asCScriptFunction *GetFuncDef(const char *type);
  123. int GetEnumValueFromObjectType(asCObjectType *objType, const char *name, asCDataType &outDt, asDWORD &outValue);
  124. int GetEnumValue(const char *name, asCDataType &outDt, asDWORD &outValue);
  125. asCString GetCleanExpressionString(asCScriptNode *n, asCScriptCode *file);
  126. void ParseScripts();
  127. void CompileFunctions();
  128. void CompileGlobalVariables();
  129. struct preMessage_t
  130. {
  131. bool isSet;
  132. asCString message;
  133. int r;
  134. int c;
  135. } preMessage;
  136. int numErrors;
  137. int numWarnings;
  138. asCArray<asCScriptCode *> scripts;
  139. asCArray<sFunctionDescription *> functions;
  140. asCArray<sGlobalVariableDescription *> globVariables;
  141. asCArray<sClassDeclaration *> classDeclarations;
  142. asCArray<sClassDeclaration *> interfaceDeclarations;
  143. asCArray<sClassDeclaration *> namedTypeDeclarations;
  144. asCArray<sFuncDef *> funcDefs;
  145. asCScriptEngine *engine;
  146. asCModule *module;
  147. asCDataType CreateDataTypeFromNode(asCScriptNode *node, asCScriptCode *file, bool acceptHandleForScope = false, asCObjectType *templateType = 0);
  148. asCDataType ModifyDataTypeFromNode(const asCDataType &type, asCScriptNode *node, asCScriptCode *file, asETypeModifiers *inOutFlag, bool *autoHandle);
  149. };
  150. END_AS_NAMESPACE
  151. #endif