as_builder.cpp 175 KB

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