as_builder.cpp 92 KB

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