expr.bmx 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214
  1. ' Copyright (c) 2013-2016 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. Method ToString$()
  27. Return "<TExpr>"
  28. End Method
  29. Method Copy:TExpr()
  30. InternalErr
  31. End Method
  32. Method Semant:TExpr()
  33. InternalErr
  34. End Method
  35. Method SemantSet:TExpr( op$,rhs:TExpr )
  36. Err ToString()+" cannot be assigned to."
  37. End Method
  38. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  39. Err ToString()+" cannot be invoked."
  40. End Method
  41. Method SemantScope:TScopeDecl()
  42. Return Null
  43. End Method
  44. Method Eval$()
  45. Err ToString()+" cannot be statically evaluated."
  46. End Method
  47. Method EvalConst:TExpr()
  48. Local expr:TExpr = New TConstExpr.Create( exprType,Eval() ).Semant()
  49. If TStringType(TConstExpr(expr).ty) Then
  50. _appInstance.mapStringConsts(TConstExpr(expr).value)
  51. End If
  52. Return expr
  53. End Method
  54. Method Trans$()
  55. Todo
  56. End Method
  57. Method TransStmt$()
  58. Return Trans()
  59. End Method
  60. Method TransVar$()
  61. InternalErr
  62. End Method
  63. 'semant and cast
  64. Method SemantAndCast:TExpr( ty:TType,castFlags:Int=0 )
  65. Local expr:TExpr=Semant()
  66. If expr.exprType.EqualsType( ty ) Return expr
  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. TInvokeExpr(args[i]).decl = TFuncDecl(TInvokeExpr(args[i]).decl.Copy(False))
  93. TInvokeExpr(args[i]).decl.actual = cp
  94. TInvokeExpr(args[i]).decl.attrs :| FUNC_PTR
  95. TFunctionPtrType(TInvokeExpr(args[i]).exprType).func = TInvokeExpr(args[i]).decl
  96. TInvokeExpr(args[i]).decl.semant()
  97. End If
  98. End If
  99. End If
  100. Next
  101. Return args
  102. End Method
  103. Method CastArgs:TExpr[]( args:TExpr[],funcDecl:TFuncDecl )
  104. If args.Length>funcDecl.argDecls.Length Then
  105. Err "Too many function parameters"
  106. End If
  107. ' FIXME
  108. 'args=args.Resize( funcDecl.argDecls.Length )
  109. ' FIXME
  110. For Local i:Int=0 Until funcDecl.argDecls.Length
  111. ' ensure funcdecl args are semanted before trying to use them.
  112. If Not funcDecl.argDecls[i].IsSemanted() Then
  113. funcDecl.argDecls[i].Semant()
  114. End If
  115. If i < args.length And args[i]
  116. If TInvokeExpr(args[i]) And Not TInvokeExpr(args[i]).invokedWithBraces Then
  117. If Not IsPointerType(funcDecl.argDecls[i].ty, TType.T_BYTE) And Not TFunctionPtrType(funcDecl.argDecls[i].ty) Then
  118. Err "Unable to convert from '" + args[i].exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
  119. End If
  120. End If
  121. If TInvokeMemberExpr(args[i]) And Not TInvokeMemberExpr(args[i]).invokedWithBraces Then
  122. If Not IsPointerType(funcDecl.argDecls[i].ty, TType.T_BYTE) And Not TFunctionPtrType(funcDecl.argDecls[i].ty) Then
  123. Err "Unable to convert from '" + args[i].exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
  124. End If
  125. End If
  126. If funcDecl.argDecls[i].ty._flags & TType.T_VAR Then
  127. If TConstExpr(args[i]) Or TBinaryExpr(args[i]) Or (TIndexExpr(args[i]) And TStringType(TIndexExpr(args[i]).expr.exprType)) Or ..
  128. TInvokeExpr(args[i]) Or TInvokeMemberExpr(args[i]) Then
  129. Err "Expression for 'Var' parameter must be a variable"
  130. End If
  131. ' Passing a "new" object into a Var, requires us to create a local variable and pass its address instead.
  132. If TNewObjectExpr(args[i]) Then
  133. Local tmp:TLocalDecl=New TLocalDecl.Create( "",TNewObjectExpr(args[i]).ty,args[i],, True )
  134. tmp.Semant()
  135. Local v:TVarExpr = New TVarExpr.Create( tmp )
  136. Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), v ).Semant()
  137. stmt.exprType = TNewObjectExpr(args[i]).ty
  138. args[i] = stmt
  139. End If
  140. End If
  141. If (funcDecl.argDecls[i].ty._flags & TType.T_VAR) And Not (funcDecl.argDecls[i].ty.EqualsType(args[i].exprType)) Then
  142. If (Not TObjectType(funcDecl.argDecls[i].ty)) Or (TObjectType(funcDecl.argDecls[i].ty) And Not args[i].exprType.ExtendsType(funcDecl.argDecls[i].ty)) Then
  143. err "Variable for 'Var' parameter is not of matching type"
  144. End If
  145. End If
  146. args[i]=args[i].Cast( funcDecl.argDecls[i].ty )
  147. Else If funcDecl.argDecls[i].init
  148. If i = args.length Then
  149. ' extend args to add default init entry
  150. args = args[..i + 1]
  151. End If
  152. args[i]=funcDecl.argDecls[i].init
  153. Else
  154. Err "Missing function argument '"+funcDecl.argDecls[i].ident+"'."
  155. EndIf
  156. Next
  157. Return args
  158. End Method
  159. Method BalanceTypes:TType( lhs:TType,rhs:TType )
  160. If TStringType( lhs ) Or TStringType( rhs ) Then
  161. If TObjectType(lhs) Or TObjectType(rhs) Then
  162. If TObjectType(lhs) And TObjectType(lhs).classDecl.ident = "Object" Then
  163. Return lhs
  164. End If
  165. If TObjectType(rhs) And TObjectType(rhs).classDecl.ident = "Object" Then
  166. Return rhs
  167. End If
  168. Else
  169. Return New TStringType
  170. End If
  171. End If
  172. If IsPointerType( lhs, 0, TType.T_POINTER ) Or IsPointerType( rhs, 0, TType.T_POINTER ) Then
  173. If IsPointerType( lhs, 0, TType.T_POINTER ) Return lhs
  174. If IsPointerType( rhs, 0, TType.T_POINTER ) Return rhs
  175. End If
  176. If TDouble128Type( lhs ) Or TDouble128Type( rhs ) Return New TDouble128Type
  177. If TFloat128Type( lhs ) Or TFloat128Type( rhs ) Return New TFloat128Type
  178. If TFloat64Type( lhs ) Or TFloat64Type( rhs ) Return New TFloat64Type
  179. If TDoubleType( lhs ) Or TDoubleType( rhs ) Return New TDoubleType
  180. If TFloatType( lhs ) Or TFloatType( rhs ) Return New TFloatType
  181. If TFunctionPtrType( lhs ) Or TFunctionPtrType( rhs ) Then
  182. If TFunctionPtrType( lhs ) Return lhs
  183. If TFunctionPtrType( rhs ) Return rhs
  184. End If
  185. If TInt128Type( lhs ) Or TInt128Type( rhs ) Return New TInt128Type
  186. If TULongType( lhs ) Or TULongType( rhs ) Return New TULongType
  187. If TSizeTType( lhs ) Or TSizeTType( rhs ) Return New TSizeTType
  188. If TWParamType( lhs ) Or TWParamType( rhs ) Return New TWParamType
  189. If TLongType( lhs ) And TUIntType( rhs ) Return New TULongType
  190. If TUIntType( lhs ) And TLongType( rhs ) Return New TULongType
  191. If TLParamType( lhs ) Or TLParamType( rhs ) Return New TLParamType
  192. If TLongType( lhs ) Or TLongType( rhs ) Return New TLongType
  193. If TUIntType( lhs ) Or TUIntType( rhs ) Return New TUIntType
  194. If TIntType( lhs ) Or TIntType( rhs ) Return New TIntType
  195. If TObjectType( lhs ) And TNullDecl(TObjectType( lhs ).classDecl) Then
  196. Return rhs
  197. End If
  198. If TObjectType( rhs ) And TNullDecl(TObjectType( rhs ).classDecl) Then
  199. Return lhs
  200. End If
  201. If lhs.ExtendsType( rhs ) Return rhs
  202. If rhs.ExtendsType( lhs ) Return lhs
  203. ' balance arrays - only for objects... to the lowest common denominator.
  204. If TArrayType( lhs ) And TArrayType( rhs ) Then
  205. If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( rhs ).elemType) Then
  206. ' lhs = Object[]
  207. If TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" Then
  208. Return lhs
  209. End If
  210. ' rhs = Object[]
  211. If TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" Then
  212. Return rhs
  213. End If
  214. ' does one extend the other? If so, return the base type
  215. If TObjectType(TArrayType( lhs ).elemType).ExtendsType(TObjectType(TArrayType( rhs ).elemType)) Then
  216. Return rhs
  217. End If
  218. If TObjectType(TArrayType( rhs ).elemType).ExtendsType(TObjectType(TArrayType( lhs ).elemType)) Then
  219. Return lhs
  220. End If
  221. ' no? then we will fallback to an Object type array
  222. ' find the Object classdecl instance
  223. Local modid$="brl.classes"
  224. Local mdecl:TModuleDecl=_env.FindModuleDecl( modid )
  225. ' return an array of Objects
  226. Return New TArrayType.Create(New TObjectType.Create(TClassDecl(mdecl.FindDecl( "object" ))))
  227. End If
  228. If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" And TStringType(TArrayType( rhs ).elemType) Then
  229. Return lhs
  230. End If
  231. If TObjectType(TArrayType( rhs ).elemType) And TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" And TStringType(TArrayType( lhs ).elemType) Then
  232. Return rhs
  233. End If
  234. If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" And TArrayType(TArrayType( rhs ).elemType) Then
  235. Return lhs
  236. End If
  237. If TObjectType(TArrayType( rhs ).elemType) And TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" And TArrayType(TArrayType( lhs ).elemType) Then
  238. Return rhs
  239. End If
  240. ' balancing primitive types
  241. If Not TArrayType( lhs ).elemType.EqualsType(TArrayType( rhs ).elemType) Then
  242. Err "Types '" + TArrayType( lhs ).elemType.ToString() + " Array' and '" + TArrayType( rhs ).elemType.ToString() + " Array' are unrelated"
  243. End If
  244. End If
  245. Err "Can't balance types "+lhs.ToString()+" and "+rhs.ToString()+"."
  246. End Method
  247. Function CopyExpr:TExpr( expr:TExpr )
  248. If Not expr Return Null
  249. Return expr.Copy()
  250. End Function
  251. Function CopyArgs:TExpr[]( exprs:TExpr[] )
  252. exprs=exprs[..]
  253. For Local i:Int=0 Until exprs.Length
  254. exprs[i]=CopyExpr( exprs[i] )
  255. Next
  256. Return exprs
  257. End Function
  258. End Type
  259. ' exec a stmt, return an expr
  260. Type TStmtExpr Extends TExpr
  261. Field stmt:TStmt
  262. Field expr:TExpr
  263. Method Create:TStmtExpr( stmt:TStmt,expr:TExpr )
  264. Self.stmt=stmt
  265. Self.expr=expr
  266. Return Self
  267. End Method
  268. Method Copy:TExpr()
  269. If exprType Return Self
  270. Return New TStmtExpr.Create( stmt,CopyExpr(expr) )
  271. End Method
  272. Method ToString$()
  273. Return "TStmtExpr(,"+expr.ToString()+")"
  274. End Method
  275. Method Semant:TExpr()
  276. If exprType Return Self
  277. stmt.Semant()
  278. expr=expr.Semant()
  279. exprType=expr.exprType
  280. Return Self
  281. End Method
  282. Method Trans$()
  283. Return _trans.TransStmtExpr( Self )
  284. End Method
  285. Method TransVar$()
  286. Semant
  287. Return _trans.TransStmtExpr( Self )
  288. End Method
  289. End Type
  290. ' literal
  291. Type TConstExpr Extends TExpr
  292. Field ty:TType
  293. Field value$
  294. Field originalValue$
  295. ' True if the const was identified as a specific type.
  296. Field typeSpecific:Int
  297. Method Create:TConstExpr( ty:TType,value$ )
  298. originalValue = value
  299. If TNumericType( ty ) And IsPointerType(ty, 0, TType.T_POINTER) Then
  300. Self.ty=ty
  301. If value Then
  302. Self.value = value
  303. Else
  304. Self.value="0"
  305. End If
  306. Return Self
  307. End If
  308. If TIntType( ty ) Or TShortType( ty ) Or TByteType( ty ) Or TLongType( ty ) Or TUIntType( ty ) Or TULongType( ty ) Or TWParamType(ty) Or TLParamType(ty)
  309. Local radix:Int
  310. If value.StartsWith( "%" )
  311. radix=1
  312. Else If value.StartsWith( "$" )
  313. radix=4
  314. EndIf
  315. If radix
  316. Local val:Long = 0
  317. For Local i:Int=1 Until value.Length
  318. Local ch:Int=value[i]
  319. If ch>=48 And ch<58
  320. val=val Shl radix | (ch & 15)
  321. Else
  322. val=val Shl radix | ((ch & 15)+9)
  323. EndIf
  324. Next
  325. If TIntType(ty) And val >= 2147483648:Long Then
  326. value = String( -2147483648:Long + (val - 2147483648:Long))
  327. Else
  328. If TShortType( ty ) Then
  329. value=String( Short(val) )
  330. Else If TByteType( ty ) Then
  331. value=String( Byte(val) )
  332. Else
  333. value=String( val )
  334. End If
  335. End If
  336. Else
  337. If TShortType( ty ) Then
  338. value = String.FromLong(Short(value.ToLong()))
  339. Else If TByteType( ty ) Then
  340. value = String.FromLong(Byte(value.ToLong()))
  341. Else
  342. Local buf:Byte[64]
  343. Local b:Int
  344. Local v:String = value.Trim()
  345. Local leading0:Int = True
  346. If v Then
  347. Local i:Int
  348. If v[0] = Asc("+") Then
  349. i = 1
  350. Else If v[0] = Asc("-") Then
  351. i = 1
  352. buf[b] = Asc("-")
  353. b:+ 1
  354. End If
  355. While i < value.Length
  356. If Not IsDigit(v[i]) Then
  357. Exit
  358. End If
  359. If leading0 And v[i] = Asc("0") Then
  360. i :+ 1
  361. Continue
  362. End If
  363. leading0 = False
  364. buf[b] = v[i]
  365. b :+ 1
  366. i :+ 1
  367. Wend
  368. If leading0 Then
  369. value = "0"
  370. Else
  371. value = String.FromBytes(buf, b)
  372. End If
  373. Else
  374. value = "0"
  375. End If
  376. End If
  377. EndIf
  378. Else If TDecimalType( ty )
  379. If Not (value.Contains("e") Or value.Contains("E") Or value.Contains(".") Or value.Contains("inf") Or value.Contains("nan"))
  380. value:+".0"
  381. EndIf
  382. EndIf
  383. Self.ty=ty
  384. Self.value=value
  385. Return Self
  386. End Method
  387. Method UpdateType(ty:TType)
  388. typeSpecific = True
  389. Create(ty, originalValue)
  390. End Method
  391. Method Copy:TExpr()
  392. Local e:TConstExpr = New TConstExpr.Create( ty,value )
  393. e.originalValue = originalValue
  394. e.typeSpecific = typeSpecific
  395. Return e
  396. End Method
  397. Method ToString$()
  398. Return "TConstExpr(~q"+value+"~q)"
  399. End Method
  400. Method Semant:TExpr()
  401. If exprType Return Self
  402. exprType=ty.Semant()
  403. Return Self
  404. End Method
  405. Method Eval$()
  406. Return value
  407. End Method
  408. Method EvalConst:TExpr()
  409. Return Self
  410. End Method
  411. Method Trans$()
  412. Semant
  413. Return _trans.TransConstExpr( Self )
  414. End Method
  415. Method SemantAndCast:TExpr( ty:TType,castFlags:Int=0 )
  416. Local expr:TExpr=Semant()
  417. If expr.exprType.EqualsType( ty ) Return expr
  418. If value = "bbNullObject" Then
  419. Err "bbNullObject"
  420. Return expr
  421. End If
  422. Return New TCastExpr.Create( ty,expr,castFlags ).Semant()
  423. End Method
  424. Method CompatibleWithType:Int(ty:TType)
  425. If Not TDecimalType(ty) Then
  426. If value.Contains("e") Or value.Contains("E") Or value.Contains(".") Or value.Contains("inf") Or value.Contains("nan") Then
  427. Return False
  428. End If
  429. Local val:Long = value.ToLong()
  430. If val < 0 Then
  431. If TByteType(ty) Or TShortType(ty) Or TUIntType(ty) Or TULongType(ty) Or TSizeTType(ty) Or TInt128Type(ty) Or TWParamType(ty) Then
  432. Return False
  433. End If
  434. Else
  435. If TByteType(ty) Then
  436. If value <> String.FromInt(Byte(Val)) Then
  437. Return False
  438. End If
  439. End If
  440. If TUIntType(ty) Or ((TSizeTType(ty) Or TWParamType(ty)) And WORD_SIZE = 4) Then
  441. If val > 4294967296:Long Then
  442. Return False
  443. End If
  444. End If
  445. If TULongType(ty) Or ((TSizeTType(ty) Or TWParamType(ty)) And WORD_SIZE = 8) Then
  446. If value.length > 20 Then
  447. Return False
  448. Else If value.length = 20 Then
  449. For Local i:Int = 0 Until value.length
  450. Local v:Int = value[i]
  451. Local n:Int = "18446744073709551616"[i]
  452. If v < n Then
  453. Exit
  454. Else If v > n Then
  455. Return False
  456. End If
  457. Next
  458. End If
  459. End If
  460. End If
  461. If TShortType(ty) Then
  462. If value <> String.FromInt(Short(val)) Then
  463. Return False
  464. End If
  465. End If
  466. If TIntType(ty) Or (TLParamType(ty) And WORD_SIZE = 4) Then
  467. If value <> String.FromInt(Int(val)) Then
  468. Return False
  469. End If
  470. End If
  471. If TLongType(ty) Or (TLParamType(ty) And WORD_SIZE = 8) Then
  472. If value <> String.FromLong(Long(val)) Then
  473. Return False
  474. End If
  475. End If
  476. End If
  477. Return True
  478. End Method
  479. End Type
  480. Type TVarExpr Extends TExpr
  481. Field decl:TVarDecl
  482. Method Create:TVarExpr( decl:TVarDecl )
  483. Self.decl=decl
  484. Return Self
  485. End Method
  486. Method Copy:TExpr()
  487. Return Self
  488. End Method
  489. Method ToString$()
  490. Return "TVarExpr("+decl.ToString()+")"
  491. End Method
  492. Method Semant:TExpr()
  493. If exprType Return Self
  494. If Not decl.IsSemanted() InternalErr
  495. exprType=decl.ty
  496. Return Self
  497. End Method
  498. Method SemantSet:TExpr( op$,rhs:TExpr )
  499. Return Semant()
  500. End Method
  501. Method Trans$()
  502. Semant
  503. Return _trans.TransTemplateCast( exprType,TVarDecl(decl.actual).ty,_trans.TransVarExpr( Self ) )
  504. End Method
  505. Method TransVar$()
  506. Semant
  507. Return _trans.TransVarExpr( Self )
  508. End Method
  509. End Type
  510. Type TMemberVarExpr Extends TExpr
  511. Field expr:TExpr
  512. Field decl:TVarDecl
  513. Method Create:TMemberVarExpr( expr:TExpr,decl:TVarDecl )
  514. Self.expr=expr
  515. Self.decl=decl
  516. Return Self
  517. End Method
  518. Method Copy:TExpr()
  519. Return Self
  520. End Method
  521. Method ToString$()
  522. Return "TMemberVarExpr("+expr.ToString()+","+decl.ToString()+")"
  523. End Method
  524. Method Semant:TExpr()
  525. If exprType Return Self
  526. If Not decl.IsSemanted() InternalErr
  527. exprType=decl.ty
  528. Return Self
  529. End Method
  530. Method SemantSet:TExpr( op$,rhs:TExpr )
  531. Return Semant()
  532. End Method
  533. Method Trans$()
  534. Return _trans.TransTemplateCast( exprType,TVarDecl(decl.actual).ty,_trans.TransMemberVarExpr( Self ) )
  535. End Method
  536. Method TransVar$()
  537. Return _trans.TransMemberVarExpr( Self )
  538. End Method
  539. End Type
  540. Type TInvokeExpr Extends TExpr
  541. Field decl:TFuncDecl
  542. Field args:TExpr[]
  543. Field invokedWithBraces:Int
  544. Field isArg:Int
  545. Field isRhs:Int
  546. Method Create:TInvokeExpr( decl:TFuncDecl,args:TExpr[]=Null,invokedWithBraces:Int=True, isArg:Int=False, isRhs:Int = False )
  547. Self.decl=decl
  548. If args Then
  549. Self.args=args
  550. Else
  551. Self.args = New TExpr[0]
  552. End If
  553. Self.invokedWithBraces = invokedWithBraces
  554. Self.isArg = isArg
  555. Self.isRhs = isRhs
  556. Return Self
  557. End Method
  558. Method Copy:TExpr()
  559. Return Self
  560. End Method
  561. Method ToString$()
  562. Local t$="TInvokeExpr("+decl.ToString()
  563. For Local arg:TExpr=EachIn args
  564. t:+","+arg.ToString()
  565. Next
  566. Return t+")"
  567. End Method
  568. Method Semant:TExpr()
  569. If exprType Return Self
  570. If Not decl.retType
  571. decl.Semant()
  572. End If
  573. 'If TIdentType(decl.retType) Then
  574. exprType = decl.retType.Semant()
  575. 'Else
  576. ' exprType=decl.retType
  577. 'End If
  578. If ((isArg Or isRhs) And Not invokedWithBraces) And (args = Null Or args.length = 0) Then
  579. ' nothing to do here, as we are probably a function pointer. i.e. no braces and no
  580. Else
  581. args=CastArgs( args,decl )
  582. End If
  583. Return Self
  584. End Method
  585. Method Trans$()
  586. ' Return _trans.TransTemplateCast( exprType,TFuncDecl(decl.actual).retType,_trans.TransInvokeExpr( Self ) )
  587. Return _trans.TransInvokeExpr( Self )
  588. End Method
  589. Method TransStmt$()
  590. Return _trans.TransInvokeExpr( Self )
  591. End Method
  592. Method Eval$()
  593. Return Super.Eval()
  594. End Method
  595. End Type
  596. Type TInvokeMemberExpr Extends TExpr
  597. Field expr:TExpr
  598. Field decl:TFuncDecl
  599. Field args:TExpr[]
  600. Field isResize:Int 'FIXME - butt ugly!
  601. Field invokedWithBraces:Int
  602. Method Create:TInvokeMemberExpr( expr:TExpr,decl:TFuncDecl,args:TExpr[]=Null, invokedWithBraces:Int = True )
  603. Self.expr=expr
  604. Self.decl=decl
  605. If args
  606. Self.args=args
  607. Else
  608. Self.args = New TExpr[0]
  609. End If
  610. Self.invokedWithBraces = invokedWithBraces
  611. Return Self
  612. End Method
  613. Method Copy:TExpr()
  614. Return Self
  615. End Method
  616. Method ToString$()
  617. Local t$="TInvokeMemberExpr("+expr.ToString()+","+decl.ToString()
  618. For Local arg:TExpr=EachIn args
  619. t:+","+arg.ToString()
  620. Next
  621. Return t+")"
  622. End Method
  623. Method Semant:TExpr()
  624. If exprType Return Self
  625. If Not decl.IsSemanted() decl.Semant()
  626. exprType=decl.retType
  627. args=SemantArgs( args )
  628. args=CastArgs( args,decl )
  629. 'Array $resize hack!
  630. If TArrayType( exprType ) And TVoidType( TArrayType( exprType ).elemType )
  631. isResize=True
  632. exprType=expr.exprType
  633. EndIf
  634. Return Self
  635. End Method
  636. Method Trans$()
  637. 'Array $resize hack!
  638. If isResize Return _trans.TransInvokeMemberExpr( Self )
  639. Return _trans.TransTemplateCast( exprType,TFuncDecl(decl.actual).retType,_trans.TransInvokeMemberExpr( Self ) )
  640. End Method
  641. Method TransStmt$()
  642. Return _trans.TransInvokeMemberExpr( Self )
  643. End Method
  644. End Type
  645. Type TNewObjectExpr Extends TExpr
  646. Field ty:TType
  647. Field args:TExpr[]
  648. Field ctor:TFuncDecl
  649. Field classDecl:TClassDecl
  650. Field instanceExpr:TExpr
  651. Method Create:TNewObjectExpr( ty:TType,args:TExpr[] )
  652. Self.ty=ty
  653. Self.args=args
  654. Return Self
  655. End Method
  656. Method Copy:TExpr()
  657. Return New TNewObjectExpr.Create( ty,CopyArgs(args) )
  658. End Method
  659. Method Semant:TExpr()
  660. If exprType Return Self
  661. Local it:TIdentType = TIdentType(ty)
  662. Local iArgs:TExpr[] = SemantArgs(CopyArgs(args))
  663. ty=ty.Semant(True)
  664. If Not ty Then
  665. ' maybe it's an instance of a type ?
  666. Local decl:TVarDecl = TVarDecl(_env.FindDecl(it.ident))
  667. If decl And TObjectType(decl.ty) Then
  668. ty = decl.ty
  669. instanceExpr = New TVarExpr.Create(decl).Semant()
  670. Else
  671. Err "Type '"+it.ident+"' not found"
  672. End If
  673. End If
  674. args=SemantArgs( args )
  675. Local objTy:TObjectType=TObjectType( ty )
  676. If Not objTy
  677. Err "Expression is not a class."
  678. EndIf
  679. '
  680. If objTy.instance Then
  681. instanceExpr = New TSelfExpr.Semant()
  682. End If
  683. classDecl=objTy.classDecl
  684. If Not instanceExpr Then
  685. If classDecl.IsInterface() Err "Cannot create instance of an interface."
  686. If classDecl.IsAbstract() Err "Cannot create instance of an abstract class."
  687. End If
  688. 'If classDecl.IsTemplateArg() Err "Cannot create instance of a generic argument."
  689. If classDecl.args And Not classDecl.instanceof Err "Cannot create instance of a generic class."
  690. Local parts:String[]
  691. If it Then
  692. parts = it.ident.ToLower().Split(".")
  693. End If
  694. If classDecl.IsExtern()
  695. Err "Cannot create instance of an extern type"
  696. 'If args Err "No suitable constructor found for class "+classDecl.ToString()+"."
  697. Else
  698. ' if the New Type doesn't have extra idents (like a create method), then don't use the args in the search.
  699. ' otherwise, the args are for the constructor.
  700. If Not parts Or parts.length = 1 Then
  701. ctor=classDecl.FindFuncDecl( "new",args,,,,,SCOPE_CLASS_HEIRARCHY )
  702. If Not ctor Err "No suitable constructor found for class "+classDecl.ToString()+"."
  703. args=CastArgs( args,ctor )
  704. Else
  705. ctor=classDecl.FindFuncDecl( "new",,,,,,SCOPE_CLASS_HEIRARCHY )
  706. If Not ctor Err "No suitable constructor found for class "+classDecl.ToString()+"."
  707. End If
  708. EndIf
  709. classDecl.attrs:|CLASS_INSTANCED
  710. exprType=ty
  711. If it Then
  712. 'Local parts:String[] = it.ident.ToLower().Split(".")
  713. Local i:Int = 0
  714. While i < parts.length And parts[i] <> classDecl.IdentLower() And parts[i] <> "self"
  715. i :+ 1
  716. Wend
  717. i :+ 1
  718. Local expr:TExpr = Self
  719. Local cdecl:TClassDecl = classDecl
  720. Local eType:TType = objTy
  721. Local errorDetails:String
  722. While i < parts.length
  723. Local id:String = parts[i]
  724. i :+ 1
  725. ' find member function.method
  726. Local fdecl:TFuncDecl
  727. Try
  728. fdecl = cdecl.FindFuncDecl(id, iArgs,,,,True,SCOPE_CLASS_HEIRARCHY)
  729. Catch errorMessage:String
  730. If errorMessage.StartsWith("Compile Error") Then
  731. Throw errorMessage
  732. Else
  733. ' couldn't find an exact match, look elsewhere
  734. If errorMessage.StartsWith("Unable") Then
  735. errorDetails = errorMessage
  736. End If
  737. End If
  738. End Try
  739. If fdecl Then
  740. expr = New TInvokeMemberExpr.Create( expr,fdecl, iArgs ).Semant()
  741. eType = expr.exprType
  742. If TObjectType(eType) Then
  743. cdecl = TObjectType(expr.exprType).classdecl
  744. End If
  745. If TArrayType(eType) Or TStringType(eType) Then
  746. cdecl = eType.GetClass()
  747. End If
  748. Continue
  749. End If
  750. ' find other member decl (field, etc)
  751. If Not errorDetails Then
  752. Local decl:TVarDecl = TVarDecl(cdecl.GetDecl(id))
  753. If decl Then
  754. Local tmp:TLocalDecl=New TLocalDecl.Create( "", eType, expr,, True )
  755. Local varExpr:TExpr = New TMemberVarExpr.Create(New TVarExpr.Create( tmp ), decl).Semant()
  756. expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), varExpr ).Semant()
  757. eType = decl.ty
  758. If TObjectType(eType) Then
  759. cdecl = TObjectType(expr.exprType).classdecl
  760. End If
  761. If TArrayType(eType) Or TStringType(eType) Then
  762. cdecl = eType.GetClass()
  763. End If
  764. Continue
  765. End If
  766. End If
  767. ' didn't match member or function??
  768. ' probably an error...
  769. If errorDetails Then
  770. Err errorDetails
  771. Else
  772. Err "Identifier '" + id + "' not found."
  773. End If
  774. Wend
  775. Return expr
  776. End If
  777. Return Self
  778. End Method
  779. Method Trans$()
  780. Return _trans.TransNewObjectExpr( Self )
  781. End Method
  782. End Type
  783. Type TNewArrayExpr Extends TExpr
  784. Field ty:TType
  785. Field expr:TExpr[]
  786. Method Create:TNewArrayExpr( ty:TType,expr:TExpr[] )
  787. Self.ty=ty
  788. Self.expr=expr
  789. Return Self
  790. End Method
  791. Method Copy:TExpr()
  792. If exprType InternalErr
  793. Local cexpr:TExpr[expr.length]
  794. For Local i:Int = 0 Until expr.length
  795. cexpr[i] = CopyExpr(expr[i])
  796. Next
  797. Return New TNewArrayExpr.Create( ty,cexpr )
  798. End Method
  799. Method Semant:TExpr()
  800. If exprType Return Self
  801. ty=ty.Semant()
  802. exprType=New TArrayType.Create( ty, expr.length )
  803. For Local i:Int = 0 Until expr.length
  804. expr[i]=expr[i].SemantAndCast( New TIntType )
  805. Next
  806. Return Self
  807. End Method
  808. Method Trans$()
  809. Return _trans.TransNewArrayExpr( Self )
  810. End Method
  811. End Type
  812. ' super.ident( args )
  813. Type TInvokeSuperExpr Extends TExpr
  814. Field ident$
  815. Field args:TExpr[]
  816. Field origFuncDecl:TFuncDecl
  817. Field funcDecl:TFuncDecl
  818. Field classScope:TClassDecl
  819. Field superClass:TClassDecl
  820. Field _identLower:String
  821. Method Create:TInvokeSuperExpr( ident$,args:TExpr[] = Null, _identLower:String = Null )
  822. Self.ident=ident
  823. If args Then
  824. Self.args=args
  825. Else
  826. Self.args = New TExpr[0]
  827. End If
  828. Self._identLower = _identLower
  829. Return Self
  830. End Method
  831. Method IdentLower:String()
  832. If Not _identLower Then
  833. _identLower = ident.ToLower()
  834. End If
  835. Return _identLower
  836. End Method
  837. Method Copy:TExpr()
  838. Return New TInvokeSuperExpr.Create( ident,CopyArgs(args), _identLower )
  839. End Method
  840. Method Semant:TExpr()
  841. If exprType Return Self
  842. 'If _env.FuncScope().IsStatic() Err "Illegal use of Super."
  843. classScope=_env.ClassScope()
  844. superClass=classScope.superClass
  845. If Not superClass Err "Type has no super class."
  846. args=SemantArgs( args )
  847. Try
  848. ' get the local version of the method from local class scope
  849. origFuncDecl=classScope.FindFuncDecl(IdentLower(),args,,,,True,SCOPE_CLASS_LOCAL)
  850. Catch errorMessage:String
  851. If errorMessage.StartsWith("Compile Error") Then
  852. Throw errorMessage
  853. Else
  854. ' if there isn't one, we'll just use a Super version of it anyway as a reference.
  855. origFuncDecl=classScope.FindFuncDecl(IdentLower(),args,,,,,SCOPE_CLASS_HEIRARCHY)
  856. End If
  857. End Try
  858. funcDecl=superClass.FindFuncDecl( IdentLower(),args,,,,,SCOPE_CLASS_HEIRARCHY )
  859. If Not funcDecl Err "Can't find superclass method '"+ident+"'."
  860. ' ensure the super function has been semanted
  861. funcDecl.Semant()
  862. ' for static scope, we need to change class scope to that of the super class
  863. If _env.FuncScope().IsStatic() Then
  864. classScope = TClassDecl(funcDecl.scope)
  865. End If
  866. args=CastArgs( args,funcDecl )
  867. exprType=funcDecl.retType
  868. Return Self
  869. End Method
  870. Method Trans$()
  871. Return _trans.TransInvokeSuperExpr( Self )
  872. End Method
  873. End Type
  874. ' Self
  875. Type TSelfExpr Extends TExpr
  876. Method Copy:TExpr()
  877. Return New TSelfExpr
  878. End Method
  879. Method Semant:TExpr()
  880. If exprType Return Self
  881. 'If _env.FuncScope().IsStatic() Err "Illegal use of Self within static scope."
  882. Local scope:TClassDecl = _env.ClassScope()
  883. If Not scope Then
  884. Err "'Self' can only be used within methods."
  885. End If
  886. exprType=New TObjectType.Create( scope )
  887. Return Self
  888. End Method
  889. Method Trans$()
  890. Return _trans.TransSelfExpr( Self )
  891. End Method
  892. End Type
  893. Const CAST_EXPLICIT:Int=1
  894. Type TCastExpr Extends TExpr
  895. Field ty:TType
  896. Field expr:TExpr
  897. Field flags:Int
  898. Method Create:TCastExpr( ty:TType,expr:TExpr,flags:Int=0 )
  899. Self.ty=ty
  900. Self.expr=expr
  901. Self.flags=flags
  902. Return Self
  903. End Method
  904. Method Copy:TExpr()
  905. Return New TCastExpr.Create( ty,CopyExpr(expr),flags )
  906. End Method
  907. Method Semant:TExpr()
  908. If exprType Return Self
  909. ty=ty.Semant()
  910. If TInvokeExpr(expr) Then
  911. TInvokeExpr(expr).isRhs = True
  912. Else If TIdentExpr(expr) Then
  913. TIdentExpr(expr).isRhs = True
  914. End If
  915. expr=expr.Semant()
  916. Local src:TType=expr.exprType
  917. 'equal?
  918. If src.EqualsType( ty ) Return expr
  919. 'upcast?
  920. If src.ExtendsType( ty )
  921. 'cast from void[] to T[]
  922. If TArrayType(src) And TVoidType( TArrayType(src).elemType )
  923. Return New TConstExpr.Create( ty,"" ).Semant()
  924. EndIf
  925. If src._flags & TType.T_VARPTR Then
  926. exprType = ty
  927. Return Self
  928. End If
  929. If TStringType(ty) And TObjectType(src)
  930. ' only if explicitly cast
  931. If flags & CAST_EXPLICIT Then
  932. exprType = ty
  933. 'Return Self
  934. End If
  935. End If
  936. 'Box/unbox?...
  937. 'If TObjectType( ty ) And Not TObjectType( src )
  938. 'Box!
  939. ' expr=New TNewObjectExpr.Create( ty,[expr] ).Semant()
  940. 'Else
  941. If TObjectType( src ) And Not TObjectType( ty ) And Not TStringType( ty )
  942. 'Unbox!
  943. Local op$
  944. 'If TBoolType( ty )
  945. ' op="ToBool"
  946. 'Else
  947. If TIntType( ty )
  948. op="ToInt"
  949. Else If TFloatType( ty )
  950. op="ToFloat"
  951. Else If TStringType( ty )
  952. op="ToString"
  953. Else If IsPointerType(ty, 0, TType.T_POINTER)
  954. exprType = ty
  955. If flags = CAST_EXPLICIT Then
  956. Return Self
  957. Else
  958. If Not TObjectType( src ).classDecl.IsExtern() Then
  959. Return Self
  960. Else
  961. Return expr
  962. End If
  963. End If
  964. Else
  965. InternalErr
  966. EndIf
  967. Local fdecl:TFuncDecl=src.GetClass().FindFuncDecl( op,,,,,,SCOPE_ALL )
  968. expr=New TInvokeMemberExpr.Create( expr,fdecl ).Semant()
  969. EndIf
  970. If TNullType(src) Then
  971. exprType = ty
  972. End If
  973. If TBoolType(src) And (TNumericType(ty) Or TStringType(ty)) Then
  974. exprType = ty
  975. End If
  976. If TNumericType(src) And (TNumericType(ty) Or TStringType(ty)) Then
  977. ' intrinsics can only cast between selves
  978. If (TIntrinsicType(src) And TIntrinsicType(ty)=Null) Or (TIntrinsicType(ty) And TIntrinsicType(src)=Null) Then
  979. If TFloat64Type(src) Or TFloat64Type(ty) Then
  980. If (TFloat64Type(src) And (TLongType(ty) Or TULongType(ty))) Or (TFloat64Type(ty) And (TLongType(src) Or TULongType(src))) Then
  981. ' ok
  982. Else
  983. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  984. End If
  985. Else
  986. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  987. End If
  988. Else If TIntrinsicType(src) And TIntrinsicType(ty) Then
  989. If (TFloat64Type(src) And TFloat64Type(ty)=Null) Or (TFloat64Type(ty) And TFloat64Type(src)=Null) Then
  990. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  991. End If
  992. End If
  993. exprType = ty
  994. End If
  995. If TObjectType(ty) And (TObjectType(src) Or TStringType(src) Or TArrayType(src)) Then
  996. exprType = ty
  997. Return Self
  998. End If
  999. If TFunctionPtrType(src) And IsPointerType(ty, 0, TType.T_POINTER) Then
  1000. exprType = ty
  1001. End If
  1002. Else If TBoolType( ty )
  1003. If TVoidType( src )
  1004. Err "Cannot convert from Void to Int."
  1005. EndIf
  1006. If flags & CAST_EXPLICIT
  1007. exprType=ty
  1008. EndIf
  1009. Else If ty.ExtendsType( src )
  1010. If flags & CAST_EXPLICIT
  1011. 'if both objects or both non-objects...
  1012. If (TObjectType(ty)<>Null)=(TObjectType(src)<>Null) Then
  1013. exprType=ty
  1014. If TFunctionPtrType(ty) And TInvokeExpr(expr) And Not TInvokeExpr(expr).invokedWithBraces Then
  1015. Return expr
  1016. End If
  1017. Return Self
  1018. End If
  1019. If (TStringType(ty) Or TArrayType(ty)) And TObjectType(src) Then
  1020. exprType=ty
  1021. Return Self
  1022. End If
  1023. 'Else ' if not explicitly cast, we can't just auto-cast it ourselves here.
  1024. 'If (TObjectType(ty)<>Null) And (TObjectType(src)<>Null) exprType=ty
  1025. EndIf
  1026. EndIf
  1027. If TArrayType(ty) And TArrayType(src) Then
  1028. If TArrayType(ty).dims = TArrayType(src).dims Then
  1029. If TArrayExpr(expr) Then
  1030. Local last:TType
  1031. For Local e:TExpr = EachIn TArrayExpr(expr).exprs
  1032. If TNullType(e.exprType) Then
  1033. Err "Auto array element has no type"
  1034. End If
  1035. 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
  1036. ' array takes generic objects, so we don't care if source elements are the same kinds.
  1037. Else
  1038. If last <> Null And Not last.EqualsType(e.exprType) Then
  1039. Err "Auto array elements must have identical types"
  1040. End If
  1041. If Not TArrayType(ty).elemType.EqualsType(e.exprType) Then
  1042. If (TObjectType(TArrayType(ty).elemType) = Null And TStringType(TArrayType(ty).elemType) = Null) Or (TObjectType(e.exprType) = Null And TStringType(e.exprType) = Null) Then
  1043. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1044. Else If TStringType(e.exprType) = Null And Not TObjectType(e.exprType).ExtendsType(TObjectType(TArrayType(ty).elemType)) Then
  1045. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1046. End If
  1047. End If
  1048. End If
  1049. last = e.exprType
  1050. Next
  1051. End If
  1052. exprType = ty
  1053. Return Self
  1054. End If
  1055. End If
  1056. 'If TStringType(src) And TStringVarPtrType(ty) Then
  1057. ' exprType = ty
  1058. ' Return Self
  1059. 'End If
  1060. ' If TArrayType(src) And TPointerType(ty) Then
  1061. ' exprType = ty
  1062. ' Return expr
  1063. ' End If
  1064. If TFunctionPtrType(ty) And TInvokeExpr(expr) Then
  1065. ' a function ptr to function ptr
  1066. If Not TInvokeExpr(expr).invokedWithBraces Then
  1067. src = New TFunctionPtrType
  1068. TFunctionPtrType(src).func = TInvokeExpr(expr).decl
  1069. ' signatures should match
  1070. If TFunctionPtrType(ty).func.EqualsFunc(TInvokeExpr(expr).decl) Then
  1071. exprType = ty
  1072. Return expr
  1073. End If
  1074. Else
  1075. ' return type should be function ptr?
  1076. ' TODO
  1077. exprType = ty
  1078. Return expr
  1079. End If
  1080. End If
  1081. 'If TIntType(ty) And Not IsPointerType(ty, 0, TType.T_POINTER) And IsPointerType(src, 0, TType.T_POINTER) Then
  1082. ' exprType = ty
  1083. ' If flags & CAST_EXPLICIT Then
  1084. ' Return Self
  1085. ' End If
  1086. ' Return expr
  1087. 'End If
  1088. ' explicit cast to number
  1089. If IsNumericType(ty) And IsPointerType(src, 0, TType.T_POINTER) Then
  1090. If flags = CAST_EXPLICIT Then
  1091. exprType = ty
  1092. Return Self
  1093. Else
  1094. exprType = Null
  1095. End If
  1096. End If
  1097. ' If TPointerType(ty) And TIntType(src) Then
  1098. ' exprType = ty
  1099. ' Return expr
  1100. ' End If
  1101. ' If TIntType(ty) And TObjectType(src) Then
  1102. ' DebugStop ' Bah woz ere
  1103. ' exprType = ty
  1104. ' Return expr
  1105. ' End If
  1106. If TObjectType(src) And TNullDecl(TObjectType(src).classDecl) Then
  1107. exprType = ty
  1108. Return expr
  1109. End If
  1110. If TObjectType(src) And TObjectType(ty) And (ty._flags & TType.T_VAR) Then ' TODO : May be VARPTR instead?
  1111. 'exprType = NewPointerType(TType.T_BYTE)
  1112. exprType = ty
  1113. Return Self
  1114. End If
  1115. If TStringType(src) And ((src._flags & TType.T_CHAR_PTR) Or (src._flags & TType.T_SHORT_PTR)) And TStringType(ty) Then
  1116. exprType = ty
  1117. Return Self
  1118. End If
  1119. ' cast from "some kind of object" array to Object[]
  1120. If TArrayType(ty) And TArrayType(src)
  1121. If (TObjectType(TArrayType(src).elemType) Or TStringType(TArrayType(src).elemType) Or TArrayType(TArrayType(src).elemType)) And TObjectType(TArrayType(ty).elemType) Then
  1122. If TObjectType(TArrayType(ty).elemType).classDecl.ident = "Object" Then
  1123. exprType = ty
  1124. Return Self
  1125. End If
  1126. Else
  1127. If TArrayType(ty).dims = TArrayType(src).dims Then
  1128. exprType = ty
  1129. End If
  1130. End If
  1131. End If
  1132. If TArrayType(ty) And TObjectType(src)
  1133. If TObjectType(src).classDecl.ident = "___Array" Then
  1134. exprType = ty
  1135. Return expr
  1136. Else If TObjectType(src).classDecl.ident = "Object" Then
  1137. exprType = ty
  1138. Return Self
  1139. End If
  1140. End If
  1141. If IsPointerType(ty, 0, TType.T_POINTER | TType.T_CHAR_PTR | TType.T_SHORT_PTR) Then
  1142. If IsNumericType(src) And Not (src._flags & TType.T_VARPTR) Then
  1143. 'If IsPointerType(ty,0,TType.T_POINTER) Then
  1144. ' exprType = TNumericType(src).ToPointer()
  1145. 'Else
  1146. exprType = ty
  1147. 'End If
  1148. Return Self
  1149. Else If TNumericType(src) And (src._flags & TType.T_VARPTR) Then
  1150. exprType = expr.exprType
  1151. Else If TArrayType(src) Then
  1152. ' for functions and index access, use a new local variable
  1153. If Not TVarExpr(expr) And Not TMemberVarExpr(expr) Then
  1154. Local tmp:TLocalDecl=New TLocalDecl.Create( "", expr.exprType, expr,, True )
  1155. tmp.Semant()
  1156. Local v:TVarExpr = New TVarExpr.Create( tmp )
  1157. expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), v ).Semant()
  1158. End If
  1159. If TNumericType(TArrayType(src).elemType) Then
  1160. exprType = TNumericType(TArrayType(src).elemType).ToPointer()
  1161. Return Self
  1162. Else
  1163. ' map arrays to byte ptr
  1164. exprType = TType.MapToPointerType(New TByteType)
  1165. End If
  1166. Else If TStringType(src) Then
  1167. exprType = ty
  1168. Return Self
  1169. End If
  1170. End If
  1171. If TStringType(src) And TStringType(ty) And (ty._flags & TType.T_VAR) Then
  1172. exprType = ty
  1173. Return Self
  1174. End If
  1175. If TVarPtrType(ty) Then
  1176. If Not TVarExpr(expr) And Not TMemberVarExpr(expr) And Not (TStmtExpr(expr) And TIndexExpr(TStmtExpr(expr).expr)) Then
  1177. If Not TIndexExpr(expr) Or (TIndexExpr(expr) And Not TVarExpr(TIndexExpr(expr).expr) And Not TMemberVarExpr(TIndexExpr(expr).expr)) Then
  1178. Err "Subexpression for 'Ptr' must be a variable"
  1179. End If
  1180. End If
  1181. exprType = src.Copy()
  1182. exprType._flags :| TType.T_VARPTR
  1183. ty = exprType
  1184. Return Self
  1185. End If
  1186. If TFunctionPtrType(ty) And IsPointerType(src, 0, TType.T_POINTER) Then
  1187. exprType = ty
  1188. Return Self
  1189. End If
  1190. If TObjectType(ty) And TObjectType(src) And TObjectType(src).classdecl.IsInterface() And flags & CAST_EXPLICIT Then
  1191. exprType = ty
  1192. Return Self
  1193. End If
  1194. If Not exprType
  1195. Err "Unable to convert from "+src.ToString()+" to "+ty.ToString()+"."
  1196. EndIf
  1197. If TConstExpr( expr ) Then
  1198. Local ex:TExpr = EvalConst()
  1199. If flags & CAST_EXPLICIT Then
  1200. Return New TCastExpr.Create(exprType, ex, 1).Semant()
  1201. Else
  1202. Return ex
  1203. End If
  1204. End If
  1205. Return Self
  1206. End Method
  1207. Method Eval$()
  1208. Local val$=expr.Eval()
  1209. If Not val Return val
  1210. If TBoolType( exprType )
  1211. If TIntegralType(expr.exprType)
  1212. If Long( val ) Return "1"
  1213. Return ""
  1214. Else If TDecimalType( expr.exprType )
  1215. If Double( val ) Return "1"
  1216. Return ""
  1217. Else If TStringType( expr.exprType )
  1218. If val.Length Return "1"
  1219. Return ""
  1220. EndIf
  1221. Else If TIntType( exprType )
  1222. If TBoolType( expr.exprType )
  1223. If val Return "1"
  1224. Return "0"
  1225. EndIf
  1226. Return Int( val )
  1227. Else If TUIntType( exprType )
  1228. Return Long( val )
  1229. Else If TShortType( exprType )
  1230. Return Short( val )
  1231. Else If TFloatType( exprType )
  1232. Return Float( val )
  1233. Else If TDoubleType( exprType )
  1234. Return Double( val )
  1235. Else If TLongType( exprType )
  1236. Return Long( val )
  1237. Else If TULongType( exprType )
  1238. Return Long( val )
  1239. Else If TSizeTType( exprType )
  1240. Return Long( val )
  1241. Else If TInt128Type( exprType )
  1242. Return Long( val )
  1243. Else If TFloat128Type( exprType )
  1244. Return Float( val )
  1245. Else If TDouble128Type( exprType )
  1246. Return Float( val )
  1247. Else If TFloat64Type( exprType )
  1248. Return Float( val )
  1249. Else If TStringType( exprType )
  1250. Return String( val )
  1251. Else If TByteType( exprType )
  1252. Return Byte( val )
  1253. Else If TWParamType( exprType )
  1254. Return Long( val )
  1255. Else If TLParamType( exprType )
  1256. Return Long( val )
  1257. Else If TObjectType( exprType )
  1258. If TStringType( expr.exprType )
  1259. Return val
  1260. End If
  1261. EndIf
  1262. Return Super.Eval()
  1263. End Method
  1264. Method Trans$()
  1265. Return _trans.TransCastExpr( Self )
  1266. End Method
  1267. Method ToString$()
  1268. Local t$="TCastExpr(" + ty.ToString()
  1269. If expr t:+","+expr.ToString()
  1270. Return t+")"
  1271. End Method
  1272. End Type
  1273. 'op = '+', '-', '~'
  1274. Type TUnaryExpr Extends TExpr
  1275. Field op$,expr:TExpr
  1276. Method Create:TUnaryExpr( op$,expr:TExpr )
  1277. Self.op=op
  1278. Self.expr=expr
  1279. Return Self
  1280. End Method
  1281. Method Copy:TExpr()
  1282. Return New TUnaryExpr.Create( op,CopyExpr(expr) )
  1283. End Method
  1284. Method Semant:TExpr()
  1285. If exprType Return Self
  1286. Select op
  1287. Case "+","-"
  1288. expr=expr.Semant()
  1289. If Not TNumericType( expr.exprType ) Or IsPointerType(expr.exprType) Then
  1290. Err expr.ToString()+" must be numeric for use with unary operator '"+op+"'"
  1291. End If
  1292. exprType=expr.exprType
  1293. ' Remove Var-ness, if required. "expr" will still be "Var"
  1294. If exprType._flags & TType.T_VAR Then
  1295. exprType = exprType.Copy()
  1296. exprType._flags :~ TType.T_VAR
  1297. End If
  1298. Case "~~"
  1299. expr=expr.SemantAndCast( New TIntType )
  1300. exprType=New TIntType
  1301. Case "not"
  1302. expr=expr.SemantAndCast( New TBoolType,CAST_EXPLICIT )
  1303. exprType=New TBoolType
  1304. Default
  1305. InternalErr
  1306. End Select
  1307. If TConstExpr( expr ) Return EvalConst()
  1308. Return Self
  1309. End Method
  1310. Method Eval$()
  1311. Local val$=expr.Eval()
  1312. Select op
  1313. Case "~~"
  1314. Return ~Int( val )
  1315. Case "+"
  1316. Return val
  1317. Case "-"
  1318. If val.StartsWith( "-" ) Return val[1..]
  1319. Return "-"+val
  1320. Case "not"
  1321. If val Return ""
  1322. Return "1"
  1323. End Select
  1324. InternalErr
  1325. End Method
  1326. Method Trans$()
  1327. Return _trans.TransUnaryExpr( Self )
  1328. End Method
  1329. End Type
  1330. Type TBinaryExpr Extends TExpr
  1331. Field op$
  1332. Field lhs:TExpr
  1333. Field rhs:TExpr
  1334. Method Trans$()
  1335. Return _trans.TransBinaryExpr( Self )
  1336. End Method
  1337. Method ToString$()
  1338. Return "(" + lhs.ToString() + " " + op + " " + rhs.ToString() + ")"
  1339. End Method
  1340. End Type
  1341. ' * / + / & ~ | ^ shl shr
  1342. Type TBinaryMathExpr Extends TBinaryExpr
  1343. Method Create:TBinaryMathExpr( op$,lhs:TExpr,rhs:TExpr )
  1344. Self.op=op
  1345. Self.lhs=lhs
  1346. Self.rhs=rhs
  1347. Return Self
  1348. End Method
  1349. Method Copy:TExpr()
  1350. Return New TBinaryMathExpr.Create( op,CopyExpr(lhs),CopyExpr(rhs) )
  1351. End Method
  1352. Method Semant:TExpr()
  1353. If exprType Return Self
  1354. lhs=lhs.Semant()
  1355. rhs=rhs.Semant()
  1356. ' operator overload?
  1357. If TObjectType(lhs.exprType) Then
  1358. Local args:TExpr[] = [rhs]
  1359. Try
  1360. Local decl:TFuncDecl = TFuncDecl(TObjectType(lhs.exprType).classDecl.FindFuncDecl(op, args,,,,True,SCOPE_CLASS_HEIRARCHY))
  1361. If decl Then
  1362. Return New TInvokeMemberExpr.Create( lhs, decl, args ).Semant()
  1363. End If
  1364. Catch error:String
  1365. Err "Operator " + op + " cannot be used with Objects."
  1366. End Try
  1367. End If
  1368. Select op
  1369. Case "&","~~","|","shl","shr"
  1370. If TFloat128Type(lhs.exprType) Then
  1371. exprType=New TInt128Type
  1372. Else If TDouble128Type(lhs.exprType) Then
  1373. exprType=New TInt128Type
  1374. Else If TFloat64Type(lhs.exprType) Then
  1375. exprType=New TInt128Type
  1376. Else If TDoubleType(lhs.exprType) Then
  1377. exprType=New TLongType
  1378. Else If TFloatType(lhs.exprType) Then
  1379. exprType=New TIntType
  1380. Else If TUIntType(lhs.exprType) Then
  1381. exprType=New TUIntType
  1382. Else If TLongType(lhs.exprType) Then
  1383. exprType=New TLongType
  1384. Else If TULongType(lhs.exprType) Then
  1385. exprType=New TULongType
  1386. Else If TSizeTType(lhs.exprType) Then
  1387. exprType=New TSizeTType
  1388. Else If TWParamType(lhs.exprType) Then
  1389. exprType=New TWParamType
  1390. Else If TLParamType(lhs.exprType) Then
  1391. exprType=New TLParamType
  1392. Else
  1393. exprType=New TIntType
  1394. End If
  1395. Case "^"
  1396. exprType=New TDoubleType
  1397. Default
  1398. exprType=BalanceTypes( lhs.exprType,rhs.exprType )
  1399. If TStringType( exprType )
  1400. If op<>"+"
  1401. Err "Illegal string operator."
  1402. EndIf
  1403. Else If TVoidType( exprType ) Then
  1404. Err "Illegal operation on a void expression."
  1405. Else If Not TNumericType( exprType ) And Not IsPointerType( exprType, 0, TType.T_POINTER ) And Not TArrayType( exprType ) And Not TBoolType( exprType )
  1406. Err "Illegal expression type."
  1407. Else If IsPointerType( exprType, 0, TType.T_POINTER ) And op <> "+" And op <> "-" Then
  1408. Err "Illegal expression type."
  1409. Else If IsPointerType( lhs.exprType, 0, TType.T_POINTER ) And IsPointerType( rhs.exprType, 0, TType.T_POINTER ) And op <> "-" Then
  1410. Err "Illegal expression type."
  1411. EndIf
  1412. End Select
  1413. If (op = "+" Or op = "-") And IsPointerType(exprType, 0, TType.T_POINTER) And TNumericType(lhs.exprType) Then
  1414. ' with pointer addition we don't cast the numeric to a pointer
  1415. Else
  1416. lhs=lhs.Cast( exprType )
  1417. End If
  1418. If (op = "+" Or op = "-") And IsPointerType(exprType, 0, TType.T_POINTER) And TNumericType(rhs.exprType) Then
  1419. ' with pointer addition we don't cast the numeric to a pointer
  1420. Else
  1421. rhs=rhs.Cast( exprType )
  1422. End If
  1423. If IsPointerType( lhs.exprType, 0, TType.T_POINTER ) And IsPointerType( rhs.exprType, 0, TType.T_POINTER ) And op = "-" Then
  1424. exprType = New TIntType
  1425. End If
  1426. If TConstExpr( lhs ) And TConstExpr( rhs ) Return EvalConst()
  1427. Return Self
  1428. End Method
  1429. Method Eval$()
  1430. Local lhs$=Self.lhs.Eval()
  1431. Local rhs$=Self.rhs.Eval()
  1432. If TIntType( exprType )
  1433. Local x:Int=Int(lhs),y:Int=Int(rhs)
  1434. Select op
  1435. Case "^" Return x^y
  1436. Case "*" Return x*y
  1437. Case "/" Return x/y
  1438. Case "mod" Return x Mod y
  1439. Case "shl" Return x Shl y
  1440. Case "shr" Return x Shr y
  1441. Case "+" Return x + y
  1442. Case "-" Return x - y
  1443. Case "&" Return x & y
  1444. Case "~~" Return x ~ y
  1445. Case "|" Return x | y
  1446. End Select
  1447. Else If TLongType( exprType ) Or TSizeTType(exprType) Or TUIntType(exprType) Or TULongType(exprType) Or TInt128Type(exprType) Or TWParamType(exprType) Or TLParamType(exprType)
  1448. Local x:Long=Long(lhs),y:Long=Long(rhs)
  1449. Select op
  1450. Case "^" Return x^y
  1451. Case "*" Return x*y
  1452. Case "/" Return x/y
  1453. Case "mod" Return x Mod y
  1454. Case "shl" Return x Shl y
  1455. Case "shr" Return x Shr y
  1456. Case "+" Return x + y
  1457. Case "-" Return x - y
  1458. Case "&" Return x & y
  1459. Case "~~" Return x ~ y
  1460. Case "|" Return x | y
  1461. End Select
  1462. Else If TFloatType( exprType )
  1463. Local x:Float=Float(lhs),y:Float=Float(rhs)
  1464. Select op
  1465. Case "^" Return x^y
  1466. Case "*" Return x * y
  1467. Case "/" Return x / y
  1468. Case "mod" Return x Mod y
  1469. Case "+" Return x + y
  1470. Case "-" Return x - y
  1471. End Select
  1472. Else If TDoubleType( exprType ) Or TFloat128Type(exprType) Or TDouble128Type(exprType) Or TFloat64Type(exprType)
  1473. Local x:Double=Double(lhs),y:Double=Double(rhs)
  1474. Select op
  1475. Case "^" Return x^y
  1476. Case "*" Return x * y
  1477. Case "/" Return x / y
  1478. Case "mod" Return x Mod y
  1479. Case "+" Return x + y
  1480. Case "-" Return x - y
  1481. End Select
  1482. Else If TStringType( exprType )
  1483. Select op
  1484. Case "+"
  1485. _appInstance.removeStringConst(lhs)
  1486. _appInstance.removeStringConst(rhs)
  1487. Return lhs+rhs
  1488. End Select
  1489. EndIf
  1490. InternalErr
  1491. End Method
  1492. End Type
  1493. '=,<>,<,<=,>,>=
  1494. Type TBinaryCompareExpr Extends TBinaryExpr
  1495. Field ty:TType
  1496. Method Create:TBinaryCompareExpr( op$,lhs:TExpr,rhs:TExpr )
  1497. Self.op=op
  1498. Self.lhs=lhs
  1499. Self.rhs=rhs
  1500. Return Self
  1501. End Method
  1502. Method Copy:TExpr()
  1503. Return New TBinaryCompareExpr.Create( op,CopyExpr(lhs),CopyExpr(rhs) )
  1504. End Method
  1505. Method Semant:TExpr()
  1506. If exprType Return Self
  1507. lhs=lhs.Semant()
  1508. rhs=rhs.Semant()
  1509. ' operator overload?
  1510. If TObjectType(lhs.exprType) Then
  1511. Local args:TExpr[] = [rhs]
  1512. Try
  1513. Local decl:TFuncDecl = TFuncDecl(TObjectType(lhs.exprType).classDecl.FindFuncDecl(op, args,,,,True,SCOPE_CLASS_HEIRARCHY))
  1514. If decl Then
  1515. Return New TInvokeMemberExpr.Create( lhs, decl, args ).Semant()
  1516. End If
  1517. Catch error:String
  1518. ' no overload, continue...
  1519. End Try
  1520. End If
  1521. ty=BalanceTypes( lhs.exprType,rhs.exprType )
  1522. lhs=lhs.Cast( ty )
  1523. rhs=rhs.Cast( ty )
  1524. exprType=New TBoolType
  1525. If TConstExpr( lhs ) And TConstExpr( rhs ) Return EvalConst()
  1526. Return Self
  1527. End Method
  1528. Method Eval$()
  1529. Local r:Int=-1
  1530. If TBoolType( ty )
  1531. Local lhs:Int=Int(Self.lhs.Eval())
  1532. Local rhs:Int=Int(Self.rhs.Eval())
  1533. Select op
  1534. Case "=" r=(lhs= rhs)
  1535. Case "<>" r=(lhs<>rhs)
  1536. End Select
  1537. Else If TIntType( ty )
  1538. Local lhs:Int=Int( Self.lhs.Eval() )
  1539. Local rhs:Int=Int( Self.rhs.Eval() )
  1540. Select op
  1541. Case "=" r=(lhs= rhs)
  1542. Case "<>" r=(lhs<>rhs)
  1543. Case "<" r=(lhs< rhs)
  1544. Case "<=", "=<" r=(lhs<=rhs)
  1545. Case ">" r=(lhs> rhs)
  1546. Case ">=", "=>" r=(lhs>=rhs)
  1547. End Select
  1548. Else If TLongType( ty ) Or TSizeTType( ty ) Or TUIntType( ty ) Or TULongType( ty ) Or TInt128Type(ty) Or TWParamType(ty) Or TLParamType(ty)
  1549. Local lhs:Long=Long( Self.lhs.Eval() )
  1550. Local rhs:Long=Long( Self.rhs.Eval() )
  1551. Select op
  1552. Case "=" r=(lhs= rhs)
  1553. Case "<>" r=(lhs<>rhs)
  1554. Case "<" r=(lhs< rhs)
  1555. Case "<=", "=<" r=(lhs<=rhs)
  1556. Case ">" r=(lhs> rhs)
  1557. Case ">=", "=>" r=(lhs>=rhs)
  1558. End Select
  1559. Else If TFloatType( ty )
  1560. Local lhs:Float=Float( Self.lhs.Eval() )
  1561. Local rhs:Float=Float( Self.rhs.Eval() )
  1562. Select op
  1563. Case "=" r=(lhs= rhs)
  1564. Case "<>" r=(lhs<>rhs)
  1565. Case "<" r=(lhs< rhs)
  1566. Case "<=", "=<" r=(lhs<=rhs)
  1567. Case ">" r=(lhs> rhs)
  1568. Case ">=", "=>" r=(lhs>=rhs)
  1569. End Select
  1570. Else If TDoubleType( ty ) Or TFloat128Type(ty) Or TDouble128Type(ty) Or TFloat64Type(ty)
  1571. Local lhs:Double=Double( Self.lhs.Eval() )
  1572. Local rhs:Double=Double( Self.rhs.Eval() )
  1573. Select op
  1574. Case "=" r=(lhs= rhs)
  1575. Case "<>" r=(lhs<>rhs)
  1576. Case "<" r=(lhs< rhs)
  1577. Case "<=", "=<" r=(lhs<=rhs)
  1578. Case ">" r=(lhs> rhs)
  1579. Case ">=", "=>" r=(lhs>=rhs)
  1580. End Select
  1581. Else If TStringType( ty )
  1582. Local lhs:String=String( Self.lhs.Eval() )
  1583. Local rhs:String=String( Self.rhs.Eval() )
  1584. Select op
  1585. Case "=" r=(lhs= rhs)
  1586. Case "<>" r=(lhs<>rhs)
  1587. Case "<" r=(lhs< rhs)
  1588. Case "<=", "=<" r=(lhs<=rhs)
  1589. Case ">" r=(lhs> rhs)
  1590. Case ">=", "=>" r=(lhs>=rhs)
  1591. End Select
  1592. EndIf
  1593. If r=1 Return "1"
  1594. If r=0 Return ""
  1595. InternalErr
  1596. End Method
  1597. End Type
  1598. 'and, or
  1599. Type TBinaryLogicExpr Extends TBinaryExpr
  1600. Method Create:TBinaryLogicExpr( op$,lhs:TExpr,rhs:TExpr )
  1601. Self.op=op
  1602. Self.lhs=lhs
  1603. Self.rhs=rhs
  1604. Return Self
  1605. End Method
  1606. Method Copy:TExpr()
  1607. Return New TBinaryLogicExpr.Create( op,CopyExpr(lhs),CopyExpr(rhs) )
  1608. End Method
  1609. Method Semant:TExpr()
  1610. If exprType Return Self
  1611. lhs=lhs.SemantAndCast( New TBoolType,CAST_EXPLICIT )
  1612. rhs=rhs.SemantAndCast( New TBoolType,CAST_EXPLICIT )
  1613. exprType=New TBoolType
  1614. If TConstExpr( lhs ) And TConstExpr( rhs ) Return EvalConst()
  1615. Return Self
  1616. End Method
  1617. Method Eval$()
  1618. Select op
  1619. Case "and" If lhs.Eval() And rhs.Eval() Return "1" Else Return ""
  1620. Case "or" If lhs.Eval() Or rhs.Eval() Return "1" Else Return ""
  1621. End Select
  1622. InternalErr
  1623. End Method
  1624. End Type
  1625. Type TIndexExpr Extends TExpr
  1626. Field expr:TExpr
  1627. Field index:TExpr[]
  1628. Method Create:TIndexExpr( expr:TExpr,index:TExpr[] )
  1629. Self.expr=expr
  1630. Self.index=index
  1631. Return Self
  1632. End Method
  1633. Method Copy:TExpr()
  1634. If exprType Return Self
  1635. Local ind:TExpr[]
  1636. For Local i:Int = 0 Until index.length
  1637. ind = ind + [CopyExpr(index[i])]
  1638. Next
  1639. Return New TIndexExpr.Create( CopyExpr(expr),ind )
  1640. End Method
  1641. Method Semant:TExpr()
  1642. If exprType Return Self
  1643. expr=expr.Semant()
  1644. ' for functions and index access, use a new local variable
  1645. If Not TVarExpr(expr) And Not TMemberVarExpr(expr) Then
  1646. Local tmp:TLocalDecl=New TLocalDecl.Create( "", TType.MapVarPointerToPointerType(expr.exprType.Copy()), expr,, True )
  1647. tmp.Semant()
  1648. Local v:TVarExpr = New TVarExpr.Create( tmp )
  1649. expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), v ).Semant()
  1650. End If
  1651. For Local i:Int = 0 Until index.length
  1652. index[i]=index[i].SemantAndCast( New TUIntType, True )
  1653. Next
  1654. If TStringType( expr.exprType )
  1655. exprType=New TIntType
  1656. If index.length > 1 Then
  1657. Err "Illegal subexpression for string index"
  1658. End If
  1659. Else If TArrayType( expr.exprType )
  1660. exprType= TArrayType( expr.exprType ).elemType
  1661. If TArrayType( expr.exprType ).dims > 1 Then
  1662. ' a multi-dimensional array of arrays is slightly more complex
  1663. If TArrayType(exprType) Then
  1664. ' Local tmpArr:TLocalDecl=New TLocalDecl.Create( "", NewPointerType(TType.T_ARRAY), expr )
  1665. ' Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), Self ).Semant()
  1666. Local sizeExpr:TExpr = New TArraySizeExpr.Create(expr, Null, index)
  1667. index = [sizeExpr]
  1668. Local tmp:TLocalDecl=New TLocalDecl.Create( "", NewPointerType(TType.T_UINT), sizeExpr,,True )
  1669. TArraySizeExpr(sizeExpr).val = tmp
  1670. Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), Self ).Semant()
  1671. stmt.exprType = exprType
  1672. Return stmt
  1673. Else
  1674. Local sizeExpr:TExpr = New TArraySizeExpr.Create(expr, Null, index).Semant()
  1675. index = [sizeExpr]
  1676. Local tmp:TLocalDecl=New TLocalDecl.Create( "", NewPointerType(TType.T_UINT), sizeExpr,,True )
  1677. TArraySizeExpr(sizeExpr).val = tmp
  1678. Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), Self ).Semant()
  1679. stmt.exprType = exprType
  1680. Return stmt
  1681. End If
  1682. End If
  1683. 'If TObjectType(exprType) And Not TStringType(exprType) And Not TArrayType(exprType) Then
  1684. ' Local tmp:TLocalDecl=New TLocalDecl.Create( "", exprType,expr )
  1685. ' Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ),New TVarExpr.Create( tmp ) ).Semant()
  1686. ' stmt.exprType = exprType
  1687. ' Return stmt
  1688. 'End If
  1689. Else If TNumericType(expr.exprType) And IsPointerType( expr.exprType, 0 , TType.T_POINTER | TType.T_VARPTR)' And Not TFunctionPtrType( expr.exprType )
  1690. exprType=TType.MapPointerToPrim(TNumericType(expr.exprType))
  1691. 'exprType=TType.intType
  1692. 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 )
  1693. exprType = expr.exprType
  1694. Else
  1695. Err "Only strings, arrays and pointers may be indexed."
  1696. EndIf
  1697. Return Self
  1698. End Method
  1699. Method SemantSet:TExpr( op$,rhs:TExpr )
  1700. Return Semant()
  1701. 'Return Self
  1702. End Method
  1703. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  1704. Local ex:TExpr = Semant()
  1705. If TArrayType( expr.exprType ) And TFunctionPtrType(exprType) Then
  1706. exprType = TFunctionPtrType(exprType).func.retType
  1707. End If
  1708. Return ex
  1709. End Method
  1710. Method Trans$()
  1711. Return _trans.TransIndexExpr( Self )
  1712. End Method
  1713. Method TransVar$()
  1714. Return _trans.TransIndexExpr( Self )
  1715. End Method
  1716. Method ToString$()
  1717. Return "<TIndexExpr<"+ expr.ToString() +"[" + index[0].ToString() + "]>>"
  1718. End Method
  1719. End Type
  1720. Type TSliceExpr Extends TExpr
  1721. Field expr:TExpr
  1722. Field from:TExpr
  1723. Field term:TExpr
  1724. Method Create:TSliceExpr( expr:TExpr,from:TExpr,term:TExpr )
  1725. Self.expr=expr
  1726. Self.from=from
  1727. Self.term=term
  1728. Return Self
  1729. End Method
  1730. Method Copy:TExpr()
  1731. Return New TSliceExpr.Create( CopyExpr(expr),CopyExpr(from),CopyExpr(term) )
  1732. End Method
  1733. Method Semant:TExpr()
  1734. If exprType Return Self
  1735. expr=expr.Semant()
  1736. If (TArrayType( expr.exprType ) And TArrayType( expr.exprType ).dims = 1) Or TStringType( expr.exprType )
  1737. If from from=from.SemantAndCast( New TIntType )
  1738. If term term=term.SemantAndCast( New TIntType )
  1739. exprType=expr.exprType
  1740. ' remove var-ness
  1741. If exprType._flags & TType.T_VAR Then
  1742. exprType = exprType.Copy()
  1743. exprType._flags :~ TType.T_VAR
  1744. End If
  1745. Else
  1746. Err "Slices can only be used with strings or one dimensional arrays"
  1747. EndIf
  1748. ' If TConstExpr( expr ) And TConstExpr( from ) And TConstExpr( term ) Return EvalConst()
  1749. Return Self
  1750. End Method
  1751. Method Eval$()
  1752. Local from:Int=Int( Self.from.Eval() )
  1753. Local term:Int=Int( Self.term.Eval() )
  1754. If TStringType( expr.exprType )
  1755. Return expr.Eval()[ from..term ]
  1756. Else If TArrayType( expr.exprType )
  1757. Todo
  1758. EndIf
  1759. End Method
  1760. Method Trans$()
  1761. Return _trans.TransSliceExpr( Self )
  1762. End Method
  1763. End Type
  1764. Type TArrayExpr Extends TExpr
  1765. Field exprs:TExpr[]
  1766. Method Create:TArrayExpr( exprs:TExpr[] )
  1767. Self.exprs=exprs
  1768. Return Self
  1769. End Method
  1770. Method Copy:TExpr()
  1771. Return New TArrayExpr.Create( CopyArgs(exprs) )
  1772. End Method
  1773. Method Semant:TExpr()
  1774. If exprType Return Self
  1775. If TIdentExpr(exprs[0]) Then
  1776. TIdentExpr(exprs[0]).isRhs = True
  1777. End If
  1778. exprs[0]=exprs[0].Semant()
  1779. Local ty:TType=exprs[0].exprType
  1780. ' convert from varptr to ptr if required
  1781. ty = TType.MapVarPointerToPointerType(ty.Copy())
  1782. If TInvokeExpr(exprs[0]) And Not TInvokeExpr(exprs[0]).invokedWithBraces Then
  1783. ty = New TFunctionPtrType
  1784. Local cp:TDecl = TInvokeExpr(exprs[0]).decl
  1785. TInvokeExpr(exprs[0]).decl = TFuncDecl(TInvokeExpr(exprs[0]).decl.Copy())
  1786. TInvokeExpr(exprs[0]).decl.actual = cp
  1787. TInvokeExpr(exprs[0]).decl.attrs :| FUNC_PTR
  1788. TFunctionPtrType(ty).func = TInvokeExpr(exprs[0]).decl
  1789. For Local i:Int=1 Until exprs.Length
  1790. If TIdentExpr(exprs[1]) Then
  1791. TIdentExpr(exprs[1]).isRhs = True
  1792. End If
  1793. exprs[i]=exprs[i].Semant()
  1794. If TInvokeExpr(exprs[i]) And Not TInvokeExpr(exprs[i]).invokedWithBraces
  1795. cp = TInvokeExpr(exprs[i]).decl
  1796. TInvokeExpr(exprs[i]).decl = TFuncDecl(TInvokeExpr(exprs[i]).decl.Copy())
  1797. TInvokeExpr(exprs[i]).decl.actual = cp
  1798. TInvokeExpr(exprs[i]).decl.attrs :| FUNC_PTR
  1799. ty=BalanceTypes( ty, New TFunctionPtrType )
  1800. Else
  1801. ty=BalanceTypes( ty,exprs[i].exprType )
  1802. End If
  1803. Next
  1804. Else
  1805. For Local i:Int=1 Until exprs.Length
  1806. exprs[i]=exprs[i].Semant()
  1807. ty=BalanceTypes( ty,exprs[i].exprType )
  1808. Next
  1809. End If
  1810. Local last:TType
  1811. For Local i:Int=0 Until exprs.Length
  1812. ' don't cast null types
  1813. If TNullType(exprs[i].exprType) <> Null Then
  1814. Err "Auto array element has no type"
  1815. End If
  1816. Local ety:TType = exprs[i].exprType
  1817. If TBoolType(ety) Then
  1818. ety = New TIntType
  1819. End If
  1820. If last <> Null And Not last.EqualsType(ety) Then
  1821. Err "Auto array elements must have identical types"
  1822. End If
  1823. last = ety
  1824. exprs[i]=exprs[i].Cast( ty )
  1825. Next
  1826. exprType=New TArrayType.Create( ty )
  1827. Return Self
  1828. End Method
  1829. Method Trans$()
  1830. Return _trans.TransArrayExpr( Self )
  1831. End Method
  1832. End Type
  1833. Type TArraySizeExpr Extends TExpr
  1834. Field expr:TExpr
  1835. Field val:TDecl
  1836. Field index:TExpr[]
  1837. Method Create:TArraySizeExpr( expr:TExpr, val:TDecl, index:TExpr[] )
  1838. Self.expr=expr
  1839. Self.val=val
  1840. Self.index=index
  1841. Return Self
  1842. End Method
  1843. Method Copy:TExpr()
  1844. Local ind:TExpr[]
  1845. For Local i:Int = 0 Until index.length
  1846. ind = ind + [CopyExpr(index[i])]
  1847. Next
  1848. Return New TArraySizeExpr.Create( CopyExpr(expr), val, ind )
  1849. End Method
  1850. Method Semant:TExpr()
  1851. If exprType Return Self
  1852. expr=expr.Semant()
  1853. For Local i:Int = 0 Until index.length
  1854. index[i]=index[i].SemantAndCast( New TUIntType )
  1855. Next
  1856. exprType=NewPointerType(TType.T_UINT)
  1857. Return Self
  1858. End Method
  1859. Method Trans$()
  1860. Return _trans.TransArraySizeExpr( Self )
  1861. End Method
  1862. Method ToString$()
  1863. Return expr.ToString() + ".Size"
  1864. End Method
  1865. End Type
  1866. Type TIdentTypeExpr Extends TExpr
  1867. Field cdecl:TClassDecl
  1868. Method Create:TIdentTypeExpr( ty:TType )
  1869. Self.exprType=ty
  1870. Return Self
  1871. End Method
  1872. Method Copy:TExpr()
  1873. Return New TIdentTypeExpr.Create( exprType )
  1874. End Method
  1875. Method _Semant()
  1876. If cdecl Return
  1877. exprType=exprType.Semant()
  1878. If TArrayType(exprType) And TObjectType(TArrayType(exprType).elemType) Then
  1879. cdecl=TObjectType(TArrayType(exprType).elemType).classDecl
  1880. Else
  1881. cdecl=exprType.GetClass()
  1882. End If
  1883. If Not cdecl InternalErr
  1884. End Method
  1885. Method Semant:TExpr()
  1886. _Semant
  1887. Err "Expression can't be used in this way"
  1888. End Method
  1889. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  1890. _Semant
  1891. If args.Length=1 And args[0] Then
  1892. If TArrayType(exprType) Then
  1893. Return args[0].Cast( exprType,CAST_EXPLICIT )
  1894. Else
  1895. Return args[0].Cast( cdecl.objectType,CAST_EXPLICIT )
  1896. End If
  1897. End If
  1898. Err "Illegal number of arguments for type conversion"
  1899. End Method
  1900. Method SemantScope:TScopeDecl()
  1901. _Semant
  1902. Return cdecl
  1903. End Method
  1904. Method Trans$()
  1905. Return _trans.TransIdentTypeExpr( Self )
  1906. End Method
  1907. End Type
  1908. Type TIdentExpr Extends TExpr
  1909. Field ident$
  1910. Field expr:TExpr
  1911. Field scope:TScopeDecl
  1912. Field static:Int
  1913. Field isArg:Int
  1914. Field isRhs:Int
  1915. Field fixedScope:Int
  1916. Field _identLower:String
  1917. Method IdentLower:String()
  1918. If Not _identLower Then
  1919. _identLower = ident.ToLower()
  1920. End If
  1921. Return _identLower
  1922. End Method
  1923. Method Create:TIdentExpr( ident$,expr:TExpr=Null, _identLower:String = Null )
  1924. Self.ident=ident
  1925. Self.expr=expr
  1926. Self._identLower = _identLower
  1927. Return Self
  1928. End Method
  1929. Method Copy:TExpr()
  1930. Return New TIdentExpr.Create( ident,CopyExpr(expr), _identLower )
  1931. End Method
  1932. Method ToString$()
  1933. Local t$="TIdentExpr(~q"+ident+"~q"
  1934. If expr t:+","+expr.ToString()
  1935. Return t+")"
  1936. End Method
  1937. Method _Semant()
  1938. If scope Return
  1939. If expr Then
  1940. scope=expr.SemantScope()
  1941. If scope
  1942. static=True
  1943. Else
  1944. expr=expr.Semant()
  1945. scope=expr.exprType.GetClass()
  1946. If Not scope Then
  1947. Err "Expression has no scope"
  1948. End If
  1949. End If
  1950. fixedScope = True
  1951. Else
  1952. scope=_env
  1953. ' determines if access is via static (like Function, or via a Type)
  1954. ' However, for Field->Field access this is not strictly true.
  1955. If _env.FuncScope()=Null
  1956. static = TModuleDecl(_env) = Null
  1957. Else
  1958. static=_env.FuncScope().IsStatic()
  1959. End If
  1960. End If
  1961. End Method
  1962. Method IdentScope:TScopeDecl()
  1963. If Not expr Return _env
  1964. Local scope:TScopeDecl=expr.SemantScope()
  1965. If scope
  1966. expr=Null
  1967. Else
  1968. expr=expr.Semant()
  1969. scope=expr.exprType.GetClass()
  1970. If Not scope Err "Expression has no scope."
  1971. EndIf
  1972. Return scope
  1973. End Method
  1974. Method IdentErr( errorDetails:String = Null )
  1975. If errorDetails Then
  1976. Err errorDetails
  1977. Else
  1978. Err "Identifier '"+ident+"' not found."
  1979. End If
  1980. End Method
  1981. Method IdentNotFound()
  1982. End Method
  1983. Method IsVar()
  1984. InternalErr
  1985. End Method
  1986. Method Semant:TExpr()
  1987. Return SemantSet( "",Null )
  1988. End Method
  1989. Method SemantSet:TExpr( op$,rhs:TExpr )
  1990. _Semant
  1991. 'Local scope:TScopeDecl=IdentScope()
  1992. Local vdecl:TValDecl=scope.FindValDecl( IdentLower(), static )
  1993. If TLocalDecl( vdecl )
  1994. ' local variable should (at least) be in the same function scope.
  1995. If vdecl.FuncScope() <> scope.FuncScope() Then
  1996. ' or the local can be in localmain..
  1997. If TModuleDecl(scope) And vdecl.FuncScope() And vdecl.FuncScope().ident = "__LocalMain" Then
  1998. ' ok
  1999. Else
  2000. vdecl = Null
  2001. End If
  2002. End If
  2003. End If
  2004. If vdecl And fixedScope And static Then
  2005. If TClassDecl(vdecl.scope) And TClassDecl(scope) Then
  2006. If Not TClassDecl(scope).ExtendsClass(TClassDecl(vdecl.scope)) Then
  2007. vdecl = Null
  2008. End If
  2009. Else
  2010. If vdecl.scope <> scope Then
  2011. vdecl = Null
  2012. End If
  2013. End If
  2014. End If
  2015. If vdecl
  2016. If TConstDecl( vdecl )
  2017. ' If rhs Err "Constant '"+ident+"' cannot be modified."
  2018. ' Return New TConstExpr.Create( vdecl.ty,TConstDecl( vdecl ).value ).Semant()
  2019. If rhs Err "Constant '"+ident+"' cannot be modified."
  2020. Local cexpr:TConstExpr =New TConstExpr.Create( vdecl.ty,TConstDecl( vdecl ).value )
  2021. If Not static And (TInvokeExpr( expr ) Or TInvokeMemberExpr( expr )) Return New TStmtExpr.Create( New TExprStmt.Create( expr ),cexpr ).Semant()
  2022. Return cexpr.Semant()
  2023. Else If TFieldDecl( vdecl )
  2024. If static Err "Field '"+ident+"' cannot be accessed from here."
  2025. If expr Return New TMemberVarExpr.Create( expr,TVarDecl( vdecl ) ).Semant()
  2026. ' If expr Return New TMemberVarExpr.Create( expr,TVarDecl( vdecl ) ).Semant()
  2027. ' If scope<>_env Or Not _env.FuncScope() Or _env.FuncScope().IsStatic() Err "Field '"+ident+"' cannot be accessed from here."
  2028. EndIf
  2029. Return New TVarExpr.Create( TVarDecl( vdecl ) ).Semant()
  2030. EndIf
  2031. If op And op<>"="
  2032. Local fdecl:TFuncDecl=scope.FindFuncDecl( IdentLower(),,,,,,SCOPE_ALL )
  2033. If Not fdecl IdentErr
  2034. If _env.ModuleScope().IsStrict() And Not fdecl.IsProperty() Err "Identifier '"+ident+"' cannot be used in this way."
  2035. Local lhs:TExpr
  2036. If fdecl.IsStatic() Or (scope=_env And Not _env.FuncScope().IsStatic())
  2037. lhs=New TInvokeExpr.Create( fdecl )
  2038. Else If expr
  2039. Local tmp:TLocalDecl=New TLocalDecl.Create( "",Null,expr,, True )
  2040. lhs=New TInvokeMemberExpr.Create( New TVarExpr.Create( tmp ),fdecl )
  2041. lhs=New TStmtExpr.Create( New TDeclStmt.Create( tmp ),lhs )
  2042. Else
  2043. Return Null
  2044. EndIf
  2045. Local bop$=op[..1]
  2046. Select bop
  2047. Case "*","/","shl","shr","+","-","&","|","~~"
  2048. rhs=New TBinaryMathExpr.Create( bop,lhs,rhs )
  2049. Default
  2050. InternalErr
  2051. End Select
  2052. rhs=rhs.Semant()
  2053. EndIf
  2054. Local args:TExpr[]
  2055. If rhs args=[rhs]
  2056. Local fdecl:TFuncDecl
  2057. Try
  2058. fdecl=scope.FindFuncDecl( IdentLower(),args, , isArg, True,True,SCOPE_ALL )
  2059. Catch errorMessage:String
  2060. If errorMessage.StartsWith("Compile Error") Then
  2061. Throw errorMessage
  2062. End If
  2063. End Try
  2064. If fdecl
  2065. If _env.ModuleScope().IsStrict() And Not fdecl.IsProperty() And Not isArg And Not fdecl.maybeFunctionPtr Err "Identifier '"+ident+"' cannot be used in this way."
  2066. fdecl.maybeFunctionPtr = False
  2067. If Not fdecl.IsStatic()
  2068. If expr Return New TInvokeMemberExpr.Create( expr,fdecl,args, False ).Semant()
  2069. If scope<>_env Or Not _env.FuncScope() Or _env.FuncScope().IsStatic() Err "Method '"+ident+"' cannot be accessed from here."
  2070. EndIf
  2071. Return New TInvokeExpr.Create( fdecl,args, False, isArg, isRhs ).Semant()
  2072. End If
  2073. ' maybe it's a classdecl?
  2074. Local cdecl:TClassDecl = TClassDecl(scope.FindDecl(IdentLower()))
  2075. If cdecl Then
  2076. Local e:TIdentTypeExpr = New TIdentTypeExpr.Create(cdecl.objectType)
  2077. e.cdecl = cdecl
  2078. Return e
  2079. End If
  2080. Local loopLabel:String = "#" + IdentLower()
  2081. ' maybe it's a loop label?
  2082. Local stmt:TLoopStmt = TLoopStmt(scope.FindLoop(loopLabel))
  2083. If stmt Then
  2084. Return New TLoopLabelExpr.Create(stmt)
  2085. End If
  2086. ' maybe it's a data label?
  2087. Local ddecl:TDefDataDecl = TDefDataDecl(_appInstance.FindDataLabel(loopLabel))
  2088. If ddecl Then
  2089. Return New TDataLabelExpr.Create(ddecl)
  2090. End If
  2091. IdentErr
  2092. End Method
  2093. Method SemantFunc:TExpr( args:TExpr[], throwError:Int = True, funcCall:Int = False )
  2094. _Semant
  2095. Local errorDetails:String
  2096. Local nearestScopeError:String
  2097. 'Local scope:TScopeDecl=IdentScope()
  2098. Local initialScope:Int = SCOPE_ALL
  2099. If scope And TClassDecl(scope) Then
  2100. initialScope = SCOPE_CLASS_HEIRARCHY
  2101. End If
  2102. Local fdecl:TFuncDecl
  2103. Try
  2104. fdecl=scope.FindFuncDecl( IdentLower(),args,,,,True,initialScope )
  2105. ' Local decl:Object=scope.FindFuncDecl( IdentLower(),args,,,,True,SCOPE_ALL )
  2106. ' If decl Then
  2107. ' If TFuncDecl(decl) Then
  2108. ' fdecl = TFuncDecl(decl)
  2109. ' Else If TFuncDeclList(decl) Then
  2110. ' If Not TFuncDeclList(decl).IsEmpty() Then
  2111. ' fdecl = TFuncDecl(TFuncDeclList(decl).First())
  2112. ' End If
  2113. ' End If
  2114. ' End If
  2115. Catch errorMessage:String
  2116. If errorMessage.StartsWith("Compile Error") Then
  2117. Throw errorMessage
  2118. Else
  2119. ' couldn't find an exact match, look elsewhere
  2120. errorDetails = errorMessage
  2121. If errorMessage.StartsWith("Unable") Then
  2122. nearestScopeError = errorDetails
  2123. End If
  2124. End If
  2125. End Try
  2126. ' if our scope is static, but the scope of the found function/method is not
  2127. ' then we should ignore it and continue looking higher up the scope stack.
  2128. If static And fdecl And Not fdecl.IsStatic() Then
  2129. Local scope2:TScopeDecl = fdecl.scope
  2130. fdecl = Null
  2131. ' if fdecl was a method, this would be the Type's scope (ie. file/module)
  2132. If scope2.scope Then
  2133. fdecl = scope2.scope.FindFuncDecl( IdentLower(),args,,,,,SCOPE_CLASS_HEIRARCHY )
  2134. End If
  2135. Else If static And Not fdecl And Not fixedScope Then
  2136. If _env.classScope() Then
  2137. ' try searching from our class scope
  2138. 'fdecl = _env.classScope().FindFuncDecl( IdentLower(),args )
  2139. If Not fdecl Then
  2140. ' try searching from our class parent scope
  2141. Try
  2142. fdecl = _env.classScope().scope.FindFuncDecl( IdentLower(),args,,,,True,SCOPE_ALL )
  2143. Catch errorMessage:String
  2144. If errorMessage.StartsWith("Compile Error") Then
  2145. Throw errorMessage
  2146. Else
  2147. ' couldn't find an exact match, look elsewhere
  2148. errorDetails = errorMessage
  2149. If Not nearestScopeError And errorDetails.StartsWith("Unable") Then
  2150. nearestScopeError = errorDetails
  2151. End If
  2152. End If
  2153. End Try
  2154. End If
  2155. Else If _env.ModuleScope() Then ' bah
  2156. ' finally, try searching from our module scope
  2157. Try
  2158. fdecl = _env.ModuleScope().FindFuncDecl( IdentLower(),args,,,,True,SCOPE_ALL )
  2159. Catch errorMessage:String
  2160. If errorMessage.StartsWith("Compile Error") Then
  2161. Throw errorMessage
  2162. Else
  2163. ' couldn't find an exact match, look elsewhere
  2164. errorDetails = errorMessage
  2165. If Not nearestScopeError And errorDetails.StartsWith("Unable") Then
  2166. nearestScopeError = errorDetails
  2167. End If
  2168. End If
  2169. End Try
  2170. End If
  2171. End If
  2172. ' couldn't find it? try a global search
  2173. If Not fdecl And Not fixedScope Then
  2174. For Local mdecl:TModuleDecl = EachIn _appInstance.globalImports.Values()
  2175. Try
  2176. fdecl=mdecl.FindFuncDecl( IdentLower(), args,,,,True,SCOPE_ALL )
  2177. Catch errorMessage:String
  2178. If errorMessage.StartsWith("Compile Error") Then
  2179. Throw errorMessage
  2180. Else
  2181. ' couldn't find an exact match, look elsewhere
  2182. errorDetails = errorMessage
  2183. If Not nearestScopeError And errorDetails.StartsWith("Unable") Then
  2184. nearestScopeError = errorDetails
  2185. End If
  2186. End If
  2187. End Try
  2188. If fdecl Exit
  2189. Next
  2190. End If
  2191. If fdecl
  2192. If Not fdecl.IsStatic()
  2193. If static Err "Method '"+ident+"' cannot be accessed from here."
  2194. If expr Return New TInvokeMemberExpr.Create( expr,fdecl,args ).Semant()
  2195. 'If scope<>_env Or _env.FuncScope().IsStatic() Err "Method '"+ident+"' cannot be accessed from here."
  2196. EndIf
  2197. If expr And Not static Then
  2198. Return New TInvokeMemberExpr.Create( expr,fdecl,args ).Semant()
  2199. Else
  2200. Return New TInvokeExpr.Create( fdecl,args, funcCall ).Semant()
  2201. End If
  2202. EndIf
  2203. 'If args.Length=1 And args[0] And TObjectType( args[0].exprType )
  2204. ' Local cdecl:TClassDecl=TClassDecl( scope.FindScopeDecl( ident ) )
  2205. ' If cdecl Return args[0].Cast( New TObjectType.Create(cdecl),CAST_EXPLICIT )
  2206. 'EndIf
  2207. Local ty:TType=scope.FindType( IdentLower(),Null )
  2208. If ty Then
  2209. If args.Length=1 And args[0] Return args[0].Cast( ty,CAST_EXPLICIT )
  2210. Err "Illegal number of arguments for type conversion"
  2211. End If
  2212. If throwError Then
  2213. If nearestScopeError Then
  2214. IdentErr(nearestScopeError)
  2215. Else
  2216. IdentErr(errorDetails)
  2217. End If
  2218. End If
  2219. End Method
  2220. Method SemantScope:TScopeDecl()
  2221. If Not expr Return _env.FindScopeDecl( IdentLower() )
  2222. Local scope:TScopeDecl=expr.SemantScope()
  2223. ' If scope is a namespace, then we are a module. Look up the module id and return it as the real scope.
  2224. If TNamespaceDecl(scope) Then
  2225. Local mdecl:TModuleDecl=TModuleDecl(scope.FindDecl(scope.IdentLower() + "." + IdentLower()))
  2226. If mdecl Then
  2227. Return mdecl
  2228. End If
  2229. End If
  2230. If scope Return scope.FindScopeDecl( IdentLower() )
  2231. End Method
  2232. ' Method Trans$()
  2233. ' Return _trans.TransIdentExpr( Self )
  2234. ' End Method
  2235. End Type
  2236. Type TBuiltinExpr Extends TExpr
  2237. Field id:String
  2238. Field expr:TExpr
  2239. Method Semant:TExpr()
  2240. If exprType Return Self
  2241. expr=expr.Semant()
  2242. exprType=expr.exprType
  2243. Return Self
  2244. End Method
  2245. Method Trans$()
  2246. Return _trans.TransBuiltinExpr( Self )
  2247. End Method
  2248. End Type
  2249. Type TLenExpr Extends TBuiltinExpr
  2250. Method Create:TLenExpr( expr:TExpr )
  2251. Self.id="len"
  2252. Self.expr=expr
  2253. Return Self
  2254. End Method
  2255. Method Semant:TExpr()
  2256. If exprType Return Self
  2257. expr=expr.Semant()
  2258. ' anything other than a string or array will become "1", and
  2259. ' return a length of 1 accordingly.
  2260. If Not TStringType(expr.exprType) And Not TArrayType(expr.exprType) Then
  2261. expr = New TConstExpr.Create( New TIntType, 1 ).Semant()
  2262. 'this is not useful for numerics
  2263. 'expr = New TConstExpr.Create( TType.stringType, "1" ).Semant()
  2264. _appInstance.mapStringConsts(TConstExpr(expr).value)
  2265. End If
  2266. exprType=New TIntType
  2267. Return Self
  2268. End Method
  2269. Method Copy:TExpr()
  2270. Return New TLenExpr.Create( CopyExpr(expr) )
  2271. End Method
  2272. Method ToString$()
  2273. Return "TLenExpr("+expr.ToString()+")"
  2274. End Method
  2275. End Type
  2276. Type TAbsExpr Extends TBuiltinExpr
  2277. Method Create:TAbsExpr( expr:TExpr )
  2278. Self.id="abs"
  2279. Self.expr=expr
  2280. Return Self
  2281. End Method
  2282. Method Semant:TExpr()
  2283. If exprType Return Self
  2284. expr=expr.Semant()
  2285. If TNumericType(expr.exprType) Or TBoolType(expr.exprType) Then
  2286. If TInt128Type(expr.exprType) Err "'Abs' does not support Int128 type. Use specific intrinsic function instead."
  2287. If TFloat64Type(expr.exprType) Err "'Abs' does not support Float64 type. Use specific intrinsic function instead."
  2288. If TFloat128Type(expr.exprType) Err "'Abs' does not support Float128 type. Use specific intrinsic function instead."
  2289. If TDouble128Type(expr.exprType) Err "'Abs' does not support Double128 type. Use specific intrinsic function instead."
  2290. If TIntType(expr.exprType) Or TByteType(expr.exprType) Or TShortType(expr.exprType) Then
  2291. exprType=New TIntType
  2292. Else
  2293. exprType=expr.exprType
  2294. End If
  2295. Else
  2296. Err "Subexpression for 'Abs' must be of numeric type"
  2297. End If
  2298. Return Self
  2299. End Method
  2300. Method Copy:TExpr()
  2301. Return New TAbsExpr.Create( CopyExpr(expr) )
  2302. End Method
  2303. Method ToString$()
  2304. Return "TAbsExpr("+expr.ToString()+")"
  2305. End Method
  2306. End Type
  2307. Type TAscExpr Extends TBuiltinExpr
  2308. Method Create:TAscExpr( expr:TExpr )
  2309. Self.id="asc"
  2310. Self.expr=expr
  2311. Return Self
  2312. End Method
  2313. Method Semant:TExpr()
  2314. If exprType Return Self
  2315. If TConstExpr(expr) Then
  2316. Local cexpr:TExpr = New TConstExpr.Create(New TIntType, Asc(TConstExpr(expr).value))
  2317. _appInstance.removeStringConst(TConstExpr(expr).value)
  2318. cexpr.Semant()
  2319. Return cexpr
  2320. End If
  2321. expr = expr.SemantAndCast( New TStringType )
  2322. exprType = New TIntType
  2323. Return Self
  2324. End Method
  2325. Method Copy:TExpr()
  2326. Return New TAscExpr.Create( CopyExpr(expr) )
  2327. End Method
  2328. Method ToString$()
  2329. Return "TAscExpr("+expr.ToString()+")"
  2330. End Method
  2331. End Type
  2332. Type TSgnExpr Extends TBuiltinExpr
  2333. Method Create:TSgnExpr( expr:TExpr )
  2334. Self.id="sgn"
  2335. Self.expr=expr
  2336. Return Self
  2337. End Method
  2338. Method Semant:TExpr()
  2339. If exprType Return Self
  2340. If TConstExpr(expr) Then
  2341. 'use different calls to only return a "float sgn"
  2342. 'when param is a float
  2343. Local val:String = TConstExpr(expr).value
  2344. Local cexpr:TExpr
  2345. If String(Int(val)) = val
  2346. cexpr = New TConstExpr.Create(New TIntType, Sgn(Int(TConstExpr(expr).value)))
  2347. Else
  2348. cexpr = New TConstExpr.Create(New TFloatType, Sgn(Float(TConstExpr(expr).value)))
  2349. End If
  2350. _appInstance.removeStringConst(TConstExpr(expr).value)
  2351. cexpr.Semant()
  2352. Return cexpr
  2353. End If
  2354. expr = expr.Semant()
  2355. If Not TNumericType(expr.exprType) Then
  2356. Err "Subexpression for 'Sgn' must be of numeric type"
  2357. End If
  2358. If TInt128Type(expr.exprType) Err "'Sgn' does not support Int128 type. Use specific intrinsic function instead."
  2359. If TFloat64Type(expr.exprType) Err "'Sgn' does not support Float64 type. Use specific intrinsic function instead."
  2360. If TFloat128Type(expr.exprType) Err "'Sgn' does not support Float128 type. Use specific intrinsic function instead."
  2361. If TDouble128Type(expr.exprType) Err "'Sgn' does not support Double128 type. Use specific intrinsic function instead."
  2362. exprType=expr.exprType
  2363. Return Self
  2364. End Method
  2365. Method Copy:TExpr()
  2366. Return New TSgnExpr.Create( CopyExpr(expr) )
  2367. End Method
  2368. Method ToString$()
  2369. Return "TSgnExpr("+expr.ToString()+")"
  2370. End Method
  2371. End Type
  2372. Type TMinExpr Extends TBuiltinExpr
  2373. Field expr2:TExpr
  2374. Method Create:TMinExpr( lhs:TExpr, rhs:TExpr )
  2375. Self.id="min"
  2376. Self.expr=lhs
  2377. Self.expr2=rhs
  2378. Return Self
  2379. End Method
  2380. Method Semant:TExpr()
  2381. If exprType Return Self
  2382. expr=expr.Semant()
  2383. expr2=expr2.Semant()
  2384. If TInt128Type(expr.exprType) Or TInt128Type(expr2.exprType) Err "'Min' does not support Int128 type. Use specific intrinsic function instead."
  2385. If TFloat64Type(expr.exprType) Or TFloat64Type(expr2.exprType) Err "'Min' does not support Float64 type. Use specific intrinsic function instead."
  2386. If TFloat128Type(expr.exprType) Or TFloat128Type(expr2.exprType) Err "'Min' does not support Float128 type. Use specific intrinsic function instead."
  2387. If TDouble128Type(expr.exprType) Or TDouble128Type(expr2.exprType) Err "'Min' does not support Double128 type. Use specific intrinsic function instead."
  2388. exprType=BalanceTypes(expr.exprType, expr2.exprType)
  2389. Return Self
  2390. End Method
  2391. Method Copy:TExpr()
  2392. Return New TMinExpr.Create( CopyExpr(expr), CopyExpr(expr2) )
  2393. End Method
  2394. Method ToString$()
  2395. Return "TMinExpr("+expr.ToString()+"," + expr2.ToString() + ")"
  2396. End Method
  2397. End Type
  2398. Type TMaxExpr Extends TBuiltinExpr
  2399. Field expr2:TExpr
  2400. Method Create:TMaxExpr( lhs:TExpr, rhs:TExpr )
  2401. Self.id="max"
  2402. Self.expr=lhs
  2403. Self.expr2=rhs
  2404. Return Self
  2405. End Method
  2406. Method Semant:TExpr()
  2407. If exprType Return Self
  2408. expr=expr.Semant()
  2409. expr2=expr2.Semant()
  2410. If TInt128Type(expr.exprType) Or TInt128Type(expr2.exprType) Err "'Max' does not support Int128 type. Use specific intrinsic function instead."
  2411. If TFloat64Type(expr.exprType) Or TFloat64Type(expr2.exprType) Err "'Max' does not support Float64 type. Use specific intrinsic function instead."
  2412. If TFloat128Type(expr.exprType) Or TFloat128Type(expr2.exprType) Err "'Max' does not support Float128 type. Use specific intrinsic function instead."
  2413. If TDouble128Type(expr.exprType) Or TDouble128Type(expr2.exprType) Err "'Max' does not support Double128 type. Use specific intrinsic function instead."
  2414. exprType=BalanceTypes(expr.exprType, expr2.exprType)
  2415. Return Self
  2416. End Method
  2417. Method Copy:TExpr()
  2418. Return New TMaxExpr.Create( CopyExpr(expr), CopyExpr(expr2) )
  2419. End Method
  2420. Method ToString$()
  2421. Return "TMaxExpr("+expr.ToString()+"," + expr2.ToString() + ")"
  2422. End Method
  2423. End Type
  2424. Type TSizeOfExpr Extends TBuiltinExpr
  2425. Method Create:TSizeOfExpr( expr:TExpr )
  2426. Self.id="sizeof"
  2427. Self.expr=expr
  2428. Return Self
  2429. End Method
  2430. Method Semant:TExpr()
  2431. If exprType Return Self
  2432. expr=expr.Semant()
  2433. exprType=New TIntType
  2434. Return Self
  2435. End Method
  2436. Method Copy:TExpr()
  2437. Return New TSizeOfExpr.Create( CopyExpr(expr) )
  2438. End Method
  2439. Method ToString$()
  2440. Return "TSizeOfExpr("+expr.ToString()+")"
  2441. End Method
  2442. End Type
  2443. Type TChrExpr Extends TBuiltinExpr
  2444. Method Create:TChrExpr( expr:TExpr )
  2445. Self.id="chr"
  2446. Self.expr=expr
  2447. Return Self
  2448. End Method
  2449. Method Semant:TExpr()
  2450. If exprType Return Self
  2451. If TConstExpr(expr) Then
  2452. Local cexpr:TConstExpr = New TConstExpr.Create(New TStringType, Chr(Int(TConstExpr(expr).value)))
  2453. cexpr.Semant()
  2454. _appInstance.mapStringConsts(cexpr.value)
  2455. Return cexpr
  2456. End If
  2457. expr = expr.SemantAndCast( New TIntType )
  2458. exprType = New TStringType
  2459. Return Self
  2460. End Method
  2461. Method Copy:TExpr()
  2462. Return New TChrExpr.Create( CopyExpr(expr) )
  2463. End Method
  2464. Method ToString$()
  2465. Return "TChrExpr("+expr.ToString()+")"
  2466. End Method
  2467. End Type
  2468. Type TFuncCallExpr Extends TExpr
  2469. Field expr:TExpr
  2470. Field args:TExpr[]
  2471. Method Create:TFuncCallExpr( expr:TExpr,args:TExpr[]=Null )
  2472. Self.expr=expr
  2473. If args Then
  2474. Self.args=args
  2475. Else
  2476. Self.args = New TExpr[0]
  2477. End If
  2478. Return Self
  2479. End Method
  2480. Method Copy:TExpr()
  2481. Return New TFuncCallExpr.Create( CopyExpr(expr),CopyArgs(args) )
  2482. End Method
  2483. Method ToString$()
  2484. Local t$="TFuncCallExpr("+expr.ToString()
  2485. For Local arg:TExpr=EachIn args
  2486. t:+","+arg.ToString()
  2487. Next
  2488. Return t+")"
  2489. End Method
  2490. Method Semant:TExpr()
  2491. args=SemantArgs( args )
  2492. If TIndexExpr(expr) Then
  2493. expr = expr.SemantFunc( args, True, True )
  2494. exprType = expr.exprType
  2495. Return Self
  2496. Else
  2497. Return expr.SemantFunc( args, True, True )
  2498. End If
  2499. End Method
  2500. Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
  2501. ' we are only likely to be called if a function returns and invokes a function pointer.
  2502. Local ex:TExpr = Semant()
  2503. If TFunctionPtrType(ex.exprType) Then
  2504. exprType = TFunctionPtrType(ex.exprType).func.retType
  2505. End If
  2506. Self.args = SemantArgs(args)
  2507. expr = ex
  2508. Return Self
  2509. End Method
  2510. Method Trans$()
  2511. Return _trans.TransFuncCallExpr( Self )
  2512. End Method
  2513. End Type
  2514. Type TScopeExpr Extends TExpr
  2515. Field scope:TScopeDecl
  2516. Method Create:TScopeExpr( scope:TScopeDecl )
  2517. Self.scope=scope
  2518. Return Self
  2519. End Method
  2520. Method Copy:TExpr()
  2521. Return Self
  2522. End Method
  2523. Method ToString$()
  2524. Return "TScopeExpr("+scope.ToString()+")"
  2525. End Method
  2526. Method Semant:TExpr()
  2527. Err "Syntax error."
  2528. End Method
  2529. Method SemantScope:TScopeDecl()
  2530. Return scope
  2531. End Method
  2532. End Type
  2533. Type TNewExpr Extends TExpr
  2534. Field isSuper:Int
  2535. Field args:TExpr[]
  2536. Field ctor:TFuncDecl
  2537. Method Create:TNewExpr( args:TExpr[]=Null, isSuper:Int = False )
  2538. If args Then
  2539. Self.args=args
  2540. Else
  2541. Self.args = New TExpr[0]
  2542. End If
  2543. Self.isSuper = isSuper
  2544. Return Self
  2545. End Method
  2546. Method Semant:TExpr()
  2547. Local fdecl:TFuncDecl = _env.FuncScope()
  2548. If Not fdecl Or TNewDecl(fdecl) = Null Or Not _env.ClassScope() Then
  2549. Err "Call to constructor not valid in this context."
  2550. End If
  2551. ' must be first statement of New() method
  2552. Local stmt:TStmt = TStmt(fdecl.stmts.First())
  2553. If TExprStmt(stmt) = Null Or TExprStmt(stmt).expr <> Self Then
  2554. Err "Call to constructor must be first statement in New()."
  2555. End If
  2556. args=SemantArgs( args )
  2557. ' validate called constructor
  2558. Try
  2559. Local cDecl:TClassDecl = _env.ClassScope()
  2560. If isSuper Then
  2561. cDecl = cDecl.superClass
  2562. End If
  2563. ctor = cDecl.FindFuncDecl("new",args,,,,True,SCOPE_CLASS_HEIRARCHY )
  2564. Catch errorMessage:String
  2565. If errorMessage.StartsWith("Compile Error") Then
  2566. Throw errorMessage
  2567. Else
  2568. Err errorMessage
  2569. End If
  2570. End Try
  2571. ' TODO : expand to full recursive test
  2572. If ctor = fdecl Then
  2573. Err "Recursive constructor invocation."
  2574. End If
  2575. ctor.Semant
  2576. ' attach to ctor
  2577. TNewDecl(fdecl).chainedCtor = Self
  2578. Return Self
  2579. End Method
  2580. Method Trans$()
  2581. 'Return _trans.TransFuncCallExpr( Self )
  2582. End Method
  2583. End Type
  2584. Type TNullExpr Extends TExpr
  2585. Method Create:TNullExpr(ty:TType)
  2586. exprType = ty
  2587. Return Self
  2588. End Method
  2589. Method Copy:TExpr()
  2590. Return New TNullExpr.Create(exprType)
  2591. End Method
  2592. Method Semant:TExpr()
  2593. Return Self
  2594. End Method
  2595. Method Trans$()
  2596. Return "NULL"
  2597. End Method
  2598. Method Eval$()
  2599. Return ""
  2600. End Method
  2601. End Type
  2602. Type TLoopLabelExpr Extends TExpr
  2603. Field loop:TLoopStmt
  2604. Method Create:TLoopLabelExpr(loop:TLoopStmt)
  2605. Self.loop = loop
  2606. Return Self
  2607. End Method
  2608. Method Copy:TExpr()
  2609. Return New TLoopLabelExpr.Create(loop)
  2610. End Method
  2611. Method Semant:TExpr()
  2612. Return Self
  2613. End Method
  2614. Method Trans$()
  2615. DebugStop
  2616. End Method
  2617. Method Eval$()
  2618. Return ""
  2619. End Method
  2620. End Type
  2621. Type TDataLabelExpr Extends TExpr
  2622. Field dataDef:TDefDataDecl
  2623. Method Create:TDataLabelExpr(dataDef:TDefDataDecl)
  2624. Self.dataDef = dataDef
  2625. Return Self
  2626. End Method
  2627. Method Copy:TExpr()
  2628. Return New TDataLabelExpr.Create(dataDef)
  2629. End Method
  2630. Method Semant:TExpr()
  2631. Return Self
  2632. End Method
  2633. Method Trans$()
  2634. DebugStop
  2635. End Method
  2636. Method Eval$()
  2637. Return ""
  2638. End Method
  2639. End Type