expr.bmx 102 KB

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