ScummVM API documentation
as_builder.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2021 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_builder.h
34 //
35 // This is the class that manages the compilation of the scripts
36 //
37 
38 
39 #ifndef AS_BUILDER_H
40 #define AS_BUILDER_H
41 
42 #include "as_config.h"
43 #include "as_symboltable.h"
44 #include "as_scriptengine.h"
45 #include "as_module.h"
46 #include "as_memory.h"
47 #include "as_scriptcode.h"
48 #include "as_scriptnode.h"
49 #include "as_datatype.h"
50 #include "as_property.h"
51 
52 BEGIN_AS_NAMESPACE
53 
54 #ifdef AS_NO_COMPILER
55 // Forward declare the structure, as it is part of some function signatures used even without the compiler
57 #endif
58 
59 #ifndef AS_NO_COMPILER
60 
62  asCScriptCode *script;
63  asCScriptNode *node;
64  asCString name;
65  asCObjectType *objType;
66  asCArray<asCString> paramNames;
67  int funcId;
68  bool isExistingShared;
69 };
70 
72  asCScriptCode *script;
73  asCScriptNode *declaredAtNode;
74  asCScriptNode *initializationNode;
75  asCString name;
76  asCGlobalProperty *property;
77  asCDataType datatype;
78  asSNameSpace *ns;
79  int index;
80  bool isCompiled;
81  bool isPureConstant;
82  bool isEnumValue;
83  asQWORD constantValue;
84 };
85 
87  sPropertyInitializer() : declNode(0), initNode(0), file(0) {}
88  sPropertyInitializer(const asCString &nm, asCScriptNode *decl, asCScriptNode *init, asCScriptCode *f) : name(nm), declNode(decl), initNode(init), file(f) {}
89  sPropertyInitializer &operator=(const sPropertyInitializer &o) {
90  name = o.name;
91  declNode = o.declNode;
92  initNode = o.initNode;
93  file = o.file;
94  return *this;
95  }
96 
97  asCString name;
98  asCScriptNode *declNode;
99  asCScriptNode *initNode;
100  asCScriptCode *file;
101 };
102 
105  script = 0;
106  node = 0;
107  validState = 0;
108  typeInfo = 0;
109  isExistingShared = false;
110  isFinal = false;
111  }
112 
113  asCScriptCode *script;
114  asCScriptNode *node;
115  asCString name;
116  int validState;
117  asCTypeInfo *typeInfo;
118  bool isExistingShared;
119  bool isFinal;
120 
122 };
123 
124 struct sFuncDef {
125  asCScriptCode *script;
126  asCScriptNode *node;
127  asCString name;
128  int idx;
129 };
130 
131 struct sMixinClass {
132  asCScriptCode *script;
133  asCScriptNode *node;
134  asCString name;
135  asSNameSpace *ns;
136 };
137 
138 #endif // AS_NO_COMPILER
139 
140 class asCBuilder {
141 public:
142  asCBuilder(asCScriptEngine *engine, asCModule *module);
143  ~asCBuilder();
144 
145  // These methods are used by the application interface
146  int VerifyProperty(asCDataType *dt, const char *decl, asCString &outName, asCDataType &outType, asSNameSpace *ns);
147  int ParseDataType(const char *datatype, asCDataType *result, asSNameSpace *implicitNamespace, bool isReturnType = false);
148  int ParseTemplateDecl(const char *decl, asCString *name, asCArray<asCString> &subtypeNames);
149  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);
150  int ParseVariableDeclaration(const char *decl, asSNameSpace *implicitNamespace, asCString &outName, asSNameSpace *&outNamespace, asCDataType &outDt);
151  int CheckNameConflict(const char *name, asCScriptNode *node, asCScriptCode *code, asSNameSpace *ns, bool isProperty, bool isVirtualProperty);
152  int CheckNameConflictMember(asCTypeInfo *type, const char *name, asCScriptNode *node, asCScriptCode *code, bool isProperty, bool isVirtualProperty);
153  int ValidateVirtualProperty(asCScriptFunction *func);
154 
155 #ifndef AS_NO_COMPILER
156  int AddCode(const char *name, const char *code, int codeLength, int lineOffset, int sectionIdx, bool makeCopy);
157  asCScriptCode *FindOrAddCode(const char *name, const char *code, size_t length);
158  int Build();
159 
160  int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asCScriptFunction **outFunc);
161  int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
162 #endif
163 
164 protected:
165  friend class asCModule;
166  friend class asCParser;
167  friend class asCScriptFunction;
168  friend class asCScriptEngine;
169 
170  void Reset();
171 
172  void WriteInfo(const asCString &scriptname, const asCString &msg, int r, int c, bool preMessage);
173  void WriteInfo(const asCString &msg, asCScriptCode *file, asCScriptNode *node);
174  void WriteError(const asCString &scriptname, const asCString &msg, int r, int c);
175  void WriteError(const asCString &msg, asCScriptCode *file, asCScriptNode *node);
176  void WriteWarning(const asCString &scriptname, const asCString &msg, int r, int c);
177  void WriteWarning(const asCString &msg, asCScriptCode *file, asCScriptNode *node);
178 
179  bool DoesGlobalPropertyExist(const char *prop, asSNameSpace *ns, asCGlobalProperty **outProp = 0, sGlobalVariableDescription **outDesc = 0, bool *isAppProp = 0);
180  asCGlobalProperty *GetGlobalProperty(const char *prop, asSNameSpace *ns, bool *isCompiled, bool *isPureConstant, asQWORD *constantValue, bool *isAppProp);
181  int ValidateDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func);
182  asCString GetCleanExpressionString(asCScriptNode *n, asCScriptCode *file);
183 
184  asSNameSpace *GetNameSpaceFromNode(asCScriptNode *node, asCScriptCode *script, asSNameSpace *implicitNs, asCScriptNode **next, asCObjectType **objType = 0);
185  asSNameSpace *GetNameSpaceByString(const asCString &nsName, asSNameSpace *implicitNs, asCScriptNode *errNode, asCScriptCode *script, asCTypeInfo **scopeType = 0, bool isRequired = true);
186  asCString GetScopeFromNode(asCScriptNode *n, asCScriptCode *script, asCScriptNode **next = 0);
187 
188  asCTypeInfo *GetType(const char *type, asSNameSpace *ns, asCObjectType *parentType);
189  asCObjectType *GetObjectType(const char *type, asSNameSpace *ns);
190  asCFuncdefType *GetFuncDef(const char *type, asSNameSpace *ns, asCObjectType *parentType);
191  asCTypeInfo *GetTypeFromTypesKnownByObject(const char *type, asCObjectType *currentType);
192  asCDataType CreateDataTypeFromNode(asCScriptNode *node, asCScriptCode *file, asSNameSpace *implicitNamespace, bool acceptHandleForScope = false, asCObjectType *currentType = 0, bool reportError = true, bool *isValid = 0);
193  asCObjectType *GetTemplateInstanceFromNode(asCScriptNode *node, asCScriptCode *file, asCObjectType *templateType, asSNameSpace *implicitNamespace, asCObjectType *currentType, asCScriptNode **next = 0);
194  asCDataType ModifyDataTypeFromNode(const asCDataType &type, asCScriptNode *node, asCScriptCode *file, asETypeModifiers *inOutFlag, bool *autoHandle);
195 
196  int numErrors;
197  int numWarnings;
198  bool silent;
199 
200  asCScriptEngine *engine;
201  asCModule *module;
202 
203 #ifndef AS_NO_COMPILER
204 protected:
205  friend class asCCompiler;
206 
207  int CheckForConflictsDueToDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func, asCObjectType *objType);
208  int GetNamespaceAndNameFromNode(asCScriptNode *n, asCScriptCode *script, asSNameSpace *implicitNs, asSNameSpace *&outNs, asCString &outName);
209  int RegisterMixinClass(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
210  sMixinClass *GetMixinClass(const char *name, asSNameSpace *ns);
211  void IncludePropertiesFromMixins(sClassDeclaration *decl);
212  void IncludeMethodsFromMixins(sClassDeclaration *decl);
213  void AddInterfaceToClass(sClassDeclaration *decl, asCScriptNode *errNode, asCObjectType *intf);
214  void AddInterfaceFromMixinToClass(sClassDeclaration *decl, asCScriptNode *errNode, sMixinClass *mixin);
215 
216  int RegisterScriptFunctionFromNode(asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false, asSNameSpace *ns = 0, bool isExistingShared = false, bool isMixin = false);
217  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);
218  int RegisterVirtualProperty(asCScriptNode *node, asCScriptCode *file, asCObjectType *object = 0, bool isInterface = false, bool isGlobalFunction = false, asSNameSpace *ns = 0, bool isExistingShared = false);
219  int RegisterImportedFunction(int funcID, asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
220  int RegisterGlobalVar(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
221  int RegisterClass(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
222  int RegisterInterface(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
223  int RegisterEnum(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
224  int RegisterTypedef(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns);
225  int RegisterFuncDef(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns, asCObjectType *parent);
226  asCScriptFunction *RegisterLambda(asCScriptNode *node, asCScriptCode *file, asCScriptFunction *funcDef, const asCString &name, asSNameSpace *ns, bool isShared);
227  void CompleteFuncDef(sFuncDef *funcDef);
228  void CompileInterfaces();
229  void CompileClasses(asUINT originalNumTempl);
230  void DetermineTypeRelations();
231  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);
232  bool DoesMethodExist(asCObjectType *objType, int methodId, asUINT *methodIndex = 0);
233  void AddDefaultConstructor(asCObjectType *objType, asCScriptCode *file);
234  asCObjectProperty *AddPropertyToClass(sClassDeclaration *c, const asCString &name, const asCDataType &type, bool isPrivate, bool isProtected, bool isInherited, asCScriptCode *file = 0, asCScriptNode *node = 0);
235  int CreateVirtualFunction(asCScriptFunction *func, int idx);
236  void ParseScripts();
237  void RegisterTypesFromScript(asCScriptNode *node, asCScriptCode *script, asSNameSpace *ns);
238  void RegisterNonTypesFromScript(asCScriptNode *node, asCScriptCode *script, asSNameSpace *ns);
239  void CompileFunctions();
240  void CompileGlobalVariables();
241  int GetEnumValueFromType(asCEnumType *type, const char *name, asCDataType &outDt, asDWORD &outValue);
242  int GetEnumValue(const char *name, asCDataType &outDt, asDWORD &outValue, asSNameSpace *ns);
243  bool DoesTypeExist(const asCString &type);
244  asCObjectProperty *GetObjectProperty(asCDataType &obj, const char *prop);
245  asCScriptFunction *GetFunctionDescription(int funcId);
246  void GetFunctionDescriptions(const char *name, asCArray<int> &funcs, asSNameSpace *ns);
247  void GetObjectMethodDescriptions(const char *name, asCObjectType *objectType, asCArray<int> &methods, bool objIsConst, const asCString &scope = "", asCScriptNode *errNode = 0, asCScriptCode *script = 0);
248  void EvaluateTemplateInstances(asUINT startIdx, bool keepSilent);
249  void CleanupEnumValues();
250 
254  asCArray<sClassDeclaration *> classDeclarations;
255  asCArray<sClassDeclaration *> interfaceDeclarations;
256  asCArray<sClassDeclaration *> namedTypeDeclarations;
257  asCArray<sFuncDef *> funcDefs;
258  asCArray<sMixinClass *> mixinClasses;
259 
260  // For use with the DoesTypeExists() method
261  bool hasCachedKnownTypes;
262  asCMap<asCString, bool> knownTypes;
263 #endif
264 };
265 
266 END_AS_NAMESPACE
267 
268 #endif
Definition: as_module.h:93
Definition: as_typeinfo.h:64
Definition: as_builder.h:140
Definition: as_scriptcode.h:48
Definition: as_scriptfunction.h:146
Definition: as_objecttype.h:100
Definition: as_builder.h:124
Definition: as_property.h:53
Definition: as_namespace.h:39
Definition: as_scriptfunction.h:114
Definition: as_compiler.h:209
Definition: as_builder.h:86
Definition: as_typeinfo.h:263
Definition: as_builder.h:103
Definition: as_datatype.h:60
Definition: as_property.h:68
Definition: as_builder.h:61
Definition: as_parser.h:50
Definition: as_typeinfo.h:292
Definition: as_builder.h:131
Definition: as_builder.h:71
Definition: as_scriptengine.h:64
Definition: as_scriptnode.h:104
Definition: as_string.h:41