as_builder.cpp 118 KB

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