as_builder.cpp 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  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. // Modified by Lasse Öörni for Urho3D
  24. //
  25. // as_builder.cpp
  26. //
  27. // This is the class that manages the compilation of the scripts
  28. //
  29. #include "as_config.h"
  30. #include "as_builder.h"
  31. #include "as_parser.h"
  32. #include "as_compiler.h"
  33. #include "as_tokendef.h"
  34. #include "as_string_util.h"
  35. #include "as_outputbuffer.h"
  36. #include "as_texts.h"
  37. #include "as_scriptobject.h"
  38. BEGIN_AS_NAMESPACE
  39. asCBuilder::asCBuilder(asCScriptEngine *engine, asCModule *module)
  40. {
  41. this->engine = engine;
  42. this->module = module;
  43. }
  44. asCBuilder::~asCBuilder()
  45. {
  46. asUINT n;
  47. // Free all functions
  48. for( n = 0; n < functions.GetLength(); n++ )
  49. {
  50. if( functions[n] )
  51. {
  52. if( functions[n]->node )
  53. {
  54. functions[n]->node->Destroy(engine);
  55. }
  56. if( functions[n]->explicitSignature )
  57. {
  58. asDELETE(functions[n]->explicitSignature,sExplicitSignature);
  59. }
  60. asDELETE(functions[n],sFunctionDescription);
  61. }
  62. functions[n] = 0;
  63. }
  64. // Free all global variables
  65. for( n = 0; n < globVariables.GetLength(); n++ )
  66. {
  67. if( globVariables[n] )
  68. {
  69. if( globVariables[n]->nextNode )
  70. {
  71. globVariables[n]->nextNode->Destroy(engine);
  72. }
  73. asDELETE(globVariables[n],sGlobalVariableDescription);
  74. globVariables[n] = 0;
  75. }
  76. }
  77. // Free all the loaded files
  78. for( n = 0; n < scripts.GetLength(); n++ )
  79. {
  80. if( scripts[n] )
  81. {
  82. asDELETE(scripts[n],asCScriptCode);
  83. }
  84. scripts[n] = 0;
  85. }
  86. // Free all class declarations
  87. for( n = 0; n < classDeclarations.GetLength(); n++ )
  88. {
  89. if( classDeclarations[n] )
  90. {
  91. if( classDeclarations[n]->node )
  92. {
  93. classDeclarations[n]->node->Destroy(engine);
  94. }
  95. asDELETE(classDeclarations[n],sClassDeclaration);
  96. classDeclarations[n] = 0;
  97. }
  98. }
  99. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  100. {
  101. if( interfaceDeclarations[n] )
  102. {
  103. if( interfaceDeclarations[n]->node )
  104. {
  105. interfaceDeclarations[n]->node->Destroy(engine);
  106. }
  107. asDELETE(interfaceDeclarations[n],sClassDeclaration);
  108. interfaceDeclarations[n] = 0;
  109. }
  110. }
  111. for( n = 0; n < namedTypeDeclarations.GetLength(); n++ )
  112. {
  113. if( namedTypeDeclarations[n] )
  114. {
  115. if( namedTypeDeclarations[n]->node )
  116. {
  117. namedTypeDeclarations[n]->node->Destroy(engine);
  118. }
  119. asDELETE(namedTypeDeclarations[n],sClassDeclaration);
  120. namedTypeDeclarations[n] = 0;
  121. }
  122. }
  123. for( n = 0; n < funcDefs.GetLength(); n++ )
  124. {
  125. if( funcDefs[n] )
  126. {
  127. if( funcDefs[n]->node )
  128. funcDefs[n]->node->Destroy(engine);
  129. asDELETE(funcDefs[n],sFuncDef);
  130. funcDefs[n] = 0;
  131. }
  132. }
  133. }
  134. int asCBuilder::AddCode(const char *name, const char *code, int codeLength, int lineOffset, int sectionIdx, bool makeCopy)
  135. {
  136. asCScriptCode *script = asNEW(asCScriptCode);
  137. int r = script->SetCode(name, code, codeLength, makeCopy);
  138. script->lineOffset = lineOffset;
  139. script->idx = sectionIdx;
  140. scripts.PushLast(script);
  141. return r;
  142. }
  143. int asCBuilder::Build()
  144. {
  145. numErrors = 0;
  146. numWarnings = 0;
  147. preMessage.isSet = false;
  148. ParseScripts();
  149. CompileClasses();
  150. CompileGlobalVariables();
  151. CompileFunctions();
  152. if( numErrors > 0 )
  153. return asERROR;
  154. return asSUCCESS;
  155. }
  156. int asCBuilder::CompileGlobalVar(const char *sectionName, const char *code, int lineOffset)
  157. {
  158. numErrors = 0;
  159. numWarnings = 0;
  160. preMessage.isSet = false;
  161. // Add the string to the script code
  162. asCScriptCode *script = asNEW(asCScriptCode);
  163. script->SetCode(sectionName, code, true);
  164. script->lineOffset = lineOffset;
  165. scripts.PushLast(script);
  166. // Parse the string
  167. asCParser parser(this);
  168. if( parser.ParseScript(scripts[0]) < 0 )
  169. return asERROR;
  170. asCScriptNode *node = parser.GetScriptNode();
  171. // Make sure there is nothing else than the global variable in the script code
  172. if( node == 0 ||
  173. node->firstChild == 0 ||
  174. node->firstChild != node->lastChild ||
  175. node->firstChild->nodeType != snGlobalVar )
  176. {
  177. WriteError(script->name.AddressOf(), TXT_ONLY_ONE_VARIABLE_ALLOWED, 0, 0);
  178. return asERROR;
  179. }
  180. node = node->firstChild;
  181. node->DisconnectParent();
  182. RegisterGlobalVar(node, script);
  183. CompileGlobalVariables();
  184. if( numErrors > 0 )
  185. {
  186. // Remove the variable from the module, if it was registered
  187. if( globVariables.GetLength() > 0 )
  188. {
  189. module->RemoveGlobalVar(module->GetGlobalVarCount()-1);
  190. }
  191. return asERROR;
  192. }
  193. return 0;
  194. }
  195. int asCBuilder::ValidateDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func)
  196. {
  197. int firstArgWithDefaultValue = -1;
  198. for( asUINT n = 0; n < func->defaultArgs.GetLength(); n++ )
  199. {
  200. if( func->defaultArgs[n] )
  201. firstArgWithDefaultValue = n;
  202. else if( firstArgWithDefaultValue >= 0 )
  203. {
  204. int r, c;
  205. script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  206. asCString str;
  207. str.Format(TXT_DEF_ARG_MISSING_IN_FUNC_s, func->GetDeclaration());
  208. WriteError(script->name.AddressOf(), str.AddressOf(), r, c);
  209. return asINVALID_DECLARATION;
  210. }
  211. }
  212. return 0;
  213. }
  214. int asCBuilder::CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asCScriptFunction **outFunc)
  215. {
  216. asASSERT(outFunc != 0);
  217. numErrors = 0;
  218. numWarnings = 0;
  219. preMessage.isSet = false;
  220. // Add the string to the script code
  221. asCScriptCode *script = asNEW(asCScriptCode);
  222. script->SetCode(sectionName, code, true);
  223. script->lineOffset = lineOffset;
  224. scripts.PushLast(script);
  225. // Parse the string
  226. asCParser parser(this);
  227. if( parser.ParseScript(scripts[0]) < 0 )
  228. return asERROR;
  229. asCScriptNode *node = parser.GetScriptNode();
  230. // Make sure there is nothing else than the function in the script code
  231. if( node == 0 ||
  232. node->firstChild == 0 ||
  233. node->firstChild != node->lastChild ||
  234. node->firstChild->nodeType != snFunction )
  235. {
  236. WriteError(script->name.AddressOf(), TXT_ONLY_ONE_FUNCTION_ALLOWED, 0, 0);
  237. return asERROR;
  238. }
  239. // Find the function node
  240. node = node->firstChild;
  241. // Create the function
  242. bool isConstructor, isDestructor, isPrivate, isFinal, isOverride;
  243. asCScriptFunction *func = asNEW(asCScriptFunction)(engine,module,asFUNC_SCRIPT);
  244. GetParsedFunctionDetails(node, scripts[0], 0, func->name, func->returnType, func->parameterTypes, func->inOutFlags, func->defaultArgs, func->isReadOnly, isConstructor, isDestructor, isPrivate, isFinal, isOverride);
  245. func->id = engine->GetNextScriptFunctionId();
  246. func->scriptSectionIdx = engine->GetScriptSectionNameIndex(sectionName ? sectionName : "");
  247. // Make sure the default args are declared correctly
  248. int r = ValidateDefaultArgs(script, node, func);
  249. if( r < 0 )
  250. {
  251. func->Release();
  252. return asERROR;
  253. }
  254. // Tell the engine that the function exists already so the compiler can access it
  255. if( compileFlags & asCOMP_ADD_TO_MODULE )
  256. {
  257. int r = CheckNameConflict(func->name.AddressOf(), node, scripts[0]);
  258. if( r < 0 )
  259. {
  260. func->Release();
  261. return asERROR;
  262. }
  263. module->globalFunctions.PushLast(func);
  264. func->AddRef();
  265. module->AddScriptFunction(func);
  266. }
  267. else
  268. engine->SetScriptFunction(func);
  269. // Fill in the function info for the builder too
  270. node->DisconnectParent();
  271. sFunctionDescription *funcDesc = asNEW(sFunctionDescription);
  272. functions.PushLast(funcDesc);
  273. funcDesc->script = scripts[0];
  274. funcDesc->node = node;
  275. funcDesc->name = func->name;
  276. funcDesc->funcId = func->id;
  277. funcDesc->explicitSignature = 0;
  278. asCCompiler compiler(engine);
  279. if( compiler.CompileFunction(this, functions[0]->script, 0, functions[0]->node, func) >= 0 )
  280. {
  281. // Return the function
  282. *outFunc = func;
  283. }
  284. else
  285. {
  286. // If the function was added to the module then remove it again
  287. if( compileFlags & asCOMP_ADD_TO_MODULE )
  288. {
  289. module->globalFunctions.RemoveValue(func);
  290. module->scriptFunctions.RemoveValue(func);
  291. func->Release();
  292. func->Release();
  293. }
  294. func->Release();
  295. return asERROR;
  296. }
  297. return asSUCCESS;
  298. }
  299. void asCBuilder::ParseScripts()
  300. {
  301. asCArray<asCParser*> parsers((int)scripts.GetLength());
  302. // Parse all the files as if they were one
  303. asUINT n = 0;
  304. for( n = 0; n < scripts.GetLength(); n++ )
  305. {
  306. asCParser *parser = asNEW(asCParser)(this);
  307. parsers.PushLast(parser);
  308. // Parse the script file
  309. parser->ParseScript(scripts[n]);
  310. }
  311. if( numErrors == 0 )
  312. {
  313. // Find all type declarations
  314. for( n = 0; n < scripts.GetLength(); n++ )
  315. {
  316. asCScriptNode *node = parsers[n]->GetScriptNode();
  317. // Find structure definitions first
  318. node = node->firstChild;
  319. while( node )
  320. {
  321. asCScriptNode *next = node->next;
  322. if( node->nodeType == snClass )
  323. {
  324. node->DisconnectParent();
  325. RegisterClass(node, scripts[n]);
  326. }
  327. else if( node->nodeType == snInterface )
  328. {
  329. node->DisconnectParent();
  330. RegisterInterface(node, scripts[n]);
  331. }
  332. // Handle enumeration
  333. else if( node->nodeType == snEnum )
  334. {
  335. node->DisconnectParent();
  336. RegisterEnum(node, scripts[n]);
  337. }
  338. // Handle typedef
  339. else if( node->nodeType == snTypedef )
  340. {
  341. node->DisconnectParent();
  342. RegisterTypedef(node, scripts[n]);
  343. }
  344. else if( node->nodeType == snFuncDef )
  345. {
  346. node->DisconnectParent();
  347. RegisterFuncDef(node, scripts[n]);
  348. }
  349. node = next;
  350. }
  351. }
  352. // Register the complete function definitions
  353. for( n = 0; n < funcDefs.GetLength(); n++ )
  354. {
  355. CompleteFuncDef(funcDefs[n]);
  356. }
  357. // Register script methods found in the interfaces
  358. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  359. {
  360. sClassDeclaration *decl = interfaceDeclarations[n];
  361. if( decl->isExistingShared ) continue; // TODO: shared: Should really verify that the methods match the original
  362. asCScriptNode *node = decl->node->firstChild->next;
  363. while( node )
  364. {
  365. asCScriptNode *next = node->next;
  366. if( node->nodeType == snFunction )
  367. {
  368. node->DisconnectParent();
  369. RegisterScriptFunction(engine->GetNextScriptFunctionId(), node, decl->script, decl->objType, true);
  370. }
  371. else if( node->nodeType == snVirtualProperty )
  372. {
  373. node->DisconnectParent();
  374. RegisterVirtualProperty(node, decl->script, decl->objType, true, false);
  375. }
  376. node = next;
  377. }
  378. }
  379. // Now the interfaces have been completely established, now we need to determine if
  380. // the same interface has already been registered before, and if so reuse the interface id.
  381. module->ResolveInterfaceIds();
  382. // Register script methods found in the structures
  383. for( n = 0; n < classDeclarations.GetLength(); n++ )
  384. {
  385. sClassDeclaration *decl = classDeclarations[n];
  386. if( decl->isExistingShared ) continue; // TODO: shared: Should really verify that the methods match the original
  387. asCScriptNode *node = decl->node->firstChild->next;
  388. // Skip list of classes and interfaces
  389. while( node && node->nodeType == snIdentifier )
  390. node = node->next;
  391. while( node )
  392. {
  393. asCScriptNode *next = node->next;
  394. if( node->nodeType == snFunction )
  395. {
  396. node->DisconnectParent();
  397. RegisterScriptFunction(engine->GetNextScriptFunctionId(), node, decl->script, decl->objType);
  398. }
  399. else if( node->nodeType == snVirtualProperty )
  400. {
  401. node->DisconnectParent();
  402. RegisterVirtualProperty(node, decl->script, decl->objType, false, false);
  403. }
  404. node = next;
  405. }
  406. // Make sure the default factory & constructor exists for classes
  407. if( decl->objType->beh.construct == engine->scriptTypeBehaviours.beh.construct )
  408. {
  409. if( decl->objType->beh.constructors.GetLength() == 1 || engine->ep.alwaysImplDefaultConstruct )
  410. {
  411. AddDefaultConstructor(decl->objType, decl->script);
  412. }
  413. else
  414. {
  415. // As the class has another constructor we shouldn't provide the default constructor
  416. if( decl->objType->beh.construct )
  417. {
  418. engine->scriptFunctions[decl->objType->beh.construct]->Release();
  419. decl->objType->beh.construct = 0;
  420. decl->objType->beh.constructors.RemoveIndex(0);
  421. }
  422. if( decl->objType->beh.factory )
  423. {
  424. engine->scriptFunctions[decl->objType->beh.factory]->Release();
  425. decl->objType->beh.factory = 0;
  426. decl->objType->beh.factories.RemoveIndex(0);
  427. }
  428. if( decl->objType->beh.copy )
  429. {
  430. engine->scriptFunctions[decl->objType->beh.copy]->Release();
  431. decl->objType->beh.copy = 0;
  432. }
  433. }
  434. }
  435. }
  436. // Find other global nodes
  437. for( n = 0; n < scripts.GetLength(); n++ )
  438. {
  439. // Find other global nodes
  440. asCScriptNode *node = parsers[n]->GetScriptNode();
  441. node = node->firstChild;
  442. while( node )
  443. {
  444. asCScriptNode *next = node->next;
  445. node->DisconnectParent();
  446. if( node->nodeType == snFunction )
  447. {
  448. RegisterScriptFunction(engine->GetNextScriptFunctionId(), node, scripts[n], 0, false, true);
  449. }
  450. else if( node->nodeType == snGlobalVar )
  451. {
  452. RegisterGlobalVar(node, scripts[n]);
  453. }
  454. else if( node->nodeType == snVirtualProperty )
  455. {
  456. RegisterVirtualProperty(node, scripts[n], 0, false, true);
  457. }
  458. else if( node->nodeType == snImport )
  459. {
  460. RegisterImportedFunction(module->GetNextImportedFunctionId(), node, scripts[n]);
  461. }
  462. else
  463. {
  464. // Unused script node
  465. int r, c;
  466. scripts[n]->ConvertPosToRowCol(node->tokenPos, &r, &c);
  467. WriteWarning(scripts[n]->name.AddressOf(), TXT_UNUSED_SCRIPT_NODE, r, c);
  468. node->Destroy(engine);
  469. }
  470. node = next;
  471. }
  472. }
  473. }
  474. for( n = 0; n < parsers.GetLength(); n++ )
  475. {
  476. asDELETE(parsers[n],asCParser);
  477. }
  478. }
  479. void asCBuilder::CompileFunctions()
  480. {
  481. // Compile each function
  482. for( asUINT n = 0; n < functions.GetLength(); n++ )
  483. {
  484. sFunctionDescription *current = functions[n];
  485. if( current == 0 ) continue;
  486. asCCompiler compiler(engine);
  487. asCScriptFunction *func = engine->scriptFunctions[current->funcId];
  488. if( current->node )
  489. {
  490. int r, c;
  491. current->script->ConvertPosToRowCol(current->node->tokenPos, &r, &c);
  492. asCString str = func->GetDeclarationStr();
  493. str.Format(TXT_COMPILING_s, str.AddressOf());
  494. WriteInfo(current->script->name.AddressOf(), str.AddressOf(), r, c, true);
  495. compiler.CompileFunction(this, current->script, current->explicitSignature, current->node, func);
  496. preMessage.isSet = false;
  497. }
  498. else if( current->name == current->objType->name )
  499. {
  500. // This is the default constructor, that is generated
  501. // automatically if not implemented by the user.
  502. compiler.CompileDefaultConstructor(this, current->script, func);
  503. }
  504. else
  505. {
  506. // This is a property accessor, but no implementation was provided for it
  507. asASSERT( current->explicitSignature );
  508. }
  509. }
  510. }
  511. int asCBuilder::ParseDataType(const char *datatype, asCDataType *result)
  512. {
  513. numErrors = 0;
  514. numWarnings = 0;
  515. preMessage.isSet = false;
  516. asCScriptCode source;
  517. source.SetCode("", datatype, true);
  518. asCParser parser(this);
  519. int r = parser.ParseDataType(&source);
  520. if( r < 0 )
  521. return asINVALID_TYPE;
  522. // Get data type and property name
  523. asCScriptNode *dataType = parser.GetScriptNode()->firstChild;
  524. *result = CreateDataTypeFromNode(dataType, &source, true);
  525. if( numErrors > 0 )
  526. return asINVALID_TYPE;
  527. return asSUCCESS;
  528. }
  529. int asCBuilder::ParseTemplateDecl(const char *decl, asCString *name, asCString *subtypeName)
  530. {
  531. numErrors = 0;
  532. numWarnings = 0;
  533. preMessage.isSet = false;
  534. asCScriptCode source;
  535. source.SetCode("", decl, true);
  536. asCParser parser(this);
  537. int r = parser.ParseTemplateDecl(&source);
  538. if( r < 0 )
  539. return asINVALID_TYPE;
  540. // Get the template name and subtype name
  541. asCScriptNode *node = parser.GetScriptNode()->firstChild;
  542. name->Assign(&decl[node->tokenPos], node->tokenLength);
  543. node = node->next;
  544. subtypeName->Assign(&decl[node->tokenPos], node->tokenLength);
  545. // TODO: template: check for name conflicts
  546. if( numErrors > 0 )
  547. return asINVALID_DECLARATION;
  548. return asSUCCESS;
  549. }
  550. int asCBuilder::VerifyProperty(asCDataType *dt, const char *decl, asCString &name, asCDataType &type)
  551. {
  552. numErrors = 0;
  553. numWarnings = 0;
  554. preMessage.isSet = false;
  555. if( dt )
  556. {
  557. // Verify that the object type exist
  558. if( dt->GetObjectType() == 0 )
  559. return asINVALID_OBJECT;
  560. }
  561. // Check property declaration and type
  562. asCScriptCode source;
  563. source.SetCode(TXT_PROPERTY, decl, true);
  564. asCParser parser(this);
  565. int r = parser.ParsePropertyDeclaration(&source);
  566. if( r < 0 )
  567. return asINVALID_DECLARATION;
  568. // Get data type and property name
  569. asCScriptNode *dataType = parser.GetScriptNode()->firstChild;
  570. asCScriptNode *nameNode = dataType->next;
  571. type = CreateDataTypeFromNode(dataType, &source);
  572. name.Assign(&decl[nameNode->tokenPos], nameNode->tokenLength);
  573. // Validate that the type really can be a registered property
  574. // We cannot use CanBeInstanciated, as it is allowed to register
  575. // properties of type that cannot otherwise be instanciated
  576. if( type.GetFuncDef() && !type.IsObjectHandle() )
  577. {
  578. // Function definitions must always be handles
  579. return asINVALID_DECLARATION;
  580. }
  581. // Verify property name
  582. if( dt )
  583. {
  584. if( CheckNameConflictMember(dt->GetObjectType(), name.AddressOf(), nameNode, &source, true) < 0 )
  585. return asNAME_TAKEN;
  586. }
  587. else
  588. {
  589. if( CheckNameConflict(name.AddressOf(), nameNode, &source) < 0 )
  590. return asNAME_TAKEN;
  591. }
  592. if( numErrors > 0 )
  593. return asINVALID_DECLARATION;
  594. return asSUCCESS;
  595. }
  596. asCObjectProperty *asCBuilder::GetObjectProperty(asCDataType &obj, const char *prop)
  597. {
  598. asASSERT(obj.GetObjectType() != 0);
  599. // TODO: optimize: Improve linear search
  600. asCArray<asCObjectProperty *> &props = obj.GetObjectType()->properties;
  601. for( asUINT n = 0; n < props.GetLength(); n++ )
  602. {
  603. if( props[n]->name == prop )
  604. {
  605. if( module->accessMask & props[n]->accessMask )
  606. return props[n];
  607. else
  608. return 0;
  609. }
  610. }
  611. return 0;
  612. }
  613. asCGlobalProperty *asCBuilder::GetGlobalProperty(const char *prop, bool *isCompiled, bool *isPureConstant, asQWORD *constantValue, bool *isAppProp)
  614. {
  615. asUINT n;
  616. if( isCompiled ) *isCompiled = true;
  617. if( isPureConstant ) *isPureConstant = false;
  618. if( isAppProp ) *isAppProp = false;
  619. // TODO: optimize: Improve linear search
  620. // Check application registered properties
  621. asCArray<asCGlobalProperty *> *props = &(engine->registeredGlobalProps);
  622. for( n = 0; n < props->GetLength(); ++n )
  623. if( (*props)[n] && (*props)[n]->name == prop )
  624. {
  625. if( module )
  626. {
  627. // Determine if the module has access to the property
  628. if( module->accessMask & (*props)[n]->accessMask )
  629. {
  630. #ifdef AS_DEPRECATED
  631. // deprecated since 2011-10-04
  632. // Find the config group for the global property
  633. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar((*props)[n]->id);
  634. if( !group || group->HasModuleAccess(module->name.AddressOf()) )
  635. continue;
  636. #endif
  637. if( isAppProp ) *isAppProp = true;
  638. return (*props)[n];
  639. }
  640. }
  641. else
  642. {
  643. // We're not compiling a module right now, so it must be a registered global property
  644. if( isAppProp ) *isAppProp = true;
  645. return (*props)[n];
  646. }
  647. }
  648. // TODO: optimize: Improve linear search
  649. // Check properties being compiled now
  650. asCArray<sGlobalVariableDescription *> *gvars = &globVariables;
  651. for( n = 0; n < gvars->GetLength(); ++n )
  652. {
  653. if( (*gvars)[n] && (*gvars)[n]->name == prop )
  654. {
  655. if( isCompiled ) *isCompiled = (*gvars)[n]->isCompiled;
  656. if( isPureConstant ) *isPureConstant = (*gvars)[n]->isPureConstant;
  657. if( constantValue ) *constantValue = (*gvars)[n]->constantValue;
  658. return (*gvars)[n]->property;
  659. }
  660. }
  661. // TODO: optimize: Improve linear search
  662. // Check previously compiled global variables
  663. if( module )
  664. {
  665. props = &module->scriptGlobals;
  666. for( n = 0; n < props->GetLength(); ++n )
  667. if( (*props)[n]->name == prop )
  668. return (*props)[n];
  669. }
  670. return 0;
  671. }
  672. int asCBuilder::ParseFunctionDeclaration(asCObjectType *objType, const char *decl, asCScriptFunction *func, bool isSystemFunction, asCArray<bool> *paramAutoHandles, bool *returnAutoHandle)
  673. {
  674. // TODO: Can't we use GetParsedFunctionDetails to do most of what is done in this function?
  675. numErrors = 0;
  676. numWarnings = 0;
  677. preMessage.isSet = false;
  678. asCScriptCode source;
  679. source.SetCode(TXT_SYSTEM_FUNCTION, decl, true);
  680. asCParser parser(this);
  681. int r = parser.ParseFunctionDefinition(&source);
  682. if( r < 0 )
  683. return asINVALID_DECLARATION;
  684. asCScriptNode *node = parser.GetScriptNode();
  685. // Find name
  686. asCScriptNode *n = node->firstChild->next->next;
  687. func->name.Assign(&source.code[n->tokenPos], n->tokenLength);
  688. // Initialize a script function object for registration
  689. bool autoHandle;
  690. // Scoped reference types are allowed to use handle when returned from application functions
  691. func->returnType = CreateDataTypeFromNode(node->firstChild, &source, true, objType);
  692. func->returnType = ModifyDataTypeFromNode(func->returnType, node->firstChild->next, &source, 0, &autoHandle);
  693. if( autoHandle && (!func->returnType.IsObjectHandle() || func->returnType.IsReference()) )
  694. return asINVALID_DECLARATION;
  695. if( returnAutoHandle ) *returnAutoHandle = autoHandle;
  696. // Reference types cannot be returned by value from system functions
  697. if( isSystemFunction &&
  698. (func->returnType.GetObjectType() &&
  699. (func->returnType.GetObjectType()->flags & asOBJ_REF)) &&
  700. !(func->returnType.IsReference() ||
  701. func->returnType.IsObjectHandle()) )
  702. return asINVALID_DECLARATION;
  703. // Count number of parameters
  704. int paramCount = 0;
  705. n = n->next->firstChild;
  706. while( n )
  707. {
  708. paramCount++;
  709. n = n->next->next;
  710. if( n && n->nodeType == snIdentifier )
  711. n = n->next;
  712. if( n && n->nodeType == snExpression )
  713. n = n->next;
  714. }
  715. // Preallocate memory
  716. func->parameterTypes.Allocate(paramCount, false);
  717. func->inOutFlags.Allocate(paramCount, false);
  718. func->defaultArgs.Allocate(paramCount, false);
  719. if( paramAutoHandles ) paramAutoHandles->Allocate(paramCount, false);
  720. n = node->firstChild->next->next->next->firstChild;
  721. while( n )
  722. {
  723. asETypeModifiers inOutFlags;
  724. asCDataType type = CreateDataTypeFromNode(n, &source, false, objType);
  725. type = ModifyDataTypeFromNode(type, n->next, &source, &inOutFlags, &autoHandle);
  726. // Reference types cannot be passed by value to system functions
  727. if( isSystemFunction &&
  728. (type.GetObjectType() &&
  729. (type.GetObjectType()->flags & asOBJ_REF)) &&
  730. !(type.IsReference() ||
  731. type.IsObjectHandle()) )
  732. return asINVALID_DECLARATION;
  733. // Store the parameter type
  734. func->parameterTypes.PushLast(type);
  735. func->inOutFlags.PushLast(inOutFlags);
  736. // Don't permit void parameters
  737. if( type.GetTokenType() == ttVoid )
  738. return asINVALID_DECLARATION;
  739. if( autoHandle && (!type.IsObjectHandle() || type.IsReference()) )
  740. return asINVALID_DECLARATION;
  741. if( paramAutoHandles ) paramAutoHandles->PushLast(autoHandle);
  742. // Make sure that var type parameters are references
  743. if( type.GetTokenType() == ttQuestion &&
  744. !type.IsReference() )
  745. return asINVALID_DECLARATION;
  746. // Move to next parameter
  747. n = n->next->next;
  748. if( n && n->nodeType == snIdentifier )
  749. n = n->next;
  750. if( n && n->nodeType == snExpression )
  751. {
  752. // Strip out white space and comments to better share the string
  753. asCString *defaultArgStr = asNEW(asCString);
  754. *defaultArgStr = GetCleanExpressionString(n, &source);
  755. func->defaultArgs.PushLast(defaultArgStr);
  756. n = n->next;
  757. }
  758. else
  759. func->defaultArgs.PushLast(0);
  760. }
  761. // Set the read-only flag if const is declared after parameter list
  762. if( node->lastChild->nodeType == snUndefined && node->lastChild->tokenType == ttConst )
  763. {
  764. if( objType == 0 )
  765. return asINVALID_DECLARATION;
  766. func->isReadOnly = true;
  767. }
  768. else
  769. func->isReadOnly = false;
  770. // Make sure the default args are declared correctly
  771. ValidateDefaultArgs(&source, node, func);
  772. if( numErrors > 0 || numWarnings > 0 )
  773. return asINVALID_DECLARATION;
  774. return 0;
  775. }
  776. int asCBuilder::ParseVariableDeclaration(const char *decl, asCObjectProperty *var)
  777. {
  778. numErrors = 0;
  779. numWarnings = 0;
  780. preMessage.isSet = false;
  781. asCScriptCode source;
  782. source.SetCode(TXT_VARIABLE_DECL, decl, true);
  783. asCParser parser(this);
  784. int r = parser.ParsePropertyDeclaration(&source);
  785. if( r < 0 )
  786. return asINVALID_DECLARATION;
  787. asCScriptNode *node = parser.GetScriptNode();
  788. // Find name
  789. asCScriptNode *n = node->firstChild->next;
  790. var->name.Assign(&source.code[n->tokenPos], n->tokenLength);
  791. // Initialize a script variable object for registration
  792. var->type = CreateDataTypeFromNode(node->firstChild, &source);
  793. if( numErrors > 0 || numWarnings > 0 )
  794. return asINVALID_DECLARATION;
  795. return 0;
  796. }
  797. int asCBuilder::CheckNameConflictMember(asCObjectType *t, const char *name, asCScriptNode *node, asCScriptCode *code, bool isProperty)
  798. {
  799. // It's not necessary to check against object types
  800. // TODO: optimize: Improve linear search
  801. asCArray<asCObjectProperty *> &props = t->properties;
  802. for( asUINT n = 0; n < props.GetLength(); n++ )
  803. {
  804. if( props[n]->name == name )
  805. {
  806. if( code )
  807. {
  808. int r, c;
  809. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  810. asCString str;
  811. str.Format(TXT_NAME_CONFLICT_s_OBJ_PROPERTY, name);
  812. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  813. }
  814. return -1;
  815. }
  816. }
  817. // Property names must be checked against method names
  818. if( isProperty )
  819. {
  820. asCArray<int> methods = t->methods;
  821. for( asUINT n = 0; n < methods.GetLength(); n++ )
  822. {
  823. if( engine->scriptFunctions[methods[n]]->name == name )
  824. {
  825. if( code )
  826. {
  827. int r, c;
  828. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  829. asCString str;
  830. str.Format(TXT_NAME_CONFLICT_s_METHOD, name);
  831. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  832. }
  833. return -1;
  834. }
  835. }
  836. }
  837. return 0;
  838. }
  839. int asCBuilder::CheckNameConflict(const char *name, asCScriptNode *node, asCScriptCode *code)
  840. {
  841. // TODO: Must verify object types in all config groups, whether the module has access or not
  842. // Check against object types
  843. if( engine->GetObjectType(name) != 0 )
  844. {
  845. if( code )
  846. {
  847. int r, c;
  848. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  849. asCString str;
  850. str.Format(TXT_NAME_CONFLICT_s_EXTENDED_TYPE, name);
  851. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  852. }
  853. return -1;
  854. }
  855. // TODO: Must verify global properties in all config groups, whether the module has access or not
  856. // Check against global properties
  857. asCGlobalProperty *prop = GetGlobalProperty(name, 0, 0, 0, 0);
  858. if( prop )
  859. {
  860. if( code )
  861. {
  862. int r, c;
  863. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  864. asCString str;
  865. str.Format(TXT_NAME_CONFLICT_s_GLOBAL_PROPERTY, name);
  866. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  867. }
  868. return -1;
  869. }
  870. // TODO: Property names must be checked against function names
  871. // Check against class types
  872. asUINT n;
  873. for( n = 0; n < classDeclarations.GetLength(); n++ )
  874. {
  875. if( classDeclarations[n]->name == name )
  876. {
  877. if( code )
  878. {
  879. int r, c;
  880. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  881. asCString str;
  882. str.Format(TXT_NAME_CONFLICT_s_STRUCT, name);
  883. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  884. }
  885. return -1;
  886. }
  887. }
  888. // Check against named types
  889. for( n = 0; n < namedTypeDeclarations.GetLength(); n++ )
  890. {
  891. if( namedTypeDeclarations[n]->name == name )
  892. {
  893. if( code )
  894. {
  895. int r, c;
  896. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  897. asCString str;
  898. str.Format(TXT_NAME_CONFLICT_s_IS_NAMED_TYPE, name);
  899. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  900. }
  901. return -1;
  902. }
  903. }
  904. // Must check for name conflicts with funcdefs
  905. for( n = 0; n < funcDefs.GetLength(); n++ )
  906. {
  907. if( funcDefs[n]->name == name )
  908. {
  909. if( code )
  910. {
  911. int r, c;
  912. code->ConvertPosToRowCol(node->tokenPos, &r, &c);
  913. asCString str;
  914. str.Format(TXT_NAME_CONFLICT_s_IS_FUNCDEF, name);
  915. WriteError(code->name.AddressOf(), str.AddressOf(), r, c);
  916. }
  917. return -1;
  918. }
  919. }
  920. return 0;
  921. }
  922. int asCBuilder::RegisterFuncDef(asCScriptNode *node, asCScriptCode *file)
  923. {
  924. // Find the name
  925. asASSERT( node->firstChild->nodeType == snDataType );
  926. asCScriptNode *n = node->firstChild->next->next;
  927. asCString name;
  928. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  929. // Check for name conflict with other types
  930. int r = CheckNameConflict(name.AddressOf(), node, file);
  931. if( asSUCCESS != r )
  932. {
  933. node->Destroy(engine);
  934. return r;
  935. }
  936. // The function definition should be stored as a asCScriptFunction so that the application
  937. // can use the asIScriptFunction interface to enumerate the return type and parameters
  938. // The return type and parameter types aren't determined in this function. A second pass is
  939. // necessary after all type declarations have been identified.
  940. sFuncDef *fd = asNEW(sFuncDef);
  941. fd->name = name;
  942. fd->node = node;
  943. fd->script = file;
  944. fd->idx = module->AddFuncDef(name.AddressOf());
  945. funcDefs.PushLast(fd);
  946. return 0;
  947. }
  948. void asCBuilder::CompleteFuncDef(sFuncDef *funcDef)
  949. {
  950. asCDataType returnType;
  951. asCArray<asCDataType> parameterTypes;
  952. asCArray<asETypeModifiers> inOutFlags;
  953. asCArray<asCString *> defaultArgs;
  954. bool isConstMethod;
  955. bool isConstructor;
  956. bool isDestructor;
  957. bool isPrivate;
  958. bool isOverride;
  959. bool isFinal;
  960. GetParsedFunctionDetails(funcDef->node, funcDef->script, 0, funcDef->name, returnType, parameterTypes, inOutFlags, defaultArgs, isConstMethod, isConstructor, isDestructor, isPrivate, isOverride, isFinal);
  961. asCScriptFunction *func = module->funcDefs[funcDef->idx];
  962. if( func )
  963. {
  964. func->returnType = returnType;
  965. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  966. {
  967. func->parameterTypes.PushLast(parameterTypes[p]);
  968. func->inOutFlags.PushLast(inOutFlags[p]);
  969. // Don't copy the default arg expression as it is not allowed for function definitions
  970. }
  971. }
  972. }
  973. int asCBuilder::RegisterGlobalVar(asCScriptNode *node, asCScriptCode *file)
  974. {
  975. // Has the application disabled global vars?
  976. if( engine->ep.disallowGlobalVars )
  977. {
  978. int r, c;
  979. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  980. WriteError(file->name.AddressOf(), TXT_GLOBAL_VARS_NOT_ALLOWED, r, c);
  981. }
  982. // What data type is it?
  983. asCDataType type = CreateDataTypeFromNode(node->firstChild, file);
  984. if( !type.CanBeInstanciated() )
  985. {
  986. asCString str;
  987. // TODO: Change to "'type' cannot be declared as variable"
  988. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format().AddressOf());
  989. int r, c;
  990. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  991. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  992. }
  993. asCScriptNode *n = node->firstChild->next;
  994. while( n )
  995. {
  996. // Verify that the name isn't taken
  997. asCString name(&file->code[n->tokenPos], n->tokenLength);
  998. CheckNameConflict(name.AddressOf(), n, file);
  999. // Register the global variable
  1000. sGlobalVariableDescription *gvar = asNEW(sGlobalVariableDescription);
  1001. globVariables.PushLast(gvar);
  1002. gvar->script = file;
  1003. gvar->name = name;
  1004. gvar->isCompiled = false;
  1005. gvar->datatype = type;
  1006. gvar->isEnumValue = false;
  1007. // TODO: Give error message if wrong
  1008. asASSERT(!gvar->datatype.IsReference());
  1009. gvar->idNode = n;
  1010. gvar->nextNode = 0;
  1011. if( n->next &&
  1012. (n->next->nodeType == snAssignment ||
  1013. n->next->nodeType == snArgList ||
  1014. n->next->nodeType == snInitList ) )
  1015. {
  1016. gvar->nextNode = n->next;
  1017. n->next->DisconnectParent();
  1018. }
  1019. gvar->property = module->AllocateGlobalProperty(name.AddressOf(), gvar->datatype);
  1020. gvar->index = gvar->property->id;
  1021. n = n->next;
  1022. }
  1023. node->Destroy(engine);
  1024. return 0;
  1025. }
  1026. int asCBuilder::RegisterClass(asCScriptNode *node, asCScriptCode *file)
  1027. {
  1028. asCScriptNode *n = node->firstChild;
  1029. bool isFinal = false;
  1030. bool isShared = false;
  1031. if( n->tokenType == ttIdentifier && file->TokenEquals(n->tokenPos, n->tokenLength, FINAL_TOKEN) )
  1032. {
  1033. isFinal = true;
  1034. n = n->next;
  1035. }
  1036. if( n->tokenType == ttIdentifier && file->TokenEquals(n->tokenPos, n->tokenLength, SHARED_TOKEN) )
  1037. {
  1038. isShared = true;
  1039. n = n->next;
  1040. // Check for final again
  1041. if( n->tokenType == ttIdentifier && file->TokenEquals(n->tokenPos, n->tokenLength, FINAL_TOKEN) )
  1042. {
  1043. isFinal = true;
  1044. n = n->next;
  1045. }
  1046. }
  1047. asCString name(&file->code[n->tokenPos], n->tokenLength);
  1048. int r, c;
  1049. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  1050. CheckNameConflict(name.AddressOf(), n, file);
  1051. sClassDeclaration *decl = asNEW(sClassDeclaration);
  1052. classDeclarations.PushLast(decl);
  1053. decl->name = name;
  1054. decl->script = file;
  1055. decl->node = node;
  1056. // If this type is shared and there already exist another shared
  1057. // type of the same name, then that one should be used instead of
  1058. // creating a new one.
  1059. if( isShared )
  1060. {
  1061. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  1062. {
  1063. asCObjectType *st = engine->classTypes[n];
  1064. if( st &&
  1065. st->IsShared() &&
  1066. st->name == name &&
  1067. !st->IsInterface() )
  1068. {
  1069. // We'll use the existing type
  1070. decl->isExistingShared = true;
  1071. decl->objType = st;
  1072. module->classTypes.PushLast(st);
  1073. st->AddRef();
  1074. return 0;
  1075. }
  1076. }
  1077. }
  1078. // Create a new object type for this class
  1079. asCObjectType *st = asNEW(asCObjectType)(engine);
  1080. st->flags = asOBJ_REF | asOBJ_SCRIPT_OBJECT;
  1081. if( isShared )
  1082. st->flags |= asOBJ_SHARED;
  1083. if( isFinal )
  1084. st->flags |= asOBJ_NOINHERIT;
  1085. if( node->tokenType == ttHandle )
  1086. st->flags |= asOBJ_IMPLICIT_HANDLE;
  1087. st->size = sizeof(asCScriptObject);
  1088. st->name = name;
  1089. module->classTypes.PushLast(st);
  1090. engine->classTypes.PushLast(st);
  1091. st->AddRef();
  1092. decl->objType = st;
  1093. // Add script classes to the GC
  1094. // TODO: optimize: Only add the class to the GC when the module won't use the type anymore
  1095. engine->gc.AddScriptObjectToGC(st, &engine->objectTypeBehaviours);
  1096. // Use the default script class behaviours
  1097. st->beh = engine->scriptTypeBehaviours.beh;
  1098. // TODO: Move this to asCObjectType so that the asCRestore can reuse it
  1099. engine->scriptFunctions[st->beh.addref]->AddRef();
  1100. engine->scriptFunctions[st->beh.release]->AddRef();
  1101. engine->scriptFunctions[st->beh.gcEnumReferences]->AddRef();
  1102. engine->scriptFunctions[st->beh.gcGetFlag]->AddRef();
  1103. engine->scriptFunctions[st->beh.gcGetRefCount]->AddRef();
  1104. engine->scriptFunctions[st->beh.gcReleaseAllReferences]->AddRef();
  1105. engine->scriptFunctions[st->beh.gcSetFlag]->AddRef();
  1106. engine->scriptFunctions[st->beh.copy]->AddRef();
  1107. engine->scriptFunctions[st->beh.factory]->AddRef();
  1108. engine->scriptFunctions[st->beh.construct]->AddRef();
  1109. for( asUINT i = 1; i < st->beh.operators.GetLength(); i += 2 )
  1110. engine->scriptFunctions[st->beh.operators[i]]->AddRef();
  1111. return 0;
  1112. }
  1113. int asCBuilder::RegisterInterface(asCScriptNode *node, asCScriptCode *file)
  1114. {
  1115. asCScriptNode *n = node->firstChild;
  1116. asCString name(&file->code[n->tokenPos], n->tokenLength);
  1117. bool isShared = false;
  1118. if( name == SHARED_TOKEN )
  1119. {
  1120. isShared = true;
  1121. n = n->next;
  1122. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  1123. }
  1124. int r, c;
  1125. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  1126. CheckNameConflict(name.AddressOf(), n, file);
  1127. sClassDeclaration *decl = asNEW(sClassDeclaration);
  1128. interfaceDeclarations.PushLast(decl);
  1129. decl->name = name;
  1130. decl->script = file;
  1131. decl->node = node;
  1132. // If this type is shared and there already exist another shared
  1133. // type of the same name, then that one should be used instead of
  1134. // creating a new one.
  1135. if( isShared )
  1136. {
  1137. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  1138. {
  1139. asCObjectType *st = engine->classTypes[n];
  1140. if( st &&
  1141. st->IsShared() &&
  1142. st->name == name &&
  1143. st->IsInterface() )
  1144. {
  1145. // We'll use the existing type
  1146. decl->isExistingShared = true;
  1147. decl->objType = st;
  1148. module->classTypes.PushLast(st);
  1149. st->AddRef();
  1150. return 0;
  1151. }
  1152. }
  1153. }
  1154. // Register the object type for the interface
  1155. asCObjectType *st = asNEW(asCObjectType)(engine);
  1156. st->flags = asOBJ_REF | asOBJ_SCRIPT_OBJECT;
  1157. if( isShared )
  1158. st->flags |= asOBJ_SHARED;
  1159. st->size = 0; // Cannot be instanciated
  1160. st->name = name;
  1161. module->classTypes.PushLast(st);
  1162. engine->classTypes.PushLast(st);
  1163. st->AddRef();
  1164. decl->objType = st;
  1165. // Use the default script class behaviours
  1166. st->beh.construct = 0;
  1167. st->beh.addref = engine->scriptTypeBehaviours.beh.addref;
  1168. engine->scriptFunctions[st->beh.addref]->AddRef();
  1169. st->beh.release = engine->scriptTypeBehaviours.beh.release;
  1170. engine->scriptFunctions[st->beh.release]->AddRef();
  1171. st->beh.copy = 0;
  1172. return 0;
  1173. }
  1174. void asCBuilder::CompileGlobalVariables()
  1175. {
  1176. bool compileSucceeded = true;
  1177. // Store state of compilation (errors, warning, output)
  1178. int currNumErrors = numErrors;
  1179. int currNumWarnings = numWarnings;
  1180. // Backup the original message stream
  1181. bool msgCallback = engine->msgCallback;
  1182. asSSystemFunctionInterface msgCallbackFunc = engine->msgCallbackFunc;
  1183. void *msgCallbackObj = engine->msgCallbackObj;
  1184. // Set the new temporary message stream
  1185. asCOutputBuffer outBuffer;
  1186. engine->SetMessageCallback(asMETHOD(asCOutputBuffer, Callback), &outBuffer, asCALL_THISCALL);
  1187. asCOutputBuffer finalOutput;
  1188. asCScriptFunction *initFunc = 0;
  1189. asCArray<asCGlobalProperty*> initOrder;
  1190. // We first try to compile all the primitive global variables, and only after that
  1191. // compile the non-primitive global variables. This permits the constructors
  1192. // for the complex types to use the already initialized variables of primitive
  1193. // type. Note, we currently don't know which global variables are used in the
  1194. // constructors, so we cannot guarantee that variables of complex types are
  1195. // initialized in the correct order, so we won't reorder those.
  1196. bool compilingPrimitives = true;
  1197. // Compile each global variable
  1198. while( compileSucceeded )
  1199. {
  1200. compileSucceeded = false;
  1201. int accumErrors = 0;
  1202. int accumWarnings = 0;
  1203. // Restore state of compilation
  1204. finalOutput.Clear();
  1205. for( asUINT n = 0; n < globVariables.GetLength(); n++ )
  1206. {
  1207. sGlobalVariableDescription *gvar = globVariables[n];
  1208. if( gvar->isCompiled )
  1209. continue;
  1210. asCByteCode init(engine);
  1211. numWarnings = 0;
  1212. numErrors = 0;
  1213. outBuffer.Clear();
  1214. // Skip this for now if we're not compiling complex types yet
  1215. if( compilingPrimitives && !gvar->datatype.IsPrimitive() )
  1216. continue;
  1217. if( gvar->nextNode )
  1218. {
  1219. int r, c;
  1220. gvar->script->ConvertPosToRowCol(gvar->nextNode->tokenPos, &r, &c);
  1221. asCString str = gvar->datatype.Format();
  1222. str += " " + gvar->name;
  1223. str.Format(TXT_COMPILING_s, str.AddressOf());
  1224. WriteInfo(gvar->script->name.AddressOf(), str.AddressOf(), r, c, true);
  1225. }
  1226. if( gvar->isEnumValue )
  1227. {
  1228. int r;
  1229. if( gvar->nextNode )
  1230. {
  1231. asCCompiler comp(engine);
  1232. asCScriptFunction func(engine, module, asFUNC_DUMMY);
  1233. // Temporarily switch the type of the variable to int so it can be compiled properly
  1234. asCDataType saveType;
  1235. saveType = gvar->datatype;
  1236. gvar->datatype = asCDataType::CreatePrimitive(ttInt, true);
  1237. r = comp.CompileGlobalVariable(this, gvar->script, gvar->nextNode, gvar, &func);
  1238. gvar->datatype = saveType;
  1239. }
  1240. else
  1241. {
  1242. r = 0;
  1243. // When there is no assignment the value is the last + 1
  1244. int enumVal = 0;
  1245. if( n > 0 )
  1246. {
  1247. sGlobalVariableDescription *gvar2 = globVariables[n-1];
  1248. if( gvar2->datatype == gvar->datatype )
  1249. {
  1250. // The integer value is stored in the lower bytes
  1251. enumVal = (*(int*)&gvar2->constantValue) + 1;
  1252. if( !gvar2->isCompiled )
  1253. {
  1254. // TODO: Need to get the correct script position
  1255. int row, col;
  1256. gvar->script->ConvertPosToRowCol(0, &row, &col);
  1257. asCString str = gvar->datatype.Format();
  1258. str += " " + gvar->name;
  1259. str.Format(TXT_COMPILING_s, str.AddressOf());
  1260. WriteInfo(gvar->script->name.AddressOf(), str.AddressOf(), row, col, true);
  1261. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, gvar2->name.AddressOf());
  1262. WriteError(gvar->script->name.AddressOf(), str.AddressOf(), row, col);
  1263. r = -1;
  1264. }
  1265. }
  1266. }
  1267. // The integer value is stored in the lower bytes
  1268. *(int*)&gvar->constantValue = enumVal;
  1269. }
  1270. if( r >= 0 )
  1271. {
  1272. // Set the value as compiled
  1273. gvar->isCompiled = true;
  1274. compileSucceeded = true;
  1275. }
  1276. }
  1277. else
  1278. {
  1279. // Compile the global variable
  1280. initFunc = asNEW(asCScriptFunction)(engine, module, asFUNC_DUMMY);
  1281. asCCompiler comp(engine);
  1282. int r = comp.CompileGlobalVariable(this, gvar->script, gvar->nextNode, gvar, initFunc);
  1283. if( r >= 0 )
  1284. {
  1285. // Compilation succeeded
  1286. gvar->isCompiled = true;
  1287. compileSucceeded = true;
  1288. }
  1289. else
  1290. {
  1291. // Compilation failed
  1292. asDELETE(initFunc, asCScriptFunction);
  1293. initFunc = 0;
  1294. }
  1295. }
  1296. if( gvar->isCompiled )
  1297. {
  1298. // Add warnings for this constant to the total build
  1299. if( numWarnings )
  1300. {
  1301. currNumWarnings += numWarnings;
  1302. if( msgCallback )
  1303. outBuffer.SendToCallback(engine, &msgCallbackFunc, msgCallbackObj);
  1304. }
  1305. // Determine order of variable initializations
  1306. if( gvar->property && !gvar->isEnumValue )
  1307. initOrder.PushLast(gvar->property);
  1308. // Does the function contain more than just a SUSPEND followed by a RET instruction?
  1309. if( initFunc && initFunc->byteCode.GetLength() > 2 )
  1310. {
  1311. // Create the init function for this variable
  1312. initFunc->id = engine->GetNextScriptFunctionId();
  1313. engine->SetScriptFunction(initFunc);
  1314. // Finalize the init function for this variable
  1315. initFunc->funcType = asFUNC_SCRIPT;
  1316. initFunc->returnType = asCDataType::CreatePrimitive(ttVoid, false);
  1317. initFunc->scriptSectionIdx = engine->GetScriptSectionNameIndex(gvar->script->name.AddressOf());
  1318. // Notify the GC of the new script function
  1319. engine->gc.AddScriptObjectToGC(initFunc, &engine->functionBehaviours);
  1320. gvar->property->SetInitFunc(initFunc);
  1321. initFunc->Release();
  1322. initFunc = 0;
  1323. }
  1324. else if( initFunc )
  1325. {
  1326. // Destroy the function as it won't be used
  1327. asDELETE(initFunc, asCScriptFunction);
  1328. initFunc = 0;
  1329. }
  1330. // Convert enums to true enum values, so subsequent compilations can access it as an enum
  1331. if( gvar->isEnumValue )
  1332. {
  1333. asCObjectType *objectType = gvar->datatype.GetObjectType();
  1334. asASSERT(NULL != objectType);
  1335. asSEnumValue *e = asNEW(asSEnumValue);
  1336. e->name = gvar->name;
  1337. e->value = *(int*)&gvar->constantValue;
  1338. objectType->enumValues.PushLast(e);
  1339. }
  1340. }
  1341. else
  1342. {
  1343. // Add output to final output
  1344. finalOutput.Append(outBuffer);
  1345. accumErrors += numErrors;
  1346. accumWarnings += numWarnings;
  1347. }
  1348. preMessage.isSet = false;
  1349. }
  1350. if( !compileSucceeded )
  1351. {
  1352. if( compilingPrimitives )
  1353. {
  1354. // No more primitives could be compiled, so
  1355. // switch to compiling the complex variables
  1356. compilingPrimitives = false;
  1357. compileSucceeded = true;
  1358. }
  1359. else
  1360. {
  1361. // No more variables can be compiled
  1362. // Add errors and warnings to total build
  1363. currNumWarnings += accumWarnings;
  1364. currNumErrors += accumErrors;
  1365. if( msgCallback )
  1366. finalOutput.SendToCallback(engine, &msgCallbackFunc, msgCallbackObj);
  1367. }
  1368. }
  1369. }
  1370. // Restore states
  1371. engine->msgCallback = msgCallback;
  1372. engine->msgCallbackFunc = msgCallbackFunc;
  1373. engine->msgCallbackObj = msgCallbackObj;
  1374. numWarnings = currNumWarnings;
  1375. numErrors = currNumErrors;
  1376. // Set the correct order of initialization
  1377. if( numErrors == 0 )
  1378. {
  1379. // If the length of the arrays are not the same, then this is the compilation
  1380. // of a single variable, in which case the initialization order of the previous
  1381. // variables must be preserved.
  1382. if( module->scriptGlobals.GetLength() == initOrder.GetLength() )
  1383. module->scriptGlobals = initOrder;
  1384. }
  1385. // Delete the enum expressions
  1386. for( asUINT n = 0; n < globVariables.GetLength(); n++ )
  1387. {
  1388. sGlobalVariableDescription *gvar = globVariables[n];
  1389. if( gvar->isEnumValue )
  1390. {
  1391. // Destroy the gvar property
  1392. if( gvar->nextNode )
  1393. {
  1394. gvar->nextNode->Destroy(engine);
  1395. gvar->nextNode = 0;
  1396. }
  1397. if( gvar->property )
  1398. {
  1399. asDELETE(gvar->property, asCGlobalProperty);
  1400. gvar->property = 0;
  1401. }
  1402. asDELETE(gvar, sGlobalVariableDescription);
  1403. globVariables[n] = 0;
  1404. }
  1405. }
  1406. }
  1407. void asCBuilder::CompileClasses()
  1408. {
  1409. asUINT n;
  1410. asCArray<sClassDeclaration*> toValidate((int)classDeclarations.GetLength());
  1411. // Determine class inheritances and interfaces
  1412. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1413. {
  1414. sClassDeclaration *decl = classDeclarations[n];
  1415. asCScriptCode *file = decl->script;
  1416. // Find the base class that this class inherits from
  1417. bool multipleInheritance = false;
  1418. asCScriptNode *node = decl->node->firstChild;
  1419. if( decl->objType->IsShared() )
  1420. {
  1421. // Skip the keyword 'shared'
  1422. asASSERT(node->tokenType == ttIdentifier);
  1423. node = node->next;
  1424. }
  1425. if( decl->objType->flags & asOBJ_NOINHERIT )
  1426. {
  1427. // skip the keyword 'final'
  1428. asASSERT(node->tokenType == ttIdentifier);
  1429. node = node->next;
  1430. }
  1431. // Skip the name of the class
  1432. asASSERT(node->tokenType == ttIdentifier);
  1433. node = node->next;
  1434. while( node && node->nodeType == snIdentifier )
  1435. {
  1436. // Get the interface name from the node
  1437. asCString name(&file->code[node->tokenPos], node->tokenLength);
  1438. // Find the object type for the interface
  1439. asCObjectType *objType = GetObjectType(name.AddressOf());
  1440. if( objType == 0 )
  1441. {
  1442. int r, c;
  1443. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1444. asCString str;
  1445. str.Format(TXT_IDENTIFIER_s_NOT_DATA_TYPE, name.AddressOf());
  1446. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  1447. }
  1448. else if( !(objType->flags & asOBJ_SCRIPT_OBJECT) ||
  1449. objType->flags & asOBJ_NOINHERIT )
  1450. {
  1451. // Either the class is not a script class or interface
  1452. // or the class has been declared as 'final'
  1453. int r, c;
  1454. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1455. asCString str;
  1456. str.Format(TXT_CANNOT_INHERIT_FROM_s_FINAL, objType->name.AddressOf());
  1457. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  1458. }
  1459. else if( objType->size != 0 )
  1460. {
  1461. // The class inherits from another script class
  1462. if( !decl->isExistingShared && decl->objType->derivedFrom != 0 )
  1463. {
  1464. if( !multipleInheritance )
  1465. {
  1466. int r, c;
  1467. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1468. WriteError(file->name.AddressOf(), TXT_CANNOT_INHERIT_FROM_MULTIPLE_CLASSES, r, c);
  1469. multipleInheritance = true;
  1470. }
  1471. }
  1472. else
  1473. {
  1474. // Make sure none of the base classes inherit from this one
  1475. asCObjectType *base = objType;
  1476. bool error = false;
  1477. while( base != 0 )
  1478. {
  1479. if( base == decl->objType )
  1480. {
  1481. int r, c;
  1482. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1483. WriteError(file->name.AddressOf(), TXT_CANNOT_INHERIT_FROM_SELF, r, c);
  1484. error = true;
  1485. break;
  1486. }
  1487. base = base->derivedFrom;
  1488. }
  1489. if( !error )
  1490. {
  1491. // A shared type may only inherit from other shared types
  1492. if( (decl->objType->IsShared()) && !(objType->IsShared()) )
  1493. {
  1494. int r, c;
  1495. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1496. asCString msg;
  1497. msg.Format(TXT_SHARED_CANNOT_INHERIT_FROM_NON_SHARED_s, objType->name.AddressOf());
  1498. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  1499. error = true;
  1500. }
  1501. }
  1502. if( !error )
  1503. {
  1504. if( decl->isExistingShared )
  1505. {
  1506. // Verify that the base class is the same as the original shared type
  1507. if( decl->objType->derivedFrom != objType )
  1508. {
  1509. int r, c;
  1510. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1511. WriteError(file->name.AddressOf(), TXT_SHARED_DOESNT_MATCH_ORIGINAL, r, c);
  1512. }
  1513. }
  1514. else
  1515. {
  1516. // Set the base class
  1517. decl->objType->derivedFrom = objType;
  1518. objType->AddRef();
  1519. }
  1520. }
  1521. }
  1522. }
  1523. else
  1524. {
  1525. // The class implements an interface
  1526. if( !decl->isExistingShared && decl->objType->Implements(objType) )
  1527. {
  1528. int r, c;
  1529. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1530. asCString msg;
  1531. msg.Format(TXT_INTERFACE_s_ALREADY_IMPLEMENTED, objType->GetName());
  1532. WriteWarning(file->name.AddressOf(), msg.AddressOf(), r, c);
  1533. }
  1534. else
  1535. {
  1536. // A shared type may only implement from shared interfaces
  1537. if( (decl->objType->IsShared()) && !(objType->IsShared()) )
  1538. {
  1539. int r, c;
  1540. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1541. asCString msg;
  1542. msg.Format(TXT_SHARED_CANNOT_IMPLEMENT_NON_SHARED_s, objType->name.AddressOf());
  1543. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  1544. }
  1545. else
  1546. {
  1547. if( decl->isExistingShared )
  1548. {
  1549. // Verify that the original implements the same interface
  1550. if( !decl->objType->Implements(objType) )
  1551. {
  1552. int r, c;
  1553. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1554. WriteError(file->name.AddressOf(), TXT_SHARED_DOESNT_MATCH_ORIGINAL, r, c);
  1555. }
  1556. }
  1557. else
  1558. {
  1559. decl->objType->interfaces.PushLast(objType);
  1560. }
  1561. }
  1562. }
  1563. }
  1564. node = node->next;
  1565. }
  1566. }
  1567. // Order class declarations so that base classes are compiled before derived classes.
  1568. // This will allow the derived classes to copy properties and methods in the next step.
  1569. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1570. {
  1571. sClassDeclaration *decl = classDeclarations[n];
  1572. asCObjectType *derived = decl->objType;
  1573. asCObjectType *base = derived->derivedFrom;
  1574. if( base == 0 ) continue;
  1575. // If the base class is found after the derived class, then move the derived class to the end of the list
  1576. for( asUINT m = n+1; m < classDeclarations.GetLength(); m++ )
  1577. {
  1578. sClassDeclaration *declBase = classDeclarations[m];
  1579. if( base == declBase->objType )
  1580. {
  1581. classDeclarations.RemoveIndex(n);
  1582. classDeclarations.PushLast(decl);
  1583. // Decrease index so that we don't skip an entry
  1584. n--;
  1585. break;
  1586. }
  1587. }
  1588. }
  1589. // Go through each of the classes and register the object type descriptions
  1590. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1591. {
  1592. sClassDeclaration *decl = classDeclarations[n];
  1593. if( decl->isExistingShared )
  1594. {
  1595. // TODO: shared: Should really validate against original
  1596. continue;
  1597. }
  1598. // Add all properties and methods from the base class
  1599. if( decl->objType->derivedFrom )
  1600. {
  1601. asCObjectType *baseType = decl->objType->derivedFrom;
  1602. // The derived class inherits all interfaces from the base class
  1603. for( unsigned int n = 0; n < baseType->interfaces.GetLength(); n++ )
  1604. {
  1605. if( !decl->objType->Implements(baseType->interfaces[n]) )
  1606. {
  1607. decl->objType->interfaces.PushLast(baseType->interfaces[n]);
  1608. }
  1609. else
  1610. {
  1611. // Warn if derived class already implements the interface
  1612. int r, c;
  1613. decl->script->ConvertPosToRowCol(decl->node->tokenPos, &r, &c);
  1614. asCString msg;
  1615. msg.Format(TXT_INTERFACE_s_ALREADY_IMPLEMENTED, baseType->interfaces[n]->GetName());
  1616. WriteWarning(decl->script->name.AddressOf(), msg.AddressOf(), r, c);
  1617. }
  1618. }
  1619. // TODO: Need to check for name conflict with new class methods
  1620. // Copy properties from base class to derived class
  1621. for( asUINT p = 0; p < baseType->properties.GetLength(); p++ )
  1622. {
  1623. asCObjectProperty *prop = AddPropertyToClass(decl, baseType->properties[p]->name, baseType->properties[p]->type, baseType->properties[p]->isPrivate);
  1624. // The properties must maintain the same offset
  1625. asASSERT(prop && prop->byteOffset == baseType->properties[p]->byteOffset); UNUSED_VAR(prop);
  1626. }
  1627. // Copy methods from base class to derived class
  1628. for( asUINT m = 0; m < baseType->methods.GetLength(); m++ )
  1629. {
  1630. // If the derived class implements the same method, then don't add the base class' method
  1631. asCScriptFunction *baseFunc = GetFunctionDescription(baseType->methods[m]);
  1632. asCScriptFunction *derivedFunc = 0;
  1633. bool found = false;
  1634. for( asUINT d = 0; d < decl->objType->methods.GetLength(); d++ )
  1635. {
  1636. derivedFunc = GetFunctionDescription(decl->objType->methods[d]);
  1637. if( derivedFunc->IsSignatureEqual(baseFunc) )
  1638. {
  1639. if( baseFunc->IsFinal() )
  1640. {
  1641. int r, c;
  1642. decl->script->ConvertPosToRowCol(decl->node->tokenPos, &r, &c);
  1643. asCString msg;
  1644. msg.Format(TXT_METHOD_CANNOT_OVERRIDE_s, baseFunc->GetDeclaration());
  1645. WriteError(decl->script->name.AddressOf(), msg.AddressOf(), r, c);
  1646. }
  1647. // Move the function from the methods array to the virtualFunctionTable
  1648. decl->objType->methods.RemoveIndex(d);
  1649. decl->objType->virtualFunctionTable.PushLast(derivedFunc);
  1650. found = true;
  1651. break;
  1652. }
  1653. }
  1654. if( !found )
  1655. {
  1656. // Push the base class function on the virtual function table
  1657. decl->objType->virtualFunctionTable.PushLast(baseType->virtualFunctionTable[m]);
  1658. baseType->virtualFunctionTable[m]->AddRef();
  1659. }
  1660. decl->objType->methods.PushLast(baseType->methods[m]);
  1661. engine->scriptFunctions[baseType->methods[m]]->AddRef();
  1662. }
  1663. }
  1664. // Move this class' methods into the virtual function table
  1665. for( asUINT m = 0; m < decl->objType->methods.GetLength(); m++ )
  1666. {
  1667. asCScriptFunction *func = GetFunctionDescription(decl->objType->methods[m]);
  1668. if( func->funcType != asFUNC_VIRTUAL )
  1669. {
  1670. // Move the reference from the method list to the virtual function list
  1671. decl->objType->methods.RemoveIndex(m);
  1672. decl->objType->virtualFunctionTable.PushLast(func);
  1673. // Substitute the function description in the method list for a virtual method
  1674. // Make sure the methods are in the same order as the virtual function table
  1675. decl->objType->methods.PushLast(CreateVirtualFunction(func, (int)decl->objType->virtualFunctionTable.GetLength() - 1));
  1676. m--;
  1677. }
  1678. }
  1679. // Enumerate each of the declared properties
  1680. asCScriptNode *node = decl->node->firstChild->next;
  1681. if( decl->objType->IsShared() )
  1682. node = node->next;
  1683. if( decl->objType->flags & asOBJ_NOINHERIT )
  1684. node = node->next;
  1685. // Skip list of classes and interfaces
  1686. while( node && node->nodeType == snIdentifier )
  1687. node = node->next;
  1688. while( node )
  1689. {
  1690. if( node->nodeType == snDeclaration )
  1691. {
  1692. bool isPrivate = false;
  1693. if( node->firstChild && node->firstChild->tokenType == ttPrivate )
  1694. isPrivate = true;
  1695. asCScriptCode *file = decl->script;
  1696. asCDataType dt = CreateDataTypeFromNode(isPrivate ? node->firstChild->next : node->firstChild, file);
  1697. asCString name(&file->code[node->lastChild->tokenPos], node->lastChild->tokenLength);
  1698. if( decl->objType->IsShared() && dt.GetObjectType() && !dt.GetObjectType()->IsShared() )
  1699. {
  1700. int r, c;
  1701. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1702. asCString msg;
  1703. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetObjectType()->name.AddressOf());
  1704. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  1705. }
  1706. if( dt.IsReadOnly() )
  1707. {
  1708. int r, c;
  1709. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1710. WriteError(file->name.AddressOf(), TXT_PROPERTY_CANT_BE_CONST, r, c);
  1711. }
  1712. CheckNameConflictMember(decl->objType, name.AddressOf(), node->lastChild, file, true);
  1713. AddPropertyToClass(decl, name, dt, isPrivate, file, node);
  1714. }
  1715. else
  1716. asASSERT(false);
  1717. node = node->next;
  1718. }
  1719. toValidate.PushLast(decl);
  1720. }
  1721. // TODO: Warn if a method overrides a base method without marking it as 'override'.
  1722. // It must be possible to turn off this warning through engine property.
  1723. // TODO: A base class should be able to mark a method as 'abstract'. This will
  1724. // allow a base class to provide a partial implementation, but still force
  1725. // derived classes to implement specific methods.
  1726. // Verify that all interface methods are implemented in the classes
  1727. // We do this here so the base class' methods have already been inherited
  1728. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1729. {
  1730. sClassDeclaration *decl = classDeclarations[n];
  1731. if( decl->isExistingShared ) continue;
  1732. asCArray<bool> overrideValidations(decl->objType->GetMethodCount());
  1733. for( asUINT k = 0; k < decl->objType->methods.GetLength(); k++ )
  1734. {
  1735. overrideValidations.PushLast( !static_cast<asCScriptFunction*>(decl->objType->GetMethodByIndex(k, false))->IsOverride() );
  1736. }
  1737. for( asUINT m = 0; m < decl->objType->interfaces.GetLength(); m++ )
  1738. {
  1739. asCObjectType *objType = decl->objType->interfaces[m];
  1740. for( asUINT i = 0; i < objType->methods.GetLength(); i++ )
  1741. {
  1742. asUINT overrideIndex;
  1743. if( !DoesMethodExist(decl->objType, objType->methods[i], &overrideIndex) )
  1744. {
  1745. int r, c;
  1746. decl->script->ConvertPosToRowCol(decl->node->tokenPos, &r, &c);
  1747. asCString str;
  1748. str.Format(TXT_MISSING_IMPLEMENTATION_OF_s,
  1749. engine->GetFunctionDeclaration(objType->methods[i]).AddressOf());
  1750. WriteError(decl->script->name.AddressOf(), str.AddressOf(), r, c);
  1751. }
  1752. else
  1753. overrideValidations[overrideIndex] = true;
  1754. }
  1755. }
  1756. bool hasBaseClass = decl->objType->derivedFrom != 0;
  1757. for( asUINT j = 0; j < overrideValidations.GetLength(); j++ )
  1758. {
  1759. if( !overrideValidations[j] && (!hasBaseClass || !DoesMethodExist(decl->objType->derivedFrom, decl->objType->methods[j])) )
  1760. {
  1761. int r, c;
  1762. decl->script->ConvertPosToRowCol(decl->node->tokenPos, &r, &c);
  1763. asCString msg;
  1764. msg.Format(TXT_METHOD_s_DOES_NOT_OVERRIDE, decl->objType->GetMethodByIndex(j, false)->GetDeclaration());
  1765. WriteError(decl->script->name.AddressOf(), msg.AddressOf(), r, c);
  1766. }
  1767. }
  1768. }
  1769. // Verify that the declared structures are valid, e.g. that the structure
  1770. // doesn't contain a member of its own type directly or indirectly
  1771. while( toValidate.GetLength() > 0 )
  1772. {
  1773. asUINT numClasses = (asUINT)toValidate.GetLength();
  1774. asCArray<sClassDeclaration*> toValidateNext((int)toValidate.GetLength());
  1775. while( toValidate.GetLength() > 0 )
  1776. {
  1777. sClassDeclaration *decl = toValidate[toValidate.GetLength()-1];
  1778. int validState = 1;
  1779. for( asUINT n = 0; n < decl->objType->properties.GetLength(); n++ )
  1780. {
  1781. // A valid structure is one that uses only primitives or other valid objects
  1782. asCObjectProperty *prop = decl->objType->properties[n];
  1783. asCDataType dt = prop->type;
  1784. if( dt.IsTemplate() )
  1785. {
  1786. asCDataType sub = dt;
  1787. while( sub.IsTemplate() && !sub.IsObjectHandle() )
  1788. sub = sub.GetSubType();
  1789. dt = sub;
  1790. }
  1791. if( dt.IsObject() && !dt.IsObjectHandle() )
  1792. {
  1793. // Find the class declaration
  1794. sClassDeclaration *pdecl = 0;
  1795. for( asUINT p = 0; p < classDeclarations.GetLength(); p++ )
  1796. {
  1797. if( classDeclarations[p]->objType == dt.GetObjectType() )
  1798. {
  1799. pdecl = classDeclarations[p];
  1800. break;
  1801. }
  1802. }
  1803. if( pdecl )
  1804. {
  1805. if( pdecl->objType == decl->objType )
  1806. {
  1807. int r, c;
  1808. decl->script->ConvertPosToRowCol(decl->node->tokenPos, &r, &c);
  1809. WriteError(decl->script->name.AddressOf(), TXT_ILLEGAL_MEMBER_TYPE, r, c);
  1810. validState = 2;
  1811. break;
  1812. }
  1813. else if( pdecl->validState != 1 )
  1814. {
  1815. validState = pdecl->validState;
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. }
  1821. if( validState == 1 )
  1822. {
  1823. decl->validState = 1;
  1824. toValidate.PopLast();
  1825. }
  1826. else if( validState == 2 )
  1827. {
  1828. decl->validState = 2;
  1829. toValidate.PopLast();
  1830. }
  1831. else
  1832. {
  1833. toValidateNext.PushLast(toValidate.PopLast());
  1834. }
  1835. }
  1836. toValidate = toValidateNext;
  1837. toValidateNext.SetLength(0);
  1838. if( numClasses == toValidate.GetLength() )
  1839. {
  1840. int r, c;
  1841. toValidate[0]->script->ConvertPosToRowCol(toValidate[0]->node->tokenPos, &r, &c);
  1842. WriteError(toValidate[0]->script->name.AddressOf(), TXT_ILLEGAL_MEMBER_TYPE, r, c);
  1843. break;
  1844. }
  1845. }
  1846. if( numErrors > 0 ) return;
  1847. // Urho3D: disable garbage collection from script classes
  1848. /*
  1849. // Verify potential circular references
  1850. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1851. {
  1852. sClassDeclaration *decl = classDeclarations[n];
  1853. if( decl->isExistingShared ) continue;
  1854. asCObjectType *ot = decl->objType;
  1855. // Is there some path in which this structure is involved in circular references?
  1856. for( asUINT p = 0; p < ot->properties.GetLength(); p++ )
  1857. {
  1858. asCDataType dt = ot->properties[p]->type;
  1859. if( dt.IsObject() )
  1860. {
  1861. if( dt.IsObjectHandle() )
  1862. {
  1863. // TODO: optimize: If it is known that the handle can't be involved in a circular reference
  1864. // then this object doesn't need to be marked as garbage collected.
  1865. // - The application could set a flag when registering the object.
  1866. // - The script classes can be marked as final, then the compiler will
  1867. // be able to determine whether the class is garbage collected or not.
  1868. ot->flags |= asOBJ_GC;
  1869. break;
  1870. }
  1871. else if( dt.GetObjectType()->flags & asOBJ_GC )
  1872. {
  1873. // TODO: optimize: Just because the member type is a potential circle doesn't mean that this one is
  1874. // Only if the object is of a type that can reference this type, either directly or indirectly
  1875. ot->flags |= asOBJ_GC;
  1876. break;
  1877. }
  1878. }
  1879. }
  1880. }
  1881. */
  1882. }
  1883. int asCBuilder::CreateVirtualFunction(asCScriptFunction *func, int idx)
  1884. {
  1885. asCScriptFunction *vf = asNEW(asCScriptFunction)(engine, module, asFUNC_VIRTUAL);
  1886. vf->funcType = asFUNC_VIRTUAL;
  1887. vf->name = func->name;
  1888. vf->returnType = func->returnType;
  1889. vf->parameterTypes = func->parameterTypes;
  1890. vf->inOutFlags = func->inOutFlags;
  1891. vf->id = engine->GetNextScriptFunctionId();
  1892. vf->scriptSectionIdx = func->scriptSectionIdx;
  1893. vf->isReadOnly = func->isReadOnly;
  1894. vf->objectType = func->objectType;
  1895. vf->signatureId = func->signatureId;
  1896. vf->isPrivate = func->isPrivate;
  1897. vf->isFinal = func->isFinal;
  1898. vf->isOverride = func->isOverride;
  1899. vf->vfTableIdx = idx;
  1900. vf->defaultArgs = func->defaultArgs;
  1901. // Copy the default arg strings to avoid multiple deletes on the same object
  1902. for( asUINT n = 0; n < vf->defaultArgs.GetLength(); n++ )
  1903. if( vf->defaultArgs[n] )
  1904. vf->defaultArgs[n] = asNEW(asCString)(*vf->defaultArgs[n]);
  1905. module->AddScriptFunction(vf);
  1906. // Add a dummy to the builder so that it doesn't mix up function ids
  1907. functions.PushLast(0);
  1908. return vf->id;
  1909. }
  1910. asCObjectProperty *asCBuilder::AddPropertyToClass(sClassDeclaration *decl, const asCString &name, const asCDataType &dt, bool isPrivate, asCScriptCode *file, asCScriptNode *node)
  1911. {
  1912. if( !dt.CanBeInstanciated() )
  1913. {
  1914. if( file && node )
  1915. {
  1916. int r, c;
  1917. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1918. asCString str;
  1919. str.Format(TXT_DATA_TYPE_CANT_BE_s, dt.Format().AddressOf());
  1920. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  1921. }
  1922. return 0;
  1923. }
  1924. return decl->objType->AddPropertyToClass(name, dt, isPrivate);
  1925. }
  1926. bool asCBuilder::DoesMethodExist(asCObjectType *objType, int methodId, asUINT *methodIndex)
  1927. {
  1928. asCScriptFunction *method = GetFunctionDescription(methodId);
  1929. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  1930. {
  1931. asCScriptFunction *m = GetFunctionDescription(objType->methods[n]);
  1932. if( m->name != method->name ) continue;
  1933. if( m->returnType != method->returnType ) continue;
  1934. if( m->isReadOnly != method->isReadOnly ) continue;
  1935. if( m->parameterTypes != method->parameterTypes ) continue;
  1936. if( m->inOutFlags != method->inOutFlags ) continue;
  1937. if( methodIndex )
  1938. *methodIndex = n;
  1939. return true;
  1940. }
  1941. return false;
  1942. }
  1943. void asCBuilder::AddDefaultConstructor(asCObjectType *objType, asCScriptCode *file)
  1944. {
  1945. int funcId = engine->GetNextScriptFunctionId();
  1946. asCDataType returnType = asCDataType::CreatePrimitive(ttVoid, false);
  1947. asCArray<asCDataType> parameterTypes;
  1948. asCArray<asETypeModifiers> inOutFlags;
  1949. asCArray<asCString *> defaultArgs;
  1950. // Add the script function
  1951. module->AddScriptFunction(file->idx, funcId, objType->name.AddressOf(), returnType, parameterTypes.AddressOf(), inOutFlags.AddressOf(), defaultArgs.AddressOf(), (asUINT)parameterTypes.GetLength(), false, objType);
  1952. // Set it as default constructor
  1953. if( objType->beh.construct )
  1954. engine->scriptFunctions[objType->beh.construct]->Release();
  1955. objType->beh.construct = funcId;
  1956. objType->beh.constructors[0] = funcId;
  1957. engine->scriptFunctions[funcId]->AddRef();
  1958. // The bytecode for the default constructor will be generated
  1959. // only after the potential inheritance has been established
  1960. sFunctionDescription *func = asNEW(sFunctionDescription);
  1961. functions.PushLast(func);
  1962. func->script = file;
  1963. func->node = 0;
  1964. func->name = objType->name;
  1965. func->objType = objType;
  1966. func->funcId = funcId;
  1967. func->explicitSignature = 0;
  1968. // Add a default factory as well
  1969. funcId = engine->GetNextScriptFunctionId();
  1970. if( objType->beh.factory )
  1971. engine->scriptFunctions[objType->beh.factory]->Release();
  1972. objType->beh.factory = funcId;
  1973. objType->beh.factories[0] = funcId;
  1974. returnType = asCDataType::CreateObjectHandle(objType, false);
  1975. module->AddScriptFunction(file->idx, funcId, objType->name.AddressOf(), returnType, parameterTypes.AddressOf(), inOutFlags.AddressOf(), defaultArgs.AddressOf(), (asUINT)parameterTypes.GetLength(), false);
  1976. functions.PushLast(0);
  1977. asCCompiler compiler(engine);
  1978. compiler.CompileFactory(this, file, engine->scriptFunctions[funcId]);
  1979. engine->scriptFunctions[funcId]->AddRef();
  1980. // If the object is shared, then the factory must also be marked as shared
  1981. if( objType->flags & asOBJ_SHARED )
  1982. engine->scriptFunctions[funcId]->isShared = true;
  1983. }
  1984. int asCBuilder::RegisterEnum(asCScriptNode *node, asCScriptCode *file)
  1985. {
  1986. // Grab the name of the enumeration
  1987. asCScriptNode *tmp = node->firstChild;
  1988. asASSERT(snDataType == tmp->nodeType);
  1989. asCString name;
  1990. asASSERT(snIdentifier == tmp->firstChild->nodeType);
  1991. name.Assign(&file->code[tmp->firstChild->tokenPos], tmp->firstChild->tokenLength);
  1992. // Check the name and add the enum
  1993. int r = CheckNameConflict(name.AddressOf(), tmp->firstChild, file);
  1994. if( asSUCCESS == r )
  1995. {
  1996. asCObjectType *st;
  1997. asCDataType dataType;
  1998. st = asNEW(asCObjectType)(engine);
  1999. dataType.CreatePrimitive(ttInt, false);
  2000. st->flags = asOBJ_ENUM;
  2001. st->size = 4;
  2002. st->name = name;
  2003. module->enumTypes.PushLast(st);
  2004. st->AddRef();
  2005. engine->classTypes.PushLast(st);
  2006. // Store the location of this declaration for reference in name collisions
  2007. sClassDeclaration *decl = asNEW(sClassDeclaration);
  2008. decl->name = name;
  2009. decl->script = file;
  2010. decl->objType = st;
  2011. namedTypeDeclarations.PushLast(decl);
  2012. asCDataType type = CreateDataTypeFromNode(tmp, file);
  2013. asASSERT(!type.IsReference());
  2014. tmp = tmp->next;
  2015. while( tmp )
  2016. {
  2017. asASSERT(snIdentifier == tmp->nodeType);
  2018. asCString name(&file->code[tmp->tokenPos], tmp->tokenLength);
  2019. // Check for name conflict errors with other values in the enum
  2020. r = 0;
  2021. for( size_t n = globVariables.GetLength(); n-- > 0; )
  2022. {
  2023. sGlobalVariableDescription *gvar = globVariables[n];
  2024. if( gvar->datatype != type )
  2025. break;
  2026. if( gvar->name == name )
  2027. {
  2028. r = asNAME_TAKEN;
  2029. break;
  2030. }
  2031. }
  2032. if( asSUCCESS != r )
  2033. {
  2034. int r, c;
  2035. file->ConvertPosToRowCol(tmp->tokenPos, &r, &c);
  2036. asCString str;
  2037. str.Format(TXT_NAME_CONFLICT_s_ALREADY_USED, name.AddressOf());
  2038. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  2039. tmp = tmp->next;
  2040. if( tmp && tmp->nodeType == snAssignment )
  2041. tmp = tmp->next;
  2042. continue;
  2043. }
  2044. // check for assignment
  2045. asCScriptNode *asnNode = tmp->next;
  2046. if( asnNode && snAssignment == asnNode->nodeType )
  2047. asnNode->DisconnectParent();
  2048. else
  2049. asnNode = 0;
  2050. // Create the global variable description so the enum value can be evaluated
  2051. sGlobalVariableDescription *gvar = asNEW(sGlobalVariableDescription);
  2052. globVariables.PushLast(gvar);
  2053. gvar->script = file;
  2054. gvar->idNode = 0;
  2055. gvar->nextNode = asnNode;
  2056. gvar->name = name;
  2057. gvar->datatype = type;
  2058. // No need to allocate space on the global memory stack since the values are stored in the asCObjectType
  2059. gvar->index = 0;
  2060. gvar->isCompiled = false;
  2061. gvar->isPureConstant = true;
  2062. gvar->isEnumValue = true;
  2063. gvar->constantValue = 0xdeadbeef;
  2064. // Allocate dummy property so we can compile the value.
  2065. // This will be removed later on so we don't add it to the engine.
  2066. gvar->property = asNEW(asCGlobalProperty);
  2067. gvar->property->name = name;
  2068. gvar->property->type = gvar->datatype;
  2069. gvar->property->id = 0;
  2070. tmp = tmp->next;
  2071. }
  2072. }
  2073. node->Destroy(engine);
  2074. return r;
  2075. }
  2076. int asCBuilder::RegisterTypedef(asCScriptNode *node, asCScriptCode *file)
  2077. {
  2078. // Get the native data type
  2079. asCScriptNode *tmp = node->firstChild;
  2080. asASSERT(NULL != tmp && snDataType == tmp->nodeType);
  2081. asCDataType dataType;
  2082. dataType.CreatePrimitive(tmp->tokenType, false);
  2083. dataType.SetTokenType(tmp->tokenType);
  2084. tmp = tmp->next;
  2085. // Grab the name of the typedef
  2086. asASSERT(NULL != tmp && NULL == tmp->next);
  2087. asCString name;
  2088. name.Assign(&file->code[tmp->tokenPos], tmp->tokenLength);
  2089. // If the name is not already in use add it
  2090. int r = CheckNameConflict(name.AddressOf(), tmp, file);
  2091. if( asSUCCESS == r )
  2092. {
  2093. // Create the new type
  2094. asCObjectType *st = asNEW(asCObjectType)(engine);
  2095. st->flags = asOBJ_TYPEDEF;
  2096. st->size = dataType.GetSizeInMemoryBytes();
  2097. st->name = name;
  2098. st->templateSubType = dataType;
  2099. st->AddRef();
  2100. module->typeDefs.PushLast(st);
  2101. engine->classTypes.PushLast(st);
  2102. // Store the location of this declaration for reference in name collisions
  2103. sClassDeclaration *decl = asNEW(sClassDeclaration);
  2104. decl->name = name;
  2105. decl->script = file;
  2106. decl->objType = st;
  2107. namedTypeDeclarations.PushLast(decl);
  2108. }
  2109. node->Destroy(engine);
  2110. if( r < 0 )
  2111. {
  2112. engine->ConfigError(r);
  2113. }
  2114. return 0;
  2115. }
  2116. void asCBuilder::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)
  2117. {
  2118. node = node->firstChild;
  2119. // Is the function a private class method?
  2120. isPrivate = false;
  2121. if( node->tokenType == ttPrivate )
  2122. {
  2123. isPrivate = true;
  2124. node = node->next;
  2125. }
  2126. // Find the name
  2127. isConstructor = false;
  2128. isDestructor = false;
  2129. asCScriptNode *n = 0;
  2130. if( node->nodeType == snDataType )
  2131. n = node->next->next;
  2132. else
  2133. {
  2134. // If the first node is a ~ token, then we know it is a destructor
  2135. if( node->tokenType == ttBitNot )
  2136. {
  2137. n = node->next;
  2138. isDestructor = true;
  2139. }
  2140. else
  2141. {
  2142. n = node;
  2143. isConstructor = true;
  2144. }
  2145. }
  2146. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  2147. // Initialize a script function object for registration
  2148. if( !isConstructor && !isDestructor )
  2149. {
  2150. returnType = CreateDataTypeFromNode(node, file);
  2151. returnType = ModifyDataTypeFromNode(returnType, node->next, file, 0, 0);
  2152. }
  2153. else
  2154. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  2155. isConstMethod = false;
  2156. isFinal = false;
  2157. isOverride = false;
  2158. if( objType && n->next->next )
  2159. {
  2160. asCScriptNode *decorator = n->next->next;
  2161. // Is this a const method?
  2162. if( decorator->tokenType == ttConst )
  2163. {
  2164. isConstMethod = true;
  2165. decorator = decorator->next;
  2166. }
  2167. while( decorator )
  2168. {
  2169. if( decorator->tokenType == ttIdentifier && file->TokenEquals(decorator->tokenPos, decorator->tokenLength, FINAL_TOKEN) )
  2170. isFinal = true;
  2171. else if( decorator->tokenType == ttIdentifier && file->TokenEquals(decorator->tokenPos, decorator->tokenLength, OVERRIDE_TOKEN) )
  2172. isOverride = true;
  2173. decorator = decorator->next;
  2174. }
  2175. }
  2176. // Count the number of parameters
  2177. int count = 0;
  2178. asCScriptNode *c = n->next->firstChild;
  2179. while( c )
  2180. {
  2181. count++;
  2182. c = c->next->next;
  2183. if( c && c->nodeType == snIdentifier )
  2184. c = c->next;
  2185. if( c && c->nodeType == snExpression )
  2186. c = c->next;
  2187. }
  2188. // Get the parameter types
  2189. parameterTypes.Allocate(count, false);
  2190. inOutFlags.Allocate(count, false);
  2191. defaultArgs.Allocate(count, false);
  2192. n = n->next->firstChild;
  2193. while( n )
  2194. {
  2195. asETypeModifiers inOutFlag;
  2196. asCDataType type = CreateDataTypeFromNode(n, file);
  2197. type = ModifyDataTypeFromNode(type, n->next, file, &inOutFlag, 0);
  2198. // Store the parameter type
  2199. parameterTypes.PushLast(type);
  2200. inOutFlags.PushLast(inOutFlag);
  2201. // Move to next parameter
  2202. n = n->next->next;
  2203. if( n && n->nodeType == snIdentifier )
  2204. n = n->next;
  2205. if( n && n->nodeType == snExpression )
  2206. {
  2207. // Strip out white space and comments to better share the string
  2208. asCString *defaultArgStr = asNEW(asCString);
  2209. *defaultArgStr = GetCleanExpressionString(n, file);
  2210. defaultArgs.PushLast(defaultArgStr);
  2211. n = n->next;
  2212. }
  2213. else
  2214. defaultArgs.PushLast(0);
  2215. }
  2216. }
  2217. asCString asCBuilder::GetCleanExpressionString(asCScriptNode *node, asCScriptCode *file)
  2218. {
  2219. asASSERT(node && node->nodeType == snExpression);
  2220. asCString str;
  2221. str.Assign(file->code + node->tokenPos, node->tokenLength);
  2222. asCString cleanStr;
  2223. for( asUINT n = 0; n < str.GetLength(); )
  2224. {
  2225. int len;
  2226. asETokenClass tok = engine->ParseToken(str.AddressOf() + n, str.GetLength() - n, &len);
  2227. if( tok != asTC_COMMENT && tok != asTC_WHITESPACE )
  2228. {
  2229. if( cleanStr.GetLength() ) cleanStr += " ";
  2230. cleanStr.Concatenate(str.AddressOf() + n, len);
  2231. }
  2232. n += len;
  2233. }
  2234. return cleanStr;
  2235. }
  2236. int asCBuilder::RegisterScriptFunction(int funcId, asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, bool isInterface, bool isGlobalFunction)
  2237. {
  2238. asCString name;
  2239. asCDataType returnType;
  2240. asCArray<asCDataType> parameterTypes;
  2241. asCArray<asETypeModifiers> inOutFlags;
  2242. asCArray<asCString *> defaultArgs;
  2243. bool isConstMethod;
  2244. bool isOverride;
  2245. bool isFinal;
  2246. bool isConstructor;
  2247. bool isDestructor;
  2248. bool isPrivate;
  2249. GetParsedFunctionDetails(node, file, objType, name, returnType, parameterTypes, inOutFlags, defaultArgs, isConstMethod, isConstructor, isDestructor, isPrivate, isOverride, isFinal);
  2250. // Check for name conflicts
  2251. if( !isConstructor && !isDestructor )
  2252. {
  2253. if( objType )
  2254. {
  2255. CheckNameConflictMember(objType, name.AddressOf(), node, file, false);
  2256. if( name == objType->name )
  2257. {
  2258. int r, c;
  2259. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2260. WriteError(file->name.AddressOf(), TXT_METHOD_CANT_HAVE_NAME_OF_CLASS, r, c);
  2261. }
  2262. }
  2263. else
  2264. CheckNameConflict(name.AddressOf(), node, file);
  2265. }
  2266. else
  2267. {
  2268. // Verify that the name of the constructor/destructor is the same as the class
  2269. if( name != objType->name )
  2270. {
  2271. int r, c;
  2272. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2273. WriteError(file->name.AddressOf(), TXT_CONSTRUCTOR_NAME_ERROR, r, c);
  2274. }
  2275. if( isDestructor )
  2276. name = "~" + name;
  2277. }
  2278. if( !isInterface )
  2279. {
  2280. sFunctionDescription *func = asNEW(sFunctionDescription);
  2281. functions.PushLast(func);
  2282. func->script = file;
  2283. func->node = node;
  2284. func->name = name;
  2285. func->objType = objType;
  2286. func->funcId = funcId;
  2287. func->explicitSignature = 0;
  2288. }
  2289. // Destructors may not have any parameters
  2290. if( isDestructor && parameterTypes.GetLength() > 0 )
  2291. {
  2292. int r, c;
  2293. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2294. WriteError(file->name.AddressOf(), TXT_DESTRUCTOR_MAY_NOT_HAVE_PARM, r, c);
  2295. }
  2296. // If class or interface is shared, then only shared types may be used in the method signature
  2297. if( objType && objType->IsShared() )
  2298. {
  2299. asCObjectType *ot = returnType.GetObjectType();
  2300. if( ot && !ot->IsShared() )
  2301. {
  2302. int r, c;
  2303. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2304. asCString msg;
  2305. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  2306. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  2307. }
  2308. for( asUINT p = 0; p < parameterTypes.GetLength(); ++p )
  2309. {
  2310. asCObjectType *ot = parameterTypes[p].GetObjectType();
  2311. if( ot && !ot->IsShared() )
  2312. {
  2313. int r, c;
  2314. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2315. asCString msg;
  2316. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  2317. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  2318. }
  2319. }
  2320. }
  2321. // TODO: Much of this can probably be reduced by using the IsSignatureEqual method
  2322. // Check that the same function hasn't been registered already
  2323. asCArray<int> funcs;
  2324. GetFunctionDescriptions(name.AddressOf(), funcs);
  2325. if( funcs.GetLength() )
  2326. {
  2327. for( asUINT n = 0; n < funcs.GetLength(); ++n )
  2328. {
  2329. asCScriptFunction *func = GetFunctionDescription(funcs[n]);
  2330. if( parameterTypes.GetLength() == func->parameterTypes.GetLength() )
  2331. {
  2332. bool match = true;
  2333. if( func->objectType != objType )
  2334. {
  2335. match = false;
  2336. break;
  2337. }
  2338. for( asUINT p = 0; p < parameterTypes.GetLength(); ++p )
  2339. {
  2340. if( parameterTypes[p] != func->parameterTypes[p] )
  2341. {
  2342. match = false;
  2343. break;
  2344. }
  2345. }
  2346. if( match )
  2347. {
  2348. int r, c;
  2349. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2350. WriteError(file->name.AddressOf(), TXT_FUNCTION_ALREADY_EXIST, r, c);
  2351. break;
  2352. }
  2353. }
  2354. }
  2355. }
  2356. // Register the function
  2357. module->AddScriptFunction(file->idx, funcId, name.AddressOf(), returnType, parameterTypes.AddressOf(), inOutFlags.AddressOf(), defaultArgs.AddressOf(), (asUINT)parameterTypes.GetLength(), isInterface, objType, isConstMethod, isGlobalFunction, isPrivate, isFinal, isOverride);
  2358. // Make sure the default args are declared correctly
  2359. ValidateDefaultArgs(file, node, engine->scriptFunctions[funcId]);
  2360. if( objType )
  2361. {
  2362. engine->scriptFunctions[funcId]->AddRef();
  2363. if( isConstructor )
  2364. {
  2365. int factoryId = engine->GetNextScriptFunctionId();
  2366. if( parameterTypes.GetLength() == 0 )
  2367. {
  2368. // Overload the default constructor
  2369. engine->scriptFunctions[objType->beh.construct]->Release();
  2370. objType->beh.construct = funcId;
  2371. objType->beh.constructors[0] = funcId;
  2372. // Register the default factory as well
  2373. engine->scriptFunctions[objType->beh.factory]->Release();
  2374. objType->beh.factory = factoryId;
  2375. objType->beh.factories[0] = factoryId;
  2376. }
  2377. else
  2378. {
  2379. objType->beh.constructors.PushLast(funcId);
  2380. // Register the factory as well
  2381. objType->beh.factories.PushLast(factoryId);
  2382. }
  2383. // We must copy the default arg strings to avoid deleting the same object multiple times
  2384. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  2385. if( defaultArgs[n] )
  2386. defaultArgs[n] = asNEW(asCString)(*defaultArgs[n]);
  2387. asCDataType dt = asCDataType::CreateObjectHandle(objType, false);
  2388. module->AddScriptFunction(file->idx, factoryId, name.AddressOf(), dt, parameterTypes.AddressOf(), inOutFlags.AddressOf(), defaultArgs.AddressOf(), (asUINT)parameterTypes.GetLength(), false);
  2389. // If the object is shared, then the factory must also be marked as shared
  2390. if( objType->flags & asOBJ_SHARED )
  2391. engine->scriptFunctions[factoryId]->isShared = true;
  2392. // Add a dummy function to the builder so that it doesn't mix up the fund Ids
  2393. functions.PushLast(0);
  2394. // Compile the factory immediately
  2395. asCCompiler compiler(engine);
  2396. compiler.CompileFactory(this, file, engine->scriptFunctions[factoryId]);
  2397. engine->scriptFunctions[factoryId]->AddRef();
  2398. }
  2399. else if( isDestructor )
  2400. objType->beh.destruct = funcId;
  2401. else
  2402. objType->methods.PushLast(funcId);
  2403. }
  2404. // We need to delete the node already if this is an interface method
  2405. if( isInterface && node )
  2406. {
  2407. node->Destroy(engine);
  2408. }
  2409. return 0;
  2410. }
  2411. int asCBuilder::RegisterScriptFunctionWithSignature(int funcId, asCScriptNode *node, asCScriptCode *file, asCString &name, sExplicitSignature *signature, asCObjectType *objType, bool isInterface, bool isGlobalFunction, bool isPrivate, bool isConst, bool isFinal, bool isOverride, bool treatAsProperty)
  2412. {
  2413. bool isConstructor = false;
  2414. bool isDestructor = false;
  2415. asCArray<asCDataType> &parameterTypes = signature->argTypes;
  2416. asCArray<asETypeModifiers> &inOutFlags = signature->argModifiers;
  2417. asCArray<asCString *> defaultArgs = signature->defaultArgs;
  2418. if( objType && asCDataType::CreatePrimitive(ttVoid, false) == signature->returnType )
  2419. {
  2420. if( 0 == name.Compare( objType->name ) )
  2421. isConstructor = true;
  2422. else if( 0 == name.Compare( "~" + objType->name ) )
  2423. isDestructor = true;
  2424. }
  2425. // Check for name conflicts
  2426. if( !isConstructor && !isDestructor )
  2427. {
  2428. if( objType )
  2429. CheckNameConflictMember(objType, name.AddressOf(), node, file, treatAsProperty);
  2430. else
  2431. CheckNameConflict(name.AddressOf(), node, file);
  2432. }
  2433. if( isInterface )
  2434. {
  2435. asASSERT(!isFinal);
  2436. asASSERT(!isOverride);
  2437. }
  2438. else
  2439. {
  2440. sFunctionDescription *func = asNEW(sFunctionDescription);
  2441. functions.PushLast(func);
  2442. func->script = file;
  2443. func->node = node;
  2444. func->name = name;
  2445. func->objType = objType;
  2446. func->funcId = funcId;
  2447. func->explicitSignature = signature;
  2448. }
  2449. // Destructors may not have any parameters
  2450. if( isDestructor && parameterTypes.GetLength() > 0 )
  2451. {
  2452. int r, c;
  2453. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2454. WriteError(file->name.AddressOf(), TXT_DESTRUCTOR_MAY_NOT_HAVE_PARM, r, c);
  2455. }
  2456. // If class or interface is shared, then only shared types may be used in the method signature
  2457. if( objType && (objType->flags & asOBJ_SHARED) )
  2458. {
  2459. asCObjectType *ot = signature->returnType.GetObjectType();
  2460. if( ot && (ot->flags & asOBJ_SCRIPT_OBJECT) && !(ot->flags & asOBJ_SHARED) )
  2461. {
  2462. int r, c;
  2463. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2464. asCString msg;
  2465. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  2466. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  2467. }
  2468. for( asUINT p = 0; p < parameterTypes.GetLength(); ++p )
  2469. {
  2470. asCObjectType *ot = parameterTypes[p].GetObjectType();
  2471. if( ot && (ot->flags & asOBJ_SCRIPT_OBJECT) && !(ot->flags & asOBJ_SHARED) )
  2472. {
  2473. int r, c;
  2474. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2475. asCString msg;
  2476. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  2477. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  2478. }
  2479. }
  2480. }
  2481. // TODO: Much of this can probably be reduced by using the IsSignatureEqual method
  2482. // Check that the same function hasn't been registered already
  2483. asCArray<int> funcs;
  2484. GetFunctionDescriptions(name.AddressOf(), funcs);
  2485. if( funcs.GetLength() )
  2486. {
  2487. for( asUINT n = 0; n < funcs.GetLength(); ++n )
  2488. {
  2489. asCScriptFunction *func = GetFunctionDescription(funcs[n]);
  2490. if( parameterTypes.GetLength() == func->parameterTypes.GetLength() )
  2491. {
  2492. bool match = true;
  2493. if( func->objectType != objType )
  2494. {
  2495. match = false;
  2496. break;
  2497. }
  2498. for( asUINT p = 0; p < parameterTypes.GetLength(); ++p )
  2499. {
  2500. if( parameterTypes[p] != func->parameterTypes[p] )
  2501. {
  2502. match = false;
  2503. break;
  2504. }
  2505. }
  2506. if( match )
  2507. {
  2508. int r, c;
  2509. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2510. WriteError(file->name.AddressOf(), TXT_FUNCTION_ALREADY_EXIST, r, c);
  2511. break;
  2512. }
  2513. }
  2514. }
  2515. }
  2516. // Register the function
  2517. module->AddScriptFunction(file->idx, funcId, name.AddressOf(), signature->returnType, parameterTypes.AddressOf(), inOutFlags.AddressOf(), defaultArgs.AddressOf(), (asUINT)parameterTypes.GetLength(), isInterface, objType, isConst, isGlobalFunction, isPrivate, isFinal, isOverride);
  2518. // Make sure the default args are declared correctly
  2519. ValidateDefaultArgs(file, node, engine->scriptFunctions[funcId]);
  2520. if( objType )
  2521. {
  2522. engine->scriptFunctions[funcId]->AddRef();
  2523. if( isConstructor )
  2524. {
  2525. int factoryId = engine->GetNextScriptFunctionId();
  2526. if( parameterTypes.GetLength() == 0 )
  2527. {
  2528. // Overload the default constructor
  2529. engine->scriptFunctions[objType->beh.construct]->Release();
  2530. objType->beh.construct = funcId;
  2531. objType->beh.constructors[0] = funcId;
  2532. // Register the default factory as well
  2533. engine->scriptFunctions[objType->beh.factory]->Release();
  2534. objType->beh.factory = factoryId;
  2535. objType->beh.factories[0] = factoryId;
  2536. }
  2537. else
  2538. {
  2539. objType->beh.constructors.PushLast(funcId);
  2540. // Register the factory as well
  2541. objType->beh.factories.PushLast(factoryId);
  2542. }
  2543. // We must copy the default arg strings to avoid deleting the same object multiple times
  2544. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  2545. if( defaultArgs[n] )
  2546. defaultArgs[n] = asNEW(asCString)(*defaultArgs[n]);
  2547. asCDataType dt = asCDataType::CreateObjectHandle(objType, false);
  2548. module->AddScriptFunction(file->idx, factoryId, name.AddressOf(), dt, parameterTypes.AddressOf(), inOutFlags.AddressOf(), defaultArgs.AddressOf(), (asUINT)parameterTypes.GetLength(), false);
  2549. // Add a dummy function to the builder so that it doesn't mix up the fund Ids
  2550. functions.PushLast(0);
  2551. // Compile the factory immediately
  2552. asCCompiler compiler(engine);
  2553. compiler.CompileFactory(this, file, engine->scriptFunctions[factoryId]);
  2554. engine->scriptFunctions[factoryId]->AddRef();
  2555. }
  2556. else if( isDestructor )
  2557. objType->beh.destruct = funcId;
  2558. else
  2559. objType->methods.PushLast(funcId);
  2560. }
  2561. // We need to delete the node already if this is an interface method
  2562. if( isInterface && node )
  2563. {
  2564. node->Destroy(engine);
  2565. }
  2566. return 0;
  2567. }
  2568. int asCBuilder::RegisterVirtualProperty(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, bool isInterface, bool isGlobalFunction)
  2569. {
  2570. if( engine->ep.propertyAccessorMode != 2 )
  2571. {
  2572. int r, c;
  2573. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2574. WriteError(file->name.AddressOf(), TXT_PROPERTY_ACCESSOR_DISABLED, r, c);
  2575. node->Destroy(engine);
  2576. return 0;
  2577. }
  2578. bool isPrivate = false;
  2579. asCString emulatedName;
  2580. asCDataType emulatedType;
  2581. if( !isGlobalFunction && node->tokenType == ttPrivate )
  2582. {
  2583. isPrivate = true;
  2584. node = node->next;
  2585. }
  2586. asCScriptNode *mainNode = node;
  2587. node = node->firstChild;
  2588. emulatedType = CreateDataTypeFromNode(node, file);
  2589. emulatedType = ModifyDataTypeFromNode(emulatedType, node->next, file, 0, 0);
  2590. node = node->next->next;
  2591. emulatedName.Assign(&file->code[node->tokenPos], node->tokenLength);
  2592. node = node->next;
  2593. while( node )
  2594. {
  2595. asCScriptNode *next = node->next;
  2596. asCScriptNode *funcNode = 0;
  2597. sExplicitSignature *signature = 0;
  2598. bool success = false;
  2599. bool isConst = false;
  2600. bool isFinal = false;
  2601. bool isOverride = false;
  2602. asCString name;
  2603. // TODO: getset: Allow private for individual property accessors
  2604. // TODO: getset: If the accessor uses its own name, then the property should be automatically declared
  2605. if( node->firstChild->nodeType == snIdentifier && file->TokenEquals(node->firstChild->tokenPos, node->firstChild->tokenLength, GET_TOKEN) )
  2606. {
  2607. funcNode = node->firstChild->next;
  2608. if( funcNode && funcNode->tokenType == ttConst )
  2609. {
  2610. isConst = true;
  2611. funcNode = funcNode->next;
  2612. }
  2613. while( funcNode && funcNode->nodeType != snStatementBlock )
  2614. {
  2615. if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, FINAL_TOKEN) )
  2616. isFinal = true;
  2617. else if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, OVERRIDE_TOKEN) )
  2618. isOverride = true;
  2619. funcNode = funcNode->next;
  2620. }
  2621. if( funcNode )
  2622. funcNode->DisconnectParent();
  2623. if( funcNode == 0 && (objType == 0 || !objType->IsInterface()) )
  2624. {
  2625. // TODO: getset: If no implementation is supplied the builder should provide an automatically generated implementation
  2626. // The compiler needs to be able to handle the different types, primitive, value type, and handle
  2627. // The code is also different for global property accessors
  2628. int r, c;
  2629. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2630. WriteError(file->name.AddressOf(), TXT_PROPERTY_ACCESSOR_MUST_BE_IMPLEMENTED, r, c);
  2631. }
  2632. signature = asNEW(sExplicitSignature);
  2633. signature->returnType = emulatedType;
  2634. name = "get_" + emulatedName;
  2635. success = true;
  2636. }
  2637. else if( node->firstChild->nodeType == snIdentifier && file->TokenEquals(node->firstChild->tokenPos, node->firstChild->tokenLength, SET_TOKEN) )
  2638. {
  2639. funcNode = node->firstChild->next;
  2640. if( funcNode && funcNode->tokenType == ttConst )
  2641. {
  2642. isConst = true;
  2643. funcNode = funcNode->next;
  2644. }
  2645. while( funcNode && funcNode->nodeType != snStatementBlock )
  2646. {
  2647. if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, FINAL_TOKEN) )
  2648. isFinal = true;
  2649. else if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, OVERRIDE_TOKEN) )
  2650. isOverride = true;
  2651. funcNode = funcNode->next;
  2652. }
  2653. if( funcNode )
  2654. funcNode->DisconnectParent();
  2655. if( funcNode == 0 && (objType == 0 || !objType->IsInterface()) )
  2656. {
  2657. int r, c;
  2658. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2659. WriteError(file->name.AddressOf(), TXT_PROPERTY_ACCESSOR_MUST_BE_IMPLEMENTED, r, c);
  2660. }
  2661. signature = asNEW(sExplicitSignature)(1);
  2662. signature->returnType = asCDataType::CreatePrimitive(ttVoid, false);
  2663. signature->argModifiers.PushLast(asTM_NONE);
  2664. signature->argNames.PushLast("value");
  2665. signature->argTypes.PushLast(emulatedType);
  2666. signature->defaultArgs.PushLast(0);
  2667. name = "set_" + emulatedName;
  2668. success = true;
  2669. }
  2670. else
  2671. {
  2672. int r, c;
  2673. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2674. WriteError(file->name.AddressOf(), TXT_UNRECOGNIZED_VIRTUAL_PROPERTY_NODE, r, c);
  2675. }
  2676. if( success )
  2677. {
  2678. RegisterScriptFunctionWithSignature(engine->GetNextScriptFunctionId(), funcNode, file, name, signature, objType, isInterface, isGlobalFunction, isPrivate, isConst, isFinal, isOverride, true);
  2679. }
  2680. node = next;
  2681. };
  2682. mainNode->Destroy(engine);
  2683. return 0;
  2684. }
  2685. int asCBuilder::RegisterImportedFunction(int importID, asCScriptNode *node, asCScriptCode *file)
  2686. {
  2687. // Find name
  2688. asCScriptNode *f = node->firstChild;
  2689. asCScriptNode *n = f->firstChild->next->next;
  2690. // Check for name conflicts
  2691. asCString name(&file->code[n->tokenPos], n->tokenLength);
  2692. CheckNameConflict(name.AddressOf(), n, file);
  2693. // Initialize a script function object for registration
  2694. asCDataType returnType;
  2695. returnType = CreateDataTypeFromNode(f->firstChild, file);
  2696. returnType = ModifyDataTypeFromNode(returnType, f->firstChild->next, file, 0, 0);
  2697. // Count the parameters
  2698. int count = 0;
  2699. asCScriptNode *c = n->next->firstChild;
  2700. while( c )
  2701. {
  2702. count++;
  2703. c = c->next->next;
  2704. if( c && c->nodeType == snIdentifier )
  2705. c = c->next;
  2706. }
  2707. asCArray<asCDataType> parameterTypes(count);
  2708. asCArray<asETypeModifiers> inOutFlags(count);
  2709. n = n->next->firstChild;
  2710. while( n )
  2711. {
  2712. asETypeModifiers inOutFlag;
  2713. asCDataType type = CreateDataTypeFromNode(n, file);
  2714. type = ModifyDataTypeFromNode(type, n->next, file, &inOutFlag, 0);
  2715. // Store the parameter type
  2716. parameterTypes.PushLast(type);
  2717. inOutFlags.PushLast(inOutFlag);
  2718. if( type.GetTokenType() == ttVoid )
  2719. {
  2720. int r, c;
  2721. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  2722. asCString str;
  2723. str.Format(TXT_PARAMETER_CANT_BE_s, type.Format().AddressOf());
  2724. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  2725. break;
  2726. }
  2727. // Move to next parameter
  2728. n = n->next->next;
  2729. if( n && n->nodeType == snIdentifier )
  2730. n = n->next;
  2731. }
  2732. // Check that the same function hasn't been registered already
  2733. asCArray<int> funcs;
  2734. GetFunctionDescriptions(name.AddressOf(), funcs);
  2735. if( funcs.GetLength() )
  2736. {
  2737. for( asUINT n = 0; n < funcs.GetLength(); ++n )
  2738. {
  2739. asCScriptFunction *func = GetFunctionDescription(funcs[n]);
  2740. if( parameterTypes.GetLength() == func->parameterTypes.GetLength() )
  2741. {
  2742. bool match = true;
  2743. for( asUINT p = 0; p < parameterTypes.GetLength(); ++p )
  2744. {
  2745. if( parameterTypes[p] != func->parameterTypes[p] )
  2746. {
  2747. match = false;
  2748. break;
  2749. }
  2750. }
  2751. if( match )
  2752. {
  2753. int r, c;
  2754. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2755. WriteError(file->name.AddressOf(), TXT_FUNCTION_ALREADY_EXIST, r, c);
  2756. break;
  2757. }
  2758. }
  2759. }
  2760. }
  2761. // Read the module name as well
  2762. n = node->firstChild->next;
  2763. asCString moduleName;
  2764. moduleName.Assign(&file->code[n->tokenPos+1], n->tokenLength-2);
  2765. node->Destroy(engine);
  2766. // Register the function
  2767. module->AddImportedFunction(importID, name.AddressOf(), returnType, parameterTypes.AddressOf(), inOutFlags.AddressOf(), (asUINT)parameterTypes.GetLength(), moduleName);
  2768. return 0;
  2769. }
  2770. asCScriptFunction *asCBuilder::GetFunctionDescription(int id)
  2771. {
  2772. // TODO: import: This should be improved when the imported functions are removed
  2773. // Get the description from the engine
  2774. if( (id & 0xFFFF0000) == 0 )
  2775. return engine->scriptFunctions[id];
  2776. else
  2777. return engine->importedFunctions[id & 0xFFFF]->importedFunctionSignature;
  2778. }
  2779. void asCBuilder::GetFunctionDescriptions(const char *name, asCArray<int> &funcs)
  2780. {
  2781. // TODO: optimize: Improve linear searches in GetFunctionDescriptions
  2782. // A large part of the compilation time seems to be spent in this function
  2783. // I need to have a map with all global functions so that it will be
  2784. // quicker to find them by name. The key should be the function name, and
  2785. // the value a list with all the functions using that name
  2786. asUINT n;
  2787. for( n = 0; n < module->scriptFunctions.GetLength(); n++ )
  2788. {
  2789. if( module->scriptFunctions[n]->name == name &&
  2790. module->scriptFunctions[n]->objectType == 0 )
  2791. funcs.PushLast(module->scriptFunctions[n]->id);
  2792. }
  2793. for( n = 0; n < module->bindInformations.GetLength(); n++ )
  2794. {
  2795. if( module->bindInformations[n]->importedFunctionSignature->name == name )
  2796. funcs.PushLast(module->bindInformations[n]->importedFunctionSignature->id);
  2797. }
  2798. // TODO: optimize: Use the registeredGlobalFunctions array instead
  2799. for( n = 0; n < engine->scriptFunctions.GetLength(); n++ )
  2800. {
  2801. if( engine->scriptFunctions[n] &&
  2802. engine->scriptFunctions[n]->funcType == asFUNC_SYSTEM &&
  2803. engine->scriptFunctions[n]->objectType == 0 &&
  2804. engine->scriptFunctions[n]->name == name )
  2805. {
  2806. // Verify if the module has access to the function
  2807. if( module->accessMask & engine->scriptFunctions[n]->accessMask )
  2808. {
  2809. #ifdef AS_DEPRECATED
  2810. // deprecated since 2011-10-04
  2811. // Find the config group for the global function
  2812. asCConfigGroup *group = engine->FindConfigGroupForFunction(engine->scriptFunctions[n]->id);
  2813. if( !group || group->HasModuleAccess(module->name.AddressOf()) )
  2814. #endif
  2815. funcs.PushLast(engine->scriptFunctions[n]->id);
  2816. }
  2817. }
  2818. }
  2819. }
  2820. void asCBuilder::GetObjectMethodDescriptions(const char *name, asCObjectType *objectType, asCArray<int> &methods, bool objIsConst, const asCString &scope)
  2821. {
  2822. if( scope != "" )
  2823. {
  2824. // Find the base class with the specified scope
  2825. while( objectType && objectType->name != scope )
  2826. objectType = objectType->derivedFrom;
  2827. // If the scope is not any of the base classes, then return no methods
  2828. if( objectType == 0 )
  2829. return;
  2830. }
  2831. // TODO: optimize: Improve linear search
  2832. if( objIsConst )
  2833. {
  2834. // Only add const methods to the list
  2835. for( asUINT n = 0; n < objectType->methods.GetLength(); n++ )
  2836. {
  2837. if( engine->scriptFunctions[objectType->methods[n]]->name == name &&
  2838. engine->scriptFunctions[objectType->methods[n]]->isReadOnly )
  2839. {
  2840. // When the scope is defined the returned methods should be the true methods, not the virtual method stubs
  2841. if( scope == "" )
  2842. methods.PushLast(engine->scriptFunctions[objectType->methods[n]]->id);
  2843. else
  2844. {
  2845. asCScriptFunction *virtFunc = engine->scriptFunctions[objectType->methods[n]];
  2846. asCScriptFunction *realFunc = objectType->virtualFunctionTable[virtFunc->vfTableIdx];
  2847. methods.PushLast(realFunc->id);
  2848. }
  2849. }
  2850. }
  2851. }
  2852. else
  2853. {
  2854. // TODO: Prefer non-const over const
  2855. for( asUINT n = 0; n < objectType->methods.GetLength(); n++ )
  2856. {
  2857. if( engine->scriptFunctions[objectType->methods[n]]->name == name )
  2858. {
  2859. // When the scope is defined the returned methods should be the true methods, not the virtual method stubs
  2860. if( scope == "" )
  2861. methods.PushLast(engine->scriptFunctions[objectType->methods[n]]->id);
  2862. else
  2863. {
  2864. asCScriptFunction *virtFunc = engine->scriptFunctions[objectType->methods[n]];
  2865. asCScriptFunction *realFunc = objectType->virtualFunctionTable[virtFunc->vfTableIdx];
  2866. methods.PushLast(realFunc->id);
  2867. }
  2868. }
  2869. }
  2870. }
  2871. }
  2872. void asCBuilder::WriteInfo(const char *scriptname, const char *message, int r, int c, bool pre)
  2873. {
  2874. // Need to store the pre message in a structure
  2875. if( pre )
  2876. {
  2877. preMessage.isSet = true;
  2878. preMessage.c = c;
  2879. preMessage.r = r;
  2880. preMessage.message = message;
  2881. }
  2882. else
  2883. {
  2884. preMessage.isSet = false;
  2885. engine->WriteMessage(scriptname, r, c, asMSGTYPE_INFORMATION, message);
  2886. }
  2887. }
  2888. void asCBuilder::WriteError(const char *scriptname, const char *message, int r, int c)
  2889. {
  2890. numErrors++;
  2891. // Need to pass the preMessage first
  2892. if( preMessage.isSet )
  2893. WriteInfo(scriptname, preMessage.message.AddressOf(), preMessage.r, preMessage.c, false);
  2894. engine->WriteMessage(scriptname, r, c, asMSGTYPE_ERROR, message);
  2895. }
  2896. void asCBuilder::WriteWarning(const char *scriptname, const char *message, int r, int c)
  2897. {
  2898. numWarnings++;
  2899. // Need to pass the preMessage first
  2900. if( preMessage.isSet )
  2901. WriteInfo(scriptname, preMessage.message.AddressOf(), preMessage.r, preMessage.c, false);
  2902. engine->WriteMessage(scriptname, r, c, asMSGTYPE_WARNING, message);
  2903. }
  2904. asCDataType asCBuilder::CreateDataTypeFromNode(asCScriptNode *node, asCScriptCode *file, bool acceptHandleForScope, asCObjectType *templateType)
  2905. {
  2906. asASSERT(node->nodeType == snDataType);
  2907. asCDataType dt;
  2908. asCScriptNode *n = node->firstChild;
  2909. bool isConst = false;
  2910. bool isImplicitHandle = false;
  2911. if( n->tokenType == ttConst )
  2912. {
  2913. isConst = true;
  2914. n = n->next;
  2915. }
  2916. if( n->tokenType == ttIdentifier )
  2917. {
  2918. asCString str;
  2919. str.Assign(&file->code[n->tokenPos], n->tokenLength);
  2920. asCObjectType *ot = 0;
  2921. // If this is for a template type, then we must first determine if the
  2922. // identifier matches any of the template subtypes
  2923. // TODO: template: it should be possible to have more than one subtypes
  2924. if( templateType && (templateType->flags & asOBJ_TEMPLATE) && str == templateType->templateSubType.GetObjectType()->name )
  2925. ot = templateType->templateSubType.GetObjectType();
  2926. if( ot == 0 )
  2927. ot = GetObjectType(str.AddressOf());
  2928. if( ot )
  2929. {
  2930. if( ot->flags & asOBJ_IMPLICIT_HANDLE )
  2931. isImplicitHandle = true;
  2932. // Make sure the module has access to the object type
  2933. #ifdef AS_DEPRECATED
  2934. // deprecated since 2011-10-04
  2935. // Find the config group for the object type
  2936. asCConfigGroup *group = engine->FindConfigGroupForObjectType(ot);
  2937. if( !module || ((module->accessMask & ot->accessMask) && (!group || group->HasModuleAccess(module->name.AddressOf()))) )
  2938. #else
  2939. if( !module || (module->accessMask & ot->accessMask) )
  2940. #endif
  2941. {
  2942. if(asOBJ_TYPEDEF == (ot->flags & asOBJ_TYPEDEF))
  2943. {
  2944. // TODO: typedef: A typedef should be considered different from the original type (though with implicit conversions between the two)
  2945. // Create primitive data type based on object flags
  2946. dt = ot->templateSubType;
  2947. dt.MakeReadOnly(isConst);
  2948. }
  2949. else
  2950. {
  2951. if( ot->flags & asOBJ_TEMPLATE )
  2952. {
  2953. n = n->next;
  2954. // Check if the subtype is a type or the template's subtype
  2955. // if it is the template's subtype then this is the actual template type,
  2956. // orderwise it is a template instance.
  2957. // Only do this for application registered interface, as the
  2958. // scripts cannot implement templates.
  2959. asCDataType subType = CreateDataTypeFromNode(n, file, false, module ? 0 : ot);
  2960. if( subType.GetObjectType() != ot->templateSubType.GetObjectType() )
  2961. {
  2962. // This is a template instance
  2963. // Need to find the correct object type
  2964. asCObjectType *otInstance = engine->GetTemplateInstanceType(ot, subType);
  2965. if( !otInstance )
  2966. {
  2967. asCString msg;
  2968. msg.Format(TXT_CANNOT_INSTANCIATE_TEMPLATE_s_WITH_s, ot->name.AddressOf(), subType.Format().AddressOf());
  2969. int r, c;
  2970. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  2971. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  2972. }
  2973. ot = otInstance;
  2974. }
  2975. }
  2976. // Create object data type
  2977. if( ot )
  2978. dt = asCDataType::CreateObject(ot, isConst);
  2979. else
  2980. dt = asCDataType::CreatePrimitive(ttInt, isConst);
  2981. }
  2982. }
  2983. else
  2984. {
  2985. asCString msg;
  2986. msg.Format(TXT_TYPE_s_NOT_AVAILABLE_FOR_MODULE, (const char *)str.AddressOf());
  2987. int r, c;
  2988. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  2989. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  2990. dt.SetTokenType(ttInt);
  2991. }
  2992. }
  2993. else if( ot == 0 )
  2994. {
  2995. // It can still be a function definition
  2996. asCScriptFunction *funcdef = GetFuncDef(str.AddressOf());
  2997. if( funcdef )
  2998. {
  2999. dt = asCDataType::CreateFuncDef(funcdef);
  3000. }
  3001. else if( funcdef == 0 )
  3002. {
  3003. asCString msg;
  3004. msg.Format(TXT_IDENTIFIER_s_NOT_DATA_TYPE, (const char *)str.AddressOf());
  3005. int r, c;
  3006. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  3007. WriteError(file->name.AddressOf(), msg.AddressOf(), r, c);
  3008. dt = asCDataType::CreatePrimitive(ttInt, isConst);
  3009. return dt;
  3010. }
  3011. }
  3012. }
  3013. else
  3014. {
  3015. // Create primitive data type
  3016. dt = asCDataType::CreatePrimitive(n->tokenType, isConst);
  3017. }
  3018. // Determine array dimensions and object handles
  3019. n = n->next;
  3020. while( n && (n->tokenType == ttOpenBracket || n->tokenType == ttHandle) )
  3021. {
  3022. if( n->tokenType == ttOpenBracket )
  3023. {
  3024. // Make sure the sub type can be instanciated
  3025. if( !dt.CanBeInstanciated() )
  3026. {
  3027. int r, c;
  3028. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  3029. asCString str;
  3030. // TODO: Change to "Array sub type cannot be 'type'"
  3031. str.Format(TXT_DATA_TYPE_CANT_BE_s, dt.Format().AddressOf());
  3032. WriteError(file->name.AddressOf(), str.AddressOf(), r, c);
  3033. }
  3034. // Make the type an array (or multidimensional array)
  3035. if( dt.MakeArray(engine) < 0 )
  3036. {
  3037. int r, c;
  3038. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  3039. WriteError(file->name.AddressOf(), TXT_NO_DEFAULT_ARRAY_TYPE, r, c);
  3040. break;
  3041. }
  3042. }
  3043. else
  3044. {
  3045. // Make the type a handle
  3046. if( dt.MakeHandle(true, acceptHandleForScope) < 0 )
  3047. {
  3048. int r, c;
  3049. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  3050. WriteError(file->name.AddressOf(), TXT_OBJECT_HANDLE_NOT_SUPPORTED, r, c);
  3051. break;
  3052. }
  3053. }
  3054. n = n->next;
  3055. }
  3056. if( isImplicitHandle )
  3057. {
  3058. // Make the type a handle
  3059. if( dt.MakeHandle(true, acceptHandleForScope) < 0 )
  3060. {
  3061. int r, c;
  3062. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  3063. WriteError(file->name.AddressOf(), TXT_OBJECT_HANDLE_NOT_SUPPORTED, r, c);
  3064. }
  3065. }
  3066. return dt;
  3067. }
  3068. asCDataType asCBuilder::ModifyDataTypeFromNode(const asCDataType &type, asCScriptNode *node, asCScriptCode *file, asETypeModifiers *inOutFlags, bool *autoHandle)
  3069. {
  3070. asCDataType dt = type;
  3071. if( inOutFlags ) *inOutFlags = asTM_NONE;
  3072. // Is the argument sent by reference?
  3073. asCScriptNode *n = node->firstChild;
  3074. if( n && n->tokenType == ttAmp )
  3075. {
  3076. dt.MakeReference(true);
  3077. n = n->next;
  3078. if( n )
  3079. {
  3080. if( inOutFlags )
  3081. {
  3082. if( n->tokenType == ttIn )
  3083. *inOutFlags = asTM_INREF;
  3084. else if( n->tokenType == ttOut )
  3085. *inOutFlags = asTM_OUTREF;
  3086. else if( n->tokenType == ttInOut )
  3087. *inOutFlags = asTM_INOUTREF;
  3088. else
  3089. asASSERT(false);
  3090. }
  3091. n = n->next;
  3092. }
  3093. else
  3094. {
  3095. if( inOutFlags )
  3096. *inOutFlags = asTM_INOUTREF; // ttInOut
  3097. }
  3098. if( !engine->ep.allowUnsafeReferences &&
  3099. inOutFlags && *inOutFlags == asTM_INOUTREF )
  3100. {
  3101. // Verify that the base type support &inout parameter types
  3102. if( !dt.IsObject() || dt.IsObjectHandle() || !dt.GetObjectType()->beh.addref || !dt.GetObjectType()->beh.release )
  3103. {
  3104. int r, c;
  3105. file->ConvertPosToRowCol(node->firstChild->tokenPos, &r, &c);
  3106. WriteError(file->name.AddressOf(), TXT_ONLY_OBJECTS_MAY_USE_REF_INOUT, r, c);
  3107. }
  3108. }
  3109. }
  3110. if( autoHandle ) *autoHandle = false;
  3111. if( n && n->tokenType == ttPlus )
  3112. {
  3113. if( autoHandle ) *autoHandle = true;
  3114. }
  3115. return dt;
  3116. }
  3117. asCObjectType *asCBuilder::GetObjectType(const char *type)
  3118. {
  3119. asCObjectType *ot = engine->GetObjectType(type);
  3120. if( !ot && module )
  3121. ot = module->GetObjectType(type);
  3122. return ot;
  3123. }
  3124. asCScriptFunction *asCBuilder::GetFuncDef(const char *type)
  3125. {
  3126. for( asUINT n = 0; n < engine->registeredFuncDefs.GetLength(); n++ )
  3127. {
  3128. // TODO: access: Only return the definitions that the module has access to
  3129. if( engine->registeredFuncDefs[n]->name == type )
  3130. {
  3131. return engine->registeredFuncDefs[n];
  3132. }
  3133. }
  3134. if( module )
  3135. {
  3136. for( asUINT n = 0; n < module->funcDefs.GetLength(); n++ )
  3137. {
  3138. if( module->funcDefs[n]->name == type )
  3139. {
  3140. return module->funcDefs[n];
  3141. }
  3142. }
  3143. }
  3144. return 0;
  3145. }
  3146. int asCBuilder::GetEnumValueFromObjectType(asCObjectType *objType, const char *name, asCDataType &outDt, asDWORD &outValue)
  3147. {
  3148. if( !objType || !(objType->flags & asOBJ_ENUM) )
  3149. return 0;
  3150. for( asUINT n = 0; n < objType->enumValues.GetLength(); ++n )
  3151. {
  3152. if( objType->enumValues[n]->name == name )
  3153. {
  3154. outDt = asCDataType::CreateObject(objType, true);
  3155. outValue = objType->enumValues[n]->value;
  3156. return 1;
  3157. }
  3158. }
  3159. return 0;
  3160. }
  3161. int asCBuilder::GetEnumValue(const char *name, asCDataType &outDt, asDWORD &outValue)
  3162. {
  3163. bool found = false;
  3164. // Search all available enum types
  3165. asUINT t;
  3166. for( t = 0; t < engine->objectTypes.GetLength(); t++ )
  3167. {
  3168. asCObjectType *ot = engine->objectTypes[t];
  3169. if( GetEnumValueFromObjectType( ot, name, outDt, outValue ) )
  3170. {
  3171. if( !found )
  3172. {
  3173. found = true;
  3174. }
  3175. else
  3176. {
  3177. // Found more than one value in different enum types
  3178. return 2;
  3179. }
  3180. }
  3181. }
  3182. for( t = 0; t < module->enumTypes.GetLength(); t++ )
  3183. {
  3184. asCObjectType *ot = module->enumTypes[t];
  3185. if( GetEnumValueFromObjectType( ot, name, outDt, outValue ) )
  3186. {
  3187. if( !found )
  3188. {
  3189. found = true;
  3190. }
  3191. else
  3192. {
  3193. // Found more than one value in different enum types
  3194. return 2;
  3195. }
  3196. }
  3197. }
  3198. if( found )
  3199. return 1;
  3200. // Didn't find any value
  3201. return 0;
  3202. }
  3203. END_AS_NAMESPACE