as_builder.cpp 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 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 Oorni 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. #include "as_debug.h"
  39. BEGIN_AS_NAMESPACE
  40. #ifndef AS_NO_COMPILER
  41. // asCSymbolTable template specializations for sGlobalVariableDescription entries
  42. template<>
  43. void asCSymbolTable<sGlobalVariableDescription>::GetKey(const sGlobalVariableDescription *entry, asSNameSpaceNamePair &key) const
  44. {
  45. asSNameSpace *ns = entry->ns;
  46. asCString name = entry->name;
  47. key = asSNameSpaceNamePair(ns, name);
  48. }
  49. // Comparator for exact variable search
  50. class asCCompGlobVarType : public asIFilter
  51. {
  52. public:
  53. const asCDataType &m_type;
  54. asCCompGlobVarType(const asCDataType &type) : m_type(type) {}
  55. bool operator()(const void *p) const
  56. {
  57. const sGlobalVariableDescription* desc = reinterpret_cast<const sGlobalVariableDescription*>(p);
  58. return desc->datatype == m_type;
  59. }
  60. private:
  61. // The assignment operator is required for MSVC9, otherwise it will complain that it is not possible to auto generate the operator
  62. asCCompGlobVarType &operator=(const asCCompGlobVarType &) {return *this;}
  63. };
  64. #endif
  65. asCBuilder::asCBuilder(asCScriptEngine *_engine, asCModule *_module)
  66. {
  67. this->engine = _engine;
  68. this->module = _module;
  69. silent = false;
  70. }
  71. asCBuilder::~asCBuilder()
  72. {
  73. #ifndef AS_NO_COMPILER
  74. asUINT n;
  75. // Free all functions
  76. for( n = 0; n < functions.GetLength(); n++ )
  77. {
  78. if( functions[n] )
  79. {
  80. if( functions[n]->node )
  81. {
  82. functions[n]->node->Destroy(engine);
  83. }
  84. asDELETE(functions[n],sFunctionDescription);
  85. }
  86. functions[n] = 0;
  87. }
  88. // Free all global variables
  89. asCSymbolTable<sGlobalVariableDescription>::iterator it = globVariables.List();
  90. while( it )
  91. {
  92. if( (*it)->declaredAtNode )
  93. (*it)->declaredAtNode->Destroy(engine);
  94. if( (*it)->initializationNode )
  95. (*it)->initializationNode->Destroy(engine);
  96. asDELETE((*it),sGlobalVariableDescription);
  97. it++;
  98. }
  99. globVariables.Clear();
  100. // Free all the loaded files
  101. for( n = 0; n < scripts.GetLength(); n++ )
  102. {
  103. if( scripts[n] )
  104. {
  105. asDELETE(scripts[n],asCScriptCode);
  106. }
  107. scripts[n] = 0;
  108. }
  109. // Free all class declarations
  110. for( n = 0; n < classDeclarations.GetLength(); n++ )
  111. {
  112. if( classDeclarations[n] )
  113. {
  114. if( classDeclarations[n]->node )
  115. {
  116. classDeclarations[n]->node->Destroy(engine);
  117. }
  118. asDELETE(classDeclarations[n],sClassDeclaration);
  119. classDeclarations[n] = 0;
  120. }
  121. }
  122. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  123. {
  124. if( interfaceDeclarations[n] )
  125. {
  126. if( interfaceDeclarations[n]->node )
  127. {
  128. interfaceDeclarations[n]->node->Destroy(engine);
  129. }
  130. asDELETE(interfaceDeclarations[n],sClassDeclaration);
  131. interfaceDeclarations[n] = 0;
  132. }
  133. }
  134. for( n = 0; n < namedTypeDeclarations.GetLength(); n++ )
  135. {
  136. if( namedTypeDeclarations[n] )
  137. {
  138. if( namedTypeDeclarations[n]->node )
  139. {
  140. namedTypeDeclarations[n]->node->Destroy(engine);
  141. }
  142. asDELETE(namedTypeDeclarations[n],sClassDeclaration);
  143. namedTypeDeclarations[n] = 0;
  144. }
  145. }
  146. for( n = 0; n < funcDefs.GetLength(); n++ )
  147. {
  148. if( funcDefs[n] )
  149. {
  150. if( funcDefs[n]->node )
  151. funcDefs[n]->node->Destroy(engine);
  152. asDELETE(funcDefs[n],sFuncDef);
  153. funcDefs[n] = 0;
  154. }
  155. }
  156. for( n = 0; n < mixinClasses.GetLength(); n++ )
  157. {
  158. if( mixinClasses[n] )
  159. {
  160. if( mixinClasses[n]->node )
  161. mixinClasses[n]->node->Destroy(engine);
  162. asDELETE(mixinClasses[n],sMixinClass);
  163. mixinClasses[n] = 0;
  164. }
  165. }
  166. #endif // AS_NO_COMPILER
  167. }
  168. void asCBuilder::Reset()
  169. {
  170. numErrors = 0;
  171. numWarnings = 0;
  172. engine->preMessage.isSet = false;
  173. #ifndef AS_NO_COMPILER
  174. // Clear the cache of known types
  175. hasCachedKnownTypes = false;
  176. knownTypes.EraseAll();
  177. #endif
  178. }
  179. #ifndef AS_NO_COMPILER
  180. int asCBuilder::AddCode(const char *name, const char *code, int codeLength, int lineOffset, int sectionIdx, bool makeCopy)
  181. {
  182. asCScriptCode *script = asNEW(asCScriptCode);
  183. if( script == 0 )
  184. return asOUT_OF_MEMORY;
  185. int r = script->SetCode(name, code, codeLength, makeCopy);
  186. if( r < 0 )
  187. {
  188. asDELETE(script, asCScriptCode);
  189. return r;
  190. }
  191. script->lineOffset = lineOffset;
  192. script->idx = sectionIdx;
  193. scripts.PushLast(script);
  194. return 0;
  195. }
  196. int asCBuilder::Build()
  197. {
  198. Reset();
  199. ParseScripts();
  200. // Compile the global variables first, so the auto types can be
  201. // resolved before the variables is used else where in the code
  202. CompileGlobalVariables();
  203. CompileInterfaces();
  204. CompileClasses();
  205. CompileFunctions();
  206. // TODO: Attempt to reorder the initialization of global variables so that
  207. // they do not access other uninitialized global variables out-of-order
  208. // The builder needs to check for each of the global variable, what functions
  209. // that are accessed, and what global variables are access by these functions.
  210. if( numWarnings > 0 && engine->ep.compilerWarnings == 2 )
  211. WriteError(TXT_WARNINGS_TREATED_AS_ERROR, 0, 0);
  212. if( numErrors > 0 )
  213. return asERROR;
  214. // Make sure something was compiled, otherwise return an error
  215. if( module->IsEmpty() )
  216. {
  217. WriteError(TXT_NOTHING_WAS_BUILT, 0, 0);
  218. return asERROR;
  219. }
  220. return asSUCCESS;
  221. }
  222. int asCBuilder::CompileGlobalVar(const char *sectionName, const char *code, int lineOffset)
  223. {
  224. Reset();
  225. // Add the string to the script code
  226. asCScriptCode *script = asNEW(asCScriptCode);
  227. if( script == 0 )
  228. return asOUT_OF_MEMORY;
  229. script->SetCode(sectionName, code, true);
  230. script->lineOffset = lineOffset;
  231. script->idx = engine->GetScriptSectionNameIndex(sectionName ? sectionName : "");
  232. scripts.PushLast(script);
  233. // Parse the string
  234. asCParser parser(this);
  235. if( parser.ParseScript(scripts[0]) < 0 )
  236. return asERROR;
  237. asCScriptNode *node = parser.GetScriptNode();
  238. // Make sure there is nothing else than the global variable in the script code
  239. if( node == 0 ||
  240. node->firstChild == 0 ||
  241. node->firstChild != node->lastChild ||
  242. node->firstChild->nodeType != snDeclaration )
  243. {
  244. WriteError(TXT_ONLY_ONE_VARIABLE_ALLOWED, script, 0);
  245. return asERROR;
  246. }
  247. node = node->firstChild;
  248. node->DisconnectParent();
  249. RegisterGlobalVar(node, script, module->defaultNamespace);
  250. CompileGlobalVariables();
  251. if( numWarnings > 0 && engine->ep.compilerWarnings == 2 )
  252. WriteError(TXT_WARNINGS_TREATED_AS_ERROR, 0, 0);
  253. if( numErrors > 0 )
  254. {
  255. // Remove the variable from the module, if it was registered
  256. if( globVariables.GetSize() > 0 )
  257. module->RemoveGlobalVar(module->GetGlobalVarCount()-1);
  258. return asERROR;
  259. }
  260. return 0;
  261. }
  262. #endif
  263. int asCBuilder::ValidateDefaultArgs(asCScriptCode *script, asCScriptNode *node, asCScriptFunction *func)
  264. {
  265. int firstArgWithDefaultValue = -1;
  266. for( asUINT n = 0; n < func->defaultArgs.GetLength(); n++ )
  267. {
  268. if( func->defaultArgs[n] )
  269. firstArgWithDefaultValue = n;
  270. else if( firstArgWithDefaultValue >= 0 )
  271. {
  272. asCString str;
  273. str.Format(TXT_DEF_ARG_MISSING_IN_FUNC_s, func->GetDeclaration());
  274. WriteError(str, script, node);
  275. return asINVALID_DECLARATION;
  276. }
  277. }
  278. return 0;
  279. }
  280. #ifndef AS_NO_COMPILER
  281. int asCBuilder::CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asCScriptFunction **outFunc)
  282. {
  283. asASSERT(outFunc != 0);
  284. Reset();
  285. // Add the string to the script code
  286. asCScriptCode *script = asNEW(asCScriptCode);
  287. if( script == 0 )
  288. return asOUT_OF_MEMORY;
  289. script->SetCode(sectionName, code, true);
  290. script->lineOffset = lineOffset;
  291. script->idx = engine->GetScriptSectionNameIndex(sectionName ? sectionName : "");
  292. scripts.PushLast(script);
  293. // Parse the string
  294. asCParser parser(this);
  295. if( parser.ParseScript(scripts[0]) < 0 )
  296. return asERROR;
  297. asCScriptNode *node = parser.GetScriptNode();
  298. // Make sure there is nothing else than the function in the script code
  299. if( node == 0 ||
  300. node->firstChild == 0 ||
  301. node->firstChild != node->lastChild ||
  302. node->firstChild->nodeType != snFunction )
  303. {
  304. WriteError(TXT_ONLY_ONE_FUNCTION_ALLOWED, script, 0);
  305. return asERROR;
  306. }
  307. // Find the function node
  308. node = node->firstChild;
  309. // Create the function
  310. bool isConstructor, isDestructor, isPrivate, isFinal, isOverride, isShared;
  311. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, compileFlags & asCOMP_ADD_TO_MODULE ? module : 0, asFUNC_SCRIPT);
  312. if( func == 0 )
  313. return asOUT_OF_MEMORY;
  314. GetParsedFunctionDetails(node, scripts[0], 0, func->name, func->returnType, func->parameterNames, func->parameterTypes, func->inOutFlags, func->defaultArgs, func->isReadOnly, isConstructor, isDestructor, isPrivate, isFinal, isOverride, isShared, module->defaultNamespace);
  315. func->id = engine->GetNextScriptFunctionId();
  316. func->scriptData->scriptSectionIdx = engine->GetScriptSectionNameIndex(sectionName ? sectionName : "");
  317. int row, col;
  318. scripts[0]->ConvertPosToRowCol(node->tokenPos, &row, &col);
  319. func->scriptData->declaredAt = (row & 0xFFFFF)|((col & 0xFFF)<<20);
  320. func->nameSpace = module->defaultNamespace;
  321. // Make sure the default args are declared correctly
  322. int r = ValidateDefaultArgs(script, node, func);
  323. if( r < 0 )
  324. {
  325. func->Release();
  326. return asERROR;
  327. }
  328. // Tell the engine that the function exists already so the compiler can access it
  329. if( compileFlags & asCOMP_ADD_TO_MODULE )
  330. {
  331. int r = CheckNameConflict(func->name.AddressOf(), node, scripts[0], module->defaultNamespace);
  332. if( r < 0 )
  333. {
  334. func->Orphan(module);
  335. return asERROR;
  336. }
  337. module->globalFunctions.Put(func);
  338. func->AddRef();
  339. module->AddScriptFunction(func);
  340. }
  341. else
  342. engine->SetScriptFunction(func);
  343. // Fill in the function info for the builder too
  344. node->DisconnectParent();
  345. sFunctionDescription *funcDesc = asNEW(sFunctionDescription);
  346. if( funcDesc == 0 )
  347. {
  348. func->Release();
  349. return asOUT_OF_MEMORY;
  350. }
  351. functions.PushLast(funcDesc);
  352. funcDesc->script = scripts[0];
  353. funcDesc->node = node;
  354. funcDesc->name = func->name;
  355. funcDesc->funcId = func->id;
  356. funcDesc->paramNames = func->parameterNames;
  357. funcDesc->isExistingShared = false;
  358. asCCompiler compiler(engine);
  359. compiler.CompileFunction(this, functions[0]->script, func->parameterNames, functions[0]->node, func, 0);
  360. if( numWarnings > 0 && engine->ep.compilerWarnings == 2 )
  361. WriteError(TXT_WARNINGS_TREATED_AS_ERROR, 0, 0);
  362. if( numErrors > 0 )
  363. {
  364. // If the function was added to the module then remove it again
  365. if( compileFlags & asCOMP_ADD_TO_MODULE )
  366. {
  367. module->globalFunctions.Erase(module->globalFunctions.GetIndex(func));
  368. module->scriptFunctions.RemoveValue(func);
  369. func->Release();
  370. func->Orphan(module);
  371. }
  372. func->Release();
  373. return asERROR;
  374. }
  375. // Return the function
  376. *outFunc = func;
  377. return asSUCCESS;
  378. }
  379. void asCBuilder::ParseScripts()
  380. {
  381. TimeIt("asCBuilder::ParseScripts");
  382. asCArray<asCParser*> parsers((int)scripts.GetLength());
  383. // Parse all the files as if they were one
  384. asUINT n = 0;
  385. for( n = 0; n < scripts.GetLength(); n++ )
  386. {
  387. asCParser *parser = asNEW(asCParser)(this);
  388. if( parser != 0 )
  389. {
  390. parsers.PushLast(parser);
  391. // Parse the script file
  392. parser->ParseScript(scripts[n]);
  393. }
  394. }
  395. if( numErrors == 0 )
  396. {
  397. // Find all type declarations
  398. for( n = 0; n < scripts.GetLength(); n++ )
  399. {
  400. asCScriptNode *node = parsers[n]->GetScriptNode();
  401. RegisterTypesFromScript(node, scripts[n], engine->nameSpaces[0]);
  402. }
  403. // Register the complete function definitions
  404. for( n = 0; n < funcDefs.GetLength(); n++ )
  405. {
  406. CompleteFuncDef(funcDefs[n]);
  407. }
  408. // Register script methods found in the interfaces
  409. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  410. {
  411. sClassDeclaration *decl = interfaceDeclarations[n];
  412. asCScriptNode *node = decl->node->firstChild->next;
  413. // Skip list of inherited interfaces
  414. while( node && node->nodeType == snIdentifier )
  415. node = node->next;
  416. while( node )
  417. {
  418. asCScriptNode *next = node->next;
  419. if( node->nodeType == snFunction )
  420. {
  421. node->DisconnectParent();
  422. RegisterScriptFunctionFromNode(node, decl->script, decl->objType, true, false, 0, decl->isExistingShared);
  423. }
  424. else if( node->nodeType == snVirtualProperty )
  425. {
  426. node->DisconnectParent();
  427. RegisterVirtualProperty(node, decl->script, decl->objType, true, false, 0, decl->isExistingShared);
  428. }
  429. node = next;
  430. }
  431. }
  432. // Register script methods found in the classes
  433. for( n = 0; n < classDeclarations.GetLength(); n++ )
  434. {
  435. sClassDeclaration *decl = classDeclarations[n];
  436. asCScriptNode *node = decl->node->firstChild->next;
  437. // Skip list of classes and interfaces
  438. while( node && node->nodeType == snIdentifier )
  439. node = node->next;
  440. while( node )
  441. {
  442. asCScriptNode *next = node->next;
  443. if( node->nodeType == snFunction )
  444. {
  445. node->DisconnectParent();
  446. RegisterScriptFunctionFromNode(node, decl->script, decl->objType, false, false, 0, decl->isExistingShared);
  447. }
  448. else if( node->nodeType == snVirtualProperty )
  449. {
  450. node->DisconnectParent();
  451. RegisterVirtualProperty(node, decl->script, decl->objType, false, false, 0, decl->isExistingShared);
  452. }
  453. node = next;
  454. }
  455. // Make sure the default factory & constructor exists for classes
  456. if( decl->objType->beh.construct == engine->scriptTypeBehaviours.beh.construct )
  457. {
  458. if( decl->objType->beh.constructors.GetLength() == 1 || engine->ep.alwaysImplDefaultConstruct )
  459. {
  460. AddDefaultConstructor(decl->objType, decl->script);
  461. }
  462. else
  463. {
  464. // As the class has another constructor we shouldn't provide the default constructor
  465. if( decl->objType->beh.construct )
  466. {
  467. engine->scriptFunctions[decl->objType->beh.construct]->Release();
  468. decl->objType->beh.construct = 0;
  469. decl->objType->beh.constructors.RemoveIndex(0);
  470. }
  471. if( decl->objType->beh.factory )
  472. {
  473. engine->scriptFunctions[decl->objType->beh.factory]->Release();
  474. decl->objType->beh.factory = 0;
  475. decl->objType->beh.factories.RemoveIndex(0);
  476. }
  477. // Only remove the opAssign method if the script hasn't provided one
  478. if( decl->objType->beh.copy == engine->scriptTypeBehaviours.beh.copy )
  479. {
  480. engine->scriptFunctions[decl->objType->beh.copy]->Release();
  481. decl->objType->beh.copy = 0;
  482. }
  483. }
  484. }
  485. }
  486. // Find other global nodes
  487. for( n = 0; n < scripts.GetLength(); n++ )
  488. {
  489. // Find other global nodes
  490. asCScriptNode *node = parsers[n]->GetScriptNode();
  491. RegisterNonTypesFromScript(node, scripts[n], engine->nameSpaces[0]);
  492. }
  493. }
  494. for( n = 0; n < parsers.GetLength(); n++ )
  495. {
  496. asDELETE(parsers[n],asCParser);
  497. }
  498. }
  499. void asCBuilder::RegisterTypesFromScript(asCScriptNode *node, asCScriptCode *script, asSNameSpace *ns)
  500. {
  501. asASSERT(node->nodeType == snScript);
  502. // Find structure definitions first
  503. node = node->firstChild;
  504. while( node )
  505. {
  506. asCScriptNode *next = node->next;
  507. if( node->nodeType == snNamespace )
  508. {
  509. // Recursively register the entities defined in the namespace
  510. asCString nsName;
  511. nsName.Assign(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  512. if( ns->name != "" )
  513. nsName = ns->name + "::" + nsName;
  514. asSNameSpace *nsChild = engine->AddNameSpace(nsName.AddressOf());
  515. RegisterTypesFromScript(node->lastChild, script, nsChild);
  516. }
  517. else
  518. {
  519. if( node->nodeType == snClass )
  520. {
  521. node->DisconnectParent();
  522. RegisterClass(node, script, ns);
  523. }
  524. else if( node->nodeType == snInterface )
  525. {
  526. node->DisconnectParent();
  527. RegisterInterface(node, script, ns);
  528. }
  529. else if( node->nodeType == snEnum )
  530. {
  531. node->DisconnectParent();
  532. RegisterEnum(node, script, ns);
  533. }
  534. else if( node->nodeType == snTypedef )
  535. {
  536. node->DisconnectParent();
  537. RegisterTypedef(node, script, ns);
  538. }
  539. else if( node->nodeType == snFuncDef )
  540. {
  541. node->DisconnectParent();
  542. RegisterFuncDef(node, script, ns);
  543. }
  544. else if( node->nodeType == snMixin )
  545. {
  546. node->DisconnectParent();
  547. RegisterMixinClass(node, script, ns);
  548. }
  549. }
  550. node = next;
  551. }
  552. }
  553. void asCBuilder::RegisterNonTypesFromScript(asCScriptNode *node, asCScriptCode *script, asSNameSpace *ns)
  554. {
  555. node = node->firstChild;
  556. while( node )
  557. {
  558. asCScriptNode *next = node->next;
  559. if( node->nodeType == snNamespace )
  560. {
  561. // Determine the name of the namespace
  562. asCString nsName;
  563. nsName.Assign(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  564. if( ns->name != "" )
  565. nsName = ns->name + "::" + nsName;
  566. // Declare the namespace, then add the entities
  567. asSNameSpace *nsChild = engine->AddNameSpace(nsName.AddressOf());
  568. RegisterNonTypesFromScript(node->lastChild, script, nsChild);
  569. }
  570. else
  571. {
  572. node->DisconnectParent();
  573. if( node->nodeType == snFunction )
  574. RegisterScriptFunctionFromNode(node, script, 0, false, true, ns);
  575. else if( node->nodeType == snDeclaration )
  576. RegisterGlobalVar(node, script, ns);
  577. else if( node->nodeType == snVirtualProperty )
  578. RegisterVirtualProperty(node, script, 0, false, true, ns);
  579. else if( node->nodeType == snImport )
  580. RegisterImportedFunction(module->GetNextImportedFunctionId(), node, script, ns);
  581. else
  582. {
  583. // Unused script node
  584. int r, c;
  585. script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  586. WriteWarning(script->name, TXT_UNUSED_SCRIPT_NODE, r, c);
  587. node->Destroy(engine);
  588. }
  589. }
  590. node = next;
  591. }
  592. }
  593. void asCBuilder::CompileFunctions()
  594. {
  595. // Compile each function
  596. for( asUINT n = 0; n < functions.GetLength(); n++ )
  597. {
  598. sFunctionDescription *current = functions[n];
  599. if( current == 0 ) continue;
  600. // Don't compile the function again if it was an existing shared function
  601. if( current->isExistingShared ) continue;
  602. asCCompiler compiler(engine);
  603. asCScriptFunction *func = engine->scriptFunctions[current->funcId];
  604. // Find the class declaration for constructors
  605. sClassDeclaration *classDecl = 0;
  606. if( current->objType && current->name == current->objType->name )
  607. {
  608. for( asUINT n = 0; n < classDeclarations.GetLength(); n++ )
  609. {
  610. if( classDeclarations[n]->objType == current->objType )
  611. {
  612. classDecl = classDeclarations[n];
  613. break;
  614. }
  615. }
  616. asASSERT( classDecl );
  617. }
  618. if( current->node )
  619. {
  620. int r, c;
  621. current->script->ConvertPosToRowCol(current->node->tokenPos, &r, &c);
  622. asCString str = func->GetDeclarationStr();
  623. str.Format(TXT_COMPILING_s, str.AddressOf());
  624. WriteInfo(current->script->name, str, r, c, true);
  625. // When compiling a constructor need to pass the class declaration for member initializations
  626. compiler.CompileFunction(this, current->script, current->paramNames, current->node, func, classDecl);
  627. engine->preMessage.isSet = false;
  628. }
  629. else if( current->objType && current->name == current->objType->name )
  630. {
  631. asCScriptNode *node = classDecl->node;
  632. int r = 0, c = 0;
  633. if( node )
  634. current->script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  635. asCString str = func->GetDeclarationStr();
  636. str.Format(TXT_COMPILING_s, str.AddressOf());
  637. WriteInfo(current->script->name, str, r, c, true);
  638. // This is the default constructor that is generated
  639. // automatically if not implemented by the user.
  640. compiler.CompileDefaultConstructor(this, current->script, node, func, classDecl);
  641. engine->preMessage.isSet = false;
  642. }
  643. else
  644. {
  645. asASSERT( false );
  646. }
  647. }
  648. }
  649. #endif
  650. // Called from module and engine
  651. int asCBuilder::ParseDataType(const char *datatype, asCDataType *result, asSNameSpace *implicitNamespace, bool isReturnType)
  652. {
  653. Reset();
  654. asCScriptCode source;
  655. source.SetCode("", datatype, true);
  656. asCParser parser(this);
  657. int r = parser.ParseDataType(&source, isReturnType);
  658. if( r < 0 )
  659. return asINVALID_TYPE;
  660. // Get data type and property name
  661. asCScriptNode *dataType = parser.GetScriptNode()->firstChild;
  662. *result = CreateDataTypeFromNode(dataType, &source, implicitNamespace, true);
  663. if( isReturnType )
  664. *result = ModifyDataTypeFromNode(*result, dataType->next, &source, 0, 0);
  665. if( numErrors > 0 )
  666. return asINVALID_TYPE;
  667. return asSUCCESS;
  668. }
  669. int asCBuilder::ParseTemplateDecl(const char *decl, asCString *name, asCArray<asCString> &subtypeNames)
  670. {
  671. Reset();
  672. asCScriptCode source;
  673. source.SetCode("", decl, true);
  674. asCParser parser(this);
  675. int r = parser.ParseTemplateDecl(&source);
  676. if( r < 0 )
  677. return asINVALID_TYPE;
  678. // Get the template name and subtype names
  679. asCScriptNode *node = parser.GetScriptNode()->firstChild;
  680. name->Assign(&decl[node->tokenPos], node->tokenLength);
  681. while( (node = node->next) != 0 )
  682. {
  683. asCString subtypeName;
  684. subtypeName.Assign(&decl[node->tokenPos], node->tokenLength);
  685. subtypeNames.PushLast(subtypeName);
  686. }
  687. // TODO: template: check for name conflicts
  688. if( numErrors > 0 )
  689. return asINVALID_DECLARATION;
  690. return asSUCCESS;
  691. }
  692. int asCBuilder::VerifyProperty(asCDataType *dt, const char *decl, asCString &name, asCDataType &type, asSNameSpace *ns)
  693. {
  694. // Either datatype or namespace must be informed
  695. asASSERT( dt || ns );
  696. Reset();
  697. if( dt )
  698. {
  699. // Verify that the object type exist
  700. if( dt->GetObjectType() == 0 )
  701. return asINVALID_OBJECT;
  702. }
  703. // Check property declaration and type
  704. asCScriptCode source;
  705. source.SetCode(TXT_PROPERTY, decl, true);
  706. asCParser parser(this);
  707. int r = parser.ParsePropertyDeclaration(&source);
  708. if( r < 0 )
  709. return asINVALID_DECLARATION;
  710. // Get data type and property name
  711. asCScriptNode *dataType = parser.GetScriptNode()->firstChild;
  712. asCScriptNode *nameNode = dataType->next;
  713. // If an object property is registered, then use the
  714. // object's namespace, otherwise use the specified namespace
  715. type = CreateDataTypeFromNode(dataType, &source, dt ? dt->GetObjectType()->nameSpace : ns);
  716. name.Assign(&decl[nameNode->tokenPos], nameNode->tokenLength);
  717. // Validate that the type really can be a registered property
  718. // We cannot use CanBeInstanciated, as it is allowed to register
  719. // properties of type that cannot otherwise be instanciated
  720. if( type.GetFuncDef() && !type.IsObjectHandle() )
  721. {
  722. // Function definitions must always be handles
  723. return asINVALID_DECLARATION;
  724. }
  725. // Verify property name
  726. if( dt )
  727. {
  728. if( CheckNameConflictMember(dt->GetObjectType(), name.AddressOf(), nameNode, &source, true) < 0 )
  729. return asNAME_TAKEN;
  730. }
  731. else
  732. {
  733. if( CheckNameConflict(name.AddressOf(), nameNode, &source, ns) < 0 )
  734. return asNAME_TAKEN;
  735. }
  736. if( numErrors > 0 )
  737. return asINVALID_DECLARATION;
  738. return asSUCCESS;
  739. }
  740. #ifndef AS_NO_COMPILER
  741. asCObjectProperty *asCBuilder::GetObjectProperty(asCDataType &obj, const char *prop)
  742. {
  743. asASSERT(obj.GetObjectType() != 0);
  744. // TODO: optimize: Improve linear search
  745. asCArray<asCObjectProperty *> &props = obj.GetObjectType()->properties;
  746. for( asUINT n = 0; n < props.GetLength(); n++ )
  747. {
  748. if( props[n]->name == prop )
  749. {
  750. if( module->accessMask & props[n]->accessMask )
  751. return props[n];
  752. else
  753. return 0;
  754. }
  755. }
  756. return 0;
  757. }
  758. #endif
  759. bool asCBuilder::DoesGlobalPropertyExist(const char *prop, asSNameSpace *ns, asCGlobalProperty **outProp, sGlobalVariableDescription **outDesc, bool *isAppProp)
  760. {
  761. if( outProp ) *outProp = 0;
  762. if( outDesc ) *outDesc = 0;
  763. if( isAppProp ) *isAppProp = false;
  764. // Check application registered properties
  765. asCString name(prop);
  766. asCGlobalProperty *globProp = engine->registeredGlobalProps.GetFirst(ns, name);
  767. if( globProp )
  768. {
  769. if( isAppProp ) *isAppProp = true;
  770. if( outProp ) *outProp = globProp;
  771. return true;
  772. }
  773. #ifndef AS_NO_COMPILER
  774. // Check properties being compiled now
  775. sGlobalVariableDescription* desc = globVariables.GetFirst(ns, prop);
  776. if( desc && !desc->isEnumValue )
  777. {
  778. if( outProp ) *outProp = desc->property;
  779. if( outDesc ) *outDesc = desc;
  780. return true;
  781. }
  782. #endif
  783. // Check previously compiled global variables
  784. if( module )
  785. {
  786. globProp = module->scriptGlobals.GetFirst(ns, prop);
  787. if( globProp )
  788. {
  789. if( outProp ) *outProp = globProp;
  790. return true;
  791. }
  792. }
  793. return false;
  794. }
  795. asCGlobalProperty *asCBuilder::GetGlobalProperty(const char *prop, asSNameSpace *ns, bool *isCompiled, bool *isPureConstant, asQWORD *constantValue, bool *isAppProp)
  796. {
  797. if( isCompiled ) *isCompiled = true;
  798. if( isPureConstant ) *isPureConstant = false;
  799. if( isAppProp ) *isAppProp = false;
  800. if( constantValue ) *constantValue = 0;
  801. asCGlobalProperty *globProp = 0;
  802. sGlobalVariableDescription *globDesc = 0;
  803. if( DoesGlobalPropertyExist(prop, ns, &globProp, &globDesc, isAppProp) )
  804. {
  805. #ifndef AS_NO_COMPILER
  806. if( globDesc )
  807. {
  808. // The property was declared in this build call, check if it has been compiled successfully already
  809. if( isCompiled ) *isCompiled = globDesc->isCompiled;
  810. if( isPureConstant ) *isPureConstant = globDesc->isPureConstant;
  811. if( constantValue ) *constantValue = globDesc->constantValue;
  812. }
  813. else
  814. #endif
  815. if( isAppProp )
  816. {
  817. // Don't return the property if the module doesn't have access to it
  818. if( !(module->accessMask & globProp->accessMask) )
  819. globProp = 0;
  820. }
  821. return globProp;
  822. }
  823. return 0;
  824. }
  825. int asCBuilder::ParseFunctionDeclaration(asCObjectType *objType, const char *decl, asCScriptFunction *func, bool isSystemFunction, asCArray<bool> *paramAutoHandles, bool *returnAutoHandle, asSNameSpace *ns, asCScriptNode **listPattern)
  826. {
  827. asASSERT( objType || ns );
  828. // TODO: Can't we use GetParsedFunctionDetails to do most of what is done in this function?
  829. Reset();
  830. asCScriptCode source;
  831. source.SetCode(TXT_SYSTEM_FUNCTION, decl, true);
  832. asCParser parser(this);
  833. int r = parser.ParseFunctionDefinition(&source, listPattern != 0);
  834. if( r < 0 )
  835. return asINVALID_DECLARATION;
  836. asCScriptNode *node = parser.GetScriptNode();
  837. // Determine scope
  838. asCScriptNode *n = node->firstChild->next->next;
  839. asCString scope = GetScopeFromNode(n, &source, &n);
  840. func->nameSpace = engine->FindNameSpace(scope.AddressOf());
  841. if( func->nameSpace == 0 )
  842. return asINVALID_DECLARATION;
  843. // Find name
  844. func->name.Assign(&source.code[n->tokenPos], n->tokenLength);
  845. // Initialize a script function object for registration
  846. bool autoHandle;
  847. // Scoped reference types are allowed to use handle when returned from application functions
  848. func->returnType = CreateDataTypeFromNode(node->firstChild, &source, objType ? objType->nameSpace : ns, true, objType);
  849. func->returnType = ModifyDataTypeFromNode(func->returnType, node->firstChild->next, &source, 0, &autoHandle);
  850. if( autoHandle && (!func->returnType.IsObjectHandle() || func->returnType.IsReference()) )
  851. return asINVALID_DECLARATION;
  852. if( returnAutoHandle ) *returnAutoHandle = autoHandle;
  853. // Reference types cannot be returned by value from system functions
  854. if( isSystemFunction &&
  855. (func->returnType.GetObjectType() &&
  856. (func->returnType.GetObjectType()->flags & asOBJ_REF)) &&
  857. !(func->returnType.IsReference() ||
  858. func->returnType.IsObjectHandle()) )
  859. return asINVALID_DECLARATION;
  860. // Count number of parameters
  861. int paramCount = 0;
  862. asCScriptNode *paramList = n->next;
  863. n = paramList->firstChild;
  864. while( n )
  865. {
  866. paramCount++;
  867. n = n->next->next;
  868. if( n && n->nodeType == snIdentifier )
  869. n = n->next;
  870. if( n && n->nodeType == snExpression )
  871. n = n->next;
  872. }
  873. // Preallocate memory
  874. func->parameterTypes.Allocate(paramCount, false);
  875. func->parameterNames.SetLength(paramCount);
  876. func->inOutFlags.Allocate(paramCount, false);
  877. func->defaultArgs.Allocate(paramCount, false);
  878. if( paramAutoHandles ) paramAutoHandles->Allocate(paramCount, false);
  879. n = paramList->firstChild;
  880. asUINT index = 0;
  881. while( n )
  882. {
  883. asETypeModifiers inOutFlags;
  884. asCDataType type = CreateDataTypeFromNode(n, &source, objType ? objType->nameSpace : ns, false, objType);
  885. type = ModifyDataTypeFromNode(type, n->next, &source, &inOutFlags, &autoHandle);
  886. // Reference types cannot be passed by value to system functions
  887. if( isSystemFunction &&
  888. (type.GetObjectType() &&
  889. (type.GetObjectType()->flags & asOBJ_REF)) &&
  890. !(type.IsReference() ||
  891. type.IsObjectHandle()) )
  892. return asINVALID_DECLARATION;
  893. // Store the parameter type
  894. func->parameterTypes.PushLast(type);
  895. func->inOutFlags.PushLast(inOutFlags);
  896. // Don't permit void parameters
  897. if( type.GetTokenType() == ttVoid )
  898. return asINVALID_DECLARATION;
  899. if( autoHandle && (!type.IsObjectHandle() || type.IsReference()) )
  900. return asINVALID_DECLARATION;
  901. if( paramAutoHandles ) paramAutoHandles->PushLast(autoHandle);
  902. // Make sure that var type parameters are references
  903. if( type.GetTokenType() == ttQuestion &&
  904. !type.IsReference() )
  905. return asINVALID_DECLARATION;
  906. // Move to next parameter
  907. n = n->next->next;
  908. if( n && n->nodeType == snIdentifier ) {
  909. func->parameterNames[index] = asCString(&source.code[n->tokenPos], n->tokenLength);
  910. n = n->next;
  911. }
  912. ++index;
  913. if( n && n->nodeType == snExpression )
  914. {
  915. // Strip out white space and comments to better share the string
  916. asCString *defaultArgStr = asNEW(asCString);
  917. if( defaultArgStr )
  918. {
  919. *defaultArgStr = GetCleanExpressionString(n, &source);
  920. func->defaultArgs.PushLast(defaultArgStr);
  921. }
  922. n = n->next;
  923. }
  924. else
  925. func->defaultArgs.PushLast(0);
  926. }
  927. // Set the read-only flag if const is declared after parameter list
  928. n = paramList->next;
  929. if( n && n->nodeType == snUndefined && n->tokenType == ttConst )
  930. {
  931. if( objType == 0 )
  932. return asINVALID_DECLARATION;
  933. func->isReadOnly = true;
  934. n = n->next;
  935. }
  936. else
  937. func->isReadOnly = false;
  938. // If the caller expects a list pattern, check for the existence, else report an error if not
  939. if( listPattern )
  940. {
  941. if( n == 0 || n->nodeType != snListPattern )
  942. return asINVALID_DECLARATION;
  943. else
  944. {
  945. *listPattern = n;
  946. n->DisconnectParent();
  947. }
  948. }
  949. else
  950. {
  951. if( n )
  952. return asINVALID_DECLARATION;
  953. }
  954. // Make sure the default args are declared correctly
  955. ValidateDefaultArgs(&source, node, func);
  956. if( numErrors > 0 || numWarnings > 0 )
  957. return asINVALID_DECLARATION;
  958. return 0;
  959. }
  960. int asCBuilder::ParseVariableDeclaration(const char *decl, asSNameSpace *implicitNamespace, asCString &outName, asSNameSpace *&outNamespace, asCDataType &outDt)
  961. {
  962. Reset();
  963. asCScriptCode source;
  964. source.SetCode(TXT_VARIABLE_DECL, decl, true);
  965. asCParser parser(this);
  966. int r = parser.ParsePropertyDeclaration(&source);
  967. if( r < 0 )
  968. return asINVALID_DECLARATION;
  969. asCScriptNode *node = parser.GetScriptNode();
  970. // Determine the scope from declaration
  971. asCScriptNode *n = node->firstChild->next;
  972. outNamespace = GetNameSpaceFromNode(n, &source, implicitNamespace, &n);
  973. if( outNamespace == 0 )
  974. return asINVALID_DECLARATION;
  975. // Find name
  976. outName.Assign(&source.code[n->tokenPos], n->tokenLength);
  977. // Initialize a script variable object for registration
  978. outDt = CreateDataTypeFromNode(node->firstChild, &source, implicitNamespace);
  979. if( numErrors > 0 || numWarnings > 0 )
  980. return asINVALID_DECLARATION;
  981. return 0;
  982. }
  983. int asCBuilder::CheckNameConflictMember(asCObjectType *t, const char *name, asCScriptNode *node, asCScriptCode *code, bool isProperty)
  984. {
  985. // It's not necessary to check against object types
  986. // TODO: optimize: Improve linear search
  987. asCArray<asCObjectProperty *> &props = t->properties;
  988. for( asUINT n = 0; n < props.GetLength(); n++ )
  989. {
  990. if( props[n]->name == name )
  991. {
  992. if( code )
  993. {
  994. asCString str;
  995. str.Format(TXT_NAME_CONFLICT_s_OBJ_PROPERTY, name);
  996. WriteError(str, code, node);
  997. }
  998. return -1;
  999. }
  1000. }
  1001. // Property names must be checked against method names
  1002. if( isProperty )
  1003. {
  1004. asCArray<int> methods = t->methods;
  1005. for( asUINT n = 0; n < methods.GetLength(); n++ )
  1006. {
  1007. if( engine->scriptFunctions[methods[n]]->name == name )
  1008. {
  1009. if( code )
  1010. {
  1011. asCString str;
  1012. str.Format(TXT_NAME_CONFLICT_s_METHOD, name);
  1013. WriteError(str, code, node);
  1014. }
  1015. return -1;
  1016. }
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. int asCBuilder::CheckNameConflict(const char *name, asCScriptNode *node, asCScriptCode *code, asSNameSpace *ns)
  1022. {
  1023. // Check against registered object types
  1024. // TODO: Must check against registered funcdefs too
  1025. if( engine->GetRegisteredObjectType(name, ns) != 0 )
  1026. {
  1027. if( code )
  1028. {
  1029. asCString str;
  1030. str.Format(TXT_NAME_CONFLICT_s_EXTENDED_TYPE, name);
  1031. WriteError(str, code, node);
  1032. }
  1033. return -1;
  1034. }
  1035. // Check against global properties
  1036. if( DoesGlobalPropertyExist(name, ns) )
  1037. {
  1038. if( code )
  1039. {
  1040. asCString str;
  1041. str.Format(TXT_NAME_CONFLICT_s_GLOBAL_PROPERTY, name);
  1042. WriteError(str, code, node);
  1043. }
  1044. return -1;
  1045. }
  1046. // TODO: Property names must be checked against function names
  1047. #ifndef AS_NO_COMPILER
  1048. // Check against class types
  1049. asUINT n;
  1050. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1051. {
  1052. if( classDeclarations[n]->name == name &&
  1053. classDeclarations[n]->objType->nameSpace == ns )
  1054. {
  1055. if( code )
  1056. {
  1057. asCString str;
  1058. str.Format(TXT_NAME_CONFLICT_s_STRUCT, name);
  1059. WriteError(str, code, node);
  1060. }
  1061. return -1;
  1062. }
  1063. }
  1064. // Check against named types
  1065. for( n = 0; n < namedTypeDeclarations.GetLength(); n++ )
  1066. {
  1067. if( namedTypeDeclarations[n]->name == name &&
  1068. namedTypeDeclarations[n]->objType->nameSpace == ns )
  1069. {
  1070. if( code )
  1071. {
  1072. asCString str;
  1073. str.Format(TXT_NAME_CONFLICT_s_IS_NAMED_TYPE, name);
  1074. WriteError(str, code, node);
  1075. }
  1076. return -1;
  1077. }
  1078. }
  1079. // Must check for name conflicts with funcdefs
  1080. for( n = 0; n < funcDefs.GetLength(); n++ )
  1081. {
  1082. if( funcDefs[n]->name == name &&
  1083. module->funcDefs[funcDefs[n]->idx]->nameSpace == ns )
  1084. {
  1085. if( code )
  1086. {
  1087. asCString str;
  1088. str.Format(TXT_NAME_CONFLICT_s_IS_FUNCDEF, name);
  1089. WriteError(str, code, node);
  1090. }
  1091. return -1;
  1092. }
  1093. }
  1094. // Check against mixin classes
  1095. if( GetMixinClass(name, ns) )
  1096. {
  1097. if( code )
  1098. {
  1099. asCString str;
  1100. str.Format(TXT_NAME_CONFLICT_s_IS_MIXIN, name);
  1101. WriteError(str, code, node);
  1102. }
  1103. return -1;
  1104. }
  1105. #endif
  1106. return 0;
  1107. }
  1108. #ifndef AS_NO_COMPILER
  1109. sMixinClass *asCBuilder::GetMixinClass(const char *name, asSNameSpace *ns)
  1110. {
  1111. for( asUINT n = 0; n < mixinClasses.GetLength(); n++ )
  1112. if( mixinClasses[n]->name == name &&
  1113. mixinClasses[n]->ns == ns )
  1114. return mixinClasses[n];
  1115. return 0;
  1116. }
  1117. int asCBuilder::RegisterFuncDef(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  1118. {
  1119. // Find the name
  1120. asASSERT( node->firstChild->nodeType == snDataType );
  1121. asCScriptNode *n = node->firstChild->next->next;
  1122. asCString name;
  1123. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  1124. // Check for name conflict with other types
  1125. int r = CheckNameConflict(name.AddressOf(), node, file, ns);
  1126. if( asSUCCESS != r )
  1127. {
  1128. node->Destroy(engine);
  1129. return r;
  1130. }
  1131. // The function definition should be stored as a asCScriptFunction so that the application
  1132. // can use the asIScriptFunction interface to enumerate the return type and parameters
  1133. // The return type and parameter types aren't determined in this function. A second pass is
  1134. // necessary after all type declarations have been identified. The second pass is implemented
  1135. // in CompleteFuncDef().
  1136. sFuncDef *fd = asNEW(sFuncDef);
  1137. if( fd == 0 )
  1138. {
  1139. node->Destroy(engine);
  1140. return asOUT_OF_MEMORY;
  1141. }
  1142. fd->name = name;
  1143. fd->node = node;
  1144. fd->script = file;
  1145. fd->idx = module->AddFuncDef(name, ns);
  1146. funcDefs.PushLast(fd);
  1147. return 0;
  1148. }
  1149. void asCBuilder::CompleteFuncDef(sFuncDef *funcDef)
  1150. {
  1151. asCArray<asCString *> defaultArgs;
  1152. bool isConstMethod;
  1153. bool isConstructor;
  1154. bool isDestructor;
  1155. bool isPrivate;
  1156. bool isOverride;
  1157. bool isFinal;
  1158. bool isShared;
  1159. asCScriptFunction *func = module->funcDefs[funcDef->idx];
  1160. asASSERT( func );
  1161. GetParsedFunctionDetails(funcDef->node, funcDef->script, 0, funcDef->name, func->returnType, func->parameterNames, func->parameterTypes, func->inOutFlags, defaultArgs, isConstMethod, isConstructor, isDestructor, isPrivate, isOverride, isFinal, isShared, func->nameSpace);
  1162. // There should not be any defaultArgs, but if there are any we need to delete them to avoid leaks
  1163. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  1164. if( defaultArgs[n] )
  1165. asDELETE(defaultArgs[n], asCString);
  1166. // TODO: Should we force the use of 'shared' for this check to be done?
  1167. // Check if there is another identical funcdef from another module and if so reuse that instead
  1168. for( asUINT n = 0; n < engine->funcDefs.GetLength(); n++ )
  1169. {
  1170. asCScriptFunction *f2 = engine->funcDefs[n];
  1171. if( f2 == 0 || func == f2 )
  1172. continue;
  1173. if( f2->name == func->name &&
  1174. f2->nameSpace == func->nameSpace &&
  1175. f2->IsSignatureExceptNameEqual(func) )
  1176. {
  1177. // Replace our funcdef for the existing one
  1178. funcDef->idx = f2->id;
  1179. module->funcDefs[module->funcDefs.IndexOf(func)] = f2;
  1180. f2->AddRef();
  1181. engine->funcDefs.RemoveValue(func);
  1182. func->Release();
  1183. // funcdefs aren't destroyed when the refCount reaches zero so we need to manually delete them
  1184. asDELETE(func, asCScriptFunction);
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. int asCBuilder::RegisterGlobalVar(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  1190. {
  1191. // Has the application disabled global vars?
  1192. if( engine->ep.disallowGlobalVars )
  1193. WriteError(TXT_GLOBAL_VARS_NOT_ALLOWED, file, node);
  1194. // What data type is it?
  1195. asCDataType type = CreateDataTypeFromNode(node->firstChild, file, ns);
  1196. if( !type.CanBeInstanciated() )
  1197. {
  1198. asCString str;
  1199. // TODO: Change to "'type' cannot be declared as variable"
  1200. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format().AddressOf());
  1201. int r, c;
  1202. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1203. WriteError(str, file, node);
  1204. }
  1205. asCScriptNode *n = node->firstChild->next;
  1206. while( n )
  1207. {
  1208. // Verify that the name isn't taken
  1209. asCString name(&file->code[n->tokenPos], n->tokenLength);
  1210. CheckNameConflict(name.AddressOf(), n, file, ns);
  1211. // Register the global variable
  1212. sGlobalVariableDescription *gvar = asNEW(sGlobalVariableDescription);
  1213. if( gvar == 0 )
  1214. {
  1215. node->Destroy(engine);
  1216. return asOUT_OF_MEMORY;
  1217. }
  1218. gvar->script = file;
  1219. gvar->name = name;
  1220. gvar->isCompiled = false;
  1221. gvar->datatype = type;
  1222. gvar->isEnumValue = false;
  1223. gvar->ns = ns;
  1224. // TODO: Give error message if wrong
  1225. asASSERT(!gvar->datatype.IsReference());
  1226. // Allocation is done when the variable is compiled, to allow for autos
  1227. gvar->property = 0;
  1228. gvar->index = 0;
  1229. globVariables.Put(gvar);
  1230. gvar->declaredAtNode = n;
  1231. n = n->next;
  1232. gvar->declaredAtNode->DisconnectParent();
  1233. gvar->initializationNode = 0;
  1234. if( n &&
  1235. ( n->nodeType == snAssignment ||
  1236. n->nodeType == snArgList ||
  1237. n->nodeType == snInitList ) )
  1238. {
  1239. gvar->initializationNode = n;
  1240. n = n->next;
  1241. gvar->initializationNode->DisconnectParent();
  1242. }
  1243. }
  1244. node->Destroy(engine);
  1245. return 0;
  1246. }
  1247. int asCBuilder::RegisterMixinClass(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  1248. {
  1249. asCScriptNode *cl = node->firstChild;
  1250. asASSERT( cl->nodeType == snClass );
  1251. asCScriptNode *n = cl->firstChild;
  1252. // Skip potential 'final' and 'shared' tokens
  1253. while( n->tokenType == ttIdentifier &&
  1254. (file->TokenEquals(n->tokenPos, n->tokenLength, FINAL_TOKEN) ||
  1255. file->TokenEquals(n->tokenPos, n->tokenLength, SHARED_TOKEN)) )
  1256. {
  1257. // Report error, because mixin class cannot be final or shared
  1258. asCString msg;
  1259. msg.Format(TXT_MIXIN_CANNOT_BE_DECLARED_AS_s, asCString(&file->code[n->tokenPos], n->tokenLength).AddressOf());
  1260. WriteError(msg, file, n);
  1261. asCScriptNode *tmp = n;
  1262. n = n->next;
  1263. // Remove the invalid node, so compilation can continue as if it wasn't there
  1264. tmp->DisconnectParent();
  1265. tmp->Destroy(engine);
  1266. }
  1267. asCString name(&file->code[n->tokenPos], n->tokenLength);
  1268. int r, c;
  1269. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  1270. CheckNameConflict(name.AddressOf(), n, file, ns);
  1271. sMixinClass *decl = asNEW(sMixinClass);
  1272. if( decl == 0 )
  1273. {
  1274. node->Destroy(engine);
  1275. return asOUT_OF_MEMORY;
  1276. }
  1277. mixinClasses.PushLast(decl);
  1278. decl->name = name;
  1279. decl->ns = ns;
  1280. decl->node = cl;
  1281. decl->script = file;
  1282. // Clean up memory
  1283. cl->DisconnectParent();
  1284. node->Destroy(engine);
  1285. return 0;
  1286. }
  1287. int asCBuilder::RegisterClass(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  1288. {
  1289. asCScriptNode *n = node->firstChild;
  1290. bool isFinal = false;
  1291. bool isShared = false;
  1292. if( n->tokenType == ttIdentifier && file->TokenEquals(n->tokenPos, n->tokenLength, FINAL_TOKEN) )
  1293. {
  1294. isFinal = true;
  1295. n = n->next;
  1296. }
  1297. if( n->tokenType == ttIdentifier && file->TokenEquals(n->tokenPos, n->tokenLength, SHARED_TOKEN) )
  1298. {
  1299. isShared = true;
  1300. n = n->next;
  1301. // Check for final again
  1302. if( n->tokenType == ttIdentifier && file->TokenEquals(n->tokenPos, n->tokenLength, FINAL_TOKEN) )
  1303. {
  1304. isFinal = true;
  1305. n = n->next;
  1306. }
  1307. }
  1308. asCString name(&file->code[n->tokenPos], n->tokenLength);
  1309. int r, c;
  1310. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  1311. CheckNameConflict(name.AddressOf(), n, file, ns);
  1312. sClassDeclaration *decl = asNEW(sClassDeclaration);
  1313. if( decl == 0 )
  1314. {
  1315. node->Destroy(engine);
  1316. return asOUT_OF_MEMORY;
  1317. }
  1318. classDeclarations.PushLast(decl);
  1319. decl->name = name;
  1320. decl->script = file;
  1321. decl->node = node;
  1322. // If this type is shared and there already exist another shared
  1323. // type of the same name, then that one should be used instead of
  1324. // creating a new one.
  1325. if( isShared )
  1326. {
  1327. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  1328. {
  1329. asCObjectType *st = engine->classTypes[n];
  1330. if( st &&
  1331. st->IsShared() &&
  1332. st->name == name &&
  1333. st->nameSpace == ns &&
  1334. !st->IsInterface() )
  1335. {
  1336. // We'll use the existing type
  1337. decl->isExistingShared = true;
  1338. decl->objType = st;
  1339. module->classTypes.PushLast(st);
  1340. st->AddRef();
  1341. return 0;
  1342. }
  1343. }
  1344. }
  1345. // Create a new object type for this class
  1346. asCObjectType *st = asNEW(asCObjectType)(engine);
  1347. if( st == 0 )
  1348. return asOUT_OF_MEMORY;
  1349. // By default all script classes are marked as garbage collected.
  1350. // Only after the complete structure and relationship between classes
  1351. // is known, can the flag be cleared for those objects that truly cannot
  1352. // form circular references. This is important because a template
  1353. // callback may be called with a script class before the compilation
  1354. // completes, and until it is known, the callback must assume the class
  1355. // is garbage collected.
  1356. st->flags = asOBJ_REF | asOBJ_SCRIPT_OBJECT | asOBJ_GC;
  1357. if( isShared )
  1358. st->flags |= asOBJ_SHARED;
  1359. if( isFinal )
  1360. st->flags |= asOBJ_NOINHERIT;
  1361. if( node->tokenType == ttHandle )
  1362. st->flags |= asOBJ_IMPLICIT_HANDLE;
  1363. st->size = sizeof(asCScriptObject);
  1364. st->name = name;
  1365. st->nameSpace = ns;
  1366. st->module = module;
  1367. module->classTypes.PushLast(st);
  1368. engine->classTypes.PushLast(st);
  1369. st->AddRef();
  1370. decl->objType = st;
  1371. // Use the default script class behaviours
  1372. st->beh = engine->scriptTypeBehaviours.beh;
  1373. // TODO: Move this to asCObjectType so that the asCRestore can reuse it
  1374. engine->scriptFunctions[st->beh.addref]->AddRef();
  1375. engine->scriptFunctions[st->beh.release]->AddRef();
  1376. engine->scriptFunctions[st->beh.gcEnumReferences]->AddRef();
  1377. engine->scriptFunctions[st->beh.gcGetFlag]->AddRef();
  1378. engine->scriptFunctions[st->beh.gcGetRefCount]->AddRef();
  1379. engine->scriptFunctions[st->beh.gcReleaseAllReferences]->AddRef();
  1380. engine->scriptFunctions[st->beh.gcSetFlag]->AddRef();
  1381. engine->scriptFunctions[st->beh.copy]->AddRef();
  1382. engine->scriptFunctions[st->beh.factory]->AddRef();
  1383. engine->scriptFunctions[st->beh.construct]->AddRef();
  1384. // TODO: weak: Should not do this if the class has been declared with noweak
  1385. engine->scriptFunctions[st->beh.getWeakRefFlag]->AddRef();
  1386. for( asUINT i = 1; i < st->beh.operators.GetLength(); i += 2 )
  1387. engine->scriptFunctions[st->beh.operators[i]]->AddRef();
  1388. return 0;
  1389. }
  1390. int asCBuilder::RegisterInterface(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  1391. {
  1392. asCScriptNode *n = node->firstChild;
  1393. asCString name(&file->code[n->tokenPos], n->tokenLength);
  1394. bool isShared = false;
  1395. if( name == SHARED_TOKEN )
  1396. {
  1397. isShared = true;
  1398. n = n->next;
  1399. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  1400. }
  1401. int r, c;
  1402. file->ConvertPosToRowCol(n->tokenPos, &r, &c);
  1403. CheckNameConflict(name.AddressOf(), n, file, ns);
  1404. sClassDeclaration *decl = asNEW(sClassDeclaration);
  1405. if( decl == 0 )
  1406. {
  1407. node->Destroy(engine);
  1408. return asOUT_OF_MEMORY;
  1409. }
  1410. interfaceDeclarations.PushLast(decl);
  1411. decl->name = name;
  1412. decl->script = file;
  1413. decl->node = node;
  1414. // If this type is shared and there already exist another shared
  1415. // type of the same name, then that one should be used instead of
  1416. // creating a new one.
  1417. if( isShared )
  1418. {
  1419. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  1420. {
  1421. asCObjectType *st = engine->classTypes[n];
  1422. if( st &&
  1423. st->IsShared() &&
  1424. st->name == name &&
  1425. st->nameSpace == ns &&
  1426. st->IsInterface() )
  1427. {
  1428. // We'll use the existing type
  1429. decl->isExistingShared = true;
  1430. decl->objType = st;
  1431. module->classTypes.PushLast(st);
  1432. st->AddRef();
  1433. return 0;
  1434. }
  1435. }
  1436. }
  1437. // Register the object type for the interface
  1438. asCObjectType *st = asNEW(asCObjectType)(engine);
  1439. if( st == 0 )
  1440. return asOUT_OF_MEMORY;
  1441. st->flags = asOBJ_REF | asOBJ_SCRIPT_OBJECT;
  1442. if( isShared )
  1443. st->flags |= asOBJ_SHARED;
  1444. st->size = 0; // Cannot be instanciated
  1445. st->name = name;
  1446. st->nameSpace = ns;
  1447. module->classTypes.PushLast(st);
  1448. engine->classTypes.PushLast(st);
  1449. st->AddRef();
  1450. decl->objType = st;
  1451. // Use the default script class behaviours
  1452. st->beh.construct = 0;
  1453. st->beh.addref = engine->scriptTypeBehaviours.beh.addref;
  1454. engine->scriptFunctions[st->beh.addref]->AddRef();
  1455. st->beh.release = engine->scriptTypeBehaviours.beh.release;
  1456. engine->scriptFunctions[st->beh.release]->AddRef();
  1457. st->beh.copy = 0;
  1458. return 0;
  1459. }
  1460. void asCBuilder::CompileGlobalVariables()
  1461. {
  1462. bool compileSucceeded = true;
  1463. // Store state of compilation (errors, warning, output)
  1464. int currNumErrors = numErrors;
  1465. int currNumWarnings = numWarnings;
  1466. // Backup the original message stream
  1467. bool msgCallback = engine->msgCallback;
  1468. asSSystemFunctionInterface msgCallbackFunc = engine->msgCallbackFunc;
  1469. void *msgCallbackObj = engine->msgCallbackObj;
  1470. // Set the new temporary message stream
  1471. asCOutputBuffer outBuffer;
  1472. engine->SetMessageCallback(asMETHOD(asCOutputBuffer, Callback), &outBuffer, asCALL_THISCALL);
  1473. asCOutputBuffer finalOutput;
  1474. asCScriptFunction *initFunc = 0;
  1475. asCSymbolTable<asCGlobalProperty> initOrder;
  1476. // We first try to compile all the primitive global variables, and only after that
  1477. // compile the non-primitive global variables. This permits the constructors
  1478. // for the complex types to use the already initialized variables of primitive
  1479. // type. Note, we currently don't know which global variables are used in the
  1480. // constructors, so we cannot guarantee that variables of complex types are
  1481. // initialized in the correct order, so we won't reorder those.
  1482. bool compilingPrimitives = true;
  1483. // Compile each global variable
  1484. while( compileSucceeded )
  1485. {
  1486. compileSucceeded = false;
  1487. int accumErrors = 0;
  1488. int accumWarnings = 0;
  1489. // Restore state of compilation
  1490. finalOutput.Clear();
  1491. asCSymbolTable<sGlobalVariableDescription>::iterator it = globVariables.List();
  1492. for( ; it; it++ )
  1493. {
  1494. sGlobalVariableDescription *gvar = *it;
  1495. if( gvar->isCompiled )
  1496. continue;
  1497. asCByteCode init(engine);
  1498. numWarnings = 0;
  1499. numErrors = 0;
  1500. outBuffer.Clear();
  1501. // Skip this for now if we're not compiling complex types yet
  1502. if( compilingPrimitives && !gvar->datatype.IsPrimitive() )
  1503. continue;
  1504. if( gvar->declaredAtNode )
  1505. {
  1506. int r, c;
  1507. gvar->script->ConvertPosToRowCol(gvar->declaredAtNode->tokenPos, &r, &c);
  1508. asCString str = gvar->datatype.Format();
  1509. str += " " + gvar->name;
  1510. str.Format(TXT_COMPILING_s, str.AddressOf());
  1511. WriteInfo(gvar->script->name, str, r, c, true);
  1512. }
  1513. if( gvar->isEnumValue )
  1514. {
  1515. int r;
  1516. if( gvar->initializationNode )
  1517. {
  1518. asCCompiler comp(engine);
  1519. asCScriptFunction func(engine, module, asFUNC_SCRIPT);
  1520. // Set the namespace that should be used during the compilation
  1521. func.nameSpace = gvar->datatype.GetObjectType()->nameSpace;
  1522. // Temporarily switch the type of the variable to int so it can be compiled properly
  1523. asCDataType saveType;
  1524. saveType = gvar->datatype;
  1525. gvar->datatype = asCDataType::CreatePrimitive(ttInt, true);
  1526. r = comp.CompileGlobalVariable(this, gvar->script, gvar->initializationNode, gvar, &func);
  1527. gvar->datatype = saveType;
  1528. // Make the function a dummy so it doesn't try to release objects while destroying the function
  1529. func.funcType = asFUNC_DUMMY;
  1530. }
  1531. else
  1532. {
  1533. r = 0;
  1534. // When there is no assignment the value is the last + 1
  1535. int enumVal = 0;
  1536. asCSymbolTable<sGlobalVariableDescription>::iterator prev_it = it;
  1537. prev_it--;
  1538. if( prev_it )
  1539. {
  1540. sGlobalVariableDescription *gvar2 = *prev_it;
  1541. if(gvar2->datatype == gvar->datatype )
  1542. {
  1543. // The integer value is stored in the lower bytes
  1544. enumVal = (*(int*)&gvar2->constantValue) + 1;
  1545. if( !gvar2->isCompiled )
  1546. {
  1547. int row, col;
  1548. gvar->script->ConvertPosToRowCol(gvar->declaredAtNode->tokenPos, &row, &col);
  1549. asCString str = gvar->datatype.Format();
  1550. str += " " + gvar->name;
  1551. str.Format(TXT_COMPILING_s, str.AddressOf());
  1552. WriteInfo(gvar->script->name, str, row, col, true);
  1553. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, gvar2->name.AddressOf());
  1554. WriteError(gvar->script->name, str, row, col);
  1555. r = -1;
  1556. }
  1557. }
  1558. }
  1559. // The integer value is stored in the lower bytes
  1560. *(int*)&gvar->constantValue = enumVal;
  1561. }
  1562. if( r >= 0 )
  1563. {
  1564. // Set the value as compiled
  1565. gvar->isCompiled = true;
  1566. compileSucceeded = true;
  1567. }
  1568. }
  1569. else
  1570. {
  1571. // Compile the global variable
  1572. initFunc = asNEW(asCScriptFunction)(engine, module, asFUNC_SCRIPT);
  1573. if( initFunc == 0 )
  1574. {
  1575. // Out of memory
  1576. return;
  1577. }
  1578. // Set the namespace that should be used for this function
  1579. initFunc->nameSpace = gvar->ns;
  1580. asCCompiler comp(engine);
  1581. int r = comp.CompileGlobalVariable(this, gvar->script, gvar->initializationNode, gvar, initFunc);
  1582. if( r >= 0 )
  1583. {
  1584. // Compilation succeeded
  1585. gvar->isCompiled = true;
  1586. compileSucceeded = true;
  1587. }
  1588. else
  1589. {
  1590. // Compilation failed
  1591. initFunc->funcType = asFUNC_DUMMY;
  1592. asDELETE(initFunc, asCScriptFunction);
  1593. initFunc = 0;
  1594. }
  1595. }
  1596. if( gvar->isCompiled )
  1597. {
  1598. // Add warnings for this constant to the total build
  1599. if( numWarnings )
  1600. {
  1601. currNumWarnings += numWarnings;
  1602. if( msgCallback )
  1603. outBuffer.SendToCallback(engine, &msgCallbackFunc, msgCallbackObj);
  1604. }
  1605. // Determine order of variable initializations
  1606. if( gvar->property && !gvar->isEnumValue )
  1607. initOrder.Put(gvar->property);
  1608. // Does the function contain more than just a SUSPEND followed by a RET instruction?
  1609. if( initFunc && initFunc->scriptData->byteCode.GetLength() > 2 )
  1610. {
  1611. // Create the init function for this variable
  1612. initFunc->id = engine->GetNextScriptFunctionId();
  1613. engine->SetScriptFunction(initFunc);
  1614. // Finalize the init function for this variable
  1615. initFunc->returnType = asCDataType::CreatePrimitive(ttVoid, false);
  1616. initFunc->scriptData->scriptSectionIdx = engine->GetScriptSectionNameIndex(gvar->script->name.AddressOf());
  1617. if( gvar->declaredAtNode )
  1618. {
  1619. int row, col;
  1620. gvar->script->ConvertPosToRowCol(gvar->declaredAtNode->tokenPos, &row, &col);
  1621. initFunc->scriptData->declaredAt = (row & 0xFFFFF)|((col & 0xFFF)<<20);
  1622. }
  1623. gvar->property->SetInitFunc(initFunc);
  1624. initFunc->Release();
  1625. initFunc = 0;
  1626. }
  1627. else if( initFunc )
  1628. {
  1629. // Destroy the function as it won't be used
  1630. initFunc->funcType = asFUNC_DUMMY;
  1631. asDELETE(initFunc, asCScriptFunction);
  1632. initFunc = 0;
  1633. }
  1634. // Convert enums to true enum values, so subsequent compilations can access it as an enum
  1635. if( gvar->isEnumValue )
  1636. {
  1637. asCObjectType *objectType = gvar->datatype.GetObjectType();
  1638. asASSERT(NULL != objectType);
  1639. asSEnumValue *e = asNEW(asSEnumValue);
  1640. if( e == 0 )
  1641. {
  1642. // Out of memory
  1643. numErrors++;
  1644. return;
  1645. }
  1646. e->name = gvar->name;
  1647. e->value = *(int*)&gvar->constantValue;
  1648. objectType->enumValues.PushLast(e);
  1649. }
  1650. }
  1651. else
  1652. {
  1653. // Add output to final output
  1654. finalOutput.Append(outBuffer);
  1655. accumErrors += numErrors;
  1656. accumWarnings += numWarnings;
  1657. }
  1658. engine->preMessage.isSet = false;
  1659. }
  1660. if( !compileSucceeded )
  1661. {
  1662. if( compilingPrimitives )
  1663. {
  1664. // No more primitives could be compiled, so
  1665. // switch to compiling the complex variables
  1666. compilingPrimitives = false;
  1667. compileSucceeded = true;
  1668. }
  1669. else
  1670. {
  1671. // No more variables can be compiled
  1672. // Add errors and warnings to total build
  1673. currNumWarnings += accumWarnings;
  1674. currNumErrors += accumErrors;
  1675. if( msgCallback )
  1676. finalOutput.SendToCallback(engine, &msgCallbackFunc, msgCallbackObj);
  1677. }
  1678. }
  1679. }
  1680. // Restore states
  1681. engine->msgCallback = msgCallback;
  1682. engine->msgCallbackFunc = msgCallbackFunc;
  1683. engine->msgCallbackObj = msgCallbackObj;
  1684. numWarnings = currNumWarnings;
  1685. numErrors = currNumErrors;
  1686. // Set the correct order of initialization
  1687. if( numErrors == 0 )
  1688. {
  1689. // If the length of the arrays are not the same, then this is the compilation
  1690. // of a single variable, in which case the initialization order of the previous
  1691. // variables must be preserved.
  1692. if( module->scriptGlobals.GetSize() == initOrder.GetSize() )
  1693. module->scriptGlobals.SwapWith(initOrder);
  1694. }
  1695. // Delete the enum expressions
  1696. asCSymbolTableIterator<sGlobalVariableDescription> it = globVariables.List();
  1697. while( it )
  1698. {
  1699. sGlobalVariableDescription *gvar = *it;
  1700. if( gvar->isEnumValue )
  1701. {
  1702. // Remove from symboltable. This has to be done prior to freeing the memeory
  1703. globVariables.Erase(it.GetIndex());
  1704. // Destroy the gvar property
  1705. if( gvar->declaredAtNode )
  1706. {
  1707. gvar->declaredAtNode->Destroy(engine);
  1708. gvar->declaredAtNode = 0;
  1709. }
  1710. if( gvar->initializationNode )
  1711. {
  1712. gvar->initializationNode->Destroy(engine);
  1713. gvar->initializationNode = 0;
  1714. }
  1715. if( gvar->property )
  1716. {
  1717. asDELETE(gvar->property, asCGlobalProperty);
  1718. gvar->property = 0;
  1719. }
  1720. asDELETE(gvar, sGlobalVariableDescription);
  1721. }
  1722. else
  1723. it++;
  1724. }
  1725. }
  1726. int asCBuilder::GetNamespaceAndNameFromNode(asCScriptNode *n, asCScriptCode *script, asSNameSpace *implicitNs, asSNameSpace *&outNs, asCString &outName)
  1727. {
  1728. asASSERT( n->nodeType == snIdentifier );
  1729. // Get the optional scope from the node
  1730. asSNameSpace *ns = GetNameSpaceFromNode(n->firstChild, script, implicitNs, 0);
  1731. if( ns == 0 )
  1732. return -1;
  1733. // Get the name
  1734. asCString name(&script->code[n->lastChild->tokenPos], n->lastChild->tokenLength);
  1735. outNs = ns;
  1736. outName = name;
  1737. return 0;
  1738. }
  1739. void asCBuilder::AddInterfaceFromMixinToClass(sClassDeclaration *decl, asCScriptNode *errNode, sMixinClass *mixin)
  1740. {
  1741. // Determine what interfaces that the mixin implements
  1742. asCScriptNode *node = mixin->node;
  1743. asASSERT(node->nodeType == snClass);
  1744. // Skip the name of the mixin
  1745. node = node->firstChild->next;
  1746. while( node && node->nodeType == snIdentifier )
  1747. {
  1748. bool ok = true;
  1749. asSNameSpace *ns;
  1750. asCString name;
  1751. if( GetNamespaceAndNameFromNode(node, mixin->script, mixin->ns, ns, name) < 0 )
  1752. ok = false;
  1753. else
  1754. {
  1755. // Find the object type for the interface
  1756. asCObjectType *objType = GetObjectType(name.AddressOf(), ns);
  1757. // Check that the object type is an interface
  1758. if( objType && objType->size == 0 && (objType->flags & asOBJ_SCRIPT_OBJECT) )
  1759. {
  1760. // Only add the interface if the class doesn't already implement it
  1761. if( !decl->objType->Implements(objType) )
  1762. AddInterfaceToClass(decl, errNode, objType);
  1763. }
  1764. else
  1765. {
  1766. WriteError(TXT_MIXIN_CLASS_CANNOT_INHERIT, mixin->script, node);
  1767. ok = false;
  1768. }
  1769. }
  1770. if( !ok )
  1771. {
  1772. // Remove this node so the error isn't reported again
  1773. asCScriptNode *delNode = node;
  1774. node = node->prev;
  1775. delNode->DisconnectParent();
  1776. delNode->Destroy(engine);
  1777. }
  1778. node = node->next;
  1779. }
  1780. }
  1781. void asCBuilder::AddInterfaceToClass(sClassDeclaration *decl, asCScriptNode *errNode, asCObjectType *intfType)
  1782. {
  1783. // A shared type may only implement from shared interfaces
  1784. if( decl->objType->IsShared() && !intfType->IsShared() )
  1785. {
  1786. asCString msg;
  1787. msg.Format(TXT_SHARED_CANNOT_IMPLEMENT_NON_SHARED_s, intfType->name.AddressOf());
  1788. WriteError(msg, decl->script, errNode);
  1789. return;
  1790. }
  1791. if( decl->isExistingShared )
  1792. {
  1793. // If the class is an existing shared class, then just check if the
  1794. // interface exists in the original declaration too
  1795. if( !decl->objType->Implements(intfType) )
  1796. {
  1797. asCString str;
  1798. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, decl->objType->GetName());
  1799. WriteError(str, decl->script, errNode);
  1800. return;
  1801. }
  1802. }
  1803. else
  1804. {
  1805. // If the interface is already in the class then don't add it again
  1806. if( decl->objType->Implements(intfType) )
  1807. return;
  1808. // Add the interface to the class
  1809. decl->objType->interfaces.PushLast(intfType);
  1810. // Add the inherited interfaces too
  1811. // For interfaces this will be done outside to handle out-of-order declarations
  1812. if( !decl->objType->IsInterface() )
  1813. {
  1814. for( asUINT n = 0; n < intfType->interfaces.GetLength(); n++ )
  1815. AddInterfaceToClass(decl, errNode, intfType->interfaces[n]);
  1816. }
  1817. }
  1818. }
  1819. void asCBuilder::CompileInterfaces()
  1820. {
  1821. asUINT n;
  1822. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  1823. {
  1824. sClassDeclaration *intfDecl = interfaceDeclarations[n];
  1825. asCObjectType *intfType = intfDecl->objType;
  1826. asCScriptNode *node = intfDecl->node;
  1827. asASSERT(node && node->nodeType == snInterface);
  1828. node = node->firstChild;
  1829. // Skip the 'shared' keyword
  1830. if( intfType->IsShared() )
  1831. node = node->next;
  1832. // Skip the name
  1833. node = node->next;
  1834. // Verify the inherited interfaces
  1835. while( node && node->nodeType == snIdentifier )
  1836. {
  1837. asSNameSpace *ns;
  1838. asCString name;
  1839. if( GetNamespaceAndNameFromNode(node, intfDecl->script, intfType->nameSpace, ns, name) < 0 )
  1840. {
  1841. node = node->next;
  1842. continue;
  1843. }
  1844. // Find the object type for the interface
  1845. asCObjectType *objType = 0;
  1846. while( ns )
  1847. {
  1848. objType = GetObjectType(name.AddressOf(), ns);
  1849. if( objType ) break;
  1850. ns = GetParentNameSpace(ns);
  1851. }
  1852. // Check that the object type is an interface
  1853. bool ok = true;
  1854. if( objType && objType->size == 0 && (objType->flags & asOBJ_SCRIPT_OBJECT) )
  1855. {
  1856. // Check that the implemented interface is shared if the base interface is shared
  1857. if( intfType->IsShared() && !objType->IsShared() )
  1858. {
  1859. asCString str;
  1860. str.Format(TXT_SHARED_CANNOT_IMPLEMENT_NON_SHARED_s, objType->GetName());
  1861. WriteError(str, intfDecl->script, node);
  1862. ok = false;
  1863. }
  1864. }
  1865. else
  1866. {
  1867. WriteError(TXT_INTERFACE_CAN_ONLY_IMPLEMENT_INTERFACE, intfDecl->script, node);
  1868. ok = false;
  1869. }
  1870. if( ok )
  1871. {
  1872. // Make sure none of the implemented interfaces implement from this one
  1873. asCObjectType *base = objType;
  1874. while( base != 0 )
  1875. {
  1876. if( base == intfType )
  1877. {
  1878. WriteError(TXT_CANNOT_IMPLEMENT_SELF, intfDecl->script, node);
  1879. ok = false;
  1880. break;
  1881. }
  1882. // At this point there is at most one implemented interface
  1883. if( base->interfaces.GetLength() )
  1884. base = base->interfaces[0];
  1885. else
  1886. break;
  1887. }
  1888. }
  1889. if( ok )
  1890. AddInterfaceToClass(intfDecl, node, objType);
  1891. // Remove the nodes so they aren't parsed again
  1892. asCScriptNode *delNode = node;
  1893. node = node->next;
  1894. delNode->DisconnectParent();
  1895. delNode->Destroy(engine);
  1896. }
  1897. }
  1898. // Order the interfaces with inheritances so that the inherited
  1899. // of inherited interfaces can be added properly
  1900. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  1901. {
  1902. sClassDeclaration *intfDecl = interfaceDeclarations[n];
  1903. asCObjectType *intfType = intfDecl->objType;
  1904. if( intfType->interfaces.GetLength() == 0 ) continue;
  1905. // If any of the derived interfaces are found after this interface, then move this to the end of the list
  1906. for( asUINT m = n+1; m < interfaceDeclarations.GetLength(); m++ )
  1907. {
  1908. if( intfType->Implements(interfaceDeclarations[m]->objType) )
  1909. {
  1910. interfaceDeclarations.RemoveIndex(n);
  1911. interfaceDeclarations.PushLast(intfDecl);
  1912. // Decrease index so that we don't skip an entry
  1913. n--;
  1914. break;
  1915. }
  1916. }
  1917. }
  1918. // Now recursively add the additional inherited interfaces
  1919. for( n = 0; n < interfaceDeclarations.GetLength(); n++ )
  1920. {
  1921. sClassDeclaration *intfDecl = interfaceDeclarations[n];
  1922. asCObjectType *intfType = intfDecl->objType;
  1923. // TODO: 2.28.1: Is this really at the correct place? Hasn't the vfTableIdx already been set here?
  1924. // Co-opt the vfTableIdx value in our own methods to indicate the
  1925. // index the function should have in the table chunk for this interface.
  1926. for( asUINT d = 0; d < intfType->methods.GetLength(); d++ )
  1927. {
  1928. asCScriptFunction *func = GetFunctionDescription(intfType->methods[d]);
  1929. func->vfTableIdx = d;
  1930. asASSERT(func->objectType == intfType);
  1931. }
  1932. // As new interfaces will be added to the end of the list, all
  1933. // interfaces will be traversed the same as recursively
  1934. for( asUINT m = 0; m < intfType->interfaces.GetLength(); m++ )
  1935. {
  1936. asCObjectType *base = intfType->interfaces[m];
  1937. // Add any interfaces not already implemented
  1938. for( asUINT l = 0; l < base->interfaces.GetLength(); l++ )
  1939. AddInterfaceToClass(intfDecl, intfDecl->node, base->interfaces[l]);
  1940. // Add the methods from the implemented interface
  1941. for( asUINT m = 0; m < base->methods.GetLength(); m++ )
  1942. {
  1943. // If the derived interface implements the same method, then don't add the base interface' method
  1944. asCScriptFunction *baseFunc = GetFunctionDescription(base->methods[m]);
  1945. asCScriptFunction *derivedFunc = 0;
  1946. bool found = false;
  1947. for( asUINT d = 0; d < intfType->methods.GetLength(); d++ )
  1948. {
  1949. derivedFunc = GetFunctionDescription(intfType->methods[d]);
  1950. if( derivedFunc->IsSignatureEqual(baseFunc) )
  1951. {
  1952. found = true;
  1953. break;
  1954. }
  1955. }
  1956. if( !found )
  1957. {
  1958. // Add the method
  1959. intfType->methods.PushLast(baseFunc->id);
  1960. baseFunc->AddRef();
  1961. }
  1962. }
  1963. }
  1964. }
  1965. }
  1966. void asCBuilder::CompileClasses()
  1967. {
  1968. asUINT n;
  1969. asCArray<sClassDeclaration*> toValidate((int)classDeclarations.GetLength());
  1970. // Determine class inheritances and interfaces
  1971. for( n = 0; n < classDeclarations.GetLength(); n++ )
  1972. {
  1973. sClassDeclaration *decl = classDeclarations[n];
  1974. asCScriptCode *file = decl->script;
  1975. // Find the base class that this class inherits from
  1976. bool multipleInheritance = false;
  1977. asCScriptNode *node = decl->node->firstChild;
  1978. if( decl->objType->IsShared() )
  1979. {
  1980. // Skip the keyword 'shared'
  1981. asASSERT(node->tokenType == ttIdentifier);
  1982. node = node->next;
  1983. }
  1984. if( decl->objType->flags & asOBJ_NOINHERIT )
  1985. {
  1986. // skip the keyword 'final'
  1987. asASSERT(node->tokenType == ttIdentifier);
  1988. node = node->next;
  1989. }
  1990. // Skip the name of the class
  1991. asASSERT(node->tokenType == ttIdentifier);
  1992. node = node->next;
  1993. while( node && node->nodeType == snIdentifier )
  1994. {
  1995. asSNameSpace *ns;
  1996. asCString name;
  1997. if( GetNamespaceAndNameFromNode(node, file, decl->objType->nameSpace, ns, name) < 0 )
  1998. {
  1999. node = node->next;
  2000. continue;
  2001. }
  2002. // Find the object type for the interface
  2003. asCObjectType *objType = 0;
  2004. sMixinClass *mixin = 0;
  2005. while( ns )
  2006. {
  2007. objType = GetObjectType(name.AddressOf(), ns);
  2008. if( objType == 0 )
  2009. mixin = GetMixinClass(name.AddressOf(), ns);
  2010. if( objType || mixin )
  2011. break;
  2012. ns = GetParentNameSpace(ns);
  2013. }
  2014. if( objType == 0 && mixin == 0 )
  2015. {
  2016. asCString str;
  2017. str.Format(TXT_IDENTIFIER_s_NOT_DATA_TYPE, name.AddressOf());
  2018. WriteError(str, file, node);
  2019. }
  2020. else if( mixin )
  2021. {
  2022. AddInterfaceFromMixinToClass(decl, node, mixin);
  2023. }
  2024. else if( !(objType->flags & asOBJ_SCRIPT_OBJECT) ||
  2025. objType->flags & asOBJ_NOINHERIT )
  2026. {
  2027. // Either the class is not a script class or interface
  2028. // or the class has been declared as 'final'
  2029. asCString str;
  2030. str.Format(TXT_CANNOT_INHERIT_FROM_s_FINAL, objType->name.AddressOf());
  2031. WriteError(str, file, node);
  2032. }
  2033. else if( objType->size != 0 )
  2034. {
  2035. // The class inherits from another script class
  2036. if( !decl->isExistingShared && decl->objType->derivedFrom != 0 )
  2037. {
  2038. if( !multipleInheritance )
  2039. {
  2040. WriteError(TXT_CANNOT_INHERIT_FROM_MULTIPLE_CLASSES, file, node);
  2041. multipleInheritance = true;
  2042. }
  2043. }
  2044. else
  2045. {
  2046. // Make sure none of the base classes inherit from this one
  2047. asCObjectType *base = objType;
  2048. bool error = false;
  2049. while( base != 0 )
  2050. {
  2051. if( base == decl->objType )
  2052. {
  2053. WriteError(TXT_CANNOT_INHERIT_FROM_SELF, file, node);
  2054. error = true;
  2055. break;
  2056. }
  2057. base = base->derivedFrom;
  2058. }
  2059. if( !error )
  2060. {
  2061. // A shared type may only inherit from other shared types
  2062. if( (decl->objType->IsShared()) && !(objType->IsShared()) )
  2063. {
  2064. asCString msg;
  2065. msg.Format(TXT_SHARED_CANNOT_INHERIT_FROM_NON_SHARED_s, objType->name.AddressOf());
  2066. WriteError(msg, file, node);
  2067. error = true;
  2068. }
  2069. }
  2070. if( !error )
  2071. {
  2072. if( decl->isExistingShared )
  2073. {
  2074. // Verify that the base class is the same as the original shared type
  2075. if( decl->objType->derivedFrom != objType )
  2076. {
  2077. asCString str;
  2078. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, decl->objType->GetName());
  2079. WriteError(str, file, node);
  2080. }
  2081. }
  2082. else
  2083. {
  2084. // Set the base class
  2085. decl->objType->derivedFrom = objType;
  2086. objType->AddRef();
  2087. }
  2088. }
  2089. }
  2090. }
  2091. else
  2092. {
  2093. // The class implements an interface
  2094. AddInterfaceToClass(decl, node, objType);
  2095. }
  2096. node = node->next;
  2097. }
  2098. }
  2099. // Order class declarations so that base classes are compiled before derived classes.
  2100. // This will allow the derived classes to copy properties and methods in the next step.
  2101. for( n = 0; n < classDeclarations.GetLength(); n++ )
  2102. {
  2103. sClassDeclaration *decl = classDeclarations[n];
  2104. asCObjectType *derived = decl->objType;
  2105. asCObjectType *base = derived->derivedFrom;
  2106. if( base == 0 ) continue;
  2107. // If the base class is found after the derived class, then move the derived class to the end of the list
  2108. for( asUINT m = n+1; m < classDeclarations.GetLength(); m++ )
  2109. {
  2110. sClassDeclaration *declBase = classDeclarations[m];
  2111. if( base == declBase->objType )
  2112. {
  2113. classDeclarations.RemoveIndex(n);
  2114. classDeclarations.PushLast(decl);
  2115. // Decrease index so that we don't skip an entry
  2116. n--;
  2117. break;
  2118. }
  2119. }
  2120. }
  2121. // Go through each of the classes and register the object type descriptions
  2122. for( n = 0; n < classDeclarations.GetLength(); n++ )
  2123. {
  2124. sClassDeclaration *decl = classDeclarations[n];
  2125. if( decl->isExistingShared )
  2126. {
  2127. // Set the declaration as validated already, so that other
  2128. // types that contain this will accept this type
  2129. decl->validState = 1;
  2130. // We'll still validate the declaration to make sure nothing new is
  2131. // added to the shared class that wasn't there in the previous
  2132. // compilation. We do not care if something that is there in the previous
  2133. // declaration is not included in the new declaration though.
  2134. asASSERT( decl->objType->interfaces.GetLength() == decl->objType->interfaceVFTOffsets.GetLength() );
  2135. }
  2136. // Methods included from mixin classes should take precedence over inherited methods
  2137. IncludeMethodsFromMixins(decl);
  2138. // Add all properties and methods from the base class
  2139. if( !decl->isExistingShared && decl->objType->derivedFrom )
  2140. {
  2141. asCObjectType *baseType = decl->objType->derivedFrom;
  2142. // The derived class inherits all interfaces from the base class
  2143. for( unsigned int n = 0; n < baseType->interfaces.GetLength(); n++ )
  2144. {
  2145. if( !decl->objType->Implements(baseType->interfaces[n]) )
  2146. decl->objType->interfaces.PushLast(baseType->interfaces[n]);
  2147. }
  2148. // TODO: Need to check for name conflict with new class methods
  2149. // Copy properties from base class to derived class
  2150. for( asUINT p = 0; p < baseType->properties.GetLength(); p++ )
  2151. {
  2152. asCObjectProperty *prop = AddPropertyToClass(decl, baseType->properties[p]->name, baseType->properties[p]->type, baseType->properties[p]->isPrivate);
  2153. // The properties must maintain the same offset
  2154. asASSERT(prop && prop->byteOffset == baseType->properties[p]->byteOffset); UNUSED_VAR(prop);
  2155. }
  2156. // Copy methods from base class to derived class
  2157. for( asUINT m = 0; m < baseType->methods.GetLength(); m++ )
  2158. {
  2159. // If the derived class implements the same method, then don't add the base class' method
  2160. asCScriptFunction *baseFunc = GetFunctionDescription(baseType->methods[m]);
  2161. asCScriptFunction *derivedFunc = 0;
  2162. bool found = false;
  2163. for( asUINT d = 0; d < decl->objType->methods.GetLength(); d++ )
  2164. {
  2165. derivedFunc = GetFunctionDescription(decl->objType->methods[d]);
  2166. if( derivedFunc->name == baseFunc->name &&
  2167. derivedFunc->IsSignatureExceptNameAndReturnTypeEqual(baseFunc) )
  2168. {
  2169. if( baseFunc->returnType != derivedFunc->returnType )
  2170. {
  2171. asCString msg;
  2172. msg.Format(TXT_DERIVED_METHOD_MUST_HAVE_SAME_RETTYPE_s, baseFunc->GetDeclaration());
  2173. WriteError(msg, decl->script, decl->node);
  2174. }
  2175. if( baseFunc->IsFinal() )
  2176. {
  2177. asCString msg;
  2178. msg.Format(TXT_METHOD_CANNOT_OVERRIDE_s, baseFunc->GetDeclaration());
  2179. WriteError(msg, decl->script, decl->node);
  2180. }
  2181. // Move the function from the methods array to the virtualFunctionTable
  2182. decl->objType->methods.RemoveIndex(d);
  2183. decl->objType->virtualFunctionTable.PushLast(derivedFunc);
  2184. found = true;
  2185. break;
  2186. }
  2187. }
  2188. if( !found )
  2189. {
  2190. // Push the base class function on the virtual function table
  2191. decl->objType->virtualFunctionTable.PushLast(baseType->virtualFunctionTable[m]);
  2192. baseType->virtualFunctionTable[m]->AddRef();
  2193. }
  2194. decl->objType->methods.PushLast(baseType->methods[m]);
  2195. engine->scriptFunctions[baseType->methods[m]]->AddRef();
  2196. }
  2197. }
  2198. if( !decl->isExistingShared )
  2199. {
  2200. // Move this class' methods into the virtual function table
  2201. for( asUINT m = 0; m < decl->objType->methods.GetLength(); m++ )
  2202. {
  2203. asCScriptFunction *func = GetFunctionDescription(decl->objType->methods[m]);
  2204. if( func->funcType != asFUNC_VIRTUAL )
  2205. {
  2206. // Move the reference from the method list to the virtual function list
  2207. decl->objType->methods.RemoveIndex(m);
  2208. decl->objType->virtualFunctionTable.PushLast(func);
  2209. // Substitute the function description in the method list for a virtual method
  2210. // Make sure the methods are in the same order as the virtual function table
  2211. decl->objType->methods.PushLast(CreateVirtualFunction(func, (int)decl->objType->virtualFunctionTable.GetLength() - 1));
  2212. m--;
  2213. }
  2214. }
  2215. // Make virtual function table chunks for each implemented interface
  2216. for( asUINT n = 0; n < decl->objType->interfaces.GetLength(); n++ )
  2217. {
  2218. asCObjectType *intf = decl->objType->interfaces[n];
  2219. // Add all the interface's functions to the virtual function table
  2220. asUINT offset = asUINT(decl->objType->virtualFunctionTable.GetLength());
  2221. decl->objType->interfaceVFTOffsets.PushLast(offset);
  2222. for( asUINT j = 0; j < intf->methods.GetLength(); j++ )
  2223. {
  2224. asCScriptFunction *intfFunc = GetFunctionDescription(intf->methods[j]);
  2225. // Only create the table for functions that are explicitly from this interface,
  2226. // inherited interface methods will be put in that interface's table.
  2227. if( intfFunc->objectType != intf )
  2228. continue;
  2229. asASSERT((asUINT)intfFunc->vfTableIdx == j);
  2230. //Find the interface function in the list of methods
  2231. asCScriptFunction *realFunc = 0;
  2232. for( asUINT p = 0; p < decl->objType->methods.GetLength(); p++ )
  2233. {
  2234. asCScriptFunction *func = GetFunctionDescription(decl->objType->methods[p]);
  2235. if( func->signatureId == intfFunc->signatureId )
  2236. {
  2237. if( func->funcType == asFUNC_VIRTUAL )
  2238. {
  2239. realFunc = decl->objType->virtualFunctionTable[func->vfTableIdx];
  2240. }
  2241. else
  2242. {
  2243. // This should not happen, all methods were moved into the virtual table
  2244. asASSERT(false);
  2245. }
  2246. break;
  2247. }
  2248. }
  2249. // If realFunc is still null, the interface was not
  2250. // implemented and we error out later in the checks.
  2251. decl->objType->virtualFunctionTable.PushLast(realFunc);
  2252. if( realFunc )
  2253. realFunc->AddRef();
  2254. }
  2255. }
  2256. }
  2257. // Enumerate each of the declared properties
  2258. asCScriptNode *node = decl->node->firstChild->next;
  2259. if( decl->objType->IsShared() )
  2260. node = node->next;
  2261. if( decl->objType->flags & asOBJ_NOINHERIT )
  2262. node = node->next;
  2263. // Skip list of classes and interfaces
  2264. while( node && node->nodeType == snIdentifier )
  2265. node = node->next;
  2266. while( node )
  2267. {
  2268. if( node->nodeType == snDeclaration )
  2269. {
  2270. asCScriptNode *n = node->firstChild;
  2271. // Is the property declared as private?
  2272. bool isPrivate = false;
  2273. if( n && n->tokenType == ttPrivate )
  2274. {
  2275. isPrivate = true;
  2276. n = n->next;
  2277. }
  2278. // Determine the type of the property
  2279. asCScriptCode *file = decl->script;
  2280. asCDataType dt = CreateDataTypeFromNode(n, file, decl->objType->nameSpace);
  2281. if( decl->objType->IsShared() && dt.GetObjectType() && !dt.GetObjectType()->IsShared() )
  2282. {
  2283. asCString msg;
  2284. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetObjectType()->name.AddressOf());
  2285. WriteError(msg, file, node);
  2286. }
  2287. if( dt.IsReadOnly() )
  2288. WriteError(TXT_PROPERTY_CANT_BE_CONST, file, node);
  2289. // Multiple properties can be declared separated by ,
  2290. n = n->next;
  2291. while( n )
  2292. {
  2293. asCString name(&file->code[n->tokenPos], n->tokenLength);
  2294. if( !decl->isExistingShared )
  2295. {
  2296. CheckNameConflictMember(decl->objType, name.AddressOf(), n, file, true);
  2297. AddPropertyToClass(decl, name, dt, isPrivate, file, n);
  2298. }
  2299. else
  2300. {
  2301. // Verify that the property exists in the original declaration
  2302. bool found = false;
  2303. for( asUINT p = 0; p < decl->objType->properties.GetLength(); p++ )
  2304. {
  2305. asCObjectProperty *prop = decl->objType->properties[p];
  2306. if( prop->isPrivate == isPrivate &&
  2307. prop->name == name &&
  2308. prop->type.IsEqualExceptRef(dt) )
  2309. {
  2310. found = true;
  2311. break;
  2312. }
  2313. }
  2314. if( !found )
  2315. {
  2316. asCString str;
  2317. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, decl->objType->GetName());
  2318. WriteError(str, file, n);
  2319. }
  2320. }
  2321. // Skip the initialization node
  2322. if( n->next && n->next->nodeType != snIdentifier )
  2323. n = n->next;
  2324. n = n->next;
  2325. }
  2326. }
  2327. else
  2328. asASSERT(false);
  2329. node = node->next;
  2330. }
  2331. // Add properties from included mixin classes that don't conflict with existing properties
  2332. IncludePropertiesFromMixins(decl);
  2333. if( !decl->isExistingShared )
  2334. toValidate.PushLast(decl);
  2335. asASSERT( decl->objType->interfaces.GetLength() == decl->objType->interfaceVFTOffsets.GetLength() );
  2336. }
  2337. // TODO: Warn if a method overrides a base method without marking it as 'override'.
  2338. // It must be possible to turn off this warning through engine property.
  2339. // TODO: A base class should be able to mark a method as 'abstract'. This will
  2340. // allow a base class to provide a partial implementation, but still force
  2341. // derived classes to implement specific methods.
  2342. // Verify that all interface methods are implemented in the classes
  2343. // We do this here so the base class' methods have already been inherited
  2344. for( n = 0; n < classDeclarations.GetLength(); n++ )
  2345. {
  2346. sClassDeclaration *decl = classDeclarations[n];
  2347. if( decl->isExistingShared ) continue;
  2348. asCArray<bool> overrideValidations(decl->objType->GetMethodCount());
  2349. for( asUINT k = 0; k < decl->objType->methods.GetLength(); k++ )
  2350. overrideValidations.PushLast( !static_cast<asCScriptFunction*>(decl->objType->GetMethodByIndex(k, false))->IsOverride() );
  2351. for( asUINT m = 0; m < decl->objType->interfaces.GetLength(); m++ )
  2352. {
  2353. asCObjectType *objType = decl->objType->interfaces[m];
  2354. for( asUINT i = 0; i < objType->methods.GetLength(); i++ )
  2355. {
  2356. // Only check the interface methods that was explicitly declared in this interface
  2357. // Methods that was inherited from other interfaces will be checked in those interfaces
  2358. if( objType != engine->scriptFunctions[objType->methods[i]]->objectType )
  2359. continue;
  2360. asUINT overrideIndex;
  2361. if( !DoesMethodExist(decl->objType, objType->methods[i], &overrideIndex) )
  2362. {
  2363. asCString str;
  2364. str.Format(TXT_MISSING_IMPLEMENTATION_OF_s,
  2365. engine->GetFunctionDeclaration(objType->methods[i]).AddressOf());
  2366. WriteError(str, decl->script, decl->node);
  2367. }
  2368. else
  2369. overrideValidations[overrideIndex] = true;
  2370. }
  2371. }
  2372. bool hasBaseClass = decl->objType->derivedFrom != 0;
  2373. for( asUINT j = 0; j < overrideValidations.GetLength(); j++ )
  2374. {
  2375. if( !overrideValidations[j] && (!hasBaseClass || !DoesMethodExist(decl->objType->derivedFrom, decl->objType->methods[j])) )
  2376. {
  2377. asCString msg;
  2378. msg.Format(TXT_METHOD_s_DOES_NOT_OVERRIDE, decl->objType->GetMethodByIndex(j, false)->GetDeclaration());
  2379. WriteError(msg, decl->script, decl->node);
  2380. }
  2381. }
  2382. }
  2383. // Verify that the declared structures are valid, e.g. that the structure
  2384. // doesn't contain a member of its own type directly or indirectly
  2385. while( toValidate.GetLength() > 0 )
  2386. {
  2387. asUINT numClasses = (asUINT)toValidate.GetLength();
  2388. asCArray<sClassDeclaration*> toValidateNext((int)toValidate.GetLength());
  2389. while( toValidate.GetLength() > 0 )
  2390. {
  2391. sClassDeclaration *decl = toValidate[toValidate.GetLength()-1];
  2392. int validState = 1;
  2393. for( asUINT n = 0; n < decl->objType->properties.GetLength(); n++ )
  2394. {
  2395. // A valid structure is one that uses only primitives or other valid objects
  2396. asCObjectProperty *prop = decl->objType->properties[n];
  2397. asCDataType dt = prop->type;
  2398. if( dt.IsTemplate() )
  2399. {
  2400. asCDataType sub = dt;
  2401. while( sub.IsTemplate() && !sub.IsObjectHandle() )
  2402. sub = sub.GetSubType();
  2403. dt = sub;
  2404. }
  2405. if( dt.IsObject() && !dt.IsObjectHandle() )
  2406. {
  2407. // Find the class declaration
  2408. sClassDeclaration *pdecl = 0;
  2409. for( asUINT p = 0; p < classDeclarations.GetLength(); p++ )
  2410. {
  2411. if( classDeclarations[p]->objType == dt.GetObjectType() )
  2412. {
  2413. pdecl = classDeclarations[p];
  2414. break;
  2415. }
  2416. }
  2417. if( pdecl )
  2418. {
  2419. if( pdecl->objType == decl->objType )
  2420. {
  2421. WriteError(TXT_ILLEGAL_MEMBER_TYPE, decl->script, decl->node);
  2422. validState = 2;
  2423. break;
  2424. }
  2425. else if( pdecl->validState != 1 )
  2426. {
  2427. validState = pdecl->validState;
  2428. break;
  2429. }
  2430. }
  2431. }
  2432. }
  2433. if( validState == 1 )
  2434. {
  2435. decl->validState = 1;
  2436. toValidate.PopLast();
  2437. }
  2438. else if( validState == 2 )
  2439. {
  2440. decl->validState = 2;
  2441. toValidate.PopLast();
  2442. }
  2443. else
  2444. {
  2445. toValidateNext.PushLast(toValidate.PopLast());
  2446. }
  2447. }
  2448. toValidate = toValidateNext;
  2449. toValidateNext.SetLength(0);
  2450. if( numClasses == toValidate.GetLength() )
  2451. {
  2452. WriteError(TXT_ILLEGAL_MEMBER_TYPE, toValidate[0]->script, toValidate[0]->node);
  2453. break;
  2454. }
  2455. }
  2456. if( numErrors > 0 ) return;
  2457. // Verify which script classes can really form circular references, and mark only those as garbage collected.
  2458. // TODO: runtime optimize: This algorithm can be further improved by checking the types that inherits from
  2459. // a base class. If the base class is not shared all the classes that derive from it
  2460. // are known at compile time, and can thus be checked for potential circular references too.
  2461. //
  2462. // Observe, that doing this would conflict with another potential future feature, which is to
  2463. // allow incremental builds, i.e. allow application to add or replace classes in an
  2464. // existing module. However, the applications that want to use that should use a special
  2465. // build flag to not finalize the module.
  2466. // Urho3D: disable garbage collection from script classes
  2467. /*
  2468. for( n = 0; n < classDeclarations.GetLength(); n++ )
  2469. {
  2470. sClassDeclaration *decl = classDeclarations[n];
  2471. // Existing shared classes won't be re-evaluated
  2472. if( decl->isExistingShared ) continue;
  2473. asCObjectType *ot = decl->objType;
  2474. // Is there some path in which this structure is involved in circular references?
  2475. bool gc = false;
  2476. for( asUINT p = 0; p < ot->properties.GetLength(); p++ )
  2477. {
  2478. asCDataType dt = ot->properties[p]->type;
  2479. if( !dt.IsObject() )
  2480. continue;
  2481. if( dt.IsObjectHandle() )
  2482. {
  2483. // If it is known that the handle can't be involved in a circular reference
  2484. // then this object doesn't need to be marked as garbage collected.
  2485. asCObjectType *prop = dt.GetObjectType();
  2486. if( prop->flags & asOBJ_SCRIPT_OBJECT )
  2487. {
  2488. // For script objects, treat non-final classes as if they can contain references
  2489. // as it is not known what derived classes might do. For final types, check all
  2490. // properties to determine if any of those can cause a circular reference.
  2491. if( prop->flags & asOBJ_NOINHERIT )
  2492. {
  2493. for( asUINT sp = 0; sp < prop->properties.GetLength(); sp++ )
  2494. {
  2495. asCDataType sdt = prop->properties[sp]->type;
  2496. if( sdt.IsObject() )
  2497. {
  2498. if( sdt.IsObjectHandle() )
  2499. {
  2500. // TODO: runtime optimize: If the handle is again to a final class, then we can recursively check if the circular reference can occur
  2501. if( sdt.GetObjectType()->flags & (asOBJ_SCRIPT_OBJECT | asOBJ_GC) )
  2502. {
  2503. gc = true;
  2504. break;
  2505. }
  2506. }
  2507. else if( sdt.GetObjectType()->flags & asOBJ_GC )
  2508. {
  2509. // TODO: runtime optimize: Just because the member type is a potential circle doesn't mean that this one is.
  2510. // Only if the object is of a type that can reference this type, either directly or indirectly
  2511. gc = true;
  2512. break;
  2513. }
  2514. }
  2515. }
  2516. if( gc )
  2517. break;
  2518. }
  2519. else
  2520. {
  2521. // Assume it is garbage collected as it is not known at compile time what might inherit from this type
  2522. gc = true;
  2523. break;
  2524. }
  2525. }
  2526. else if( prop->flags & asOBJ_GC )
  2527. {
  2528. // If a type is not a script object, adopt its GC flag
  2529. gc = true;
  2530. break;
  2531. }
  2532. }
  2533. else if( dt.GetObjectType()->flags & asOBJ_GC )
  2534. {
  2535. // TODO: runtime optimize: Just because the member type is a potential circle doesn't mean that this one is.
  2536. // Only if the object is of a type that can reference this type, either directly or indirectly
  2537. gc = true;
  2538. break;
  2539. }
  2540. }
  2541. // Update the flag in the object type
  2542. if( gc )
  2543. ot->flags |= asOBJ_GC;
  2544. else
  2545. ot->flags &= ~asOBJ_GC;
  2546. }
  2547. */
  2548. }
  2549. void asCBuilder::IncludeMethodsFromMixins(sClassDeclaration *decl)
  2550. {
  2551. asCScriptNode *node = decl->node->firstChild;
  2552. // Skip the 'final' and 'shared' keywords
  2553. if( decl->objType->IsShared() )
  2554. node = node->next;
  2555. if( decl->objType->flags & asOBJ_NOINHERIT )
  2556. node = node->next;
  2557. // Skip the name of the class
  2558. node = node->next;
  2559. // Find the included mixin classes
  2560. while( node && node->nodeType == snIdentifier )
  2561. {
  2562. asSNameSpace *ns;
  2563. asCString name;
  2564. if( GetNamespaceAndNameFromNode(node, decl->script, decl->objType->nameSpace, ns, name) < 0 )
  2565. {
  2566. node = node->next;
  2567. continue;
  2568. }
  2569. sMixinClass *mixin = 0;
  2570. while( ns )
  2571. {
  2572. // Need to make sure the name is not an object type
  2573. asCObjectType *objType = GetObjectType(name.AddressOf(), ns);
  2574. if( objType == 0 )
  2575. mixin = GetMixinClass(name.AddressOf(), ns);
  2576. if( objType || mixin )
  2577. break;
  2578. ns = GetParentNameSpace(ns);
  2579. }
  2580. if( mixin )
  2581. {
  2582. // Find methods from mixin declaration
  2583. asCScriptNode *n = mixin->node->firstChild;
  2584. // Skip to the member declarations
  2585. // Possible keywords 'final' and 'shared' are removed in RegisterMixinClass so we don't need to worry about those here
  2586. while( n && n->nodeType == snIdentifier )
  2587. n = n->next;
  2588. // Add methods from the mixin that are not already existing in the class
  2589. while( n )
  2590. {
  2591. if( n->nodeType == snFunction )
  2592. {
  2593. // Instead of disconnecting the node, we need to clone it, otherwise other
  2594. // classes that include the same mixin will not see the methods
  2595. asCScriptNode *copy = n->CreateCopy(engine);
  2596. // Register the method, but only if it doesn't already exist in the class
  2597. RegisterScriptFunctionFromNode(copy, mixin->script, decl->objType, false, false, 0, false, true);
  2598. }
  2599. else if( n->nodeType == snVirtualProperty )
  2600. {
  2601. // TODO: mixin: Support virtual properties too
  2602. WriteError("The virtual property syntax is currently not supported for mixin classes", mixin->script, n);
  2603. //RegisterVirtualProperty(node, decl->script, decl->objType, false, false);
  2604. }
  2605. n = n->next;
  2606. }
  2607. }
  2608. node = node->next;
  2609. }
  2610. }
  2611. void asCBuilder::IncludePropertiesFromMixins(sClassDeclaration *decl)
  2612. {
  2613. asCScriptNode *node = decl->node->firstChild;
  2614. // Skip the 'final' and 'shared' keywords
  2615. if( decl->objType->IsShared() )
  2616. node = node->next;
  2617. if( decl->objType->flags & asOBJ_NOINHERIT )
  2618. node = node->next;
  2619. // Skip the name of the class
  2620. node = node->next;
  2621. // Find the included mixin classes
  2622. while( node && node->nodeType == snIdentifier )
  2623. {
  2624. asSNameSpace *ns;
  2625. asCString name;
  2626. if( GetNamespaceAndNameFromNode(node, decl->script, decl->objType->nameSpace, ns, name) < 0 )
  2627. {
  2628. node = node->next;
  2629. continue;
  2630. }
  2631. sMixinClass *mixin = 0;
  2632. while( ns )
  2633. {
  2634. // Need to make sure the name is not an object type
  2635. asCObjectType *objType = GetObjectType(name.AddressOf(), ns);
  2636. if( objType == 0 )
  2637. mixin = GetMixinClass(name.AddressOf(), ns);
  2638. if( objType || mixin )
  2639. break;
  2640. ns = GetParentNameSpace(ns);
  2641. }
  2642. if( mixin )
  2643. {
  2644. // Find properties from mixin declaration
  2645. asCScriptNode *n = mixin->node->firstChild;
  2646. // Skip to the member declarations
  2647. // Possible keywords 'final' and 'shared' are removed in RegisterMixinClass so we don't need to worry about those here
  2648. while( n && n->nodeType == snIdentifier )
  2649. n = n->next;
  2650. // Add properties from the mixin that are not already existing in the class
  2651. while( n )
  2652. {
  2653. if( n->nodeType == snDeclaration )
  2654. {
  2655. asCScriptNode *n2 = n->firstChild;
  2656. bool isPrivate = false;
  2657. if( n2 && n2->tokenType == ttPrivate )
  2658. {
  2659. isPrivate = true;
  2660. n2 = n2->next;
  2661. }
  2662. asCScriptCode *file = mixin->script;
  2663. asCDataType dt = CreateDataTypeFromNode(n2, file, mixin->ns);
  2664. if( decl->objType->IsShared() && dt.GetObjectType() && !dt.GetObjectType()->IsShared() )
  2665. {
  2666. asCString msg;
  2667. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetObjectType()->name.AddressOf());
  2668. WriteError(msg, file, n);
  2669. WriteInfo(TXT_WHILE_INCLUDING_MIXIN, decl->script, node);
  2670. }
  2671. if( dt.IsReadOnly() )
  2672. WriteError(TXT_PROPERTY_CANT_BE_CONST, file, n);
  2673. n2 = n2->next;
  2674. while( n2 )
  2675. {
  2676. asCString name(&file->code[n2->tokenPos], n2->tokenLength);
  2677. // Add the property only if it doesn't already exist in the class
  2678. bool exists = false;
  2679. for( asUINT p = 0; p < decl->objType->properties.GetLength(); p++ )
  2680. if( decl->objType->properties[p]->name == name )
  2681. {
  2682. exists = true;
  2683. break;
  2684. }
  2685. if( !exists )
  2686. {
  2687. if( !decl->isExistingShared )
  2688. {
  2689. // It must not conflict with the name of methods
  2690. int r = CheckNameConflictMember(decl->objType, name.AddressOf(), n2, file, true);
  2691. if( r < 0 )
  2692. WriteInfo(TXT_WHILE_INCLUDING_MIXIN, decl->script, node);
  2693. AddPropertyToClass(decl, name, dt, isPrivate, file, n2);
  2694. }
  2695. else
  2696. {
  2697. // Verify that the property exists in the original declaration
  2698. bool found = false;
  2699. for( asUINT p = 0; p < decl->objType->properties.GetLength(); p++ )
  2700. {
  2701. asCObjectProperty *prop = decl->objType->properties[p];
  2702. if( prop->isPrivate == isPrivate &&
  2703. prop->name == name &&
  2704. prop->type == dt )
  2705. {
  2706. found = true;
  2707. break;
  2708. }
  2709. }
  2710. if( !found )
  2711. {
  2712. asCString str;
  2713. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, decl->objType->GetName());
  2714. WriteError(str, decl->script, decl->node);
  2715. WriteInfo(TXT_WHILE_INCLUDING_MIXIN, decl->script, node);
  2716. }
  2717. }
  2718. }
  2719. // Skip the initialization expression
  2720. if( n2->next && n2->next->nodeType != snIdentifier )
  2721. n2 = n2->next;
  2722. n2 = n2->next;
  2723. }
  2724. }
  2725. n = n->next;
  2726. }
  2727. }
  2728. node = node->next;
  2729. }
  2730. }
  2731. int asCBuilder::CreateVirtualFunction(asCScriptFunction *func, int idx)
  2732. {
  2733. asCScriptFunction *vf = asNEW(asCScriptFunction)(engine, module, asFUNC_VIRTUAL);
  2734. if( vf == 0 )
  2735. return asOUT_OF_MEMORY;
  2736. vf->name = func->name;
  2737. vf->returnType = func->returnType;
  2738. vf->parameterTypes = func->parameterTypes;
  2739. vf->inOutFlags = func->inOutFlags;
  2740. vf->id = engine->GetNextScriptFunctionId();
  2741. vf->isReadOnly = func->isReadOnly;
  2742. vf->objectType = func->objectType;
  2743. vf->signatureId = func->signatureId;
  2744. vf->isPrivate = func->isPrivate;
  2745. vf->isFinal = func->isFinal;
  2746. vf->isOverride = func->isOverride;
  2747. vf->vfTableIdx = idx;
  2748. // It is not necessary to copy the default args, as they have no meaning in the virtual function
  2749. module->AddScriptFunction(vf);
  2750. // Add a dummy to the builder so that it doesn't mix up function ids
  2751. functions.PushLast(0);
  2752. return vf->id;
  2753. }
  2754. asCObjectProperty *asCBuilder::AddPropertyToClass(sClassDeclaration *decl, const asCString &name, const asCDataType &dt, bool isPrivate, asCScriptCode *file, asCScriptNode *node)
  2755. {
  2756. // If the declaration node is not given, then
  2757. // this property is inherited from a base class
  2758. if( node )
  2759. {
  2760. // Check if the property is allowed
  2761. if( !dt.CanBeInstanciated() )
  2762. {
  2763. if( file && node )
  2764. {
  2765. asCString str;
  2766. str.Format(TXT_DATA_TYPE_CANT_BE_s, dt.Format().AddressOf());
  2767. WriteError(str, file, node);
  2768. }
  2769. return 0;
  2770. }
  2771. // Register the initialization expression (if any) to be compiled later
  2772. asCScriptNode *declNode = node;
  2773. asCScriptNode *initNode = 0;
  2774. if( node->next && node->next->nodeType != snIdentifier )
  2775. {
  2776. asASSERT( node->next->nodeType == snAssignment );
  2777. initNode = node->next;
  2778. }
  2779. sPropertyInitializer p(name, declNode, initNode, file);
  2780. decl->propInits.PushLast(p);
  2781. }
  2782. // Add the property to the object type
  2783. return decl->objType->AddPropertyToClass(name, dt, isPrivate);
  2784. }
  2785. bool asCBuilder::DoesMethodExist(asCObjectType *objType, int methodId, asUINT *methodIndex)
  2786. {
  2787. asCScriptFunction *method = GetFunctionDescription(methodId);
  2788. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  2789. {
  2790. asCScriptFunction *m = GetFunctionDescription(objType->methods[n]);
  2791. if( m->name != method->name ) continue;
  2792. if( m->returnType != method->returnType ) continue;
  2793. if( m->isReadOnly != method->isReadOnly ) continue;
  2794. if( m->parameterTypes != method->parameterTypes ) continue;
  2795. if( m->inOutFlags != method->inOutFlags ) continue;
  2796. if( methodIndex )
  2797. *methodIndex = n;
  2798. return true;
  2799. }
  2800. return false;
  2801. }
  2802. void asCBuilder::AddDefaultConstructor(asCObjectType *objType, asCScriptCode *file)
  2803. {
  2804. int funcId = engine->GetNextScriptFunctionId();
  2805. asCDataType returnType = asCDataType::CreatePrimitive(ttVoid, false);
  2806. asCArray<asCDataType> parameterTypes;
  2807. asCArray<asETypeModifiers> inOutFlags;
  2808. asCArray<asCString *> defaultArgs;
  2809. asCArray<asCString> parameterNames;
  2810. // Add the script function
  2811. // TODO: declaredAt should be set to where the class has been declared
  2812. module->AddScriptFunction(file->idx, 0, funcId, objType->name, returnType, parameterTypes, parameterNames, inOutFlags, defaultArgs, false, objType);
  2813. // Set it as default constructor
  2814. if( objType->beh.construct )
  2815. engine->scriptFunctions[objType->beh.construct]->Release();
  2816. objType->beh.construct = funcId;
  2817. objType->beh.constructors[0] = funcId;
  2818. engine->scriptFunctions[funcId]->AddRef();
  2819. // The bytecode for the default constructor will be generated
  2820. // only after the potential inheritance has been established
  2821. sFunctionDescription *func = asNEW(sFunctionDescription);
  2822. if( func == 0 )
  2823. {
  2824. // Out of memory
  2825. return;
  2826. }
  2827. functions.PushLast(func);
  2828. func->script = file;
  2829. func->node = 0;
  2830. func->name = objType->name;
  2831. func->objType = objType;
  2832. func->funcId = funcId;
  2833. func->isExistingShared = false;
  2834. // Add a default factory as well
  2835. funcId = engine->GetNextScriptFunctionId();
  2836. if( objType->beh.factory )
  2837. engine->scriptFunctions[objType->beh.factory]->Release();
  2838. objType->beh.factory = funcId;
  2839. objType->beh.factories[0] = funcId;
  2840. returnType = asCDataType::CreateObjectHandle(objType, false);
  2841. // TODO: should be the same as the constructor
  2842. module->AddScriptFunction(file->idx, 0, funcId, objType->name, returnType, parameterTypes, parameterNames, inOutFlags, defaultArgs, false);
  2843. functions.PushLast(0);
  2844. asCCompiler compiler(engine);
  2845. compiler.CompileFactory(this, file, engine->scriptFunctions[funcId]);
  2846. engine->scriptFunctions[funcId]->AddRef();
  2847. // If the object is shared, then the factory must also be marked as shared
  2848. if( objType->flags & asOBJ_SHARED )
  2849. engine->scriptFunctions[funcId]->isShared = true;
  2850. }
  2851. int asCBuilder::RegisterEnum(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  2852. {
  2853. // Is it a shared enum?
  2854. bool isShared = false;
  2855. asCObjectType *existingSharedType = 0;
  2856. asCScriptNode *tmp = node->firstChild;
  2857. if( tmp->nodeType == snIdentifier && file->TokenEquals(tmp->tokenPos, tmp->tokenLength, SHARED_TOKEN) )
  2858. {
  2859. isShared = true;
  2860. tmp = tmp->next;
  2861. }
  2862. // Grab the name of the enumeration
  2863. asCString name;
  2864. asASSERT(snDataType == tmp->nodeType);
  2865. asASSERT(snIdentifier == tmp->firstChild->nodeType);
  2866. name.Assign(&file->code[tmp->firstChild->tokenPos], tmp->firstChild->tokenLength);
  2867. if( isShared )
  2868. {
  2869. // Look for a pre-existing shared enum with the same signature
  2870. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  2871. {
  2872. asCObjectType *o = engine->classTypes[n];
  2873. if( o &&
  2874. o->IsShared() &&
  2875. (o->flags & asOBJ_ENUM) &&
  2876. o->name == name &&
  2877. o->nameSpace == ns )
  2878. {
  2879. existingSharedType = o;
  2880. break;
  2881. }
  2882. }
  2883. }
  2884. // Check the name and add the enum
  2885. int r = CheckNameConflict(name.AddressOf(), tmp->firstChild, file, ns);
  2886. if( asSUCCESS == r )
  2887. {
  2888. asCObjectType *st;
  2889. if( existingSharedType )
  2890. st = existingSharedType;
  2891. else
  2892. {
  2893. st = asNEW(asCObjectType)(engine);
  2894. if( st == 0 )
  2895. return asOUT_OF_MEMORY;
  2896. st->flags = asOBJ_ENUM;
  2897. if( isShared )
  2898. st->flags |= asOBJ_SHARED;
  2899. st->size = 4;
  2900. st->name = name;
  2901. st->nameSpace = ns;
  2902. }
  2903. module->enumTypes.PushLast(st);
  2904. st->AddRef();
  2905. // TODO: cleanup: Should the enum type really be stored in the engine->classTypes?
  2906. // http://www.gamedev.net/topic/616912-c-header-file-shared-with-scripts/page__gopid__4895940
  2907. if( !existingSharedType )
  2908. engine->classTypes.PushLast(st);
  2909. // Store the location of this declaration for reference in name collisions
  2910. sClassDeclaration *decl = asNEW(sClassDeclaration);
  2911. if( decl == 0 )
  2912. return asOUT_OF_MEMORY;
  2913. decl->name = name;
  2914. decl->script = file;
  2915. decl->objType = st;
  2916. namedTypeDeclarations.PushLast(decl);
  2917. asCDataType type = CreateDataTypeFromNode(tmp, file, ns);
  2918. asASSERT(!type.IsReference());
  2919. // Register the enum values
  2920. tmp = tmp->next;
  2921. while( tmp )
  2922. {
  2923. asASSERT(snIdentifier == tmp->nodeType);
  2924. asCString name(&file->code[tmp->tokenPos], tmp->tokenLength);
  2925. if( existingSharedType )
  2926. {
  2927. // If this is a pre-existent shared enum, then just double check
  2928. // that the value is already defined in the original declaration
  2929. bool found = false;
  2930. for( size_t n = 0; n < st->enumValues.GetLength(); n++ )
  2931. if( st->enumValues[n]->name == name )
  2932. {
  2933. found = true;
  2934. break;
  2935. }
  2936. if( !found )
  2937. {
  2938. asCString str;
  2939. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, st->GetName());
  2940. WriteError(str, file, tmp);
  2941. break;
  2942. }
  2943. tmp = tmp->next;
  2944. if( tmp && tmp->nodeType == snAssignment )
  2945. tmp = tmp->next;
  2946. continue;
  2947. }
  2948. else
  2949. {
  2950. // Check for name conflict errors with other values in the enum
  2951. if( globVariables.GetFirst(ns, name, asCCompGlobVarType(type)) )
  2952. {
  2953. asCString str;
  2954. str.Format(TXT_NAME_CONFLICT_s_ALREADY_USED, name.AddressOf());
  2955. WriteError(str, file, tmp);
  2956. tmp = tmp->next;
  2957. if( tmp && tmp->nodeType == snAssignment )
  2958. tmp = tmp->next;
  2959. continue;
  2960. }
  2961. // Check for assignment
  2962. asCScriptNode *asnNode = tmp->next;
  2963. if( asnNode && snAssignment == asnNode->nodeType )
  2964. asnNode->DisconnectParent();
  2965. else
  2966. asnNode = 0;
  2967. // Create the global variable description so the enum value can be evaluated
  2968. sGlobalVariableDescription *gvar = asNEW(sGlobalVariableDescription);
  2969. if( gvar == 0 )
  2970. return asOUT_OF_MEMORY;
  2971. gvar->script = file;
  2972. gvar->declaredAtNode = tmp;
  2973. tmp = tmp->next;
  2974. gvar->declaredAtNode->DisconnectParent();
  2975. gvar->initializationNode = asnNode;
  2976. gvar->name = name;
  2977. gvar->datatype = type;
  2978. gvar->ns = ns;
  2979. // No need to allocate space on the global memory stack since the values are stored in the asCObjectType
  2980. // Set the index to a negative to allow compiler to diferentiate from ordinary global var when compiling the initialization
  2981. gvar->index = -1;
  2982. gvar->isCompiled = false;
  2983. gvar->isPureConstant = true;
  2984. gvar->isEnumValue = true;
  2985. gvar->constantValue = 0xdeadbeef;
  2986. // Allocate dummy property so we can compile the value.
  2987. // This will be removed later on so we don't add it to the engine.
  2988. gvar->property = asNEW(asCGlobalProperty);
  2989. if( gvar->property == 0 )
  2990. return asOUT_OF_MEMORY;
  2991. gvar->property->name = name;
  2992. gvar->property->nameSpace = ns;
  2993. gvar->property->type = gvar->datatype;
  2994. gvar->property->id = 0;
  2995. globVariables.Put(gvar);
  2996. }
  2997. }
  2998. }
  2999. node->Destroy(engine);
  3000. return r;
  3001. }
  3002. int asCBuilder::RegisterTypedef(asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  3003. {
  3004. // Get the native data type
  3005. asCScriptNode *tmp = node->firstChild;
  3006. asASSERT(NULL != tmp && snDataType == tmp->nodeType);
  3007. asCDataType dataType;
  3008. dataType.CreatePrimitive(tmp->tokenType, false);
  3009. dataType.SetTokenType(tmp->tokenType);
  3010. tmp = tmp->next;
  3011. // Grab the name of the typedef
  3012. asASSERT(NULL != tmp && NULL == tmp->next);
  3013. asCString name;
  3014. name.Assign(&file->code[tmp->tokenPos], tmp->tokenLength);
  3015. // If the name is not already in use add it
  3016. int r = CheckNameConflict(name.AddressOf(), tmp, file, ns);
  3017. asCObjectType *st = 0;
  3018. if( asSUCCESS == r )
  3019. {
  3020. // Create the new type
  3021. st = asNEW(asCObjectType)(engine);
  3022. if( st == 0 )
  3023. r = asOUT_OF_MEMORY;
  3024. }
  3025. if( asSUCCESS == r )
  3026. {
  3027. st->flags = asOBJ_TYPEDEF;
  3028. st->size = dataType.GetSizeInMemoryBytes();
  3029. st->name = name;
  3030. st->nameSpace = ns;
  3031. st->templateSubTypes.PushLast(dataType);
  3032. st->AddRef();
  3033. module->typeDefs.PushLast(st);
  3034. engine->classTypes.PushLast(st);
  3035. // Store the location of this declaration for reference in name collisions
  3036. sClassDeclaration *decl = asNEW(sClassDeclaration);
  3037. if( decl == 0 )
  3038. r = asOUT_OF_MEMORY;
  3039. else
  3040. {
  3041. decl->name = name;
  3042. decl->script = file;
  3043. decl->objType = st;
  3044. namedTypeDeclarations.PushLast(decl);
  3045. }
  3046. }
  3047. node->Destroy(engine);
  3048. return r;
  3049. }
  3050. void asCBuilder::GetParsedFunctionDetails(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, asCString &name, asCDataType &returnType, asCArray<asCString> &parameterNames, asCArray<asCDataType> &parameterTypes, asCArray<asETypeModifiers> &inOutFlags, asCArray<asCString *> &defaultArgs, bool &isConstMethod, bool &isConstructor, bool &isDestructor, bool &isPrivate, bool &isOverride, bool &isFinal, bool &isShared, asSNameSpace *implicitNamespace)
  3051. {
  3052. node = node->firstChild;
  3053. // Is the function a private class method?
  3054. isPrivate = false;
  3055. if( node->tokenType == ttPrivate )
  3056. {
  3057. isPrivate = true;
  3058. node = node->next;
  3059. }
  3060. // Is the function shared?
  3061. isShared = false;
  3062. if( node->tokenType == ttIdentifier && file->TokenEquals(node->tokenPos, node->tokenLength, SHARED_TOKEN) )
  3063. {
  3064. isShared = true;
  3065. node = node->next;
  3066. }
  3067. // Find the name
  3068. isConstructor = false;
  3069. isDestructor = false;
  3070. asCScriptNode *n = 0;
  3071. if( node->nodeType == snDataType )
  3072. n = node->next->next;
  3073. else
  3074. {
  3075. // If the first node is a ~ token, then we know it is a destructor
  3076. if( node->tokenType == ttBitNot )
  3077. {
  3078. n = node->next;
  3079. isDestructor = true;
  3080. }
  3081. else
  3082. {
  3083. n = node;
  3084. isConstructor = true;
  3085. }
  3086. }
  3087. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  3088. // Initialize a script function object for registration
  3089. if( !isConstructor && !isDestructor )
  3090. {
  3091. returnType = CreateDataTypeFromNode(node, file, implicitNamespace);
  3092. returnType = ModifyDataTypeFromNode(returnType, node->next, file, 0, 0);
  3093. if( engine->ep.disallowValueAssignForRefType &&
  3094. returnType.GetObjectType() &&
  3095. (returnType.GetObjectType()->flags & asOBJ_REF) &&
  3096. !(returnType.GetObjectType()->flags & asOBJ_SCOPED) &&
  3097. !returnType.IsReference() &&
  3098. !returnType.IsObjectHandle() )
  3099. {
  3100. WriteError(TXT_REF_TYPE_CANT_BE_RETURNED_BY_VAL, file, node);
  3101. }
  3102. }
  3103. else
  3104. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  3105. isConstMethod = false;
  3106. isFinal = false;
  3107. isOverride = false;
  3108. if( objType && n->next->next )
  3109. {
  3110. asCScriptNode *decorator = n->next->next;
  3111. // Is this a const method?
  3112. if( decorator->tokenType == ttConst )
  3113. {
  3114. isConstMethod = true;
  3115. decorator = decorator->next;
  3116. }
  3117. while( decorator )
  3118. {
  3119. if( decorator->tokenType == ttIdentifier && file->TokenEquals(decorator->tokenPos, decorator->tokenLength, FINAL_TOKEN) )
  3120. isFinal = true;
  3121. else if( decorator->tokenType == ttIdentifier && file->TokenEquals(decorator->tokenPos, decorator->tokenLength, OVERRIDE_TOKEN) )
  3122. isOverride = true;
  3123. decorator = decorator->next;
  3124. }
  3125. }
  3126. // Count the number of parameters
  3127. int count = 0;
  3128. asCScriptNode *c = n->next->firstChild;
  3129. while( c )
  3130. {
  3131. count++;
  3132. c = c->next->next;
  3133. if( c && c->nodeType == snIdentifier )
  3134. c = c->next;
  3135. if( c && c->nodeType == snExpression )
  3136. c = c->next;
  3137. }
  3138. // Get the parameter types
  3139. parameterNames.Allocate(count, false);
  3140. parameterTypes.Allocate(count, false);
  3141. inOutFlags.Allocate(count, false);
  3142. defaultArgs.Allocate(count, false);
  3143. n = n->next->firstChild;
  3144. while( n )
  3145. {
  3146. asETypeModifiers inOutFlag;
  3147. asCDataType type = CreateDataTypeFromNode(n, file, implicitNamespace);
  3148. type = ModifyDataTypeFromNode(type, n->next, file, &inOutFlag, 0);
  3149. if( engine->ep.disallowValueAssignForRefType &&
  3150. type.GetObjectType() &&
  3151. (type.GetObjectType()->flags & asOBJ_REF) &&
  3152. !(type.GetObjectType()->flags & asOBJ_SCOPED) &&
  3153. !type.IsReference() &&
  3154. !type.IsObjectHandle() )
  3155. {
  3156. WriteError(TXT_REF_TYPE_CANT_BE_PASSED_BY_VAL, file, node);
  3157. }
  3158. // Store the parameter type
  3159. parameterTypes.PushLast(type);
  3160. inOutFlags.PushLast(inOutFlag);
  3161. // Move to next parameter
  3162. n = n->next->next;
  3163. if( n && n->nodeType == snIdentifier )
  3164. {
  3165. asCString name;
  3166. name.Assign(&file->code[n->tokenPos], n->tokenLength);
  3167. parameterNames.PushLast(name);
  3168. n = n->next;
  3169. }
  3170. else
  3171. {
  3172. // No name was given for the parameter
  3173. parameterNames.PushLast(asCString());
  3174. }
  3175. if( n && n->nodeType == snExpression )
  3176. {
  3177. // Strip out white space and comments to better share the string
  3178. asCString *defaultArgStr = asNEW(asCString);
  3179. if( defaultArgStr )
  3180. *defaultArgStr = GetCleanExpressionString(n, file);
  3181. defaultArgs.PushLast(defaultArgStr);
  3182. n = n->next;
  3183. }
  3184. else
  3185. defaultArgs.PushLast(0);
  3186. }
  3187. }
  3188. #endif
  3189. asCString asCBuilder::GetCleanExpressionString(asCScriptNode *node, asCScriptCode *file)
  3190. {
  3191. asASSERT(node && node->nodeType == snExpression);
  3192. asCString str;
  3193. str.Assign(file->code + node->tokenPos, node->tokenLength);
  3194. asCString cleanStr;
  3195. for( asUINT n = 0; n < str.GetLength(); )
  3196. {
  3197. int len;
  3198. asETokenClass tok = engine->ParseToken(str.AddressOf() + n, str.GetLength() - n, &len);
  3199. if( tok != asTC_COMMENT && tok != asTC_WHITESPACE )
  3200. {
  3201. if( cleanStr.GetLength() ) cleanStr += " ";
  3202. cleanStr.Concatenate(str.AddressOf() + n, len);
  3203. }
  3204. n += len;
  3205. }
  3206. return cleanStr;
  3207. }
  3208. #ifndef AS_NO_COMPILER
  3209. int asCBuilder::RegisterScriptFunctionFromNode(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, bool isInterface, bool isGlobalFunction, asSNameSpace *ns, bool isExistingShared, bool isMixin)
  3210. {
  3211. asCString name;
  3212. asCDataType returnType;
  3213. asCArray<asCString> parameterNames;
  3214. asCArray<asCDataType> parameterTypes;
  3215. asCArray<asETypeModifiers> inOutFlags;
  3216. asCArray<asCString *> defaultArgs;
  3217. bool isConstMethod;
  3218. bool isOverride;
  3219. bool isFinal;
  3220. bool isConstructor;
  3221. bool isDestructor;
  3222. bool isPrivate;
  3223. bool isShared;
  3224. asASSERT( (objType && ns == 0) || isGlobalFunction );
  3225. // Set the default namespace
  3226. if( ns == 0 )
  3227. {
  3228. if( objType )
  3229. ns = objType->nameSpace;
  3230. else
  3231. ns = engine->nameSpaces[0];
  3232. }
  3233. GetParsedFunctionDetails(node, file, objType, name, returnType, parameterNames, parameterTypes, inOutFlags, defaultArgs, isConstMethod, isConstructor, isDestructor, isPrivate, isOverride, isFinal, isShared, ns);
  3234. return RegisterScriptFunction(node, file, objType, isInterface, isGlobalFunction, ns, isExistingShared, isMixin, name, returnType, parameterNames, parameterTypes, inOutFlags, defaultArgs, isConstMethod, isConstructor, isDestructor, isPrivate, isOverride, isFinal, isShared);
  3235. }
  3236. int asCBuilder::RegisterScriptFunction(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, bool isInterface, bool isGlobalFunction, asSNameSpace *ns, bool isExistingShared, bool isMixin, asCString &name, asCDataType &returnType, asCArray<asCString> &parameterNames, asCArray<asCDataType> &parameterTypes, asCArray<asETypeModifiers> &inOutFlags, asCArray<asCString *> &defaultArgs, bool isConstMethod, bool isConstructor, bool isDestructor, bool isPrivate, bool isOverride, bool isFinal, bool isShared)
  3237. {
  3238. // Determine default namespace if not specified
  3239. if( ns == 0 )
  3240. {
  3241. if( objType )
  3242. ns = objType->nameSpace;
  3243. else
  3244. ns = engine->nameSpaces[0];
  3245. }
  3246. if( isExistingShared )
  3247. {
  3248. asASSERT( objType );
  3249. // Should validate that the function really exists in the class/interface
  3250. bool found = false;
  3251. if( isConstructor || isDestructor )
  3252. {
  3253. // TODO: shared: Should check the existance of these too
  3254. found = true;
  3255. }
  3256. else
  3257. {
  3258. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  3259. {
  3260. asCScriptFunction *func = engine->scriptFunctions[objType->methods[n]];
  3261. if( func->name == name &&
  3262. func->IsSignatureExceptNameEqual(returnType, parameterTypes, inOutFlags, objType, isConstMethod) )
  3263. {
  3264. found = true;
  3265. break;
  3266. }
  3267. }
  3268. }
  3269. if( !found )
  3270. {
  3271. asCString str;
  3272. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, objType->GetName());
  3273. WriteError(str, file, node);
  3274. }
  3275. // Free the default args
  3276. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  3277. if( defaultArgs[n] )
  3278. asDELETE(defaultArgs[n], asCString);
  3279. node->Destroy(engine);
  3280. return 0;
  3281. }
  3282. // Check for name conflicts
  3283. if( !isConstructor && !isDestructor )
  3284. {
  3285. if( objType )
  3286. {
  3287. CheckNameConflictMember(objType, name.AddressOf(), node, file, false);
  3288. if( name == objType->name )
  3289. WriteError(TXT_METHOD_CANT_HAVE_NAME_OF_CLASS, file, node);
  3290. }
  3291. else
  3292. CheckNameConflict(name.AddressOf(), node, file, ns);
  3293. }
  3294. else
  3295. {
  3296. if( isMixin )
  3297. {
  3298. // Mixins cannot implement constructors/destructors
  3299. WriteError(TXT_MIXIN_CANNOT_HAVE_CONSTRUCTOR, file, node);
  3300. // Free the default args
  3301. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  3302. if( defaultArgs[n] )
  3303. asDELETE(defaultArgs[n], asCString);
  3304. node->Destroy(engine);
  3305. return 0;
  3306. }
  3307. // Verify that the name of the constructor/destructor is the same as the class
  3308. if( name != objType->name )
  3309. WriteError(TXT_CONSTRUCTOR_NAME_ERROR, file, node);
  3310. if( isDestructor )
  3311. name = "~" + name;
  3312. }
  3313. isExistingShared = false;
  3314. int funcId = engine->GetNextScriptFunctionId();
  3315. if( !isInterface )
  3316. {
  3317. sFunctionDescription *func = asNEW(sFunctionDescription);
  3318. if( func == 0 )
  3319. {
  3320. // Free the default args
  3321. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  3322. if( defaultArgs[n] )
  3323. asDELETE(defaultArgs[n], asCString);
  3324. return asOUT_OF_MEMORY;
  3325. }
  3326. functions.PushLast(func);
  3327. func->script = file;
  3328. func->node = node;
  3329. func->name = name;
  3330. func->objType = objType;
  3331. func->funcId = funcId;
  3332. func->isExistingShared = false;
  3333. func->paramNames = parameterNames;
  3334. if( isShared )
  3335. {
  3336. // Look for a pre-existing shared function with the same signature
  3337. for( asUINT n = 0; n < engine->scriptFunctions.GetLength(); n++ )
  3338. {
  3339. asCScriptFunction *f = engine->scriptFunctions[n];
  3340. if( f &&
  3341. f->isShared &&
  3342. f->name == name &&
  3343. f->nameSpace == ns &&
  3344. f->objectType == objType &&
  3345. f->IsSignatureExceptNameEqual(returnType, parameterTypes, inOutFlags, 0, false) )
  3346. {
  3347. funcId = func->funcId = f->id;
  3348. isExistingShared = func->isExistingShared = true;
  3349. break;
  3350. }
  3351. }
  3352. }
  3353. }
  3354. // Destructors may not have any parameters
  3355. if( isDestructor && parameterTypes.GetLength() > 0 )
  3356. WriteError(TXT_DESTRUCTOR_MAY_NOT_HAVE_PARM, file, node);
  3357. // If a function, class, or interface is shared then only shared types may be used in the signature
  3358. if( (objType && objType->IsShared()) || isShared )
  3359. {
  3360. asCObjectType *ot = returnType.GetObjectType();
  3361. if( ot && !ot->IsShared() )
  3362. {
  3363. asCString msg;
  3364. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  3365. WriteError(msg, file, node);
  3366. }
  3367. for( asUINT p = 0; p < parameterTypes.GetLength(); ++p )
  3368. {
  3369. asCObjectType *ot = parameterTypes[p].GetObjectType();
  3370. if( ot && !ot->IsShared() )
  3371. {
  3372. asCString msg;
  3373. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  3374. WriteError(msg, file, node);
  3375. }
  3376. }
  3377. }
  3378. // Check that the same function hasn't been registered already in the namespace
  3379. asCArray<int> funcs;
  3380. if( objType )
  3381. GetObjectMethodDescriptions(name.AddressOf(), objType, funcs, false);
  3382. else
  3383. GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  3384. for( asUINT n = 0; n < funcs.GetLength(); ++n )
  3385. {
  3386. asCScriptFunction *func = GetFunctionDescription(funcs[n]);
  3387. if( func->IsSignatureExceptNameAndReturnTypeEqual(parameterTypes, inOutFlags, objType, isConstMethod) )
  3388. {
  3389. if( isMixin )
  3390. {
  3391. // Clean up the memory, as the function will not be registered
  3392. if( node )
  3393. node->Destroy(engine);
  3394. sFunctionDescription *func = functions.PopLast();
  3395. asDELETE(func, sFunctionDescription);
  3396. // Free the default args
  3397. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  3398. if( defaultArgs[n] )
  3399. asDELETE(defaultArgs[n], asCString);
  3400. return 0;
  3401. }
  3402. WriteError(TXT_FUNCTION_ALREADY_EXIST, file, node);
  3403. break;
  3404. }
  3405. }
  3406. // Register the function
  3407. if( isExistingShared )
  3408. {
  3409. // Delete the default args as they won't be used anymore
  3410. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  3411. if( defaultArgs[n] )
  3412. asDELETE(defaultArgs[n], asCString);
  3413. asCScriptFunction *f = engine->scriptFunctions[funcId];
  3414. module->AddScriptFunction(f);
  3415. // TODO: clean up: This should be done by AddScriptFunction() itself
  3416. module->globalFunctions.Put(f);
  3417. f->AddRef();
  3418. }
  3419. else
  3420. {
  3421. int row = 0, col = 0;
  3422. if( node )
  3423. file->ConvertPosToRowCol(node->tokenPos, &row, &col);
  3424. module->AddScriptFunction(file->idx, (row&0xFFFFF)|((col&0xFFF)<<20), funcId, name, returnType, parameterTypes, parameterNames, inOutFlags, defaultArgs, isInterface, objType, isConstMethod, isGlobalFunction, isPrivate, isFinal, isOverride, isShared, ns);
  3425. }
  3426. // Make sure the default args are declared correctly
  3427. ValidateDefaultArgs(file, node, engine->scriptFunctions[funcId]);
  3428. if( objType )
  3429. {
  3430. asASSERT( !isExistingShared );
  3431. engine->scriptFunctions[funcId]->AddRef();
  3432. if( isConstructor )
  3433. {
  3434. int factoryId = engine->GetNextScriptFunctionId();
  3435. if( parameterTypes.GetLength() == 0 )
  3436. {
  3437. // Overload the default constructor
  3438. engine->scriptFunctions[objType->beh.construct]->Release();
  3439. objType->beh.construct = funcId;
  3440. objType->beh.constructors[0] = funcId;
  3441. // Register the default factory as well
  3442. engine->scriptFunctions[objType->beh.factory]->Release();
  3443. objType->beh.factory = factoryId;
  3444. objType->beh.factories[0] = factoryId;
  3445. }
  3446. else
  3447. {
  3448. objType->beh.constructors.PushLast(funcId);
  3449. // Register the factory as well
  3450. objType->beh.factories.PushLast(factoryId);
  3451. }
  3452. // We must copy the default arg strings to avoid deleting the same object multiple times
  3453. for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
  3454. if( defaultArgs[n] )
  3455. defaultArgs[n] = asNEW(asCString)(*defaultArgs[n]);
  3456. asCDataType dt = asCDataType::CreateObjectHandle(objType, false);
  3457. module->AddScriptFunction(file->idx, engine->scriptFunctions[funcId]->scriptData->declaredAt, factoryId, name, dt, parameterTypes, parameterNames, inOutFlags, defaultArgs, false);
  3458. // If the object is shared, then the factory must also be marked as shared
  3459. if( objType->flags & asOBJ_SHARED )
  3460. engine->scriptFunctions[factoryId]->isShared = true;
  3461. // Add a dummy function to the builder so that it doesn't mix up the fund Ids
  3462. functions.PushLast(0);
  3463. // Compile the factory immediately
  3464. asCCompiler compiler(engine);
  3465. compiler.CompileFactory(this, file, engine->scriptFunctions[factoryId]);
  3466. engine->scriptFunctions[factoryId]->AddRef();
  3467. }
  3468. else if( isDestructor )
  3469. objType->beh.destruct = funcId;
  3470. else
  3471. {
  3472. // If the method is the assignment operator we need to replace the default implementation
  3473. asCScriptFunction *f = engine->scriptFunctions[funcId];
  3474. if( f->name == "opAssign" && f->parameterTypes.GetLength() == 1 &&
  3475. f->parameterTypes[0].GetObjectType() == f->objectType &&
  3476. (f->inOutFlags[0] & asTM_INREF) )
  3477. {
  3478. engine->scriptFunctions[objType->beh.copy]->Release();
  3479. objType->beh.copy = funcId;
  3480. f->AddRef();
  3481. }
  3482. objType->methods.PushLast(funcId);
  3483. }
  3484. }
  3485. // We need to delete the node already if this is an interface method
  3486. if( isInterface && node )
  3487. node->Destroy(engine);
  3488. return 0;
  3489. }
  3490. int asCBuilder::RegisterVirtualProperty(asCScriptNode *node, asCScriptCode *file, asCObjectType *objType, bool isInterface, bool isGlobalFunction, asSNameSpace *ns, bool isExistingShared)
  3491. {
  3492. if( engine->ep.propertyAccessorMode != 2 )
  3493. {
  3494. WriteError(TXT_PROPERTY_ACCESSOR_DISABLED, file, node);
  3495. node->Destroy(engine);
  3496. return 0;
  3497. }
  3498. asASSERT( (objType && ns == 0) || isGlobalFunction );
  3499. if( ns == 0 )
  3500. {
  3501. if( objType )
  3502. ns = objType->nameSpace;
  3503. else
  3504. ns = engine->nameSpaces[0];
  3505. }
  3506. bool isPrivate = false;
  3507. asCString emulatedName;
  3508. asCDataType emulatedType;
  3509. asCScriptNode *mainNode = node;
  3510. node = node->firstChild;
  3511. if( !isGlobalFunction && node->tokenType == ttPrivate )
  3512. {
  3513. isPrivate = true;
  3514. node = node->next;
  3515. }
  3516. emulatedType = CreateDataTypeFromNode(node, file, ns);
  3517. emulatedType = ModifyDataTypeFromNode(emulatedType, node->next, file, 0, 0);
  3518. node = node->next->next;
  3519. emulatedName.Assign(&file->code[node->tokenPos], node->tokenLength);
  3520. if( node->next == 0 )
  3521. WriteError(TXT_PROPERTY_WITHOUT_ACCESSOR, file, node);
  3522. node = node->next;
  3523. while( node )
  3524. {
  3525. asCScriptNode *next = node->next;
  3526. asCScriptNode *funcNode = 0;
  3527. bool success = false;
  3528. bool isConst = false;
  3529. bool isFinal = false;
  3530. bool isOverride = false;
  3531. asCDataType returnType;
  3532. asCArray<asCString> paramNames;
  3533. asCArray<asCDataType> paramTypes;
  3534. asCArray<asETypeModifiers> paramModifiers;
  3535. asCArray<asCString*> defaultArgs;
  3536. asCString name;
  3537. // TODO: getset: Allow private for individual property accessors
  3538. // TODO: getset: If the accessor uses its own name, then the property should be automatically declared
  3539. if( node->firstChild->nodeType == snIdentifier && file->TokenEquals(node->firstChild->tokenPos, node->firstChild->tokenLength, GET_TOKEN) )
  3540. {
  3541. funcNode = node->firstChild->next;
  3542. if( funcNode && funcNode->tokenType == ttConst )
  3543. {
  3544. isConst = true;
  3545. funcNode = funcNode->next;
  3546. }
  3547. while( funcNode && funcNode->nodeType != snStatementBlock )
  3548. {
  3549. if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, FINAL_TOKEN) )
  3550. isFinal = true;
  3551. else if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, OVERRIDE_TOKEN) )
  3552. isOverride = true;
  3553. funcNode = funcNode->next;
  3554. }
  3555. if( funcNode )
  3556. funcNode->DisconnectParent();
  3557. if( funcNode == 0 && (objType == 0 || !objType->IsInterface()) )
  3558. {
  3559. // TODO: getset: If no implementation is supplied the builder should provide an automatically generated implementation
  3560. // The compiler needs to be able to handle the different types, primitive, value type, and handle
  3561. // The code is also different for global property accessors
  3562. WriteError(TXT_PROPERTY_ACCESSOR_MUST_BE_IMPLEMENTED, file, node);
  3563. }
  3564. // Setup the signature for the get accessor method
  3565. returnType = emulatedType;
  3566. name = "get_" + emulatedName;
  3567. success = true;
  3568. }
  3569. else if( node->firstChild->nodeType == snIdentifier && file->TokenEquals(node->firstChild->tokenPos, node->firstChild->tokenLength, SET_TOKEN) )
  3570. {
  3571. funcNode = node->firstChild->next;
  3572. if( funcNode && funcNode->tokenType == ttConst )
  3573. {
  3574. isConst = true;
  3575. funcNode = funcNode->next;
  3576. }
  3577. while( funcNode && funcNode->nodeType != snStatementBlock )
  3578. {
  3579. if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, FINAL_TOKEN) )
  3580. isFinal = true;
  3581. else if( funcNode->tokenType == ttIdentifier && file->TokenEquals(funcNode->tokenPos, funcNode->tokenLength, OVERRIDE_TOKEN) )
  3582. isOverride = true;
  3583. funcNode = funcNode->next;
  3584. }
  3585. if( funcNode )
  3586. funcNode->DisconnectParent();
  3587. if( funcNode == 0 && (objType == 0 || !objType->IsInterface()) )
  3588. WriteError(TXT_PROPERTY_ACCESSOR_MUST_BE_IMPLEMENTED, file, node);
  3589. // Setup the signature for the set accessor method
  3590. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  3591. paramModifiers.PushLast(asTM_NONE);
  3592. paramNames.PushLast("value");
  3593. paramTypes.PushLast(emulatedType);
  3594. defaultArgs.PushLast(0);
  3595. name = "set_" + emulatedName;
  3596. success = true;
  3597. }
  3598. else
  3599. WriteError(TXT_UNRECOGNIZED_VIRTUAL_PROPERTY_NODE, file, node);
  3600. if( success )
  3601. {
  3602. if( !isExistingShared )
  3603. RegisterScriptFunction(funcNode, file, objType, isInterface, isGlobalFunction, ns, false, false, name, returnType, paramNames, paramTypes, paramModifiers, defaultArgs, isConst, false, false, isPrivate, isOverride, isFinal, false);
  3604. else
  3605. {
  3606. // Free the funcNode as it won't be used
  3607. if( funcNode ) funcNode->Destroy(engine);
  3608. // Should validate that the function really exists in the class/interface
  3609. bool found = false;
  3610. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  3611. {
  3612. asCScriptFunction *func = engine->scriptFunctions[objType->methods[n]];
  3613. if( func->name == name &&
  3614. func->IsSignatureExceptNameEqual(returnType, paramTypes, paramModifiers, objType, isConst) )
  3615. {
  3616. found = true;
  3617. break;
  3618. }
  3619. }
  3620. if( !found )
  3621. {
  3622. asCString str;
  3623. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, objType->GetName());
  3624. WriteError(str, file, node);
  3625. }
  3626. }
  3627. }
  3628. node = next;
  3629. };
  3630. mainNode->Destroy(engine);
  3631. return 0;
  3632. }
  3633. int asCBuilder::RegisterImportedFunction(int importID, asCScriptNode *node, asCScriptCode *file, asSNameSpace *ns)
  3634. {
  3635. asCString name;
  3636. asCDataType returnType;
  3637. asCArray<asCString> parameterNames;
  3638. asCArray<asCDataType> parameterTypes;
  3639. asCArray<asETypeModifiers> inOutFlags;
  3640. asCArray<asCString *> defaultArgs;
  3641. bool isConstMethod, isOverride, isFinal, isConstructor, isDestructor, isPrivate, isShared;
  3642. if( ns == 0 )
  3643. ns = engine->nameSpaces[0];
  3644. GetParsedFunctionDetails(node->firstChild, file, 0, name, returnType, parameterNames, parameterTypes, inOutFlags, defaultArgs, isConstMethod, isConstructor, isDestructor, isPrivate, isOverride, isFinal, isShared, ns);
  3645. CheckNameConflict(name.AddressOf(), node, file, ns);
  3646. // Check that the same function hasn't been registered already in the namespace
  3647. asCArray<int> funcs;
  3648. GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  3649. for( asUINT n = 0; n < funcs.GetLength(); ++n )
  3650. {
  3651. asCScriptFunction *func = GetFunctionDescription(funcs[n]);
  3652. if( func->IsSignatureExceptNameAndReturnTypeEqual(parameterTypes, inOutFlags, 0, false) )
  3653. {
  3654. WriteError(TXT_FUNCTION_ALREADY_EXIST, file, node);
  3655. break;
  3656. }
  3657. }
  3658. // Read the module name as well
  3659. asCScriptNode *nd = node->lastChild;
  3660. asASSERT( nd->nodeType == snConstant && nd->tokenType == ttStringConstant );
  3661. asCString moduleName;
  3662. moduleName.Assign(&file->code[nd->tokenPos+1], nd->tokenLength-2);
  3663. node->Destroy(engine);
  3664. // Register the function
  3665. module->AddImportedFunction(importID, name, returnType, parameterTypes, inOutFlags, defaultArgs, ns, moduleName);
  3666. return 0;
  3667. }
  3668. asCScriptFunction *asCBuilder::GetFunctionDescription(int id)
  3669. {
  3670. // TODO: import: This should be improved when the imported functions are removed
  3671. // Get the description from the engine
  3672. if( (id & FUNC_IMPORTED) == 0 )
  3673. return engine->scriptFunctions[id];
  3674. else
  3675. return engine->importedFunctions[id & ~FUNC_IMPORTED]->importedFunctionSignature;
  3676. }
  3677. void asCBuilder::GetFunctionDescriptions(const char *name, asCArray<int> &funcs, asSNameSpace *ns)
  3678. {
  3679. asUINT n;
  3680. // Get the script declared global functions
  3681. const asCArray<unsigned int> &idxs = module->globalFunctions.GetIndexes(ns, name);
  3682. for( n = 0; n < idxs.GetLength(); n++ )
  3683. {
  3684. const asCScriptFunction *f = module->globalFunctions.Get(idxs[n]);
  3685. asASSERT( f->objectType == 0 );
  3686. funcs.PushLast(f->id);
  3687. }
  3688. // Add the imported functions
  3689. // TODO: optimize: Linear search: This is probably not that critial. Also bindInformation will probably be removed in near future
  3690. for( n = 0; n < module->bindInformations.GetLength(); n++ )
  3691. {
  3692. if( module->bindInformations[n]->importedFunctionSignature->name == name )
  3693. funcs.PushLast(module->bindInformations[n]->importedFunctionSignature->id);
  3694. }
  3695. // Add the registered global functions
  3696. const asCArray<unsigned int> &idxs2 = engine->registeredGlobalFuncs.GetIndexes(ns, name);
  3697. for( n = 0; n < idxs2.GetLength(); n++ )
  3698. {
  3699. asCScriptFunction *f = engine->registeredGlobalFuncs.Get(idxs2[n]);
  3700. // Verify if the module has access to the function
  3701. if( module->accessMask & f->accessMask )
  3702. {
  3703. funcs.PushLast(f->id);
  3704. }
  3705. }
  3706. }
  3707. void asCBuilder::GetObjectMethodDescriptions(const char *name, asCObjectType *objectType, asCArray<int> &methods, bool objIsConst, const asCString &scope)
  3708. {
  3709. if( scope != "" )
  3710. {
  3711. // Find the base class with the specified scope
  3712. while( objectType && objectType->name != scope )
  3713. objectType = objectType->derivedFrom;
  3714. // If the scope is not any of the base classes, then return no methods
  3715. if( objectType == 0 )
  3716. return;
  3717. }
  3718. // TODO: optimize: Improve linear search
  3719. if( objIsConst )
  3720. {
  3721. // Only add const methods to the list
  3722. for( asUINT n = 0; n < objectType->methods.GetLength(); n++ )
  3723. {
  3724. if( engine->scriptFunctions[objectType->methods[n]]->name == name &&
  3725. engine->scriptFunctions[objectType->methods[n]]->isReadOnly )
  3726. {
  3727. // When the scope is defined the returned methods should be the true methods, not the virtual method stubs
  3728. if( scope == "" )
  3729. methods.PushLast(engine->scriptFunctions[objectType->methods[n]]->id);
  3730. else
  3731. {
  3732. asCScriptFunction *virtFunc = engine->scriptFunctions[objectType->methods[n]];
  3733. asCScriptFunction *realFunc = objectType->virtualFunctionTable[virtFunc->vfTableIdx];
  3734. methods.PushLast(realFunc->id);
  3735. }
  3736. }
  3737. }
  3738. }
  3739. else
  3740. {
  3741. // TODO: Prefer non-const over const
  3742. for( asUINT n = 0; n < objectType->methods.GetLength(); n++ )
  3743. {
  3744. if( engine->scriptFunctions[objectType->methods[n]]->name == name )
  3745. {
  3746. // When the scope is defined the returned methods should be the true methods, not the virtual method stubs
  3747. if( scope == "" )
  3748. methods.PushLast(engine->scriptFunctions[objectType->methods[n]]->id);
  3749. else
  3750. {
  3751. asCScriptFunction *virtFunc = engine->scriptFunctions[objectType->methods[n]];
  3752. asCScriptFunction *realFunc = objectType->virtualFunctionTable[virtFunc->vfTableIdx];
  3753. methods.PushLast(realFunc->id);
  3754. }
  3755. }
  3756. }
  3757. }
  3758. }
  3759. #endif
  3760. void asCBuilder::WriteInfo(const asCString &scriptname, const asCString &message, int r, int c, bool pre)
  3761. {
  3762. // Need to store the pre message in a structure
  3763. if( pre )
  3764. {
  3765. engine->preMessage.isSet = true;
  3766. engine->preMessage.c = c;
  3767. engine->preMessage.r = r;
  3768. engine->preMessage.message = message;
  3769. engine->preMessage.scriptname = scriptname;
  3770. }
  3771. else
  3772. {
  3773. engine->preMessage.isSet = false;
  3774. if( !silent )
  3775. engine->WriteMessage(scriptname.AddressOf(), r, c, asMSGTYPE_INFORMATION, message.AddressOf());
  3776. }
  3777. }
  3778. void asCBuilder::WriteInfo(const asCString &message, asCScriptCode *file, asCScriptNode *node)
  3779. {
  3780. int r = 0, c = 0;
  3781. if( node )
  3782. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  3783. WriteInfo(file->name, message, r, c, false);
  3784. }
  3785. void asCBuilder::WriteError(const asCString &message, asCScriptCode *file, asCScriptNode *node)
  3786. {
  3787. int r = 0, c = 0;
  3788. if( node && file )
  3789. file->ConvertPosToRowCol(node->tokenPos, &r, &c);
  3790. WriteError(file ? file->name : asCString(""), message, r, c);
  3791. }
  3792. void asCBuilder::WriteError(const asCString &scriptname, const asCString &message, int r, int c)
  3793. {
  3794. numErrors++;
  3795. if( !silent )
  3796. engine->WriteMessage(scriptname.AddressOf(), r, c, asMSGTYPE_ERROR, message.AddressOf());
  3797. }
  3798. void asCBuilder::WriteWarning(const asCString &scriptname, const asCString &message, int r, int c)
  3799. {
  3800. if( engine->ep.compilerWarnings )
  3801. {
  3802. numWarnings++;
  3803. if( !silent )
  3804. engine->WriteMessage(scriptname.AddressOf(), r, c, asMSGTYPE_WARNING, message.AddressOf());
  3805. }
  3806. }
  3807. asCString asCBuilder::GetScopeFromNode(asCScriptNode *node, asCScriptCode *script, asCScriptNode **next)
  3808. {
  3809. asCString scope;
  3810. asCScriptNode *sn = node;
  3811. if( sn->tokenType == ttScope )
  3812. {
  3813. scope = "::";
  3814. sn = sn->next;
  3815. }
  3816. while( sn && sn->next && sn->next->tokenType == ttScope )
  3817. {
  3818. asCString tmp;
  3819. tmp.Assign(&script->code[sn->tokenPos], sn->tokenLength);
  3820. if( scope != "" && scope != "::" )
  3821. scope += "::";
  3822. scope += tmp;
  3823. sn = sn->next->next;
  3824. }
  3825. if( next )
  3826. *next = sn;
  3827. return scope;
  3828. }
  3829. asSNameSpace *asCBuilder::GetNameSpaceFromNode(asCScriptNode *node, asCScriptCode *script, asSNameSpace *implicitNs, asCScriptNode **next)
  3830. {
  3831. asCString scope = GetScopeFromNode(node, script, next);
  3832. asSNameSpace *ns = implicitNs;
  3833. if( scope == "::" )
  3834. ns = engine->nameSpaces[0];
  3835. else if( scope != "" )
  3836. {
  3837. ns = engine->FindNameSpace(scope.AddressOf());
  3838. if( ns == 0 )
  3839. {
  3840. asCString msg;
  3841. msg.Format(TXT_NAMESPACE_s_DOESNT_EXIST, scope.AddressOf());
  3842. WriteError(msg, script, node);
  3843. }
  3844. }
  3845. return ns;
  3846. }
  3847. asSNameSpace *asCBuilder::GetParentNameSpace(asSNameSpace *ns)
  3848. {
  3849. if( ns == 0 ) return 0;
  3850. if( ns == engine->nameSpaces[0] ) return 0;
  3851. asCString scope = ns->name;
  3852. int pos = scope.FindLast("::");
  3853. if( pos >= 0 )
  3854. {
  3855. scope = scope.SubString(0, pos);
  3856. return engine->FindNameSpace(scope.AddressOf());
  3857. }
  3858. return engine->nameSpaces[0];
  3859. }
  3860. asCDataType asCBuilder::CreateDataTypeFromNode(asCScriptNode *node, asCScriptCode *file, asSNameSpace *implicitNamespace, bool acceptHandleForScope, asCObjectType *currentType)
  3861. {
  3862. asASSERT(node->nodeType == snDataType);
  3863. asCDataType dt;
  3864. asCScriptNode *n = node->firstChild;
  3865. bool isConst = false;
  3866. bool isImplicitHandle = false;
  3867. if( n->tokenType == ttConst )
  3868. {
  3869. isConst = true;
  3870. n = n->next;
  3871. }
  3872. // Determine namespace
  3873. asSNameSpace *ns = GetNameSpaceFromNode(n, file, implicitNamespace, &n);
  3874. if( ns == 0 )
  3875. {
  3876. // The namespace doesn't exist. Return a dummy type instead.
  3877. dt = asCDataType::CreatePrimitive(ttInt, false);
  3878. return dt;
  3879. }
  3880. if( n->tokenType == ttIdentifier )
  3881. {
  3882. bool found = false;
  3883. asCScriptNode *nameToken = n;
  3884. asCString str;
  3885. str.Assign(&file->code[n->tokenPos], n->tokenLength);
  3886. // Recursively search parent namespaces for matching type
  3887. while( ns && !found )
  3888. {
  3889. asCObjectType *ot = 0;
  3890. // If this is for a template type, then we must first determine if the
  3891. // identifier matches any of the template subtypes
  3892. if( currentType && (currentType->flags & asOBJ_TEMPLATE))
  3893. {
  3894. for( asUINT subtypeIndex = 0; subtypeIndex < currentType->templateSubTypes.GetLength(); subtypeIndex++)
  3895. {
  3896. asCObjectType *type = currentType->templateSubTypes[subtypeIndex].GetObjectType();
  3897. if( type && str == type->name )
  3898. {
  3899. ot = type;
  3900. break;
  3901. }
  3902. }
  3903. }
  3904. if( ot == 0 )
  3905. ot = GetObjectType(str.AddressOf(), ns);
  3906. if( ot == 0 && !module && currentType )
  3907. ot = GetObjectTypeFromTypesKnownByObject(str.AddressOf(), currentType);
  3908. if( ot )
  3909. {
  3910. found = true;
  3911. if( ot->flags & asOBJ_IMPLICIT_HANDLE )
  3912. isImplicitHandle = true;
  3913. // Make sure the module has access to the object type
  3914. if( !module || (module->accessMask & ot->accessMask) )
  3915. {
  3916. if(asOBJ_TYPEDEF == (ot->flags & asOBJ_TYPEDEF))
  3917. {
  3918. // TODO: typedef: A typedef should be considered different from the original type (though with implicit conversions between the two)
  3919. // Create primitive data type based on object flags
  3920. dt = ot->templateSubTypes[0];
  3921. dt.MakeReadOnly(isConst);
  3922. }
  3923. else
  3924. {
  3925. if( ot->flags & asOBJ_TEMPLATE )
  3926. {
  3927. // Check if the subtype is a type or the template's subtype
  3928. // if it is the template's subtype then this is the actual template type,
  3929. // orderwise it is a template instance.
  3930. // Only do this for application registered interface, as the
  3931. // scripts cannot implement templates.
  3932. asCArray<asCDataType> subTypes;
  3933. asUINT subtypeIndex;
  3934. while( n && n->next && n->next->nodeType == snDataType )
  3935. {
  3936. n = n->next;
  3937. asCDataType subType = CreateDataTypeFromNode(n, file, implicitNamespace, false, module ? 0 : ot);
  3938. subTypes.PushLast(subType);
  3939. if( subType.IsReadOnly() )
  3940. {
  3941. asCString msg;
  3942. msg.Format(TXT_TMPL_SUBTYPE_MUST_NOT_BE_READ_ONLY);
  3943. WriteError(msg, file, n);
  3944. // Return a dummy
  3945. return asCDataType::CreatePrimitive(ttInt, false);
  3946. }
  3947. }
  3948. if( subTypes.GetLength() != ot->templateSubTypes.GetLength() )
  3949. {
  3950. asCString msg;
  3951. msg.Format(TXT_TMPL_s_EXPECTS_d_SUBTYPES, ot->name.AddressOf(), int(ot->templateSubTypes.GetLength()));
  3952. WriteError(msg, file, nameToken);
  3953. // Return a dummy
  3954. return asCDataType::CreatePrimitive(ttInt, false);
  3955. }
  3956. asASSERT( subTypes.GetLength() == ot->templateSubTypes.GetLength() );
  3957. // Check if any of the given subtypes are different from the template's declared subtypes
  3958. bool isDifferent = false;
  3959. for( subtypeIndex = 0; subtypeIndex < subTypes.GetLength(); subtypeIndex++ )
  3960. {
  3961. if( subTypes[subtypeIndex].GetObjectType() != ot->templateSubTypes[subtypeIndex].GetObjectType() )
  3962. {
  3963. isDifferent = true;
  3964. break;
  3965. }
  3966. }
  3967. if( isDifferent )
  3968. {
  3969. // This is a template instance
  3970. // Need to find the correct object type
  3971. asCObjectType *otInstance = engine->GetTemplateInstanceType(ot, subTypes);
  3972. if( !otInstance )
  3973. {
  3974. asCString msg;
  3975. // TODO: Should name all subtypes
  3976. msg.Format(TXT_CANNOT_INSTANCIATE_TEMPLATE_s_WITH_s, ot->name.AddressOf(), subTypes[0].Format().AddressOf());
  3977. WriteError(msg, file, n);
  3978. }
  3979. ot = otInstance;
  3980. }
  3981. }
  3982. // Create object data type
  3983. if( ot )
  3984. dt = asCDataType::CreateObject(ot, isConst);
  3985. else
  3986. dt = asCDataType::CreatePrimitive(ttInt, isConst);
  3987. }
  3988. }
  3989. else
  3990. {
  3991. asCString msg;
  3992. msg.Format(TXT_TYPE_s_NOT_AVAILABLE_FOR_MODULE, (const char *)str.AddressOf());
  3993. WriteError(msg, file, n);
  3994. dt.SetTokenType(ttInt);
  3995. }
  3996. }
  3997. else if( ot == 0 )
  3998. {
  3999. // It can still be a function definition
  4000. asCScriptFunction *funcdef = GetFuncDef(str.AddressOf());
  4001. if( funcdef )
  4002. {
  4003. dt = asCDataType::CreateFuncDef(funcdef);
  4004. found = true;
  4005. }
  4006. }
  4007. if( !found )
  4008. {
  4009. // Try to find it in the parent namespace
  4010. ns = GetParentNameSpace(ns);
  4011. }
  4012. }
  4013. if( !found )
  4014. {
  4015. asCString msg;
  4016. msg.Format(TXT_IDENTIFIER_s_NOT_DATA_TYPE, (const char *)str.AddressOf());
  4017. WriteError(msg, file, n);
  4018. dt = asCDataType::CreatePrimitive(ttInt, isConst);
  4019. return dt;
  4020. }
  4021. }
  4022. else if( n->tokenType == ttAuto )
  4023. {
  4024. dt = asCDataType::CreateAuto(isConst);
  4025. }
  4026. else
  4027. {
  4028. // Create primitive data type
  4029. dt = asCDataType::CreatePrimitive(n->tokenType, isConst);
  4030. }
  4031. // Determine array dimensions and object handles
  4032. n = n->next;
  4033. while( n && (n->tokenType == ttOpenBracket || n->tokenType == ttHandle) )
  4034. {
  4035. if( n->tokenType == ttOpenBracket )
  4036. {
  4037. // Make sure the sub type can be instanciated
  4038. if( !dt.CanBeInstanciated() )
  4039. {
  4040. asCString str;
  4041. // TODO: Change to "Array sub type cannot be 'type'"
  4042. str.Format(TXT_DATA_TYPE_CANT_BE_s, dt.Format().AddressOf());
  4043. WriteError(str, file, n);
  4044. }
  4045. // Make the type an array (or multidimensional array)
  4046. if( dt.MakeArray(engine) < 0 )
  4047. {
  4048. WriteError(TXT_NO_DEFAULT_ARRAY_TYPE, file, n);
  4049. break;
  4050. }
  4051. }
  4052. else
  4053. {
  4054. // Make the type a handle
  4055. if( dt.IsObjectHandle() )
  4056. {
  4057. WriteError(TXT_HANDLE_OF_HANDLE_IS_NOT_ALLOWED, file, n);
  4058. break;
  4059. }
  4060. else if( dt.MakeHandle(true, acceptHandleForScope) < 0 )
  4061. {
  4062. WriteError(TXT_OBJECT_HANDLE_NOT_SUPPORTED, file, n);
  4063. break;
  4064. }
  4065. }
  4066. n = n->next;
  4067. }
  4068. if( isImplicitHandle )
  4069. {
  4070. // Make the type a handle
  4071. if( dt.MakeHandle(true, acceptHandleForScope) < 0 )
  4072. WriteError(TXT_OBJECT_HANDLE_NOT_SUPPORTED, file, n);
  4073. }
  4074. return dt;
  4075. }
  4076. asCDataType asCBuilder::ModifyDataTypeFromNode(const asCDataType &type, asCScriptNode *node, asCScriptCode *file, asETypeModifiers *inOutFlags, bool *autoHandle)
  4077. {
  4078. asCDataType dt = type;
  4079. if( inOutFlags ) *inOutFlags = asTM_NONE;
  4080. // Is the argument sent by reference?
  4081. asCScriptNode *n = node->firstChild;
  4082. if( n && n->tokenType == ttAmp )
  4083. {
  4084. dt.MakeReference(true);
  4085. n = n->next;
  4086. if( n )
  4087. {
  4088. if( inOutFlags )
  4089. {
  4090. if( n->tokenType == ttIn )
  4091. *inOutFlags = asTM_INREF;
  4092. else if( n->tokenType == ttOut )
  4093. *inOutFlags = asTM_OUTREF;
  4094. else if( n->tokenType == ttInOut )
  4095. *inOutFlags = asTM_INOUTREF;
  4096. else
  4097. asASSERT(false);
  4098. }
  4099. n = n->next;
  4100. }
  4101. else
  4102. {
  4103. if( inOutFlags )
  4104. *inOutFlags = asTM_INOUTREF; // ttInOut
  4105. }
  4106. if( !engine->ep.allowUnsafeReferences &&
  4107. inOutFlags && *inOutFlags == asTM_INOUTREF )
  4108. {
  4109. // Verify that the base type support &inout parameter types
  4110. if( !dt.IsObject() || dt.IsObjectHandle() || !((dt.GetObjectType()->flags & asOBJ_NOCOUNT) || (dt.GetObjectType()->beh.addref && dt.GetObjectType()->beh.release)) )
  4111. WriteError(TXT_ONLY_OBJECTS_MAY_USE_REF_INOUT, file, node->firstChild);
  4112. }
  4113. }
  4114. if( autoHandle ) *autoHandle = false;
  4115. if( n && n->tokenType == ttPlus )
  4116. {
  4117. // Autohandles are not supported for types with NOCOUNT
  4118. // If the type is not a handle then there was an error with building the type, but
  4119. // this error would already have been reported so no need to report another error here
  4120. if( dt.IsObjectHandle() && (dt.GetObjectType()->flags & asOBJ_NOCOUNT) )
  4121. WriteError(TXT_AUTOHANDLE_CANNOT_BE_USED_FOR_NOCOUNT, file, node->firstChild);
  4122. if( autoHandle ) *autoHandle = true;
  4123. }
  4124. return dt;
  4125. }
  4126. asCObjectType *asCBuilder::GetObjectType(const char *type, asSNameSpace *ns)
  4127. {
  4128. asCObjectType *ot = engine->GetRegisteredObjectType(type, ns);
  4129. if( !ot && module )
  4130. ot = module->GetObjectType(type, ns);
  4131. return ot;
  4132. }
  4133. #ifndef AS_NO_COMPILER
  4134. // This function will return true if there are any types in the engine or module
  4135. // with the given name. The namespace is ignored in this verification.
  4136. bool asCBuilder::DoesTypeExist(const asCString &type)
  4137. {
  4138. asUINT n;
  4139. // This function is only used when parsing expressions for building bytecode
  4140. // and this is only done after all types are known. For this reason the types
  4141. // can be safely cached in a map for quick lookup. Once the builder is released
  4142. // the cache will also be destroyed thus avoiding unnecessary memory consumption.
  4143. if( !hasCachedKnownTypes )
  4144. {
  4145. // Only do this once
  4146. hasCachedKnownTypes = true;
  4147. // Add registered object types
  4148. asSMapNode<asSNameSpaceNamePair, asCObjectType*> *cursor;
  4149. engine->allRegisteredTypes.MoveFirst(&cursor);
  4150. while( cursor )
  4151. {
  4152. if( !knownTypes.MoveTo(0, cursor->key.name) )
  4153. knownTypes.Insert(cursor->key.name, true);
  4154. engine->allRegisteredTypes.MoveNext(&cursor, cursor);
  4155. }
  4156. // Add registered funcdefs
  4157. for( n = 0; n < engine->registeredFuncDefs.GetLength(); n++ )
  4158. if( !knownTypes.MoveTo(0, engine->registeredFuncDefs[n]->name) )
  4159. knownTypes.Insert(engine->registeredFuncDefs[n]->name, true);
  4160. if( module )
  4161. {
  4162. // Add script classes and interfaces
  4163. for( n = 0; n < module->classTypes.GetLength(); n++ )
  4164. if( !knownTypes.MoveTo(0, module->classTypes[n]->name) )
  4165. knownTypes.Insert(module->classTypes[n]->name, true);
  4166. // Add script enums
  4167. for( n = 0; n < module->enumTypes.GetLength(); n++ )
  4168. if( !knownTypes.MoveTo(0, module->enumTypes[n]->name) )
  4169. knownTypes.Insert(module->enumTypes[n]->name, true);
  4170. // Add script typedefs
  4171. for( n = 0; n < module->typeDefs.GetLength(); n++ )
  4172. if( !knownTypes.MoveTo(0, module->typeDefs[n]->name) )
  4173. knownTypes.Insert(module->typeDefs[n]->name, true);
  4174. // Add script funcdefs
  4175. for( n = 0; n < module->funcDefs.GetLength(); n++ )
  4176. if( !knownTypes.MoveTo(0, module->funcDefs[n]->name) )
  4177. knownTypes.Insert(module->funcDefs[n]->name, true);
  4178. }
  4179. }
  4180. // Check if the type is known
  4181. return knownTypes.MoveTo(0, type);
  4182. }
  4183. #endif
  4184. asCObjectType *asCBuilder::GetObjectTypeFromTypesKnownByObject(const char *type, asCObjectType *currentType)
  4185. {
  4186. if( currentType->name == type )
  4187. return currentType;
  4188. asUINT n;
  4189. asCObjectType *found = 0;
  4190. for( n = 0; found == 0 && n < currentType->properties.GetLength(); n++ )
  4191. if( currentType->properties[n]->type.GetObjectType() &&
  4192. currentType->properties[n]->type.GetObjectType()->name == type )
  4193. found = currentType->properties[n]->type.GetObjectType();
  4194. for( n = 0; found == 0 && n < currentType->methods.GetLength(); n++ )
  4195. {
  4196. asCScriptFunction *func = engine->scriptFunctions[currentType->methods[n]];
  4197. if( func->returnType.GetObjectType() &&
  4198. func->returnType.GetObjectType()->name == type )
  4199. found = func->returnType.GetObjectType();
  4200. for( asUINT f = 0; found == 0 && f < func->parameterTypes.GetLength(); f++ )
  4201. if( func->parameterTypes[f].GetObjectType() &&
  4202. func->parameterTypes[f].GetObjectType()->name == type )
  4203. found = func->parameterTypes[f].GetObjectType();
  4204. }
  4205. if( found )
  4206. {
  4207. // In case we find a template instance it mustn't be returned
  4208. // because it is not known if the subtype is really matching
  4209. if( found->flags & asOBJ_TEMPLATE )
  4210. return 0;
  4211. }
  4212. return found;
  4213. }
  4214. asCScriptFunction *asCBuilder::GetFuncDef(const char *type)
  4215. {
  4216. for( asUINT n = 0; n < engine->registeredFuncDefs.GetLength(); n++ )
  4217. // TODO: access: Only return the definitions that the module has access to
  4218. if( engine->registeredFuncDefs[n]->name == type )
  4219. return engine->registeredFuncDefs[n];
  4220. if( module )
  4221. {
  4222. for( asUINT n = 0; n < module->funcDefs.GetLength(); n++ )
  4223. if( module->funcDefs[n]->name == type )
  4224. return module->funcDefs[n];
  4225. }
  4226. return 0;
  4227. }
  4228. #ifndef AS_NO_COMPILER
  4229. int asCBuilder::GetEnumValueFromObjectType(asCObjectType *objType, const char *name, asCDataType &outDt, asDWORD &outValue)
  4230. {
  4231. if( !objType || !(objType->flags & asOBJ_ENUM) )
  4232. return 0;
  4233. for( asUINT n = 0; n < objType->enumValues.GetLength(); ++n )
  4234. {
  4235. if( objType->enumValues[n]->name == name )
  4236. {
  4237. outDt = asCDataType::CreateObject(objType, true);
  4238. outValue = objType->enumValues[n]->value;
  4239. return 1;
  4240. }
  4241. }
  4242. return 0;
  4243. }
  4244. int asCBuilder::GetEnumValue(const char *name, asCDataType &outDt, asDWORD &outValue, asSNameSpace *ns)
  4245. {
  4246. bool found = false;
  4247. // Search all available enum types
  4248. asUINT t;
  4249. for( t = 0; t < engine->registeredEnums.GetLength(); t++ )
  4250. {
  4251. asCObjectType *ot = engine->registeredEnums[t];
  4252. if( ns != ot->nameSpace ) continue;
  4253. // Don't bother with types the module doesn't have access to
  4254. if( (ot->accessMask & module->accessMask) == 0 )
  4255. continue;
  4256. if( GetEnumValueFromObjectType(ot, name, outDt, outValue) )
  4257. {
  4258. if( !found )
  4259. found = true;
  4260. else
  4261. {
  4262. // Found more than one value in different enum types
  4263. return 2;
  4264. }
  4265. }
  4266. }
  4267. for( t = 0; t < module->enumTypes.GetLength(); t++ )
  4268. {
  4269. asCObjectType *ot = module->enumTypes[t];
  4270. if( ns != ot->nameSpace ) continue;
  4271. if( GetEnumValueFromObjectType(ot, name, outDt, outValue) )
  4272. {
  4273. if( !found )
  4274. found = true;
  4275. else
  4276. {
  4277. // Found more than one value in different enum types
  4278. return 2;
  4279. }
  4280. }
  4281. }
  4282. if( found )
  4283. return 1;
  4284. // Didn't find any value
  4285. return 0;
  4286. }
  4287. #endif // AS_NO_COMPILER
  4288. END_AS_NAMESPACE