as_builder.cpp 186 KB

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