expr.bmx 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014
  1. ' Copyright (c) 2013-2026 Bruce A Henderson
  2. '
  3. ' Based on the public domain Monkey "trans" by Mark Sibly
  4. '
  5. ' This software is provided 'as-is', without any express or implied
  6. ' warranty. In no event will the authors be held liable for any damages
  7. ' arising from the use of this software.
  8. '
  9. ' Permission is granted to anyone to use this software for any purpose,
  10. ' including commercial applications, and to alter it and redistribute it
  11. ' freely, subject to the following restrictions:
  12. '
  13. ' 1. The origin of this software must not be misrepresented; you must not
  14. ' claim that you wrote the original software. If you use this software
  15. ' in a product, an acknowledgment in the product documentation would be
  16. ' appreciated but is not required.
  17. '
  18. ' 2. Altered source versions must be plainly marked as such, and must not be
  19. ' misrepresented as being the original software.
  20. '
  21. ' 3. This notice may not be removed or altered from any source
  22. ' distribution.
  23. '
  24. Type TExpr
  25. Field exprType:TType
  26. Field static:Int
  27. Method ToString$()
  28. Return "<TExpr>"
  29. End Method
  30. Method Copy:TExpr()
  31. InternalErr "TExpr.Copy"
  32. End Method
  33. Method Semant:TExpr(options:Int = 0)
  34. InternalErr "TExpr.Semant"
  35. End Method
  36. Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
  37. Err ToString()+" cannot be assigned to."
  38. End Method
  39. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  40. Err ToString()+" cannot be invoked."
  41. End Method
  42. Method SemantScope:TScopeDecl()
  43. Return Null
  44. End Method
  45. Method Eval$()
  46. Err ToString()+" cannot be statically evaluated."
  47. End Method
  48. Method EvalConst:TExpr()
  49. Local expr:TExpr = New TConstExpr.Create( exprType,Eval() ).Semant()
  50. If TStringType(TConstExpr(expr).ty) Then
  51. _appInstance.mapStringConsts(TConstExpr(expr).value)
  52. End If
  53. Return expr
  54. End Method
  55. Method Trans$()
  56. Todo
  57. End Method
  58. Method TransStmt$()
  59. Return Trans()
  60. End Method
  61. Method TransVar$()
  62. InternalErr "TExpr.TransVar"
  63. End Method
  64. 'semant and cast
  65. Method SemantAndCast:TExpr( ty:TType,castFlags:Int=0 )
  66. Local expr:TExpr=Semant()
  67. Return New TCastExpr.Create( ty,expr,castFlags ).Semant()
  68. End Method
  69. 'expr and ty already semanted!
  70. Method Cast:TExpr( ty:TType,castFlags:Int=0 )
  71. If Not exprType Then
  72. Semant()
  73. End If
  74. If exprType.EqualsType( ty ) Return Self
  75. Return New TCastExpr.Create( ty,Self,castFlags ).Semant()
  76. End Method
  77. Method SemantArgs:TExpr[]( args:TExpr[] )
  78. args=args[..]
  79. For Local i:Int=0 Until args.Length
  80. If args[i] Then
  81. If TIdentExpr(args[i]) Then
  82. TIdentExpr(args[i]).isArg = True
  83. End If
  84. args[i]=args[i].Semant()
  85. ' if an arg is a invocation without braces, it is *probably* a function pointer.
  86. If TInvokeExpr(args[i]) And Not TInvokeExpr(args[i]).invokedWithBraces Then
  87. ' but not if we've already processed it...
  88. If Not (TInvokeExpr(args[i]).decl.attrs & FUNC_PTR) Then
  89. TInvokeExpr(args[i]).exprType = New TFunctionPtrType
  90. Local cp:TDecl = TInvokeExpr(args[i]).decl
  91. cp.Semant
  92. If TFuncDecl(cp) and TFuncDecl(cp).IsMethod() Then
  93. Err "Method cannot be used as a function pointer"
  94. End If
  95. TInvokeExpr(args[i]).decl = TFuncDecl(TInvokeExpr(args[i]).decl.Copy(False))
  96. TInvokeExpr(args[i]).decl.actual = cp
  97. TInvokeExpr(args[i]).decl.attrs :| FUNC_PTR
  98. TFunctionPtrType(TInvokeExpr(args[i]).exprType).func = TInvokeExpr(args[i]).decl
  99. TInvokeExpr(args[i]).decl.semant()
  100. End If
  101. End If
  102. End If
  103. Next
  104. Return args
  105. End Method
  106. Method CastArgs:TExpr[]( args:TExpr[],funcDecl:TFuncDecl )
  107. If args.Length>funcDecl.argDecls.Length Then
  108. Err "Too many function parameters"
  109. End If
  110. ' FIXME
  111. 'args=args.Resize( funcDecl.argDecls.Length )
  112. ' FIXME
  113. For Local i:Int=0 Until funcDecl.argDecls.Length
  114. ' ensure funcdecl args are semanted before trying to use them.
  115. If Not funcDecl.argDecls[i].IsSemanted() Then
  116. funcDecl.argDecls[i].Semant()
  117. End If
  118. If i < args.length And args[i]
  119. Local argExpr:TExpr = args[i]
  120. If TInvokeExpr(argExpr) And Not TInvokeExpr(argExpr).invokedWithBraces Then
  121. If Not IsPointerType(funcDecl.argDecls[i].ty, TType.T_BYTE) And Not TFunctionPtrType(funcDecl.argDecls[i].ty) Then
  122. Err "Unable to convert from '" + argExpr.exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
  123. End If
  124. End If
  125. If TInvokeMemberExpr(argExpr) And Not TInvokeMemberExpr(argExpr).invokedWithBraces Then
  126. If Not IsPointerType(funcDecl.argDecls[i].ty, TType.T_BYTE) And Not TFunctionPtrType(funcDecl.argDecls[i].ty) Then
  127. Err "Unable to convert from '" + argExpr.exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
  128. End If
  129. End If
  130. If funcDecl.argDecls[i].ty._flags & TType.T_VAR Then
  131. If TConstExpr(argExpr) Or TBinaryExpr(argExpr) Or (TIndexExpr(argExpr) And TStringType(TIndexExpr(argExpr).expr.exprType)) Or ..
  132. TInvokeExpr(argExpr) Or TInvokeMemberExpr(argExpr) Or TNewObjectExpr(argExpr) or TNewArrayExpr(argExpr) Then
  133. Err "Expression for 'Var' parameter must be a variable or an element of an array or pointer"
  134. End If
  135. If TVarExpr(argExpr) Or TMemberVarExpr(argExpr) Then
  136. Local decl:TDecl
  137. If TVarExpr(argExpr) Then
  138. decl = TVarExpr(argExpr).decl
  139. Else
  140. decl = TMemberVarExpr(argExpr).decl
  141. End If
  142. If decl.IsReadOnly() Then
  143. If TFieldDecl(decl) Then
  144. Local scope:TFuncDecl = _env.FuncScope()
  145. If Not scope Or Not scope.IsCtor() Or (decl.ClassScope() <> scope.ClassScope()) Then
  146. Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
  147. End If
  148. Else
  149. Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
  150. End If
  151. End If
  152. End If
  153. ' passing a non volatile local as Var from within a Try block?
  154. If TVarExpr(argExpr) Then
  155. Local ldecl:TLocalDecl = TLocalDecl(TVarExpr(argExpr).decl)
  156. If ldecl And Not ldecl.volatile Then
  157. Local tryStmtDecl:TTryStmtDecl = _env.FindTry()
  158. If tryStmtDecl And (Not ldecl.declaredInTry Or tryStmtDecl <> ldecl.declaredInTry) Then
  159. ldecl.volatile = True
  160. End If
  161. End If
  162. End If
  163. End If
  164. If (funcDecl.argDecls[i].ty._flags & TType.T_VAR) And Not (funcDecl.argDecls[i].ty.EqualsType(argExpr.exprType)) Then
  165. If (Not TObjectType(funcDecl.argDecls[i].ty)) Or (TObjectType(funcDecl.argDecls[i].ty) And Not argExpr.exprType.ExtendsType(funcDecl.argDecls[i].ty)) Then
  166. err "Variable for 'Var' parameter is not of matching type"
  167. End If
  168. End If
  169. ' re-test auto array for compatible consts.
  170. If TArrayExpr(argExpr) And TArrayType(funcDecl.argDecls[i].ty) And TNumericType(TArrayType(funcDecl.argDecls[i].ty).elemType) Then
  171. TArrayExpr(argExpr).toType = TArrayType(funcDecl.argDecls[i].ty).elemType
  172. argExpr.exprType = Null
  173. argExpr.Semant()
  174. End If
  175. args[i]=argExpr.Cast( funcDecl.argDecls[i].ty )
  176. Else If funcDecl.argDecls[i].init
  177. If i = args.length Then
  178. ' extend args to add default init entry
  179. args = args[..i + 1]
  180. End If
  181. args[i]=funcDecl.argDecls[i].init
  182. Else
  183. Err "Missing function argument '"+funcDecl.argDecls[i].ident+"'."
  184. EndIf
  185. Next
  186. Return args
  187. End Method
  188. Method BalanceTypes:TType( lhs:TType,rhs:TType, balancePtrs:Int = True )
  189. If TStringType( lhs ) Or TStringType( rhs ) Then
  190. If TObjectType(lhs) Or TObjectType(rhs) Then
  191. If TObjectType(lhs) And TObjectType(lhs).classDecl.ident = "Object" Then
  192. Return lhs
  193. End If
  194. If TObjectType(rhs) And TObjectType(rhs).classDecl.ident = "Object" Then
  195. Return rhs
  196. End If
  197. Return New TStringType
  198. Else
  199. Return New TStringType
  200. End If
  201. End If
  202. If IsPointerType( lhs, 0, TType.T_POINTER ) Or IsPointerType( rhs, 0, TType.T_POINTER ) Then
  203. If balancePtrs Then
  204. If IsPointerType( lhs, 0, TType.T_POINTER ) Return lhs
  205. If IsPointerType( rhs, 0, TType.T_POINTER ) Return rhs
  206. Else
  207. If IsPointerType( lhs, 0, TType.T_POINTER ) Then
  208. If Not IsPointerType( rhs, 0, TType.T_POINTER ) And Not TNullType(rhs) Then
  209. Err "Can't balance types "+lhs.ToString()+" and "+rhs.ToString()+"."
  210. End If
  211. Return lhs
  212. End If
  213. If IsPointerType( rhs, 0, TType.T_POINTER ) Then
  214. If Not IsPointerType( lhs, 0, TType.T_POINTER ) And Not TNullType(lhs) Then
  215. Return rhs
  216. End If
  217. End If
  218. End If
  219. End If
  220. If TDouble128Type( lhs ) Or TDouble128Type( rhs ) Return New TDouble128Type
  221. If TFloat128Type( lhs ) Or TFloat128Type( rhs ) Return New TFloat128Type
  222. If TFloat64Type( lhs ) Or TFloat64Type( rhs ) Return New TFloat64Type
  223. If TDoubleType( lhs ) Or TDoubleType( rhs ) Return New TDoubleType
  224. If TFloatType( lhs ) Or TFloatType( rhs ) Return New TFloatType
  225. If TFunctionPtrType( lhs ) Or TFunctionPtrType( rhs ) Then
  226. If TFunctionPtrType( lhs ) Return lhs
  227. If TFunctionPtrType( rhs ) Return rhs
  228. End If
  229. If TInt128Type( lhs ) Or TInt128Type( rhs ) Return New TInt128Type
  230. If TULongType( lhs ) Or TULongType( rhs ) Return New TULongType
  231. If TSizeTType( lhs ) Or TSizeTType( rhs ) Return New TSizeTType
  232. If TWParamType( lhs ) Or TWParamType( rhs ) Return New TWParamType
  233. If TLongType( lhs ) And TUIntType( rhs ) Return New TULongType
  234. If TUIntType( lhs ) And TLongType( rhs ) Return New TULongType
  235. If TLParamType( lhs ) Or TLParamType( rhs ) Return New TLParamType
  236. If TULongIntType( lhs ) Or TULongIntType( rhs ) Return New TULongIntType
  237. If TLongType( lhs ) Or TLongType( rhs ) Return New TLongType
  238. If TLongIntType( lhs ) Or TLongIntType( rhs ) Return New TLongIntType
  239. If TUIntType( lhs ) Or TUIntType( rhs ) Return New TUIntType
  240. If TIntType( lhs ) Or TIntType( rhs ) Return New TIntType
  241. If TEnumType( lhs ) Or TEnumType( rhs ) Then
  242. If TEnumType( lhs ) Return lhs
  243. If TEnumType( rhs ) Return rhs
  244. End If
  245. If TObjectType( lhs ) And TNullDecl(TObjectType( lhs ).classDecl) Then
  246. Return rhs
  247. End If
  248. If TObjectType( rhs ) And TNullDecl(TObjectType( rhs ).classDecl) Then
  249. Return lhs
  250. End If
  251. If lhs.ExtendsType( rhs ) Return rhs
  252. If rhs.ExtendsType( lhs ) Return lhs
  253. ' balance arrays - only for objects... to the lowest common denominator.
  254. If TArrayType( lhs ) And TArrayType( rhs ) Then
  255. If TArrayType( lhs ).isStatic - TArrayType( rhs ).isStatic <> 0 Then
  256. Err "Cannot mix arrays and static arrays"
  257. End If
  258. If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( rhs ).elemType) Then
  259. ' lhs = Object[]
  260. If TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" Then
  261. Return lhs
  262. End If
  263. ' rhs = Object[]
  264. If TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" Then
  265. Return rhs
  266. End If
  267. ' does one extend the other? If so, return the base type
  268. If TObjectType(TArrayType( lhs ).elemType).ExtendsType(TObjectType(TArrayType( rhs ).elemType)) Then
  269. Return rhs
  270. End If
  271. If TObjectType(TArrayType( rhs ).elemType).ExtendsType(TObjectType(TArrayType( lhs ).elemType)) Then
  272. Return lhs
  273. End If
  274. ' no? then we will fallback to an Object type array
  275. ' find the Object classdecl instance
  276. Local modid$="brl.classes"
  277. Local mdecl:TModuleDecl=_env.FindModuleDecl( modid )
  278. ' return an array of Objects
  279. Return New TArrayType.Create(New TObjectType.Create(TClassDecl(mdecl.FindDecl( "object" ))))
  280. End If
  281. If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" And TStringType(TArrayType( rhs ).elemType) Then
  282. Return lhs
  283. End If
  284. If TObjectType(TArrayType( rhs ).elemType) And TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" And TStringType(TArrayType( lhs ).elemType) Then
  285. Return rhs
  286. End If
  287. If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" And TArrayType(TArrayType( rhs ).elemType) Then
  288. Return lhs
  289. End If
  290. If TObjectType(TArrayType( rhs ).elemType) And TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" And TArrayType(TArrayType( lhs ).elemType) Then
  291. Return rhs
  292. End If
  293. ' balancing primitive types
  294. If Not TArrayType( lhs ).elemType.EqualsType(TArrayType( rhs ).elemType) Then
  295. Err "Types '" + TArrayType( lhs ).elemType.ToString() + " Array' and '" + TArrayType( rhs ).elemType.ToString() + " Array' are unrelated"
  296. End If
  297. End If
  298. ' balance structs
  299. If TObjectType( lhs ) And TObjectType( rhs ) And TObjectType( lhs ).EqualsType(rhs) And TObjectType( lhs ).classDecl.IsStruct() And TObjectType( rhs ).classDecl.IsStruct() Then
  300. Return lhs
  301. End If
  302. Err "Can't balance types "+lhs.ToString()+" and "+rhs.ToString()+"."
  303. End Method
  304. Function CopyExpr:TExpr( expr:TExpr )
  305. If Not expr Return Null
  306. Return expr.Copy()
  307. End Function
  308. Function CopyArgs:TExpr[]( exprs:TExpr[] )
  309. exprs=exprs[..]
  310. For Local i:Int=0 Until exprs.Length
  311. exprs[i]=CopyExpr( exprs[i] )
  312. Next
  313. Return exprs
  314. End Function
  315. End Type
  316. ' exec a stmt, return an expr
  317. Type TStmtExpr Extends TExpr
  318. Field stmt:TStmt
  319. Field expr:TExpr
  320. Method Create:TStmtExpr( stmt:TStmt,expr:TExpr )
  321. Self.stmt=stmt
  322. Self.expr=expr
  323. Return Self
  324. End Method
  325. Method Copy:TExpr()
  326. If exprType Return Self
  327. Return New TStmtExpr.Create( stmt,CopyExpr(expr) )
  328. End Method
  329. Method ToString$()
  330. Return "TStmtExpr(,"+expr.ToString()+")"
  331. End Method
  332. Method Semant:TExpr(options:Int = 0)
  333. If exprType Return Self
  334. stmt.Semant()
  335. expr=expr.Semant()
  336. exprType=expr.exprType
  337. Return Self
  338. End Method
  339. Method Trans$()
  340. Return _trans.TransStmtExpr( Self )
  341. End Method
  342. Method TransVar$()
  343. Semant
  344. Return _trans.TransStmtExpr( Self )
  345. End Method
  346. End Type
  347. ' literal
  348. Type TConstExpr Extends TExpr
  349. Field ty:TType
  350. Field value$
  351. Field originalValue$
  352. ' True if the const was identified as a specific type.
  353. Field typeSpecific:Int
  354. Method Create:TConstExpr( ty:TType,value$ )
  355. originalValue = value
  356. If TNumericType( ty ) And IsPointerType(ty, 0, TType.T_POINTER) Then
  357. Self.ty=ty
  358. If value Then
  359. Self.value = value
  360. Else
  361. Self.value="0"
  362. End If
  363. Return Self
  364. End If
  365. If TIntType( ty ) Or TShortType( ty ) Or TByteType( ty ) Or TLongType( ty ) Or TUIntType( ty ) Or TULongType( ty ) Or TWParamType(ty) Or TLParamType(ty) Or TLongIntType(ty) Or TULongIntType(ty) Or TSizeTType(ty) Then
  366. Local radix:Int
  367. If value.StartsWith( "%" )
  368. radix=1
  369. Else If value.StartsWith( "$" )
  370. radix=4
  371. EndIf
  372. If radix
  373. If radix = 4 Then
  374. value = HexToDecString(value[1..])
  375. value = WrapForType(value, ty)
  376. Else
  377. Local val:Long = 0
  378. For Local i:Int=1 Until value.Length
  379. Local ch:Int=value[i]
  380. If ch>=48 And ch<58
  381. val=val Shl radix | (ch & 15)
  382. Else
  383. val=val Shl radix | ((ch & 15)+9)
  384. EndIf
  385. Next
  386. If TIntType(ty) And val >= 2147483648:Long Then
  387. value = String( -2147483648:Long + (val - 2147483648:Long))
  388. Else
  389. If TShortType( ty ) Then
  390. value=String( Short(val) )
  391. Else If TByteType( ty ) Then
  392. value=String( Byte(val) )
  393. Else
  394. value=String( val )
  395. End If
  396. End If
  397. End If
  398. Else
  399. Local buf:Byte[64]
  400. Local b:Int
  401. Local v:String = value.Trim()
  402. Local leading0:Int = True
  403. If v Then
  404. Local i:Int
  405. If v[0] = Asc("+") Then
  406. i = 1
  407. Else If v[0] = Asc("-") Then
  408. i = 1
  409. buf[b] = Asc("-")
  410. b:+ 1
  411. End If
  412. While i < v.Length
  413. If Not IsDigit(v[i]) Then
  414. Exit
  415. End If
  416. If leading0 And v[i] = Asc("0") Then
  417. i :+ 1
  418. Continue
  419. End If
  420. leading0 = False
  421. buf[b] = v[i]
  422. b :+ 1
  423. i :+ 1
  424. Wend
  425. Local cleaned:String
  426. If leading0 Then
  427. cleaned = "0"
  428. Else
  429. cleaned = String.FromBytes(buf, b)
  430. End If
  431. value = WrapForType(cleaned, ty)
  432. Else
  433. value = "0"
  434. End If
  435. EndIf
  436. Else If TDecimalType( ty )
  437. If Not (value.Contains("e") Or value.Contains("E") Or value.Contains(".") Or value.Contains("inf") Or value.Contains("nan") Or value.Contains("NaN") Or value.Contains("Inf"))
  438. If TFloatType(ty) Then
  439. value:+".00000000"
  440. Else
  441. value:+".0000000000000000"
  442. End If
  443. EndIf
  444. EndIf
  445. Self.ty=ty
  446. Self.value=value
  447. Return Self
  448. End Method
  449. Method WrapForType:String(num:String, ty:TType)
  450. Local wrapped:String
  451. If TIntType(ty) Then
  452. wrapped = WrapDecToBitsString(num, 32, True)
  453. Else If TShortType(ty) Then
  454. wrapped = WrapDecToBitsString(num, 16, False)
  455. Else If TByteType(ty) Then
  456. wrapped = WrapDecToBitsString(num, 8, False)
  457. Else If TUIntType(ty) Then
  458. wrapped = WrapDecToBitsString(num, 32, False)
  459. Else If TLongType(ty) Then
  460. wrapped = WrapDecToBitsString(num, 64, True)
  461. Else If TULongType(ty) Then
  462. wrapped = WrapDecToBitsString(num, 64, False)
  463. Else If TWParamType(ty) Then
  464. If WORD_SIZE = 4 Then
  465. wrapped = WrapDecToBitsString(num, 32, False)
  466. Else
  467. wrapped = WrapDecToBitsString(num, 64, False)
  468. End If
  469. Else If TLParamType(ty) Then
  470. If WORD_SIZE = 4 Then
  471. wrapped = WrapDecToBitsString(num, 32, True)
  472. Else
  473. wrapped = WrapDecToBitsString(num, 64, True)
  474. End If
  475. Else If TLongIntType(ty) Then
  476. If TLongIntType(ty).GetSize() = 4 Then
  477. wrapped = WrapDecToBitsString(num, 32, True)
  478. Else
  479. wrapped = WrapDecToBitsString(num, 64, True)
  480. End If
  481. Else If TULongIntType(ty) Then
  482. If TULongIntType(ty).GetSize() = 4 Then
  483. wrapped = WrapDecToBitsString(num, 32, False)
  484. Else
  485. wrapped = WrapDecToBitsString(num, 64, False)
  486. End If
  487. Else If TSizeTType(ty) Then
  488. If WORD_SIZE = 4 Then
  489. wrapped = WrapDecToBitsString(num, 32, False)
  490. Else
  491. wrapped = WrapDecToBitsString(num, 64, False)
  492. End If
  493. End If
  494. Return wrapped
  495. End Method
  496. Method UpdateType(ty:TType)
  497. typeSpecific = True
  498. Create(ty, originalValue)
  499. End Method
  500. Method Copy:TExpr()
  501. Local e:TConstExpr = New TConstExpr.Create( ty,value )
  502. e.originalValue = originalValue
  503. e.typeSpecific = typeSpecific
  504. Return e
  505. End Method
  506. Method ToString$()
  507. Return "TConstExpr(~q"+value+"~q)"
  508. End Method
  509. Method Semant:TExpr(options:Int = 0)
  510. If exprType Return Self
  511. exprType=ty.Semant()
  512. Return Self
  513. End Method
  514. Method Eval$()
  515. Return value
  516. End Method
  517. Method EvalConst:TExpr()
  518. Return Self
  519. End Method
  520. Method Trans$()
  521. Semant
  522. Return _trans.TransConstExpr( Self )
  523. End Method
  524. Method SemantAndCast:TExpr( ty:TType,castFlags:Int=0 )
  525. Local expr:TExpr=Semant()
  526. If expr.exprType.EqualsType( ty ) Return expr
  527. If value = "bbNullObject" Then
  528. Err "bbNullObject"
  529. Return expr
  530. End If
  531. Return New TCastExpr.Create( ty,expr,castFlags ).Semant()
  532. End Method
  533. Method CompatibleWithType:Int(ty:TType)
  534. If Not TDecimalType(ty) Then
  535. If value.Contains("e") Or value.Contains("E") Or value.Contains(".") Or value.Contains("inf") Or value.Contains("nan") Then
  536. Return False
  537. End If
  538. Local val:Long = value.ToLong()
  539. If val < 0 Then
  540. If TByteType(ty) Or TShortType(ty) Or TUIntType(ty) Or TULongType(ty) Or TSizeTType(ty) Or TInt128Type(ty) Or TWParamType(ty) or TULongIntType(ty) Then
  541. Return False
  542. End If
  543. Else
  544. If TByteType(ty) Then
  545. If value <> String.FromInt(Byte(Val)) Then
  546. Return False
  547. End If
  548. End If
  549. If TUIntType(ty) Or ((TSizeTType(ty) Or TWParamType(ty)) And WORD_SIZE = 4) Or (TULongIntType(ty) And TULongIntType(ty).GetSize() = 4) Then
  550. If val > 4294967296:Long Then
  551. Return False
  552. End If
  553. End If
  554. If TULongType(ty) Or ((TSizeTType(ty) Or TWParamType(ty)) And WORD_SIZE = 8) Or (TULongIntType(ty) And TULongIntType(ty).GetSize() = 8) Then
  555. If value.length > 20 Then
  556. Return False
  557. Else If value.length = 20 Then
  558. For Local i:Int = 0 Until value.length
  559. Local v:Int = value[i]
  560. Local n:Int = "18446744073709551616"[i]
  561. If v < n Then
  562. Exit
  563. Else If v > n Then
  564. Return False
  565. End If
  566. Next
  567. End If
  568. End If
  569. End If
  570. If TShortType(ty) Then
  571. If value <> String.FromInt(Short(val)) Then
  572. Return False
  573. End If
  574. End If
  575. If TIntType(ty) Or (TLParamType(ty) And WORD_SIZE = 4) Or (TLongIntType(ty) And TLongIntType(ty).GetSize() = 4) Then
  576. If value <> String.FromInt(Int(val)) Then
  577. Return False
  578. End If
  579. End If
  580. If TLongType(ty) Or (TLParamType(ty) And WORD_SIZE = 8) Or (TLongIntType(ty) And TLongIntType(ty).GetSize() = 8) Then
  581. If value <> String.FromLong(Long(val)) Then
  582. Return False
  583. End If
  584. End If
  585. End If
  586. Return True
  587. End Method
  588. Function HexToDecString:String(hex:String)
  589. ?bmxng
  590. Local StaticArray digits:Int[32]
  591. ?Not bmxng
  592. Local digits:Int[32] ' 2^64 fits in 20 decimal digits; 32 is safe
  593. ?
  594. Local nd:Int = 1
  595. digits[0] = 0
  596. For Local i:Int = 0 Until hex.Length
  597. Local ch:Int = hex[i]
  598. Local v:Int
  599. If ch >= 48 And ch <= 57 Then
  600. v = ch - 48
  601. Else If ch >= 65 And ch <= 70 Then
  602. v = ch - 55
  603. Else If ch >= 97 And ch <= 102 Then
  604. v = ch - 87
  605. Else
  606. ' invalid
  607. Return ""
  608. EndIf
  609. ' multiply current decimal by 16, add v
  610. Local carry:Int = v
  611. For Local j:Int = 0 Until nd
  612. Local x:Int = digits[j] * 16 + carry
  613. digits[j] = x Mod 10
  614. carry = x / 10
  615. Next
  616. While carry > 0
  617. If nd >= digits.Length Then
  618. Err "Overflow: Too many digits"
  619. Return "0"
  620. End If
  621. digits[nd] = carry Mod 10
  622. carry = carry / 10
  623. nd :+ 1
  624. Wend
  625. Next
  626. ' strip leading zeros
  627. While nd > 1 And digits[nd-1] = 0
  628. nd :- 1
  629. Wend
  630. ' build string reversed
  631. Local sb:TStringBuffer = New TStringBuffer(nd)
  632. For Local k:Int = nd-1 To 0 Step -1
  633. sb.AppendChar(digits[k] + 48)
  634. Next
  635. Return sb.ToString()
  636. End Function
  637. Function WrapDecToBitsString:String( s:String, bits:Int, isSigned:Int )
  638. If bits<>8 And bits<>16 And bits<>32 And bits<>64 Then
  639. Return "0"
  640. End If
  641. If s.Length = 0 Then
  642. Return "0"
  643. End If
  644. ' --- parse sign ---
  645. Local i:Int = 0
  646. Local neg:Int = 0
  647. Local c0:Int = s[0]
  648. If c0 = 45 Then ' '-'
  649. neg = 1
  650. i = 1
  651. Else If c0 = 43 Then ' '+'
  652. i = 1
  653. End If
  654. ' --- accumulate modulo 2^64 in (hi, lo) base 2^32 ---
  655. Local hi:Long = 0
  656. Local lo:Long = 0
  657. Const BASE:Long = 4294967296:Long ' 2^32
  658. Const MASK32:Long = $FFFFFFFF:Long
  659. For Local k:Int = i Until s.Length
  660. Local ch:Int = s[k]
  661. If ch < 48 Or ch > 57 Then
  662. Exit
  663. End If
  664. Local d:Long = (ch - 48)
  665. ' (hi,lo) = (hi,lo) * 10 + d (mod 2^64)
  666. Local lo10:Long = lo * 10:Long + d
  667. Local carry:Long = lo10 / BASE
  668. lo = lo10 & MASK32
  669. hi = (hi * 10:Long + carry) & MASK32
  670. Next
  671. ' --- apply negative sign as modulo wrap: u = (-u) mod 2^64 ---
  672. If neg Then
  673. ' two’s complement negate in 64 bits
  674. lo = ((~lo) + 1:Long) & MASK32
  675. If lo = 0 Then
  676. hi = ((~hi) + 1:Long) & MASK32
  677. Else
  678. hi = (~hi) & MASK32
  679. End If
  680. End If
  681. ' --- mask down to requested bit-width (mod 2^bits) ---
  682. If bits < 64 Then
  683. If bits <= 32 Then
  684. Local maskLo:Long
  685. If bits = 32 Then
  686. maskLo = MASK32
  687. Else
  688. maskLo = (1:Long Shl bits) - 1:Long
  689. End If
  690. lo = lo & maskLo
  691. hi = 0
  692. Else
  693. ' bits is 33..63
  694. Local hiBits:Int = bits - 32
  695. Local maskHi:Long = (1:Long Shl hiBits) - 1:Long
  696. hi = hi & maskHi
  697. lo = lo & MASK32
  698. End If
  699. Else
  700. hi = hi & MASK32
  701. lo = lo & MASK32
  702. End If
  703. ' --- format result ---
  704. If isSigned Then
  705. ' signed interpretation
  706. If bits < 64 Then
  707. ' sign bit test and subtract 2^bits if set
  708. If bits <= 32 Then
  709. Local top:Long = 1:Long Shl (bits - 1)
  710. Local v:Long = lo
  711. If (v & top) <> 0 Then v :- (1:Long Shl bits)
  712. Return String(v)
  713. Else
  714. ' 33..63: sign is in hi
  715. Local signMask:Long = 1:Long Shl ((bits - 32) - 1)
  716. Local vhi:Long = hi
  717. Local vlo:Long = lo
  718. If (vhi & signMask) <> 0 Then
  719. ' subtract 2^bits: subtract from hi part
  720. vhi :- (1:Long Shl (bits - 32))
  721. End If
  722. ' combine as signed 64 in Long (safe for bits<=63)
  723. Local v:Long = (vhi Shl 32) | (vlo & MASK32)
  724. Return String(v)
  725. End If
  726. Else
  727. ' 64-bit signed: just reinterpret (hi,lo) as signed Long
  728. Local v:Long = (hi Shl 32) | (lo & MASK32)
  729. Return String(v)
  730. End If
  731. Else
  732. ' unsigned interpretation
  733. If bits <= 32 Then
  734. ' lo is 0..2^32-1 and fits in signed Long as positive
  735. Return String(lo)
  736. Else
  737. Return U64ToDecString(hi, lo)
  738. End If
  739. End If
  740. End Function
  741. ' Convert unsigned 64-bit (hi,lo) base 2^32 to decimal string without ULong.
  742. Function U64ToDecString:String( hi:Long, lo:Long )
  743. Const MASK32:Long = $FFFFFFFF:Long
  744. hi :& MASK32
  745. lo :& MASK32
  746. If hi = 0 And lo = 0 Then
  747. Return "0"
  748. End If
  749. If hi = 0 Then
  750. Return String(lo)
  751. End If
  752. Const BASE:Long = 4294967296:Long ' 2^32
  753. ' collect digits in reverse (max 20 digits for 64-bit)
  754. ?bmxng
  755. Local StaticArray buf:Int[32]
  756. ?not bmxng
  757. Local buf:Int[32]
  758. ?
  759. Local n:Int = 0
  760. While (hi <> 0) Or (lo <> 0)
  761. If n >= buf.Length Then
  762. Err "Overflow: Too many digits"
  763. Return "0"
  764. End If
  765. ' Divide (hi,lo) by 10:
  766. ' q_hi = hi / 10, r_hi = hi % 10
  767. ' x = r_hi*2^32 + lo
  768. ' q_lo = x / 10, r = x % 10
  769. Local qhi:Long = hi / 10:Long
  770. Local rhi:Long = hi - qhi * 10:Long
  771. Local x:Long = rhi * BASE + lo ' fits in signed Long (<= 9*2^32 + (2^32-1))
  772. Local qlo:Long = x / 10:Long
  773. Local r:Int = Int(x - qlo * 10:Long)
  774. hi = qhi & MASK32
  775. lo = qlo & MASK32
  776. buf[n] = r + 48
  777. n :+ 1
  778. Wend
  779. Local sb:TStringBuffer = New TStringBuffer(n)
  780. For Local i:Int = n-1 To 0 Step -1
  781. sb.AppendChar(buf[i])
  782. Next
  783. Return sb.ToString()
  784. End Function
  785. End Type
  786. Type TVarExpr Extends TExpr
  787. Field decl:TVarDecl
  788. Method Create:TVarExpr( decl:TVarDecl )
  789. Self.decl=decl
  790. Return Self
  791. End Method
  792. Method Copy:TExpr()
  793. Return Self
  794. End Method
  795. Method ToString$()
  796. Return "TVarExpr("+decl.ToString()+")"
  797. End Method
  798. Method Semant:TExpr(options:Int = 0)
  799. If exprType Return Self
  800. If Not decl.IsSemanted() InternalErr "TVarExpr.Semant"
  801. exprType=decl.ty.Copy()
  802. Return Self
  803. End Method
  804. Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
  805. Return Semant()
  806. End Method
  807. Method Trans$()
  808. Semant
  809. Return _trans.TransTemplateCast( exprType,TVarDecl(decl.actual).ty,_trans.TransVarExpr( Self ) )
  810. End Method
  811. Method TransVar$()
  812. Semant
  813. Return _trans.TransVarExpr( Self )
  814. End Method
  815. End Type
  816. Type TMemberVarExpr Extends TExpr
  817. Field expr:TExpr
  818. Field decl:TVarDecl
  819. Method Create:TMemberVarExpr( expr:TExpr,decl:TVarDecl )
  820. Self.expr=expr
  821. Self.decl=decl
  822. Return Self
  823. End Method
  824. Method Copy:TExpr()
  825. Return Self
  826. End Method
  827. Method ToString$()
  828. Return "TMemberVarExpr("+expr.ToString()+","+decl.ToString()+")"
  829. End Method
  830. Method Semant:TExpr(options:Int = 0)
  831. If exprType Return Self
  832. If Not decl.IsSemanted() InternalErr "TMemberVarExpr.Semant"
  833. exprType=decl.ty.Semant()
  834. Return Self
  835. End Method
  836. Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
  837. Return Semant()
  838. End Method
  839. Method Trans$()
  840. Return _trans.TransTemplateCast( exprType,TVarDecl(decl.actual).ty,_trans.TransMemberVarExpr( Self ) )
  841. End Method
  842. Method TransVar$()
  843. Return _trans.TransMemberVarExpr( Self )
  844. End Method
  845. End Type
  846. Type TInvokeExpr Extends TExpr
  847. Field decl:TFuncDecl
  848. Field args:TExpr[]
  849. Field invokedWithBraces:Int
  850. Field isArg:Int
  851. Field isRhs:Int
  852. Method Create:TInvokeExpr( decl:TFuncDecl,args:TExpr[]=Null,invokedWithBraces:Int=True, isArg:Int=False, isRhs:Int = False )
  853. Self.decl=decl
  854. If args Then
  855. Self.args=args
  856. Else
  857. Self.args = New TExpr[0]
  858. End If
  859. Self.invokedWithBraces = invokedWithBraces
  860. Self.isArg = isArg
  861. Self.isRhs = isRhs
  862. Return Self
  863. End Method
  864. Method Copy:TExpr()
  865. Return Self
  866. End Method
  867. Method ToString$()
  868. Local t$="TInvokeExpr("+decl.ToString()
  869. For Local arg:TExpr=EachIn args
  870. t:+","+arg.ToString()
  871. Next
  872. Return t+")"
  873. End Method
  874. Method Semant:TExpr(options:Int = 0)
  875. If exprType Return Self
  876. If Not decl.retType
  877. decl.Semant()
  878. End If
  879. 'If TIdentType(decl.retType) Then
  880. exprType = decl.retType.Semant()
  881. 'Else
  882. ' exprType=decl.retType
  883. 'End If
  884. 'If ((isArg Or isRhs) And Not invokedWithBraces) And (args = Null Or args.length = 0) Then
  885. ' if the call was a statement (even one written without parentheses), then invokedWithBraces is true
  886. ' so no complicated checks are needed here; if invokedWithBraces is false, this is definitely not a call
  887. If Not invokedWithBraces Then
  888. If decl.IsMethod() Then
  889. Err "Method cannot be used as a function pointer"
  890. End If
  891. ' nothing to do here, as we are a function pointer. i.e. no braces
  892. ' and our expression type is a function ptr...
  893. exprType = New TFunctionPtrType.Create(decl)
  894. Else
  895. args=CastArgs( args,decl )
  896. End If
  897. Return Self
  898. End Method
  899. Method Trans$()
  900. ' Return _trans.TransTemplateCast( exprType,TFuncDecl(decl.actual).retType,_trans.TransInvokeExpr( Self ) )
  901. Return _trans.TransInvokeExpr( Self )
  902. End Method
  903. Method TransStmt$()
  904. Return _trans.TransInvokeExpr( Self )
  905. End Method
  906. Method Eval$()
  907. Return Super.Eval()
  908. End Method
  909. End Type
  910. Type TInvokeMemberExpr Extends TExpr
  911. Field expr:TExpr
  912. Field decl:TFuncDecl
  913. Field args:TExpr[]
  914. Field isResize:Int 'FIXME - butt ugly!
  915. Field invokedWithBraces:Int
  916. Method Create:TInvokeMemberExpr( expr:TExpr,decl:TFuncDecl,args:TExpr[]=Null, invokedWithBraces:Int = True )
  917. Self.expr=expr
  918. Self.decl=decl
  919. If args
  920. Self.args=args
  921. Else
  922. Self.args = New TExpr[0]
  923. End If
  924. Self.invokedWithBraces = invokedWithBraces
  925. Return Self
  926. End Method
  927. Method Copy:TExpr()
  928. Return Self
  929. End Method
  930. Method ToString$()
  931. Local t$="TInvokeMemberExpr("+expr.ToString()+","+decl.ToString()
  932. For Local arg:TExpr=EachIn args
  933. t:+","+arg.ToString()
  934. Next
  935. Return t+")"
  936. End Method
  937. Method Semant:TExpr(options:Int = 0)
  938. If exprType Return Self
  939. If Not decl.IsSemanted() decl.Semant()
  940. exprType=decl.retType
  941. args=SemantArgs( args )
  942. args=CastArgs( args,decl )
  943. 'Array $resize hack!
  944. If TArrayType( exprType ) And TVoidType( TArrayType( exprType ).elemType )
  945. isResize=True
  946. exprType=expr.exprType
  947. EndIf
  948. Return Self
  949. End Method
  950. Method Trans$()
  951. 'Array $resize hack!
  952. If isResize Return _trans.TransInvokeMemberExpr( Self )
  953. Return _trans.TransTemplateCast( exprType,TFuncDecl(decl.actual).retType,_trans.TransInvokeMemberExpr( Self ) )
  954. End Method
  955. Method TransStmt$()
  956. Return _trans.TransInvokeMemberExpr( Self )
  957. End Method
  958. End Type
  959. Type TNewObjectExpr Extends TExpr
  960. Field ty:TType
  961. Field args:TExpr[]
  962. Field ctor:TFuncDecl
  963. Field classDecl:TClassDecl
  964. Field instanceExpr:TExpr
  965. Method Create:TNewObjectExpr( ty:TType,args:TExpr[] )
  966. Self.ty=ty
  967. Self.args=args
  968. Return Self
  969. End Method
  970. Method Copy:TExpr()
  971. Return New TNewObjectExpr.Create( ty,CopyArgs(args) )
  972. End Method
  973. Method Semant:TExpr(options:Int = 0)
  974. If exprType Return Self
  975. Local it:TIdentType = TIdentType(ty)
  976. Local iArgs:TExpr[] = SemantArgs(CopyArgs(args))
  977. Local isNewSelf:Int = (it And it.ident = "self")
  978. ty=ty.Semant(True)
  979. If Not ty Then
  980. ' maybe it's an instance of a type ?
  981. Local decl:TVarDecl = TVarDecl(_env.FindDecl(it.ident))
  982. If decl And TObjectType(decl.ty) Then
  983. ' this legacy feature is deprecated. Issue a warning but let it go for now...
  984. ty = decl.ty
  985. instanceExpr = New TVarExpr.Create(decl).Semant()
  986. '
  987. Warn("Use of New <Object instance> is deprecated, and support will be removed in a future update.")
  988. Else
  989. Err "Type '"+it.ident+"' not found"
  990. End If
  991. Else If isNewSelf Then
  992. Warn("Use of New Self is deprecated, and support will be removed in a future update.")
  993. End If
  994. args=SemantArgs( args )
  995. Local objTy:TObjectType=TObjectType( ty )
  996. Local clsTy:TClassType=TClassType( ty )
  997. If Not objTy And Not clsTy
  998. Err "Expression is not a class."
  999. EndIf
  1000. If objTy And Not objTy.classDecl.Semanted() Then
  1001. objTy.classDecl.Semant()
  1002. End If
  1003. '
  1004. If clsTy And clsTy.instance Then
  1005. instanceExpr = New TSelfExpr.Semant()
  1006. End If
  1007. If objTy Then
  1008. classDecl=objTy.classDecl
  1009. Else
  1010. classDecl=clsTy.classDecl
  1011. End If
  1012. If Not instanceExpr Then
  1013. If classDecl.IsInterface() Err "Cannot create instance of an interface."
  1014. If classDecl.IsAbstract() Err "Cannot create instance of abstract type " + classDecl.ToString() + ..
  1015. ", which is either declared Abstract or has (or inherits) an abstract Method."
  1016. End If
  1017. 'If classDecl.IsTemplateArg() Err "Cannot create instance of a generic argument."
  1018. If classDecl.args And Not classDecl.instanceof Err "Cannot create instance of a generic class."
  1019. Local parts:String[]
  1020. If it Then
  1021. parts = it.ident.ToLower().Split(".")
  1022. End If
  1023. If classDecl.IsExtern()
  1024. Err "Cannot create instance of an extern type"
  1025. 'If args Err "No suitable constructor found for class "+classDecl.ToString()+"."
  1026. Else
  1027. ' if the New Type doesn't have extra idents (like a create method), then don't use the args in the search.
  1028. ' otherwise, the args are for the constructor.
  1029. If Not parts Or parts.length = 1 Then
  1030. ctor=classDecl.FindFuncDecl( "new",args,,,,,SCOPE_CLASS_HEIRARCHY )
  1031. If Not ctor Err "No suitable constructor found for class "+classDecl.ToString()+"."
  1032. args=CastArgs( args,ctor )
  1033. Else
  1034. ctor=classDecl.FindFuncDecl( "new",,,,,,SCOPE_CLASS_HEIRARCHY )
  1035. If Not ctor Err "No suitable constructor found for class "+classDecl.ToString()+"."
  1036. End If
  1037. EndIf
  1038. ' New Self doesn't necessarily create an instance of ourself - we might be an instance of
  1039. ' a subclass at the time...
  1040. If Not isNewSelf Then
  1041. classDecl.attrs:|CLASS_INSTANCED
  1042. End If
  1043. If TClassType(ty) Then
  1044. exprType=New TObjectType.Create(TClassType(ty).classDecl)
  1045. Else
  1046. exprType=ty
  1047. End If
  1048. If it Then
  1049. 'Local parts:String[] = it.ident.ToLower().Split(".")
  1050. Local i:Int = 0
  1051. While i < parts.length And parts[i] <> classDecl.IdentLower() And parts[i] <> "self"
  1052. i :+ 1
  1053. Wend
  1054. i :+ 1
  1055. Local expr:TExpr = Self
  1056. Local cdecl:TClassDecl = classDecl
  1057. Local eType:TType = ty
  1058. Local errorDetails:String
  1059. While i < parts.length
  1060. Local id:String = parts[i]
  1061. i :+ 1
  1062. ' find other member decl (field, etc)
  1063. Local decl:TValDecl = TValDecl(cdecl.GetDecl(id))
  1064. If TVarDecl(decl) Then
  1065. decl.Semant
  1066. Local tmp:TLocalDecl=New TLocalDecl.Create( "", eType, expr,, True )
  1067. Local varExpr:TExpr = New TMemberVarExpr.Create(New TVarExpr.Create( tmp ), TVarDecl(decl)).Semant()
  1068. expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), varExpr ).Semant()
  1069. eType = decl.ty
  1070. If TObjectType(eType) Then
  1071. cdecl = TObjectType(expr.exprType).classdecl
  1072. End If
  1073. If TArrayType(eType) Or TStringType(eType) Then
  1074. cdecl = eType.GetClass()
  1075. End If
  1076. Continue
  1077. End If
  1078. If TConstDecl(decl) Then
  1079. decl.Semant()
  1080. expr = New TConstExpr.Create(decl.ty, TConstDecl(decl).value).Semant()
  1081. eType = decl.ty
  1082. Continue
  1083. End If
  1084. ' find member function.method
  1085. Local fdecl:TFuncDecl
  1086. Try
  1087. fdecl = cdecl.FindFuncDecl(id, iArgs,,,,True,SCOPE_CLASS_HEIRARCHY)
  1088. Catch errorMessage:String
  1089. If errorMessage.StartsWith("Compile Error") Then
  1090. Throw errorMessage
  1091. Else
  1092. ' couldn't find an exact match, look elsewhere
  1093. If errorMessage.StartsWith("Unable") Then
  1094. errorDetails = errorMessage
  1095. End If
  1096. End If
  1097. End Try
  1098. If fdecl Then
  1099. expr = New TInvokeMemberExpr.Create( expr,fdecl, iArgs ).Semant()
  1100. eType = expr.exprType
  1101. If TObjectType(eType) Then
  1102. cdecl = TObjectType(expr.exprType).classdecl
  1103. End If
  1104. If TArrayType(eType) Or TStringType(eType) Then
  1105. cdecl = eType.GetClass()
  1106. End If
  1107. Continue
  1108. End If
  1109. ' didn't match member or function??
  1110. ' probably an error...
  1111. If errorDetails Then
  1112. Err errorDetails
  1113. Else
  1114. Err "Identifier '" + id + "' not found."
  1115. End If
  1116. Wend
  1117. Return expr
  1118. End If
  1119. Return Self
  1120. End Method
  1121. Method Trans$()
  1122. Return _trans.TransNewObjectExpr( Self )
  1123. End Method
  1124. End Type
  1125. Type TNewArrayExpr Extends TExpr
  1126. Field ty:TType
  1127. Field expr:TExpr[]
  1128. Method Create:TNewArrayExpr( ty:TType,expr:TExpr[] )
  1129. Self.ty=ty
  1130. Self.expr=expr
  1131. Return Self
  1132. End Method
  1133. Method Copy:TExpr()
  1134. 'If exprType InternalErr "TNewArrayExpr.Copy"
  1135. Local cexpr:TExpr[expr.length]
  1136. For Local i:Int = 0 Until expr.length
  1137. cexpr[i] = CopyExpr(expr[i])
  1138. Next
  1139. Return New TNewArrayExpr.Create( ty,cexpr )
  1140. End Method
  1141. Method Semant:TExpr(options:Int = 0)
  1142. If exprType Return Self
  1143. ty=ty.Semant()
  1144. exprType=New TArrayType.Create( ty, expr.length )
  1145. For Local i:Int = 0 Until expr.length
  1146. expr[i]=expr[i].SemantAndCast( New TIntType )
  1147. Next
  1148. Return Self
  1149. End Method
  1150. Method Trans$()
  1151. Return _trans.TransNewArrayExpr( Self )
  1152. End Method
  1153. End Type
  1154. ' super.ident( args )
  1155. Type TInvokeSuperExpr Extends TExpr
  1156. Field ident$
  1157. Field args:TExpr[]
  1158. Field origFuncDecl:TFuncDecl
  1159. Field funcDecl:TFuncDecl
  1160. Field classScope:TClassDecl
  1161. Field superClass:TClassDecl
  1162. Field _identLower:String
  1163. Method Create:TInvokeSuperExpr( ident$,args:TExpr[] = Null, _identLower:String = Null )
  1164. Self.ident=ident
  1165. If args Then
  1166. Self.args=args
  1167. Else
  1168. Self.args = New TExpr[0]
  1169. End If
  1170. Self._identLower = _identLower
  1171. Return Self
  1172. End Method
  1173. Method IdentLower:String()
  1174. If Not _identLower Then
  1175. _identLower = ident.ToLower()
  1176. End If
  1177. Return _identLower
  1178. End Method
  1179. Method Copy:TExpr()
  1180. Return New TInvokeSuperExpr.Create( ident,CopyArgs(args), _identLower )
  1181. End Method
  1182. Method Semant:TExpr(options:Int = 0)
  1183. If exprType Return Self
  1184. 'If _env.FuncScope().IsStatic() Err "Illegal use of Super."
  1185. classScope=_env.ClassScope()
  1186. superClass=classScope.superClass
  1187. If Not superClass Err "Type has no super class."
  1188. args=SemantArgs( args )
  1189. Try
  1190. ' get the local version of the method from local class scope
  1191. origFuncDecl=classScope.FindFuncDecl(IdentLower(),args,,,,True,SCOPE_CLASS_LOCAL)
  1192. Catch errorMessage:String
  1193. If errorMessage.StartsWith("Compile Error") Then
  1194. Throw errorMessage
  1195. Else
  1196. ' if there isn't one, we'll just use a Super version of it anyway as a reference.
  1197. origFuncDecl=classScope.FindFuncDecl(IdentLower(),args,,,,,SCOPE_CLASS_HEIRARCHY)
  1198. End If
  1199. End Try
  1200. funcDecl=superClass.FindFuncDecl( IdentLower(),args,,,,,SCOPE_CLASS_HEIRARCHY )
  1201. If Not funcDecl Err "Can't find superclass method '"+ident+"'."
  1202. ' ensure the super function has been semanted
  1203. funcDecl.Semant()
  1204. ' cannot directly call abstract methods
  1205. If funcDecl.isAbstract() Then
  1206. Err "Abstract method '" + funcDecl.ident + "' cannot be accessed directly."
  1207. End If
  1208. ' for static scope, we need to change class scope to that of the super class
  1209. If _env.FuncScope().IsStatic() Then
  1210. classScope = TClassDecl(funcDecl.scope)
  1211. End If
  1212. args=CastArgs( args,funcDecl )
  1213. exprType=funcDecl.retType
  1214. Return Self
  1215. End Method
  1216. Method Trans$()
  1217. Return _trans.TransInvokeSuperExpr( Self )
  1218. End Method
  1219. End Type
  1220. ' Self
  1221. Type TSelfExpr Extends TExpr
  1222. Method Copy:TExpr()
  1223. Local expr:TExpr = New TSelfExpr
  1224. expr.static = static
  1225. Return expr
  1226. End Method
  1227. Method Semant:TExpr(options:Int = 0)
  1228. If exprType Return Self
  1229. If _env.FuncScope().IsStatic() Then
  1230. static = True
  1231. End If
  1232. Local scope:TClassDecl = _env.ClassScope()
  1233. If Not scope Then
  1234. Err "'Self' can only be used within methods."
  1235. End If
  1236. Local funcScope:TFuncDecl = _env.FuncScope()
  1237. If funcScope.IsAnyMethod() Then
  1238. exprType=New TObjectType.Create( scope )
  1239. TObjectType(exprType).instance = True
  1240. Else
  1241. exprType=New TClassType.Create( scope )
  1242. End If
  1243. Return Self
  1244. End Method
  1245. Method Trans$()
  1246. Return _trans.TransSelfExpr( Self )
  1247. End Method
  1248. End Type
  1249. Const CAST_EXPLICIT:Int=1
  1250. Const CAST_TWO:Int=2
  1251. Type TCastExpr Extends TExpr
  1252. Field ty:TType
  1253. Field expr:TExpr
  1254. Field flags:Int
  1255. Method Create:TCastExpr( ty:TType,expr:TExpr,flags:Int=0 )
  1256. Self.ty=ty
  1257. Self.expr=expr
  1258. Self.flags=flags
  1259. Return Self
  1260. End Method
  1261. Method Copy:TExpr()
  1262. Return New TCastExpr.Create( ty,CopyExpr(expr),flags )
  1263. End Method
  1264. Method CheckArrayExpr:TType(src:TType, e:TExpr, last:TType)
  1265. If TNullType(e.exprType) Then
  1266. Err "Auto array element has no type"
  1267. End If
  1268. If TObjectType(TArrayType(ty).elemType) And TObjectType(TArrayType(ty).elemType).classDecl.ident = "Object" And (TStringType(e.exprType) Or TObjectType(e.exprType) Or TArrayType(e.exprType)) Then
  1269. ' array takes generic objects, so we don't care if source elements are the same kinds.
  1270. Else
  1271. If last <> Null And Not last.EqualsType(e.exprType) Then
  1272. Err "Auto array elements must have identical types"
  1273. End If
  1274. If Not TArrayType(ty).elemType.EqualsType(e.exprType) Then
  1275. If (TObjectType(TArrayType(ty).elemType) = Null And TStringType(TArrayType(ty).elemType) = Null) Or (TObjectType(e.exprType) = Null And TStringType(e.exprType) = Null) Then
  1276. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1277. Else If TStringType(e.exprType) = Null And Not TObjectType(e.exprType).ExtendsType(TObjectType(TArrayType(ty).elemType)) Then
  1278. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1279. End If
  1280. End If
  1281. End If
  1282. Return e.exprType
  1283. End Method
  1284. Method CheckArrayType:Int(ty:TArrayType, src:TArrayType)
  1285. If src.dims <> ty.dims Then
  1286. Return False
  1287. End If
  1288. If TObjectType(TArrayType(ty).elemType) Then
  1289. If TObjectType(TArrayType(ty).elemType).classDecl.ident = "Object" And (TStringType(TArrayType(src).elemType) Or TObjectType(TArrayType(src).elemType) Or TArrayType(TArrayType(src).elemType)) Then
  1290. ' array takes generic objects, so we don't care if source elements are the same kinds.
  1291. Else
  1292. If TObjectType(TArrayType(src).elemType) And Not (TObjectType(TArrayType(src).elemType)).ExtendsType(TArrayType(ty).elemType) And Not TArrayType(ty).elemType.EqualsType(TArrayType(src).elemType) Then
  1293. Return False
  1294. End If
  1295. End If
  1296. Else If TArrayType(TArrayType(ty).elemType) And TArrayType(TArrayType(src).elemType) Then
  1297. If Not CheckArrayType(TArrayType(TArrayType(ty).elemType), TArrayType(TArrayType(src).elemType)) Then
  1298. Return False
  1299. End If
  1300. Else If Not TArrayType(ty).elemType.EqualsType(TArrayType(src).elemType) Then
  1301. Return False
  1302. End If
  1303. Return True
  1304. End Method
  1305. Method Semant:TExpr(options:Int = 0)
  1306. If exprType Return Self
  1307. ty=ty.Semant()
  1308. If TInvokeExpr(expr) Then
  1309. TInvokeExpr(expr).isRhs = True
  1310. Else If TIdentExpr(expr) Then
  1311. TIdentExpr(expr).isRhs = True
  1312. End If
  1313. expr=expr.Semant()
  1314. Local src:TType=expr.exprType
  1315. 'equal?
  1316. If src.EqualsType( ty ) Return expr
  1317. 'upcast?
  1318. If src.ExtendsType( ty )
  1319. 'cast from void[] to T[]
  1320. If TArrayType(src) And TVoidType( TArrayType(src).elemType )
  1321. Return New TConstExpr.Create( ty,"" ).Semant()
  1322. EndIf
  1323. If src._flags & TType.T_VARPTR Then
  1324. exprType = ty
  1325. Return Self
  1326. End If
  1327. If TStringType(ty) And TObjectType(src)
  1328. ' only if explicitly cast
  1329. If flags & CAST_EXPLICIT Then
  1330. exprType = ty
  1331. 'Return Self
  1332. End If
  1333. End If
  1334. 'Box/unbox?...
  1335. 'If TObjectType( ty ) And Not TObjectType( src )
  1336. 'Box!
  1337. ' expr=New TNewObjectExpr.Create( ty,[expr] ).Semant()
  1338. 'Else
  1339. If TObjectType( src ) And Not TObjectType( ty ) And Not TStringType( ty )
  1340. 'Unbox!
  1341. Local op$
  1342. 'If TBoolType( ty )
  1343. ' op="ToBool"
  1344. 'Else
  1345. If TIntType( ty )
  1346. op="ToInt"
  1347. Else If TFloatType( ty )
  1348. op="ToFloat"
  1349. Else If TStringType( ty )
  1350. op="ToString"
  1351. Else If IsPointerType(ty, 0, TType.T_POINTER)
  1352. exprType = ty
  1353. If flags = CAST_EXPLICIT Then
  1354. Return Self
  1355. Else
  1356. If Not TObjectType( src ).classDecl.IsExtern() Then
  1357. Return Self
  1358. Else
  1359. Return expr
  1360. End If
  1361. End If
  1362. Else
  1363. InternalErr "TCastExpr.Semant"
  1364. EndIf
  1365. Local fdecl:TFuncDecl=src.GetClass().FindFuncDecl( op,,,,,,SCOPE_ALL )
  1366. expr=New TInvokeMemberExpr.Create( expr,fdecl ).Semant()
  1367. EndIf
  1368. If TNullType(src) And Not TVoidType(ty) And Not (TArrayType(ty) And TArrayType(ty).IsStatic) Then
  1369. exprType = ty
  1370. End If
  1371. If TBoolType(src) And (TNumericType(ty) Or TStringType(ty)) Then
  1372. exprType = ty
  1373. End If
  1374. If TNumericType(src) And (TNumericType(ty) Or TStringType(ty)) Then
  1375. ' intrinsics can only cast between selves
  1376. If (TIntrinsicType(src) And TIntrinsicType(ty)=Null) Or (TIntrinsicType(ty) And TIntrinsicType(src)=Null) Then
  1377. If TFloat64Type(src) Or TFloat64Type(ty) Then
  1378. If (TFloat64Type(src) And (TLongType(ty) Or TULongType(ty) Or (TLongIntType(ty) And TLongIntType(ty).GetSize() = 8) Or (TULongIntType(ty) And TULongIntType(ty).GetSize() = 8))) Or (TFloat64Type(ty) And (TLongType(src) Or TULongType(src) Or (TLongIntType(src) And TLongIntType(src).GetSize() = 8) Or (TULongIntType(src) And TULongIntType(src).GetSize() = 8))) Then
  1379. ' ok
  1380. Else
  1381. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1382. End If
  1383. Else
  1384. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1385. End If
  1386. Else If TIntrinsicType(src) And TIntrinsicType(ty) Then
  1387. If (TFloat64Type(src) And TFloat64Type(ty)=Null) Or (TFloat64Type(ty) And TFloat64Type(src)=Null) Then
  1388. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1389. End If
  1390. End If
  1391. exprType = ty
  1392. End If
  1393. If TObjectType(ty) And (TObjectType(src) Or TStringType(src) Or TArrayType(src)) Then
  1394. exprType = ty
  1395. Return Self
  1396. End If
  1397. If TFunctionPtrType(src) And IsPointerType(ty, 0, TType.T_POINTER) Then
  1398. exprType = ty
  1399. End If
  1400. Else If TBoolType( ty )
  1401. If TVoidType( src )
  1402. Err "Cannot convert from Void to Int."
  1403. EndIf
  1404. If flags & CAST_EXPLICIT
  1405. exprType=ty
  1406. EndIf
  1407. Else If ty.ExtendsType( src )
  1408. If flags & CAST_EXPLICIT
  1409. 'if both objects or both non-objects...
  1410. If (TObjectType(ty)<>Null)=(TObjectType(src)<>Null) Then
  1411. exprType=ty
  1412. If TFunctionPtrType(ty) And TInvokeExpr(expr) And Not TInvokeExpr(expr).invokedWithBraces Then
  1413. Return expr
  1414. End If
  1415. Return Self
  1416. End If
  1417. If (TStringType(ty) Or TArrayType(ty)) And TObjectType(src) Then
  1418. exprType=ty
  1419. Return Self
  1420. End If
  1421. 'Else ' if not explicitly cast, we can't just auto-cast it ourselves here.
  1422. 'If (TObjectType(ty)<>Null) And (TObjectType(src)<>Null) exprType=ty
  1423. EndIf
  1424. EndIf
  1425. If TArrayType(ty) And TArrayType(src) Then
  1426. If TArrayType(ty).isStatic - TArrayType(src).isStatic <> 0 Then
  1427. Err "Unable to convert from " + src.ToString() + " to " + ty.ToString()
  1428. End If
  1429. If TArrayType(ty).dims = TArrayType(src).dims Then
  1430. If TArrayExpr(expr) Then
  1431. Local last:TType
  1432. For Local e:TExpr = EachIn TArrayExpr(expr).exprs
  1433. last = CheckArrayExpr(src, e, last)
  1434. Next
  1435. Else
  1436. If TObjectType(TArrayType(ty).elemType) Then
  1437. If TObjectType(TArrayType(ty).elemType).classDecl.ident = "Object" And (TStringType(TArrayType(src).elemType) Or TObjectType(TArrayType(src).elemType) Or TArrayType(TArrayType(src).elemType)) Then
  1438. ' array takes generic objects, so we don't care if source elements are the same kinds.
  1439. Else
  1440. If TObjectType(TArrayType(src).elemType) Then
  1441. If Not (TObjectType(TArrayType(src).elemType)).ExtendsType(TArrayType(ty).elemType) And Not TArrayType(ty).elemType.EqualsType(TArrayType(src).elemType) Then
  1442. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1443. End If
  1444. Else If Not TArrayType(ty).elemType.EqualsType(TArrayType(src).elemType) Then
  1445. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1446. End If
  1447. End If
  1448. Else If TArrayType(TArrayType(ty).elemType) And TArrayType(TArrayType(src).elemType) Then
  1449. If Not CheckArrayType(TArrayType(TArrayType(ty).elemType), TArrayType(TArrayType(src).elemType)) Then
  1450. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1451. End If
  1452. Else If Not TArrayType(ty).elemType.EqualsType(TArrayType(src).elemType) Then
  1453. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1454. End If
  1455. End If
  1456. exprType = ty
  1457. Return Self
  1458. End If
  1459. End If
  1460. 'If TStringType(src) And TStringVarPtrType(ty) Then
  1461. ' exprType = ty
  1462. ' Return Self
  1463. 'End If
  1464. ' If TArrayType(src) And TPointerType(ty) Then
  1465. ' exprType = ty
  1466. ' Return expr
  1467. ' End If
  1468. If TFunctionPtrType(ty) And TInvokeExpr(expr) Then
  1469. ' a function ptr to function ptr
  1470. If Not TInvokeExpr(expr).invokedWithBraces Then
  1471. src = New TFunctionPtrType
  1472. TFunctionPtrType(src).func = TInvokeExpr(expr).decl
  1473. ' signatures should match
  1474. If TInvokeExpr(expr).decl.equalsFunc(TFunctionPtrType(ty).func) Then
  1475. exprType = ty
  1476. Return expr
  1477. End If
  1478. Else
  1479. ' return type should be function ptr?
  1480. Local retType:TType = expr.exprType
  1481. If TFunctionPtrType(retType) And TFunctionPtrType(ty).func.EqualsFunc(TFunctionPtrType(retType).func) Then
  1482. exprType = retType
  1483. Return expr
  1484. End If
  1485. End If
  1486. End If
  1487. 'If TIntType(ty) And Not IsPointerType(ty, 0, TType.T_POINTER) And IsPointerType(src, 0, TType.T_POINTER) Then
  1488. ' exprType = ty
  1489. ' If flags & CAST_EXPLICIT Then
  1490. ' Return Self
  1491. ' End If
  1492. ' Return expr
  1493. 'End If
  1494. ' explicit cast to number
  1495. If IsNumericType(ty) And IsPointerType(src, 0, TType.T_POINTER) Then
  1496. If flags = CAST_EXPLICIT Then
  1497. exprType = ty
  1498. Return Self
  1499. Else
  1500. exprType = Null
  1501. End If
  1502. End If
  1503. ' If TPointerType(ty) And TIntType(src) Then
  1504. ' exprType = ty
  1505. ' Return expr
  1506. ' End If
  1507. ' If TIntType(ty) And TObjectType(src) Then
  1508. ' DebugStop ' Bah woz ere
  1509. ' exprType = ty
  1510. ' Return expr
  1511. ' End If
  1512. If TObjectType(src) And TNullDecl(TObjectType(src).classDecl) Then
  1513. exprType = ty
  1514. Return expr
  1515. End If
  1516. If TObjectType(src) And TObjectType(ty) And (ty._flags & TType.T_VAR) Then ' TODO : May be VARPTR instead?
  1517. 'exprType = NewPointerType(TType.T_BYTE)
  1518. exprType = ty
  1519. Return Self
  1520. End If
  1521. If TStringType(src) And ((src._flags & TType.T_CHAR_PTR) Or (src._flags & TType.T_SHORT_PTR)) And TStringType(ty) Then
  1522. exprType = ty
  1523. Return Self
  1524. End If
  1525. ' cast from "some kind of object" array to Object[]
  1526. If TArrayType(ty) And TArrayType(src)
  1527. If (TObjectType(TArrayType(src).elemType) Or TStringType(TArrayType(src).elemType) Or TArrayType(TArrayType(src).elemType)) And TObjectType(TArrayType(ty).elemType) Then
  1528. If TObjectType(TArrayType(ty).elemType).classDecl.ident = "Object" Then
  1529. exprType = ty
  1530. Return Self
  1531. End If
  1532. Else
  1533. If TArrayType(ty).dims = TArrayType(src).dims Then
  1534. exprType = ty
  1535. End If
  1536. End If
  1537. End If
  1538. If TArrayType(ty) And TObjectType(src)
  1539. If TObjectType(src).classDecl.ident = "___Array" Then
  1540. exprType = ty
  1541. Return expr
  1542. Else If TObjectType(src).classDecl.ident = "Object" And flags & CAST_EXPLICIT Then
  1543. exprType = ty
  1544. Return Self
  1545. End If
  1546. End If
  1547. If IsPointerType(ty, 0, TType.T_POINTER | TType.T_CHAR_PTR | TType.T_SHORT_PTR) Then
  1548. If IsNumericType(src) And Not (src._flags & TType.T_VARPTR) Then
  1549. ' no decimal casts to pointers
  1550. If TDecimalType(src) Then
  1551. exprType = Null
  1552. Else
  1553. exprType = ty
  1554. Return Self
  1555. End If
  1556. Else If TNumericType(src) And (src._flags & TType.T_VARPTR) Then
  1557. exprType = expr.exprType
  1558. Else If TObjectType(src) And (src._flags & TType.T_VARPTR) Then
  1559. exprType = expr.exprType
  1560. Else If TArrayType(src) Then
  1561. ' for functions and index access, use a new local variable
  1562. If Not TVarExpr(expr) And Not TMemberVarExpr(expr) Then
  1563. Local tmp:TLocalDecl=New TLocalDecl.Create( "", expr.exprType, expr,, True )
  1564. tmp.Semant()
  1565. Local v:TVarExpr = New TVarExpr.Create( tmp )
  1566. expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), v ).Semant()
  1567. End If
  1568. If TNumericType(TArrayType(src).elemType) Then
  1569. exprType = TNumericType(TArrayType(src).elemType).ToPointer()
  1570. Return Self
  1571. Else
  1572. ' map arrays to byte ptr
  1573. exprType = TType.MapToPointerType(New TByteType)
  1574. End If
  1575. Else If TStringType(src) Then
  1576. exprType = ty
  1577. Return Self
  1578. End If
  1579. End If
  1580. If TStringType(src) And TStringType(ty) And (ty._flags & TType.T_VAR) Then
  1581. exprType = ty
  1582. Return Self
  1583. End If
  1584. If TVarPtrType(ty) Then
  1585. If Not TVarExpr(expr) And Not TMemberVarExpr(expr) And Not (TStmtExpr(expr) And TIndexExpr(TStmtExpr(expr).expr)) Then
  1586. If Not TIndexExpr(expr) Or (TIndexExpr(expr) And Not TVarExpr(TIndexExpr(expr).expr) And Not TMemberVarExpr(TIndexExpr(expr).expr)) Then
  1587. Err "Subexpression for 'Varptr' must be a variable or an element of an array, pointer or string"
  1588. End If
  1589. End If
  1590. exprType = src.Copy()
  1591. exprType._flags :| TType.T_VARPTR
  1592. ty = exprType
  1593. Return Self
  1594. End If
  1595. If TFunctionPtrType(ty) And IsPointerType(src, 0, TType.T_POINTER) Then
  1596. exprType = ty
  1597. Return Self
  1598. End If
  1599. If TObjectType(ty) And TObjectType(src) And TObjectType(src).classdecl.IsInterface() And flags & CAST_EXPLICIT Then
  1600. exprType = ty
  1601. Return Self
  1602. End If
  1603. If TObjectType(ty) And TObjectType(src) And TObjectType(ty).classdecl.IsInterface() And flags & CAST_EXPLICIT Then
  1604. exprType = ty
  1605. Return Self
  1606. End If
  1607. If TStringType(ty) And TEnumType(src) Then
  1608. exprType = ty
  1609. Return Self
  1610. End If
  1611. If TEnumType(src) And TEnumType(ty) And (ty._flags & TType.T_VAR) Then
  1612. Return expr
  1613. End If
  1614. If TIntegralType(ty) And TEnumType(src) And (flags & CAST_EXPLICIT Or flags & CAST_TWO) Then
  1615. exprType = ty
  1616. Return Self
  1617. End If
  1618. If TIntegralType(src) And TEnumType(ty) Then
  1619. If flags & CAST_TWO Then
  1620. exprType = src
  1621. Return Self
  1622. Else If flags & CAST_EXPLICIT Then
  1623. ' validate const
  1624. If TConstExpr( expr ) And Not TEnumType(ty).decl.CastsToEnum(TConstExpr( expr )) Then
  1625. Err "The value " + TConstExpr( expr ).value + " is not valid for enum " + TEnumType(ty).decl.ToString()
  1626. End If
  1627. exprType = ty
  1628. Return Self
  1629. End If
  1630. End If
  1631. If Not exprType
  1632. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1633. EndIf
  1634. If TConstExpr( expr ) Then
  1635. If TDecimalType(TConstExpr( expr ).ty) And TDecimalType(ty) Then
  1636. Return New TConstExpr.Create(ty, TConstExpr( expr ).value).Semant()
  1637. End If
  1638. Local ex:TExpr = EvalConst()
  1639. If flags & CAST_EXPLICIT Then
  1640. Return New TCastExpr.Create(exprType, ex, CAST_EXPLICIT).Semant()
  1641. Else
  1642. Return ex
  1643. End If
  1644. End If
  1645. Return Self
  1646. End Method
  1647. Method Eval$()
  1648. Local val$=expr.Eval()
  1649. If TBoolType( exprType )
  1650. If TIntegralType(expr.exprType)
  1651. If Long( val ) Return "1"
  1652. Return ""
  1653. Else If TDecimalType( expr.exprType )
  1654. If Double( val ) Return "1"
  1655. Return ""
  1656. Else If TStringType( expr.exprType )
  1657. If val.Length Return "1"
  1658. Return ""
  1659. EndIf
  1660. Else If TIntType( exprType )
  1661. If TBoolType( expr.exprType )
  1662. If val Return "1"
  1663. Return "0"
  1664. EndIf
  1665. Return Int( val )
  1666. Else If TUIntType( exprType )
  1667. Return Long( val )
  1668. Else If TShortType( exprType )
  1669. Return Short( val )
  1670. Else If TFloatType( exprType )
  1671. Return Float( val )
  1672. Else If TDoubleType( exprType )
  1673. Return Double( val )
  1674. Else If TLongType( exprType )
  1675. Return Long( val )
  1676. Else If TULongType( exprType )
  1677. Return Long( val )
  1678. Else If TLongIntType( exprType )
  1679. Return Long( val )
  1680. Else If TULongIntType( exprType )
  1681. Return Long( val )
  1682. Else If TSizeTType( exprType )
  1683. Return Long( val )
  1684. Else If TInt128Type( exprType )
  1685. Return Long( val )
  1686. Else If TFloat128Type( exprType )
  1687. Return Float( val )
  1688. Else If TDouble128Type( exprType )
  1689. Return Float( val )
  1690. Else If TFloat64Type( exprType )
  1691. Return Float( val )
  1692. Else If TStringType( exprType )
  1693. If TBoolType( expr.exprType )
  1694. If val Return "1"
  1695. Return "0"
  1696. EndIf
  1697. Return String( val )
  1698. Else If TByteType( exprType )
  1699. Return Byte( val )
  1700. Else If TWParamType( exprType )
  1701. Return Long( val )
  1702. Else If TLParamType( exprType )
  1703. Return Long( val )
  1704. Else If TObjectType( exprType )
  1705. If TStringType( expr.exprType )
  1706. Return val
  1707. End If
  1708. EndIf
  1709. Return Super.Eval()
  1710. End Method
  1711. Method Trans$()
  1712. Return _trans.TransCastExpr( Self )
  1713. End Method
  1714. Method ToString$()
  1715. Local t$="TCastExpr(" + ty.ToString()
  1716. If expr t:+","+expr.ToString()
  1717. Return t+")"
  1718. End Method
  1719. End Type
  1720. 'op = '+', '-', '~'
  1721. Type TUnaryExpr Extends TExpr
  1722. Field op$,expr:TExpr
  1723. Method Create:TUnaryExpr( op$,expr:TExpr )
  1724. Self.op=op
  1725. Self.expr=expr
  1726. Return Self
  1727. End Method
  1728. Method Copy:TExpr()
  1729. Return New TUnaryExpr.Create( op,CopyExpr(expr) )
  1730. End Method
  1731. Method Semant:TExpr(options:Int = 0)
  1732. If exprType Return Self
  1733. expr = expr.Semant()
  1734. ' operator overload?
  1735. If TObjectType(expr.exprType) And (op = "+" Or op = "-" Or op = "~~") Then
  1736. 'Local args:TExpr[] = [rhs]
  1737. Try
  1738. Local decl:TFuncDecl = TFuncDecl(TObjectType(expr.exprType).classDecl.FindFuncDecl(op, Null,,,,True,SCOPE_CLASS_HEIRARCHY))
  1739. If decl Then
  1740. Return New TInvokeMemberExpr.Create( expr, decl, Null ).Semant()
  1741. End If
  1742. Catch error:String
  1743. If error.StartsWith("Compile Error") Then
  1744. Throw error
  1745. Else
  1746. Err "Operator " + op + " is not defined for type '" + expr.exprType.ToString() + "'"
  1747. End If
  1748. End Try
  1749. End If
  1750. Select op
  1751. Case "+","-"
  1752. expr=expr.Semant()
  1753. If Not TNumericType( expr.exprType ) Or IsPointerType(expr.exprType) Then
  1754. Err expr.ToString()+" must be numeric for use with unary operator '"+op+"'"
  1755. End If
  1756. exprType=expr.exprType
  1757. ' Remove Var-ness, if required. "expr" will still be "Var"
  1758. If exprType._flags & TType.T_VAR Then
  1759. exprType = exprType.Copy()
  1760. exprType._flags :~ TType.T_VAR
  1761. End If
  1762. Case "~~"
  1763. expr=expr.Semant()
  1764. If Not (TIntegralType(expr.exprType) Or (TEnumType(expr.exprType) And TEnumType(expr.exprType).decl.isFlags)) Or IsPointerType(expr.exprType) Then
  1765. Err "Bitwise complement can only be used with integrals"
  1766. End If
  1767. If TByteType(expr.exprType) Or TShortType(expr.exprType) Then
  1768. expr=expr.SemantAndCast( New TIntType )
  1769. exprType=New TIntType
  1770. Else
  1771. exprType = expr.exprType
  1772. End If
  1773. Case "not"
  1774. expr=expr.SemantAndCast( New TBoolType,CAST_EXPLICIT )
  1775. exprType=New TBoolType
  1776. Default
  1777. InternalErr "TUnaryExpr.Semant"
  1778. End Select
  1779. If TConstExpr( expr ) Return EvalConst()
  1780. Return Self
  1781. End Method
  1782. Method Eval$()
  1783. Local val$=expr.Eval()
  1784. Select op
  1785. Case "~~"
  1786. If TIntType(exprType) Return ~Int( val )
  1787. If TLongType(exprType) Return ~Long( val )
  1788. If TLongIntType(exprType) Return bmx_bitwise_not_longint( val, TLongIntType(exprType).GetSize() )
  1789. If TULongIntType(exprType) Return bmx_bitwise_not_ulongint( val, TULongIntType(exprType).GetSize() )
  1790. ?Not bmxng
  1791. If TUIntType(exprType) Return bmx_bitwise_not_uint( val )
  1792. If TSizeTType(exprType) Return bmx_bitwise_not_sizet( val )
  1793. If TULongType(exprType) Return bmx_bitwise_not_ulong( val )
  1794. ?bmxng
  1795. If TUIntType(exprType) Return ~UInt( val )
  1796. If TSizeTType(exprType) Return ~Size_T( val )
  1797. If TULongType(exprType) Return ~ULong( val )
  1798. ?
  1799. Case "+"
  1800. Return val
  1801. Case "-"
  1802. If val.StartsWith( "-" ) Return val[1..]
  1803. Return "-"+val
  1804. Case "not"
  1805. If val Return ""
  1806. Return "1"
  1807. End Select
  1808. InternalErr "TUnaryExpr.Eval"
  1809. End Method
  1810. Method Trans$()
  1811. Return _trans.TransUnaryExpr( Self )
  1812. End Method
  1813. End Type
  1814. Type TBinaryExpr Extends TExpr
  1815. Field op$
  1816. Field lhs:TExpr
  1817. Field rhs:TExpr
  1818. Method Trans$()
  1819. Return _trans.TransBinaryExpr( Self )
  1820. End Method
  1821. Method ToString$()
  1822. Return "(" + lhs.ToString() + " " + op + " " + rhs.ToString() + ")"
  1823. End Method
  1824. End Type
  1825. ' * / + / & ~ | ^ shl shr sar
  1826. Type TBinaryMathExpr Extends TBinaryExpr
  1827. Method Create:TBinaryMathExpr( op$,lhs:TExpr,rhs:TExpr )
  1828. Self.op=op
  1829. Self.lhs=lhs
  1830. Self.rhs=rhs
  1831. Return Self
  1832. End Method
  1833. Method Copy:TExpr()
  1834. Return New TBinaryMathExpr.Create( op,CopyExpr(lhs),CopyExpr(rhs) )
  1835. End Method
  1836. Method Semant:TExpr(options:Int = 0)
  1837. If exprType Return Self
  1838. lhs=lhs.Semant()
  1839. If TIdentExpr(rhs) Then
  1840. TIdentExpr(rhs).isRhs = True
  1841. End If
  1842. rhs=rhs.Semant()
  1843. ' operator overload?
  1844. If TObjectType(lhs.exprType) Then
  1845. If TObjectType(lhs.exprType).classDecl.IsStruct() and IsPointerType( lhs.exprType, 0, TType.T_POINTER ) Then
  1846. '
  1847. Else
  1848. Local args:TExpr[] = [rhs]
  1849. Try
  1850. Local decl:TFuncDecl = TFuncDecl(TObjectType(lhs.exprType).classDecl.FindFuncDecl(op, args,,,,True,SCOPE_CLASS_HEIRARCHY))
  1851. If decl Then
  1852. Return New TInvokeMemberExpr.Create( lhs, decl, args ).Semant()
  1853. End If
  1854. Catch error:String
  1855. If error.StartsWith("Compile Error") Then
  1856. Throw error
  1857. Else
  1858. Err "Operator " + op + " is not defined between types '" + lhs.exprType.ToString() + "' and '" + rhs.exprType.ToString() + "'"
  1859. End If
  1860. End Try
  1861. End if
  1862. End If
  1863. Local bitEnumOp:Int
  1864. Select op
  1865. Case "&","~~","|","shl","shr","sar"
  1866. If TFloat128Type(lhs.exprType) Then
  1867. exprType=New TInt128Type
  1868. Else If TDouble128Type(lhs.exprType) Then
  1869. exprType=New TInt128Type
  1870. Else If TFloat64Type(lhs.exprType) Then
  1871. exprType=New TInt128Type
  1872. Else If TDoubleType(lhs.exprType) Then
  1873. exprType=New TLongType
  1874. Else If TFloatType(lhs.exprType) Then
  1875. exprType=New TIntType
  1876. Else If TUIntType(lhs.exprType) Then
  1877. exprType=New TUIntType
  1878. Else If TLongType(lhs.exprType) Then
  1879. exprType=New TLongType
  1880. Else If TULongType(lhs.exprType) Then
  1881. exprType=New TULongType
  1882. Else If TLongIntType(lhs.exprType) Then
  1883. exprType=New TLongIntType
  1884. Else If TULongIntType(lhs.exprType) Then
  1885. exprType=New TULongIntType
  1886. Else If TSizeTType(lhs.exprType) Then
  1887. exprType=New TSizeTType
  1888. Else If TWParamType(lhs.exprType) Then
  1889. exprType=New TWParamType
  1890. Else If TLParamType(lhs.exprType) Then
  1891. exprType=New TLParamType
  1892. Else If TEnumType(lhs.exprType) And TEnumType(lhs.exprType).decl.isFlags Then
  1893. exprType = lhs.exprType.Copy()
  1894. bitEnumOp = 2
  1895. Else If TEnumType(rhs.exprType) And TEnumType(rhs.exprType).decl.isFlags Then
  1896. exprType = rhs.exprType.Copy()
  1897. bitEnumOp = 2
  1898. Else
  1899. exprType=New TIntType
  1900. End If
  1901. Case "^"
  1902. If TIntegralType(lhs.exprType) And TIntegralType(rhs.exprType) Then
  1903. exprType=New TLongType
  1904. Else
  1905. exprType=New TDoubleType
  1906. End If
  1907. Default
  1908. exprType=BalanceTypes( lhs.exprType,rhs.exprType )
  1909. If TStringType( exprType )
  1910. If op<>"+"
  1911. Err "Illegal string operator."
  1912. EndIf
  1913. Else If TVoidType( exprType ) Then
  1914. Err "Illegal operation on a void expression."
  1915. Else If Not TNumericType( exprType ) And Not IsPointerType( exprType, 0, TType.T_POINTER ) And Not TArrayType( exprType ) And Not TBoolType( exprType )
  1916. Err "Operator " + op + " is not defined between types '" + lhs.exprType.ToString() + "' and '" + rhs.exprType.ToString() + "'"
  1917. Else If IsPointerType( exprType, 0, TType.T_POINTER ) And op <> "+" And op <> "-" Then
  1918. Err "Illegal expression type."
  1919. Else If IsPointerType( lhs.exprType, 0, TType.T_POINTER ) And IsPointerType( rhs.exprType, 0, TType.T_POINTER ) And op <> "-" Then
  1920. Err "Illegal expression type."
  1921. EndIf
  1922. End Select
  1923. If (op = "+" Or op = "-") And IsPointerType(exprType, 0, TType.T_POINTER) And TNumericType(lhs.exprType) Then
  1924. ' with pointer addition we don't cast the numeric to a pointer
  1925. Else
  1926. lhs=lhs.Cast( exprType, bitEnumOp )
  1927. End If
  1928. If (op = "+" Or op = "-") And IsPointerType(exprType, 0, TType.T_POINTER) And TNumericType(rhs.exprType) Then
  1929. ' with pointer addition we don't cast the numeric to a pointer
  1930. Else
  1931. rhs=rhs.Cast( exprType, bitEnumOp )
  1932. End If
  1933. If IsPointerType( lhs.exprType, 0, TType.T_POINTER ) And IsPointerType( rhs.exprType, 0, TType.T_POINTER ) And op = "-" Then
  1934. exprType = New TIntType
  1935. End If
  1936. If TConstExpr( lhs ) And TConstExpr( rhs ) Return EvalConst()
  1937. If TConstExpr( rhs ) And (op = "/" Or op = "mod") And TIntegralType(rhs.exprType) And Not Long(rhs.Eval()) Then
  1938. Err "Integer division by zero"
  1939. End If
  1940. Return Self
  1941. End Method
  1942. Method Eval$()
  1943. Local lhs$=Self.lhs.Eval()
  1944. Local rhs$=Self.rhs.Eval()
  1945. If TIntType( exprType ) Or TByteType( exprType ) Or TShortType( exprType )
  1946. Local x:Int=Int(lhs),y:Int=Int(rhs)
  1947. Select op
  1948. Case "^" Return Double(lhs)^Double(rhs)
  1949. Case "*" Return x*y
  1950. Case "/"
  1951. If Not y Then
  1952. Err "Integer division by zero"
  1953. End If
  1954. Return x/y
  1955. Case "mod"
  1956. If Not y Then
  1957. Err "Integer division by zero"
  1958. End If
  1959. Return x Mod y
  1960. Case "shl" Return x Shl y
  1961. Case "shr" Return x Shr y
  1962. Case "sar" Return x Sar y
  1963. Case "+" Return x + y
  1964. Case "-" Return x - y
  1965. Case "&" Return x & y
  1966. Case "~~" Return x ~ y
  1967. Case "|" Return x | y
  1968. End Select
  1969. Else If TLongType( exprType ) Or TInt128Type(exprType) Or TWParamType(exprType) Or TLParamType(exprType) Or TLongIntType(exprType)
  1970. Local x:Long=Long(lhs),y:Long=Long(rhs)
  1971. Select op
  1972. Case "^" Return Double(lhs)^Double(rhs)
  1973. Case "*" Return x*y
  1974. Case "/"
  1975. If Not y Then
  1976. Err "Integer division by zero"
  1977. End If
  1978. Return x/y
  1979. Case "mod"
  1980. If Not y Then
  1981. Err "Integer division by zero"
  1982. End If
  1983. Return x Mod y
  1984. Case "shl" Return x Shl y
  1985. Case "shr" Return x Shr y
  1986. Case "sar" Return x Sar y
  1987. Case "+" Return x + y
  1988. Case "-" Return x - y
  1989. Case "&" Return x & y
  1990. Case "~~" Return x ~ y
  1991. Case "|" Return x | y
  1992. End Select
  1993. Else If TSizeTType(exprType)
  1994. ?bmxng
  1995. Local x:Size_T=Size_T(lhs),y:Size_T=Size_T(rhs)
  1996. Select op
  1997. Case "^" Return Double(lhs)^Double(rhs)
  1998. Case "*" Return x*y
  1999. Case "/"
  2000. If Not y Then
  2001. Err "Integer division by zero"
  2002. End If
  2003. Return x/y
  2004. Case "mod"
  2005. If Not y Then
  2006. Err "Integer division by zero"
  2007. End If
  2008. Return x Mod y
  2009. Case "shl" Return x Shl y
  2010. Case "shr" Return x Shr y
  2011. Case "sar" Return x Sar y
  2012. Case "+" Return x + y
  2013. Case "-" Return x - y
  2014. Case "&" Return x & y
  2015. Case "~~" Return x ~ y
  2016. Case "|" Return x | y
  2017. End Select
  2018. ?Not bmxng
  2019. Local opInt:Int = OpToInt(op)
  2020. Select op
  2021. Case "^" Return Double(lhs)^Double(rhs)
  2022. Case "/"
  2023. If Not Long(rhs) Then
  2024. Err "Integer division by zero"
  2025. End If
  2026. Return bmx_binarymathexpr_sizet(opInt, lhs, rhs)
  2027. Case "mod"
  2028. If Not Long(rhs) Then
  2029. Err "Integer division by zero"
  2030. End If
  2031. Return bmx_binarymathexpr_sizet(opInt, lhs, rhs)
  2032. Default
  2033. Return bmx_binarymathexpr_sizet(opInt, lhs, rhs)
  2034. End Select
  2035. ?
  2036. Else If TUIntType(exprType)
  2037. ?bmxng
  2038. Local x:UInt=UInt(lhs),y:UInt=UInt(rhs)
  2039. Select op
  2040. Case "^" Return Double(lhs)^Double(rhs)
  2041. Case "*" Return x*y
  2042. Case "/"
  2043. If Not y Then
  2044. Err "Integer division by zero"
  2045. End If
  2046. Return x/y
  2047. Case "mod"
  2048. If Not y Then
  2049. Err "Integer division by zero"
  2050. End If
  2051. Return x Mod y
  2052. Case "shl" Return x Shl y
  2053. Case "shr" Return x Shr y
  2054. Case "sar" Return x Sar y
  2055. Case "+" Return x + y
  2056. Case "-" Return x - y
  2057. Case "&" Return x & y
  2058. Case "~~" Return x ~ y
  2059. Case "|" Return x | y
  2060. End Select
  2061. ?Not bmxng
  2062. Local opInt:Int = OpToInt(op)
  2063. Select op
  2064. Case "^" Return Double(lhs)^Double(rhs)
  2065. Case "/"
  2066. If Not Long(rhs) Then
  2067. Err "Integer division by zero"
  2068. End If
  2069. Return bmx_binarymathexpr_uint(opInt, lhs, rhs)
  2070. Case "mod"
  2071. If Not Long(rhs) Then
  2072. Err "Integer division by zero"
  2073. End If
  2074. Return bmx_binarymathexpr_uint(opInt, lhs, rhs)
  2075. Default
  2076. Return bmx_binarymathexpr_uint(opInt, lhs, rhs)
  2077. End Select
  2078. ?
  2079. Else If TULongType(exprType) Or TULongIntType(exprType)
  2080. ?bmxng
  2081. Local x:ULong=ULong(lhs),y:ULong=ULong(rhs)
  2082. Select op
  2083. Case "^" Return Double(lhs)^Double(rhs)
  2084. Case "*" Return x*y
  2085. Case "/"
  2086. If Not y Then
  2087. Err "Integer division by zero"
  2088. End If
  2089. Return x/y
  2090. Case "mod"
  2091. If Not y Then
  2092. Err "Integer division by zero"
  2093. End If
  2094. Return x Mod y
  2095. Case "shl" Return x Shl y
  2096. Case "shr" Return x Shr y
  2097. Case "sar" Return x Sar y
  2098. Case "+" Return x + y
  2099. Case "-" Return x - y
  2100. Case "&" Return x & y
  2101. Case "~~" Return x ~ y
  2102. Case "|" Return x | y
  2103. End Select
  2104. ?Not bmxng
  2105. Local opInt:Int = OpToInt(op)
  2106. Select op
  2107. Case "^" Return Double(lhs)^Double(rhs)
  2108. Case "/"
  2109. If Not Long(rhs) Then
  2110. Err "Integer division by zero"
  2111. End If
  2112. Return bmx_binarymathexpr_ulong(opInt, lhs, rhs)
  2113. Case "mod"
  2114. If Not Long(rhs) Then
  2115. Err "Integer division by zero"
  2116. End If
  2117. Return bmx_binarymathexpr_ulong(opInt, lhs, rhs)
  2118. Default
  2119. Return bmx_binarymathexpr_ulong(opInt, lhs, rhs)
  2120. End Select
  2121. ?
  2122. Else If TFloatType( exprType )
  2123. Local x:Double=Double(lhs),y:Double=Double(rhs)
  2124. Select op
  2125. Case "^" Return Double(x^y)
  2126. Case "*" Return Float(x * y)
  2127. Case "/" Return Float(x / y)
  2128. Case "mod" Return Float(x Mod y)
  2129. Case "+" Return Float(x + y)
  2130. Case "-" Return Float(x - y)
  2131. End Select
  2132. Else If TDoubleType( exprType ) Or TFloat128Type(exprType) Or TDouble128Type(exprType) Or TFloat64Type(exprType)
  2133. Local x:Double=Double(lhs),y:Double=Double(rhs)
  2134. Select op
  2135. Case "^" Return x^y
  2136. Case "*" Return x * y
  2137. Case "/" Return x / y
  2138. Case "mod" Return x Mod y
  2139. Case "+" Return x + y
  2140. Case "-" Return x - y
  2141. End Select
  2142. Else If TStringType( exprType )
  2143. Select op
  2144. Case "+"
  2145. _appInstance.removeStringConst(lhs)
  2146. _appInstance.removeStringConst(rhs)
  2147. Return lhs+rhs
  2148. End Select
  2149. Else If TEnumType( exprType )
  2150. Local x:Long=Long(lhs),y:Long=Long(rhs)
  2151. Select op
  2152. Case "shl" Return x Shl y
  2153. Case "shr" Return x Shr y
  2154. Case "sar" Return x Sar y
  2155. Case "+" Return x + y
  2156. Case "-" Return x - y
  2157. Case "&" Return x & y
  2158. Case "~~" Return x ~ y
  2159. Case "|" Return x | y
  2160. End Select
  2161. EndIf
  2162. InternalErr "TBinaryMathExpr.Eval"
  2163. End Method
  2164. End Type
  2165. '=,<>,<,<=,>,>=
  2166. Type TBinaryCompareExpr Extends TBinaryExpr
  2167. Field ty:TType
  2168. Method Create:TBinaryCompareExpr( op$,lhs:TExpr,rhs:TExpr )
  2169. Self.op=op
  2170. Self.lhs=lhs
  2171. Self.rhs=rhs
  2172. Return Self
  2173. End Method
  2174. Method Copy:TExpr()
  2175. Return New TBinaryCompareExpr.Create( op,CopyExpr(lhs),CopyExpr(rhs) )
  2176. End Method
  2177. Method Semant:TExpr(options:Int = 0)
  2178. If exprType Return Self
  2179. lhs=lhs.Semant()
  2180. rhs=rhs.Semant()
  2181. ' operator overload?
  2182. If TObjectType(lhs.exprType) Then
  2183. If TNullExpr(rhs) And TObjectType(lhs.exprType).classDecl.IsStruct() Then
  2184. If op = "=" Then
  2185. ty=New TBoolType
  2186. exprType=New TBoolType
  2187. If Not IsPointerType(lhs.exprType, 0, TType.T_POINTER) Then
  2188. lhs = New TConstExpr.Create(New TIntType, 1).Semant()
  2189. End If
  2190. rhs = New TConstExpr.Create(New TIntType, 0).Semant()
  2191. Return Self
  2192. Else
  2193. op = "<>"
  2194. ty = New TBoolType
  2195. exprType=New TBoolType
  2196. If Not IsPointerType(lhs.exprType, 0, TType.T_POINTER) Then
  2197. lhs = New TConstExpr.Create(New TIntType, 1).Semant()
  2198. End If
  2199. rhs = New TConstExpr.Create(New TIntType, 0).Semant()
  2200. Return Self
  2201. End If
  2202. End If
  2203. Local args:TExpr[] = [rhs]
  2204. Try
  2205. Local decl:TFuncDecl = TFuncDecl(TObjectType(lhs.exprType).classDecl.FindFuncDecl(op, args,,,,True,SCOPE_CLASS_HEIRARCHY))
  2206. If decl Then
  2207. Return New TInvokeMemberExpr.Create( lhs, decl, args ).Semant()
  2208. End If
  2209. Catch error:String
  2210. ' Structs must define an operator overload for the given op
  2211. If TObjectType(lhs.exprType).classDecl.IsStruct() Then
  2212. Err "No overloaded operator '" + op + "' found for " + TObjectType(lhs.exprType).classDecl.ToString()
  2213. End If
  2214. ' otherwise, no overload, continue...
  2215. End Try
  2216. Else If (TArrayType(lhs.exprType) And TArrayType(lhs.exprType).isStatic) Or (TArrayType(rhs.exprType) And TArrayType(rhs.exprType).isStatic) Then
  2217. Err "Static arrays cannot be compared"
  2218. End If
  2219. ty=BalanceTypes( lhs.exprType,rhs.exprType, False )
  2220. lhs=lhs.Cast( ty )
  2221. rhs=rhs.Cast( ty )
  2222. exprType=New TBoolType
  2223. If TConstExpr( lhs ) And TConstExpr( rhs ) Return EvalConst()
  2224. Return Self
  2225. End Method
  2226. Method Eval$()
  2227. Local r:Int=-1
  2228. If TBoolType( ty )
  2229. Local lhs:Int=Int(Self.lhs.Eval())
  2230. Local rhs:Int=Int(Self.rhs.Eval())
  2231. Select op
  2232. Case "=" r=(lhs= rhs)
  2233. Case "<>" r=(lhs<>rhs)
  2234. End Select
  2235. Else If TIntType( ty )
  2236. Local lhs:Int=Int( Self.lhs.Eval() )
  2237. Local rhs:Int=Int( Self.rhs.Eval() )
  2238. Select op
  2239. Case "=" r=(lhs= rhs)
  2240. Case "<>" r=(lhs<>rhs)
  2241. Case "<" r=(lhs< rhs)
  2242. Case "<=", "=<" r=(lhs<=rhs)
  2243. Case ">" r=(lhs> rhs)
  2244. Case ">=", "=>" r=(lhs>=rhs)
  2245. End Select
  2246. Else If TLongType( ty ) Or TSizeTType( ty ) Or TUIntType( ty ) Or TULongType( ty ) Or TInt128Type(ty) Or TWParamType(ty) Or TLParamType(ty) Or TLongIntType(ty) Or TULongIntType(ty)
  2247. Local lhs:Long=Long( Self.lhs.Eval() )
  2248. Local rhs:Long=Long( Self.rhs.Eval() )
  2249. Select op
  2250. Case "=" r=(lhs= rhs)
  2251. Case "<>" r=(lhs<>rhs)
  2252. Case "<" r=(lhs< rhs)
  2253. Case "<=", "=<" r=(lhs<=rhs)
  2254. Case ">" r=(lhs> rhs)
  2255. Case ">=", "=>" r=(lhs>=rhs)
  2256. End Select
  2257. Else If TFloatType( ty )
  2258. Local lhs:Float=Float( Self.lhs.Eval() )
  2259. Local rhs:Float=Float( Self.rhs.Eval() )
  2260. Select op
  2261. Case "=" r=(lhs= rhs)
  2262. Case "<>" r=(lhs<>rhs)
  2263. Case "<" r=(lhs< rhs)
  2264. Case "<=", "=<" r=(lhs<=rhs)
  2265. Case ">" r=(lhs> rhs)
  2266. Case ">=", "=>" r=(lhs>=rhs)
  2267. End Select
  2268. Else If TDoubleType( ty ) Or TFloat128Type(ty) Or TDouble128Type(ty) Or TFloat64Type(ty)
  2269. Local lhs:Double=Double( Self.lhs.Eval() )
  2270. Local rhs:Double=Double( Self.rhs.Eval() )
  2271. Select op
  2272. Case "=" r=(lhs= rhs)
  2273. Case "<>" r=(lhs<>rhs)
  2274. Case "<" r=(lhs< rhs)
  2275. Case "<=", "=<" r=(lhs<=rhs)
  2276. Case ">" r=(lhs> rhs)
  2277. Case ">=", "=>" r=(lhs>=rhs)
  2278. End Select
  2279. Else If TStringType( ty )
  2280. Local lhs:String=String( Self.lhs.Eval() )
  2281. Local rhs:String=String( Self.rhs.Eval() )
  2282. Select op
  2283. Case "=" r=(lhs= rhs)
  2284. Case "<>" r=(lhs<>rhs)
  2285. Case "<" r=(lhs< rhs)
  2286. Case "<=", "=<" r=(lhs<=rhs)
  2287. Case ">" r=(lhs> rhs)
  2288. Case ">=", "=>" r=(lhs>=rhs)
  2289. End Select
  2290. EndIf
  2291. If r=1 Return "1"
  2292. If r=0 Return ""
  2293. InternalErr "TBinaryCompareExpr.Eval"
  2294. End Method
  2295. End Type
  2296. 'and, or
  2297. Type TBinaryLogicExpr Extends TBinaryExpr
  2298. Method Create:TBinaryLogicExpr( op$,lhs:TExpr,rhs:TExpr )
  2299. Self.op=op
  2300. Self.lhs=lhs
  2301. Self.rhs=rhs
  2302. Return Self
  2303. End Method
  2304. Method Copy:TExpr()
  2305. Return New TBinaryLogicExpr.Create( op,CopyExpr(lhs),CopyExpr(rhs) )
  2306. End Method
  2307. Method Semant:TExpr(options:Int = 0)
  2308. If exprType Return Self
  2309. lhs=lhs.SemantAndCast( New TBoolType,CAST_EXPLICIT )
  2310. rhs=rhs.SemantAndCast( New TBoolType,CAST_EXPLICIT )
  2311. exprType=New TBoolType
  2312. If TConstExpr( lhs ) And TConstExpr( rhs ) Return EvalConst()
  2313. Return Self
  2314. End Method
  2315. Method Eval$()
  2316. Select op
  2317. Case "and" If lhs.Eval() And rhs.Eval() Return "1" Else Return ""
  2318. Case "or" If lhs.Eval() Or rhs.Eval() Return "1" Else Return ""
  2319. End Select
  2320. InternalErr "TBinaryLogicExpr.Eval"
  2321. End Method
  2322. End Type
  2323. Type TIndexExpr Extends TExpr
  2324. Field expr:TExpr
  2325. Field index:TExpr[]
  2326. Method Create:TIndexExpr( expr:TExpr,index:TExpr[] )
  2327. Self.expr=expr
  2328. Self.index=index
  2329. Return Self
  2330. End Method
  2331. Method Copy:TExpr()
  2332. If exprType Return Self
  2333. Local ind:TExpr[]
  2334. For Local i:Int = 0 Until index.length
  2335. ind = ind + [CopyExpr(index[i])]
  2336. Next
  2337. Return New TIndexExpr.Create( CopyExpr(expr),ind )
  2338. End Method
  2339. Method _Semant:TExpr(set:Int, rhs:TExpr)
  2340. If exprType Return Self
  2341. expr=expr.Semant()
  2342. ' for functions and index access, use a new local variable
  2343. If Not TVarExpr(expr) And Not TMemberVarExpr(expr) Then
  2344. Local tmp:TLocalDecl=New TLocalDecl.Create( "", TType.MapVarPointerToPointerType(expr.exprType.Copy()), expr,, True )
  2345. tmp.Semant()
  2346. Local v:TVarExpr = New TVarExpr.Create( tmp )
  2347. expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), v ).Semant()
  2348. End If
  2349. For Local i:Int = 0 Until index.length
  2350. If Not TObjectType(expr.exprType) And Not (TNumericType(expr.exprType) And IsPointerType( expr.exprType, 0 , TType.T_POINTER | TType.T_VARPTR)) Then
  2351. index[i]=index[i].SemantAndCast( New TUIntType, True )
  2352. Else
  2353. index[i]=index[i].Semant()
  2354. End If
  2355. Next
  2356. ' operator overload?
  2357. If TObjectType(expr.exprType) And Not IsPointerType( expr.exprType, 0 , TType.T_POINTER | TType.T_VARPTR) Then
  2358. Local args:TExpr[]
  2359. Local op:String
  2360. If set Then
  2361. args = index + [rhs]
  2362. op = "[]="
  2363. Else
  2364. args = index
  2365. op = "[]"
  2366. End If
  2367. Try
  2368. Local decl:TFuncDecl = TFuncDecl(TObjectType(expr.exprType).classDecl.FindFuncDecl(op, args,,,,True,SCOPE_CLASS_HEIRARCHY))
  2369. If decl Then
  2370. Return New TInvokeMemberExpr.Create( expr, decl, args ).Semant()
  2371. End If
  2372. Catch error:String
  2373. If error.StartsWith("Compile Error") Then
  2374. Throw error
  2375. Else
  2376. Local istr:String
  2377. Local vstr:String
  2378. If index.length = 1 Then
  2379. istr = " with '" + index[0].exprType.ToString() + "' index"
  2380. Else
  2381. For Local i:TExpr = EachIn index
  2382. istr :+ ", '" + i.exprType.ToString() + "'"
  2383. Next
  2384. istr = " with " + istr[1..] + " indices"
  2385. End If
  2386. If set Then vstr = " and '" + rhs.exprType.ToString() + "' value"
  2387. Err "Operator " + op + istr + vstr + " is not defined for type '" + expr.exprType.ToString() + "'"
  2388. End If
  2389. End Try
  2390. End If
  2391. If TStringType( expr.exprType )
  2392. exprType=New TIntType
  2393. If index.length > 1 Then
  2394. Err "Illegal subexpression for string index"
  2395. End If
  2396. Else If TArrayType( expr.exprType )
  2397. exprType= TArrayType( expr.exprType ).elemType
  2398. If index.Length <> TArrayType( expr.exprType ).dims Then
  2399. Err "Wrong number of indices for array. Expected " + TArrayType( expr.exprType ).dims + ", got " + index.Length
  2400. End If
  2401. If TArrayType( expr.exprType ).dims > 1 Then
  2402. ' a multi-dimensional array of arrays is slightly more complex
  2403. If TArrayType(exprType) Then
  2404. Local sizeExpr:TExpr = New TArraySizeExpr.Create(expr, Null, index)
  2405. index = [sizeExpr]
  2406. Local tmp:TLocalDecl=New TLocalDecl.Create( "", NewPointerType(TType.T_UINT), sizeExpr,,True )
  2407. TArraySizeExpr(sizeExpr).val = tmp
  2408. Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), Self ).Semant()
  2409. stmt.exprType = exprType
  2410. Return stmt
  2411. Else
  2412. Local sizeExpr:TExpr = New TArraySizeExpr.Create(expr, Null, index).Semant()
  2413. index = [sizeExpr]
  2414. Local tmp:TLocalDecl=New TLocalDecl.Create( "", NewPointerType(TType.T_UINT), sizeExpr,,True )
  2415. TArraySizeExpr(sizeExpr).val = tmp
  2416. Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), Self ).Semant()
  2417. stmt.exprType = exprType
  2418. Return stmt
  2419. End If
  2420. End If
  2421. Else If TNumericType(expr.exprType) And IsPointerType( expr.exprType, 0 , TType.T_POINTER | TType.T_VARPTR)' And Not TFunctionPtrType( expr.exprType )
  2422. exprType=TType.MapPointerToPrim(TNumericType(expr.exprType))
  2423. Else If TObjectType(expr.exprType) And TObjectType(expr.exprType).classDecl.IsStruct() And IsPointerType( expr.exprType, 0 , TType.T_POINTER | TType.T_VARPTR)' And Not TFunctionPtrType( expr.exprType )
  2424. If IsPointerType( expr.exprType, 0 , TType.T_POINTER) Then
  2425. exprType = TType.MapFromPointer(expr.exprType)
  2426. Else
  2427. exprType = expr.exprType
  2428. End If
  2429. Else
  2430. Err "Expression of type '" + expr.exprType.ToString() + "' cannot be indexed"
  2431. EndIf
  2432. Return Self
  2433. End Method
  2434. Method Semant:TExpr(options:Int = 0)
  2435. Return _Semant(False, Null)
  2436. End Method
  2437. Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
  2438. Return _Semant(True, rhs)
  2439. End Method
  2440. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  2441. Local ex:TExpr = Semant()
  2442. If TArrayType( expr.exprType ) Then
  2443. If TFunctionPtrType(exprType) Then
  2444. exprType = TFunctionPtrType(exprType).func.retType
  2445. Else
  2446. If funcCall Then
  2447. Err "Expression of type '" + exprType.ToString() + "' cannot be invoked."
  2448. End If
  2449. End If
  2450. End If
  2451. Return ex
  2452. End Method
  2453. Method Trans$()
  2454. Return _trans.TransIndexExpr( Self )
  2455. End Method
  2456. Method TransVar$()
  2457. Return _trans.TransIndexExpr( Self )
  2458. End Method
  2459. Method ToString$()
  2460. Return "<TIndexExpr<"+ expr.ToString() +"[" + index[0].ToString() + "]>>"
  2461. End Method
  2462. End Type
  2463. Type TSliceExpr Extends TExpr
  2464. Field expr:TExpr
  2465. Field from:TExpr
  2466. Field term:TExpr
  2467. Method Create:TSliceExpr( expr:TExpr,from:TExpr,term:TExpr )
  2468. Self.expr=expr
  2469. Self.from=from
  2470. Self.term=term
  2471. Return Self
  2472. End Method
  2473. Method Copy:TExpr()
  2474. Return New TSliceExpr.Create( CopyExpr(expr),CopyExpr(from),CopyExpr(term) )
  2475. End Method
  2476. Method Semant:TExpr(options:Int = 0)
  2477. If exprType Return Self
  2478. expr=expr.Semant()
  2479. If (TArrayType( expr.exprType ) And TArrayType( expr.exprType ).dims = 1) Or TStringType( expr.exprType )
  2480. If from from=from.SemantAndCast( New TIntType )
  2481. If term term=term.SemantAndCast( New TIntType )
  2482. exprType=expr.exprType
  2483. ' remove var-ness
  2484. If exprType._flags & TType.T_VAR Then
  2485. exprType = exprType.Copy()
  2486. exprType._flags :~ TType.T_VAR
  2487. End If
  2488. Else
  2489. Err "Slices can only be used with strings or one dimensional arrays"
  2490. EndIf
  2491. ' If TConstExpr( expr ) And TConstExpr( from ) And TConstExpr( term ) Return EvalConst()
  2492. Return Self
  2493. End Method
  2494. Method Eval$()
  2495. Local from:Int=Int( Self.from.Eval() )
  2496. Local term:Int=Int( Self.term.Eval() )
  2497. If TStringType( expr.exprType )
  2498. Return expr.Eval()[ from..term ]
  2499. Else If TArrayType( expr.exprType )
  2500. Todo
  2501. EndIf
  2502. End Method
  2503. Method Trans$()
  2504. Return _trans.TransSliceExpr( Self )
  2505. End Method
  2506. End Type
  2507. Type TArrayExpr Extends TExpr
  2508. Field exprs:TExpr[]
  2509. Field toType:TType
  2510. Method Create:TArrayExpr( exprs:TExpr[] )
  2511. Self.exprs=exprs
  2512. Return Self
  2513. End Method
  2514. Method Copy:TExpr()
  2515. Local expr:TArrayExpr = New TArrayExpr.Create( CopyArgs(exprs) )
  2516. expr.toType = toType
  2517. Return expr
  2518. End Method
  2519. Method Semant:TExpr(options:Int = 0)
  2520. If exprType Return Self
  2521. If TIdentExpr(exprs[0]) Then
  2522. TIdentExpr(exprs[0]).isRhs = True
  2523. End If
  2524. exprs[0]=exprs[0].Semant()
  2525. Local ty:TType=exprs[0].exprType
  2526. ' convert from varptr to ptr if required
  2527. ty = TType.MapVarPointerToPointerType(ty.Copy())
  2528. If TInvokeExpr(exprs[0]) And Not TInvokeExpr(exprs[0]).invokedWithBraces Then
  2529. ty = New TFunctionPtrType
  2530. Local cp:TDecl = TInvokeExpr(exprs[0]).decl
  2531. TInvokeExpr(exprs[0]).decl = TFuncDecl(TInvokeExpr(exprs[0]).decl.Copy())
  2532. TInvokeExpr(exprs[0]).decl.actual = cp
  2533. TInvokeExpr(exprs[0]).decl.attrs :| FUNC_PTR
  2534. TFunctionPtrType(ty).func = TInvokeExpr(exprs[0]).decl
  2535. For Local i:Int=1 Until exprs.Length
  2536. If TIdentExpr(exprs[1]) Then
  2537. TIdentExpr(exprs[1]).isRhs = True
  2538. End If
  2539. exprs[i]=exprs[i].Semant()
  2540. If TInvokeExpr(exprs[i]) And Not TInvokeExpr(exprs[i]).invokedWithBraces
  2541. cp = TInvokeExpr(exprs[i]).decl
  2542. TInvokeExpr(exprs[i]).decl = TFuncDecl(TInvokeExpr(exprs[i]).decl.Copy())
  2543. TInvokeExpr(exprs[i]).decl.actual = cp
  2544. TInvokeExpr(exprs[i]).decl.attrs :| FUNC_PTR
  2545. ty=BalanceTypes( ty, New TFunctionPtrType )
  2546. Else
  2547. ty=BalanceTypes( ty,exprs[i].exprType )
  2548. End If
  2549. Next
  2550. Else
  2551. For Local i:Int=1 Until exprs.Length
  2552. exprs[i]=exprs[i].Semant()
  2553. ty=BalanceTypes( ty,exprs[i].exprType )
  2554. Next
  2555. End If
  2556. Local comp:Int = True
  2557. Local last:TType
  2558. Local base:TType
  2559. For Local i:Int=0 Until exprs.Length
  2560. Local expr:TExpr = exprs[i]
  2561. ' don't cast null types
  2562. If TNullType(expr.exprType) <> Null Then
  2563. Err "Auto array element has no type"
  2564. End If
  2565. Local ety:TType = expr.exprType
  2566. If TBoolType(ety) Then
  2567. ety = New TIntType
  2568. End If
  2569. If TObjectType(ety) And Not base Then
  2570. base = ety
  2571. End If
  2572. If last <> Null And Not last.EqualsType(ety) Then
  2573. If TObjectType(ety) Then
  2574. If base.ExtendsType(ety) Then
  2575. base = ety
  2576. Else If Not ety.ExtendsType(base) Then
  2577. Err "Auto array elements must be compatible types : Index " + i
  2578. End If
  2579. Else
  2580. If (Not TConstExpr(expr) And Not IsNumericType(ety)) Or (TConstExpr(expr) And IsNumericType(ety) And Not TConstExpr(expr).CompatibleWithType(ty)) Then
  2581. Err "Auto array elements must have identical types : Index " + i
  2582. End If
  2583. End If
  2584. End If
  2585. If toType And TConstExpr(expr) And Not TConstExpr(expr).CompatibleWithType(toType) Then
  2586. comp = False
  2587. End If
  2588. last = ety
  2589. exprs[i]=expr.Cast( ty )
  2590. Next
  2591. If comp And toType Then
  2592. exprType=New TArrayType.Create( toType )
  2593. Else
  2594. exprType=New TArrayType.Create( ty )
  2595. End If
  2596. Return Self
  2597. End Method
  2598. Method Trans$()
  2599. Return _trans.TransArrayExpr( Self )
  2600. End Method
  2601. End Type
  2602. Type TArraySizeExpr Extends TExpr
  2603. Field expr:TExpr
  2604. Field val:TDecl
  2605. Field index:TExpr[]
  2606. Method Create:TArraySizeExpr( expr:TExpr, val:TDecl, index:TExpr[] )
  2607. Self.expr=expr
  2608. Self.val=val
  2609. Self.index=index
  2610. Return Self
  2611. End Method
  2612. Method Copy:TExpr()
  2613. Local ind:TExpr[]
  2614. For Local i:Int = 0 Until index.length
  2615. ind = ind + [CopyExpr(index[i])]
  2616. Next
  2617. Return New TArraySizeExpr.Create( CopyExpr(expr), val, ind )
  2618. End Method
  2619. Method Semant:TExpr(options:Int = 0)
  2620. If exprType Return Self
  2621. expr=expr.Semant()
  2622. For Local i:Int = 0 Until index.length
  2623. index[i]=index[i].SemantAndCast( New TUIntType )
  2624. Next
  2625. exprType=NewPointerType(TType.T_UINT)
  2626. Return Self
  2627. End Method
  2628. Method Trans$()
  2629. Return _trans.TransArraySizeExpr( Self )
  2630. End Method
  2631. Method ToString$()
  2632. Return expr.ToString() + ".Size"
  2633. End Method
  2634. End Type
  2635. Type TIdentTypeExpr Extends TExpr
  2636. Field cdecl:TClassDecl
  2637. Method Create:TIdentTypeExpr( ty:TType )
  2638. Self.exprType=ty
  2639. Return Self
  2640. End Method
  2641. Method Copy:TExpr()
  2642. Return New TIdentTypeExpr.Create( exprType )
  2643. End Method
  2644. Method _Semant()
  2645. If cdecl Return
  2646. exprType=exprType.Semant()
  2647. If TArrayType(exprType) And TObjectType(TArrayType(exprType).elemType) Then
  2648. cdecl=TObjectType(TArrayType(exprType).elemType).classDecl
  2649. Else
  2650. cdecl=exprType.GetClass()
  2651. End If
  2652. If Not cdecl InternalErr "TIdentTypeExpr._Semant"
  2653. End Method
  2654. Method Semant:TExpr(options:Int = 0)
  2655. _Semant
  2656. Err "Expression can't be used in this way"
  2657. End Method
  2658. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  2659. _Semant
  2660. If args.Length=1 And args[0] Then
  2661. If TArrayType(exprType) Then
  2662. Return args[0].Cast( exprType,CAST_EXPLICIT )
  2663. Else
  2664. Return args[0].Cast( cdecl.objectType,CAST_EXPLICIT )
  2665. End If
  2666. End If
  2667. Err "Illegal number of arguments for type conversion"
  2668. End Method
  2669. Method SemantScope:TScopeDecl()
  2670. _Semant
  2671. Return cdecl
  2672. End Method
  2673. Method Trans$()
  2674. Return _trans.TransIdentTypeExpr( Self )
  2675. End Method
  2676. Method Cast:TExpr( ty:TType,castFlags:Int=0 )
  2677. Err "Unable to convert from Type to " + ty.ToString()
  2678. End Method
  2679. End Type
  2680. Type TIdentExpr Extends TExpr
  2681. Field ident$
  2682. Field expr:TExpr
  2683. Field scope:TScopeDecl
  2684. Field static:Int
  2685. Field isArg:Int
  2686. Field isRhs:Int
  2687. Field fixedScope:Int
  2688. Field _identLower:String
  2689. Field unknownIdentsEvalFalse:Int
  2690. Method IdentLower:String()
  2691. If Not _identLower Then
  2692. _identLower = ident.ToLower()
  2693. End If
  2694. Return _identLower
  2695. End Method
  2696. Method Create:TIdentExpr( ident$,expr:TExpr=Null, _identLower:String = Null, unknownIdentsEvalFalse:Int = False )
  2697. Self.ident=ident
  2698. Self.expr=expr
  2699. Self._identLower = _identLower
  2700. Self.unknownIdentsEvalFalse = unknownIdentsEvalFalse
  2701. Return Self
  2702. End Method
  2703. Method Copy:TExpr()
  2704. Local i:TIdentExpr = New TIdentExpr.Create( ident,CopyExpr(expr), _identLower )
  2705. i.static = static
  2706. i.isArg = isArg
  2707. i.isRhs = isRhs
  2708. i.fixedScope = fixedScope
  2709. Return i
  2710. End Method
  2711. Method ToString$()
  2712. Local t$="TIdentExpr(~q"+ident+"~q"
  2713. If expr t:+","+expr.ToString()
  2714. Return t+")"
  2715. End Method
  2716. Method _Semant()
  2717. If scope Return
  2718. If expr Then
  2719. scope=expr.SemantScope()
  2720. If scope
  2721. static=True
  2722. Else
  2723. expr=expr.Semant()
  2724. static = expr.static
  2725. scope=expr.exprType.GetClassScope()
  2726. If Not scope Then
  2727. Local e:String = "Member '" + ident + "' Not found in "
  2728. If expr.exprType Then
  2729. e :+ "type '" + expr.exprType.ToString() + "'"
  2730. Else
  2731. e :+ "'" + expr.ToString() + "'"
  2732. End If
  2733. Err e
  2734. Else
  2735. scope.Semant
  2736. End If
  2737. End If
  2738. fixedScope = True
  2739. Else
  2740. scope=_env
  2741. ' determines if access is via static (like Function, or via a Type)
  2742. ' However, for Field->Field access this is not strictly true.
  2743. If _env.FuncScope()=Null
  2744. static = TModuleDecl(_env) = Null
  2745. Else
  2746. static=_env.FuncScope().IsStatic()
  2747. End If
  2748. End If
  2749. End Method
  2750. Method IdentScope:TScopeDecl()
  2751. If Not expr Return _env
  2752. Local scope:TScopeDecl=expr.SemantScope()
  2753. If scope
  2754. expr=Null
  2755. Else
  2756. expr=expr.Semant()
  2757. scope=expr.exprType.GetClass()
  2758. If Not scope Err "Expression has no scope."
  2759. EndIf
  2760. Return scope
  2761. End Method
  2762. Method IdentErr( errorDetails:String = Null )
  2763. If errorDetails Then
  2764. Err errorDetails
  2765. Else
  2766. Err "Identifier '"+ident+"' not found."
  2767. End If
  2768. End Method
  2769. Method IdentNotFound()
  2770. End Method
  2771. Method IsVar()
  2772. InternalErr "TIdentExpr.IsVar"
  2773. End Method
  2774. Method Semant:TExpr(options:Int = 0)
  2775. Return SemantSet( "",Null, options )
  2776. End Method
  2777. Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
  2778. _Semant
  2779. Select options
  2780. Case OPTION_WANT_LOOP_LABEL
  2781. Local loopLabel:String = "#" + IdentLower()
  2782. ' maybe it's a loop label?
  2783. Local stmt:TLoopStmt = TLoopStmt(scope.FindLoop(loopLabel))
  2784. If stmt Then
  2785. Return New TLoopLabelExpr.Create(stmt)
  2786. End If
  2787. Return Self
  2788. Case OPTION_WANT_DATA_LABEL
  2789. Local loopLabel:String = "#" + IdentLower()
  2790. ' maybe it's a data label?
  2791. Local ddecl:TDefDataDecl = TDefDataDecl(_appInstance.FindDataLabel(loopLabel))
  2792. If ddecl Then
  2793. Return New TDataLabelExpr.Create(ddecl)
  2794. End If
  2795. Return Self
  2796. Default
  2797. 'Local scope:TScopeDecl=IdentScope()
  2798. Local vdecl:TValDecl=scope.FindValDecl( IdentLower(), static )
  2799. If TLocalDecl( vdecl )
  2800. ' local variable should (at least) be in the same function scope.
  2801. If vdecl.FuncScope() <> scope.FuncScope() Then
  2802. ' or the local can be in localmain..
  2803. If TModuleDecl(scope) And vdecl.FuncScope() And vdecl.FuncScope().ident = "__LocalMain" Then
  2804. ' ok
  2805. Else
  2806. vdecl = Null
  2807. End If
  2808. End If
  2809. End If
  2810. If vdecl And fixedScope And static Then
  2811. If TClassDecl(vdecl.scope) And TClassDecl(scope) Then
  2812. If Not TClassDecl(scope).ExtendsClass(TClassDecl(vdecl.scope)) Then
  2813. vdecl = Null
  2814. End If
  2815. Else
  2816. If vdecl.scope <> scope Then
  2817. vdecl = Null
  2818. End If
  2819. End If
  2820. End If
  2821. If vdecl
  2822. If op And TLocalDecl( vdecl )
  2823. Local ldecl:TLocalDecl = TLocalDecl( vdecl )
  2824. If Not ldecl.volatile Then
  2825. Local tryStmtDecl:TTryStmtDecl = scope.FindTry()
  2826. If tryStmtDecl And (Not ldecl.declaredInTry Or tryStmtDecl <> ldecl.declaredInTry) Then
  2827. ldecl.volatile = True
  2828. End If
  2829. End If
  2830. Else If TConstDecl( vdecl )
  2831. ' If rhs Err "Constant '"+ident+"' cannot be modified."
  2832. ' Return New TConstExpr.Create( vdecl.ty,TConstDecl( vdecl ).value ).Semant()
  2833. If rhs Err "Constant '"+ident+"' cannot be modified."
  2834. Local cexpr:TConstExpr =New TConstExpr.Create( vdecl.ty,TConstDecl( vdecl ).value )
  2835. If Not static And (TInvokeExpr( expr ) Or TInvokeMemberExpr( expr )) Return New TStmtExpr.Create( New TExprStmt.Create( expr ),cexpr ).Semant()
  2836. Return cexpr.Semant()
  2837. Else If TFieldDecl( vdecl )
  2838. If static Err "Field '"+ident+"' cannot be accessed from here."
  2839. If expr Return New TMemberVarExpr.Create( expr,TVarDecl( vdecl ) ).Semant()
  2840. ' If expr Return New TMemberVarExpr.Create( expr,TVarDecl( vdecl ) ).Semant()
  2841. ' If scope<>_env Or Not _env.FuncScope() Or _env.FuncScope().IsStatic() Err "Field '"+ident+"' cannot be accessed from here."
  2842. EndIf
  2843. Return New TVarExpr.Create( TVarDecl( vdecl ) ).Semant()
  2844. EndIf
  2845. Local args:TExpr[]
  2846. If rhs args=[rhs]
  2847. Local decl:TDecl = TDecl(scope.FindDecl(IdentLower()))
  2848. ' maybe it's an enum?
  2849. Local edecl:TEnumValueDecl = TEnumValueDecl(decl)
  2850. If edecl Then
  2851. Return New TIdentEnumExpr.Create(edecl)
  2852. End If
  2853. Local fdecl:TFuncDecl
  2854. Try
  2855. fdecl=scope.FindFuncDecl( IdentLower(),args, , isArg, True,True,SCOPE_ALL )
  2856. Catch errorMessage:String
  2857. If errorMessage.StartsWith("Compile Error") Then
  2858. Throw errorMessage
  2859. End If
  2860. End Try
  2861. If fdecl
  2862. If Not isArg And Not fdecl.maybeFunctionPtr Err "Identifier '"+ident+"' cannot be used in this way."
  2863. fdecl.maybeFunctionPtr = False
  2864. If Not fdecl.IsStatic()
  2865. If static Err "Method '"+ident+"' cannot be accessed from here."
  2866. 'If expr Return New TInvokeMemberExpr.Create( expr,fdecl,args, False ).Semant()
  2867. EndIf
  2868. Return New TInvokeExpr.Create( fdecl,args, False, isArg, isRhs ).Semant()
  2869. End If
  2870. ' maybe it's a classdecl?
  2871. Local cdecl:TClassDecl = TClassDecl(decl)
  2872. If cdecl Then
  2873. Local e:TIdentTypeExpr = New TIdentTypeExpr.Create(cdecl.objectType)
  2874. e.cdecl = cdecl
  2875. Return e
  2876. End If
  2877. If unknownIdentsEvalFalse Then
  2878. Return New TConstExpr.Create( New TIntType, 0 ).Semant()
  2879. End If
  2880. End Select
  2881. IdentErr
  2882. End Method
  2883. Method SemantFunc:TExpr( args:TExpr[], throwError:Int = True, funcCall:Int = False )
  2884. _Semant
  2885. Local errorDetails:String
  2886. Local nearestScopeError:String
  2887. 'Local scope:TScopeDecl=IdentScope()
  2888. Local initialScope:Int = SCOPE_ALL
  2889. If scope Then
  2890. If TClassDecl(scope) Then
  2891. initialScope = SCOPE_CLASS_HEIRARCHY
  2892. Else If TModuleDecl(scope) Then
  2893. initialScope = SCOPE_MODULE
  2894. End If
  2895. End If
  2896. Local fdecl:TFuncDecl
  2897. Try
  2898. fdecl=scope.FindFuncDecl( IdentLower(),args,,,,True,initialScope )
  2899. ' Local decl:Object=scope.FindFuncDecl( IdentLower(),args,,,,True,SCOPE_ALL )
  2900. ' If decl Then
  2901. ' If TFuncDecl(decl) Then
  2902. ' fdecl = TFuncDecl(decl)
  2903. ' Else If TFuncDeclList(decl) Then
  2904. ' If Not TFuncDeclList(decl).IsEmpty() Then
  2905. ' fdecl = TFuncDecl(TFuncDeclList(decl).First())
  2906. ' End If
  2907. ' End If
  2908. ' End If
  2909. Catch errorMessage:String
  2910. If errorMessage.StartsWith("Compile Error") Then
  2911. Throw errorMessage
  2912. Else
  2913. ' couldn't find an exact match, look elsewhere
  2914. errorDetails = errorMessage
  2915. If errorMessage.StartsWith("Unable") Then
  2916. nearestScopeError = errorDetails
  2917. End If
  2918. End If
  2919. End Try
  2920. ' if our scope is static, but the scope of the found function/method is not
  2921. ' then we should ignore it and continue looking higher up the scope stack.
  2922. If static And fdecl And Not fdecl.IsStatic() Then
  2923. Local scope2:TScopeDecl = fdecl.scope
  2924. fdecl = Null
  2925. ' if fdecl was a method, this would be the Type's scope (ie. file/module)
  2926. If scope2.scope Then
  2927. fdecl = scope2.scope.FindFuncDecl( IdentLower(),args,,,,,SCOPE_CLASS_HEIRARCHY )
  2928. End If
  2929. Else If static And Not fdecl And Not fixedScope Then
  2930. If _env.classScope() Then
  2931. ' try searching from our class scope
  2932. 'fdecl = _env.classScope().FindFuncDecl( IdentLower(),args )
  2933. If Not fdecl Then
  2934. ' try searching from our class parent scope
  2935. Try
  2936. fdecl = _env.classScope().scope.FindFuncDecl( IdentLower(),args,,,,True,SCOPE_ALL )
  2937. Catch errorMessage:String
  2938. If errorMessage.StartsWith("Compile Error") Then
  2939. Throw errorMessage
  2940. Else
  2941. ' couldn't find an exact match, look elsewhere
  2942. errorDetails = errorMessage
  2943. If Not nearestScopeError And errorDetails.StartsWith("Unable") Then
  2944. nearestScopeError = errorDetails
  2945. End If
  2946. End If
  2947. End Try
  2948. End If
  2949. Else If _env.ModuleScope() Then ' bah
  2950. ' finally, try searching from our module scope
  2951. Try
  2952. fdecl = _env.ModuleScope().FindFuncDecl( IdentLower(),args,,,,True,SCOPE_ALL )
  2953. Catch errorMessage:String
  2954. If errorMessage.StartsWith("Compile Error") Then
  2955. Throw errorMessage
  2956. Else
  2957. ' couldn't find an exact match, look elsewhere
  2958. errorDetails = errorMessage
  2959. If Not nearestScopeError And errorDetails.StartsWith("Unable") Then
  2960. nearestScopeError = errorDetails
  2961. End If
  2962. End If
  2963. End Try
  2964. End If
  2965. End If
  2966. ' couldn't find it? try a global search
  2967. If Not fdecl And Not fixedScope Then
  2968. For Local mdecl:TModuleDecl = EachIn _appInstance.globalImports.Values()
  2969. Try
  2970. fdecl=mdecl.FindFuncDecl( IdentLower(), args,,,,True,SCOPE_ALL )
  2971. Catch errorMessage:String
  2972. If errorMessage.StartsWith("Compile Error") Then
  2973. Throw errorMessage
  2974. Else
  2975. ' couldn't find an exact match, look elsewhere
  2976. errorDetails = errorMessage
  2977. If Not nearestScopeError And errorDetails.StartsWith("Unable") Then
  2978. nearestScopeError = errorDetails
  2979. End If
  2980. End If
  2981. End Try
  2982. If fdecl Exit
  2983. Next
  2984. End If
  2985. If fdecl
  2986. If Not fdecl.IsStatic()
  2987. If static Err "Method '"+ident+"' cannot be accessed from here."
  2988. If expr Return New TInvokeMemberExpr.Create( expr,fdecl,args ).Semant()
  2989. 'If scope<>_env Or _env.FuncScope().IsStatic() Err "Method '"+ident+"' cannot be accessed from here."
  2990. EndIf
  2991. If expr And Not static Then
  2992. Return New TInvokeMemberExpr.Create( expr,fdecl,args ).Semant()
  2993. Else
  2994. If fdecl.IsStatic() And fdecl.IsAbstract() Err "Cannot call abstract " + fdecl.ToString()
  2995. Return New TInvokeExpr.Create( fdecl,args, funcCall ).Semant()
  2996. End If
  2997. EndIf
  2998. 'If args.Length=1 And args[0] And TObjectType( args[0].exprType )
  2999. ' Local cdecl:TClassDecl=TClassDecl( scope.FindScopeDecl( ident ) )
  3000. ' If cdecl Return args[0].Cast( New TObjectType.Create(cdecl),CAST_EXPLICIT )
  3001. 'EndIf
  3002. If Not expr Then
  3003. Local ty:TType=scope.FindType( IdentLower(),Null )
  3004. If ty Then
  3005. If args.Length=1 And args[0] Return args[0].Cast( ty,CAST_EXPLICIT )
  3006. Err "Illegal number of arguments for type conversion"
  3007. End If
  3008. End If
  3009. If throwError Then
  3010. If nearestScopeError Then
  3011. IdentErr(nearestScopeError)
  3012. Else
  3013. IdentErr(errorDetails)
  3014. End If
  3015. End If
  3016. End Method
  3017. Method SemantScope:TScopeDecl()
  3018. If Not expr Return _env.FindScopeDecl( IdentLower() )
  3019. Local scope:TScopeDecl=expr.SemantScope()
  3020. ' If scope is a namespace, then we are a module. Look up the module id and return it as the real scope.
  3021. If TNamespaceDecl(scope) Then
  3022. Local mdecl:TModuleDecl=TModuleDecl(scope.FindDecl(scope.IdentLower() + "." + IdentLower()))
  3023. If mdecl Then
  3024. Return mdecl
  3025. End If
  3026. End If
  3027. If scope Return scope.FindScopeDecl( IdentLower() )
  3028. End Method
  3029. ' Method Trans$()
  3030. ' Return _trans.TransIdentExpr( Self )
  3031. ' End Method
  3032. End Type
  3033. Type TBuiltinExpr Extends TExpr
  3034. Field id:String
  3035. Field expr:TExpr
  3036. Method Semant:TExpr(options:Int = 0)
  3037. If exprType Return Self
  3038. expr=expr.Semant()
  3039. exprType=expr.exprType
  3040. Return Self
  3041. End Method
  3042. Method Trans$()
  3043. Return _trans.TransBuiltinExpr( Self )
  3044. End Method
  3045. End Type
  3046. Type TLenExpr Extends TBuiltinExpr
  3047. Method Create:TLenExpr( expr:TExpr )
  3048. Self.id="len"
  3049. Self.expr=expr
  3050. Return Self
  3051. End Method
  3052. Method Semant:TExpr(options:Int = 0)
  3053. If exprType Return Self
  3054. expr=expr.Semant()
  3055. ' anything other than a string or array will become "1", and
  3056. ' return a length of 1 accordingly.
  3057. If Not TStringType(expr.exprType) And Not TArrayType(expr.exprType) Then
  3058. expr = New TConstExpr.Create( New TIntType, 1 ).Semant()
  3059. 'this is not useful for numerics
  3060. 'expr = New TConstExpr.Create( TType.stringType, "1" ).Semant()
  3061. _appInstance.mapStringConsts(TConstExpr(expr).value)
  3062. End If
  3063. exprType=New TIntType
  3064. Return Self
  3065. End Method
  3066. Method Copy:TExpr()
  3067. Return New TLenExpr.Create( CopyExpr(expr) )
  3068. End Method
  3069. Method ToString$()
  3070. Return "TLenExpr("+expr.ToString()+")"
  3071. End Method
  3072. End Type
  3073. Type TAscExpr Extends TBuiltinExpr
  3074. Method Create:TAscExpr( expr:TExpr )
  3075. Self.id="asc"
  3076. Self.expr=expr
  3077. Return Self
  3078. End Method
  3079. Method Semant:TExpr(options:Int = 0)
  3080. If exprType Return Self
  3081. If TConstExpr(expr) Then
  3082. Local cexpr:TExpr = New TConstExpr.Create(New TIntType, Asc(TConstExpr(expr).value))
  3083. _appInstance.removeStringConst(TConstExpr(expr).value)
  3084. cexpr.Semant()
  3085. Return cexpr
  3086. End If
  3087. expr = expr.SemantAndCast( New TStringType )
  3088. exprType = New TIntType
  3089. Return Self
  3090. End Method
  3091. Method Copy:TExpr()
  3092. Return New TAscExpr.Create( CopyExpr(expr) )
  3093. End Method
  3094. Method ToString$()
  3095. Return "TAscExpr("+expr.ToString()+")"
  3096. End Method
  3097. End Type
  3098. Type TSizeOfExpr Extends TBuiltinExpr
  3099. Method Create:TSizeOfExpr( expr:TExpr )
  3100. Self.id="sizeof"
  3101. Self.expr=expr
  3102. Return Self
  3103. End Method
  3104. Method Semant:TExpr(options:Int = 0)
  3105. If exprType Return Self
  3106. expr=expr.Semant()
  3107. exprType=New TSizeTType
  3108. Return Self
  3109. End Method
  3110. Method Copy:TExpr()
  3111. Return New TSizeOfExpr.Create( CopyExpr(expr) )
  3112. End Method
  3113. Method ToString$()
  3114. Return "TSizeOfExpr("+expr.ToString()+")"
  3115. End Method
  3116. End Type
  3117. Type TChrExpr Extends TBuiltinExpr
  3118. Method Create:TChrExpr( expr:TExpr )
  3119. Self.id="chr"
  3120. Self.expr=expr
  3121. Return Self
  3122. End Method
  3123. Method Semant:TExpr(options:Int = 0)
  3124. If exprType Return Self
  3125. If TConstExpr(expr) Then
  3126. Local cexpr:TConstExpr = New TConstExpr.Create(New TStringType, Chr(Int(TConstExpr(expr).value)))
  3127. cexpr.Semant()
  3128. _appInstance.mapStringConsts(cexpr.value)
  3129. Return cexpr
  3130. End If
  3131. expr = expr.SemantAndCast( New TIntType )
  3132. exprType = New TStringType
  3133. Return Self
  3134. End Method
  3135. Method Copy:TExpr()
  3136. Return New TChrExpr.Create( CopyExpr(expr) )
  3137. End Method
  3138. Method ToString$()
  3139. Return "TChrExpr("+expr.ToString()+")"
  3140. End Method
  3141. End Type
  3142. Type TFuncCallExpr Extends TExpr
  3143. Field expr:TExpr
  3144. Field args:TExpr[]
  3145. Method Create:TFuncCallExpr( expr:TExpr,args:TExpr[]=Null )
  3146. Self.expr=expr
  3147. If args Then
  3148. Self.args=args
  3149. Else
  3150. Self.args = New TExpr[0]
  3151. End If
  3152. Return Self
  3153. End Method
  3154. Method Copy:TExpr()
  3155. Return New TFuncCallExpr.Create( CopyExpr(expr),CopyArgs(args) )
  3156. End Method
  3157. Method ToString$()
  3158. Local t$="TFuncCallExpr("+expr.ToString()
  3159. For Local arg:TExpr=EachIn args
  3160. t:+","+arg.ToString()
  3161. Next
  3162. Return t+")"
  3163. End Method
  3164. Method Semant:TExpr(options:Int = 0)
  3165. args=SemantArgs( args )
  3166. If TIndexExpr(expr) Then
  3167. expr = expr.SemantFunc( args, True, True )
  3168. exprType = expr.exprType
  3169. Return Self
  3170. Else
  3171. Return expr.SemantFunc( args, True, True )
  3172. End If
  3173. End Method
  3174. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  3175. ' we are only likely to be called if a function returns and invokes a function pointer.
  3176. Local ex:TExpr = Semant()
  3177. If TFunctionPtrType(ex.exprType) Then
  3178. exprType = TFunctionPtrType(ex.exprType).func.retType
  3179. End If
  3180. Self.args = SemantArgs(args)
  3181. expr = ex
  3182. Return Self
  3183. End Method
  3184. Method Trans$()
  3185. Return _trans.TransFuncCallExpr( Self )
  3186. End Method
  3187. End Type
  3188. Type TScopeExpr Extends TExpr
  3189. Field scope:TScopeDecl
  3190. Method Create:TScopeExpr( scope:TScopeDecl )
  3191. Self.scope=scope
  3192. Return Self
  3193. End Method
  3194. Method Copy:TExpr()
  3195. Return Self
  3196. End Method
  3197. Method ToString$()
  3198. Return "TScopeExpr("+scope.ToString()+")"
  3199. End Method
  3200. Method Semant:TExpr(options:Int = 0)
  3201. Err "Syntax error."
  3202. End Method
  3203. Method SemantScope:TScopeDecl()
  3204. Return scope
  3205. End Method
  3206. End Type
  3207. Type TNewExpr Extends TExpr
  3208. Field isSuper:Int
  3209. Field args:TExpr[]
  3210. Field ctor:TFuncDecl
  3211. Method Create:TNewExpr( args:TExpr[]=Null, isSuper:Int = False )
  3212. If args Then
  3213. Self.args=args
  3214. Else
  3215. Self.args = New TExpr[0]
  3216. End If
  3217. Self.isSuper = isSuper
  3218. Return Self
  3219. End Method
  3220. Method Copy:TExpr()
  3221. Return New TNewExpr.Create(CopyArgs(args), isSuper)
  3222. End Method
  3223. Method Semant:TExpr(options:Int = 0)
  3224. Local fdecl:TFuncDecl = _env.FuncScope()
  3225. If Not fdecl Or TNewDecl(fdecl) = Null Or Not _env.ClassScope() Then
  3226. Err "Call to constructor not valid in this context."
  3227. End If
  3228. ' must be first statement of New() method
  3229. Local stmt:TStmt = TStmt(fdecl.stmts.First())
  3230. If TExprStmt(stmt) = Null Or TExprStmt(stmt).expr <> Self Then
  3231. Err "Call to constructor must be first statement in New()."
  3232. End If
  3233. args=SemantArgs( args )
  3234. ' validate called constructor
  3235. Try
  3236. Local cDecl:TClassDecl = _env.ClassScope()
  3237. If isSuper Then
  3238. cDecl = cDecl.superClass
  3239. End If
  3240. ctor = cDecl.FindFuncDecl("new",args,,,,True,SCOPE_CLASS_HEIRARCHY )
  3241. Catch errorMessage:String
  3242. If errorMessage.StartsWith("Compile Error") Then
  3243. Throw errorMessage
  3244. Else
  3245. Err errorMessage
  3246. End If
  3247. End Try
  3248. ' TODO : expand to full recursive test
  3249. If ctor = fdecl Then
  3250. Err "Recursive constructor invocation."
  3251. End If
  3252. ctor.Semant
  3253. args=CastArgs(args, ctor)
  3254. ' attach to ctor
  3255. TNewDecl(fdecl).chainedCtor = Self
  3256. Return Self
  3257. End Method
  3258. Method Trans$()
  3259. 'Return _trans.TransFuncCallExpr( Self )
  3260. End Method
  3261. End Type
  3262. Type TNullExpr Extends TExpr
  3263. Method Create:TNullExpr(ty:TType)
  3264. exprType = ty
  3265. Return Self
  3266. End Method
  3267. Method Copy:TExpr()
  3268. Return New TNullExpr.Create(exprType)
  3269. End Method
  3270. Method Semant:TExpr(options:Int = 0)
  3271. Return Self
  3272. End Method
  3273. Method Trans$()
  3274. Return "NULL"
  3275. End Method
  3276. Method Eval$()
  3277. Return ""
  3278. End Method
  3279. End Type
  3280. Type TLoopLabelExpr Extends TExpr
  3281. Field loop:TLoopStmt
  3282. Method Create:TLoopLabelExpr(loop:TLoopStmt)
  3283. Self.loop = loop
  3284. Return Self
  3285. End Method
  3286. Method Copy:TExpr()
  3287. Return New TLoopLabelExpr.Create(loop)
  3288. End Method
  3289. Method Semant:TExpr(options:Int = 0)
  3290. Return Self
  3291. End Method
  3292. Method Trans$()
  3293. End Method
  3294. Method Eval$()
  3295. Return ""
  3296. End Method
  3297. End Type
  3298. Type TDataLabelExpr Extends TExpr
  3299. Field dataDef:TDefDataDecl
  3300. Method Create:TDataLabelExpr(dataDef:TDefDataDecl)
  3301. Self.dataDef = dataDef
  3302. Return Self
  3303. End Method
  3304. Method Copy:TExpr()
  3305. Return New TDataLabelExpr.Create(dataDef)
  3306. End Method
  3307. Method Semant:TExpr(options:Int = 0)
  3308. Return Self
  3309. End Method
  3310. Method Trans$()
  3311. End Method
  3312. Method Eval$()
  3313. Return ""
  3314. End Method
  3315. End Type
  3316. Type TIdentEnumExpr Extends TExpr
  3317. Field value:TEnumValueDecl
  3318. Method Create:TIdentEnumExpr( value:TEnumValueDecl )
  3319. Self.exprType=New TEnumType.Create(TEnumDecl(value.scope))
  3320. Self.value = value
  3321. Return Self
  3322. End Method
  3323. Method Copy:TExpr()
  3324. Return New TIdentEnumExpr.Create( value )
  3325. End Method
  3326. Method Semant:TExpr(options:Int = 0)
  3327. Return Self
  3328. End Method
  3329. Method Trans$()
  3330. Return value.Value()
  3331. End Method
  3332. Method Eval$()
  3333. Return value.Value()
  3334. End Method
  3335. End Type
  3336. Type TStackAllocExpr Extends TBuiltinExpr
  3337. Method Create:TStackAllocExpr( expr:TExpr )
  3338. Self.id="stackalloc"
  3339. Self.expr=expr
  3340. Return Self
  3341. End Method
  3342. Method Semant:TExpr(options:Int = 0)
  3343. If exprType Return Self
  3344. expr = expr.SemantAndCast( New TSizeTType )
  3345. exprType = TType.MapToPointerType(New TByteType)
  3346. Return Self
  3347. End Method
  3348. Method Copy:TExpr()
  3349. Return New TStackAllocExpr.Create( CopyExpr(expr) )
  3350. End Method
  3351. Method ToString$()
  3352. Return "TStackAllocExpr("+expr.ToString()+")"
  3353. End Method
  3354. End Type
  3355. Type TFieldOffsetExpr Extends TBuiltinExpr
  3356. Field typeExpr:TExpr
  3357. Field fieldExpr:TExpr
  3358. Method Create:TFieldOffsetExpr( typeExpr:TExpr, fieldExpr:TExpr )
  3359. Self.id="fieldoffset"
  3360. Self.typeExpr=typeExpr
  3361. Self.fieldExpr = fieldExpr
  3362. Return Self
  3363. End Method
  3364. Method Semant:TExpr(options:Int = 0)
  3365. If exprType Return Self
  3366. ' validate type and field
  3367. typeExpr = typeExpr.Semant()
  3368. If Not TIdentTypeExpr(typeExpr) Then
  3369. Err "Expecting Type or Struct"
  3370. End If
  3371. TIdentExpr(fieldExpr).scope = TIdentTypeExpr(typeExpr).cdecl
  3372. fieldExpr = fieldExpr.Semant()
  3373. If Not TVarExpr(fieldExpr) Or Not TFieldDecl(TVarExpr(fieldExpr).decl) Then
  3374. Err "Expecting Field"
  3375. End If
  3376. exprType = New TSizeTType
  3377. Return Self
  3378. End Method
  3379. Method Copy:TExpr()
  3380. Return New TFieldOffsetExpr.Create( typeExpr, fieldExpr )
  3381. End Method
  3382. Method ToString$()
  3383. Return "TFieldOffsetExpr("+typeExpr.ToString()+"," + fieldExpr.ToString() + ")"
  3384. End Method
  3385. End Type