as_builder.cpp 144 KB

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