ncnv.pas 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totypedef : tdef;
  29. totypedefderef : tderef;
  30. convtype : tconverttype;
  31. warn_pointer_to_signed: boolean;
  32. constructor create(node : tnode;def:tdef);virtual;
  33. constructor create_explicit(node : tnode;def:tdef);
  34. constructor create_internal(node : tnode;def:tdef);
  35. constructor create_proc_to_procvar(node : tnode);
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. function dogetcopy : tnode;override;
  41. procedure printnodeinfo(var t : text);override;
  42. function pass_1 : tnode;override;
  43. function pass_typecheck:tnode;override;
  44. function simplify:tnode; override;
  45. procedure mark_write;override;
  46. function docompare(p: tnode) : boolean; override;
  47. function assign_allowed:boolean;
  48. procedure second_call_helper(c : tconverttype);
  49. private
  50. function typecheck_int_to_int : tnode;
  51. function typecheck_cord_to_pointer : tnode;
  52. function typecheck_chararray_to_string : tnode;
  53. function typecheck_string_to_chararray : tnode;
  54. function typecheck_string_to_string : tnode;
  55. function typecheck_char_to_string : tnode;
  56. function typecheck_char_to_chararray : tnode;
  57. function typecheck_int_to_real : tnode;
  58. function typecheck_real_to_real : tnode;
  59. function typecheck_real_to_currency : tnode;
  60. function typecheck_cchar_to_pchar : tnode;
  61. function typecheck_cstring_to_pchar : tnode;
  62. function typecheck_cstring_to_int : tnode;
  63. function typecheck_char_to_char : tnode;
  64. function typecheck_arrayconstructor_to_set : tnode;
  65. function typecheck_set_to_set : tnode;
  66. function typecheck_pchar_to_string : tnode;
  67. function typecheck_interface_to_guid : tnode;
  68. function typecheck_dynarray_to_openarray : tnode;
  69. function typecheck_pwchar_to_string : tnode;
  70. function typecheck_variant_to_dynarray : tnode;
  71. function typecheck_dynarray_to_variant : tnode;
  72. function typecheck_call_helper(c : tconverttype) : tnode;
  73. function typecheck_variant_to_enum : tnode;
  74. function typecheck_enum_to_variant : tnode;
  75. function typecheck_proc_to_procvar : tnode;
  76. function typecheck_variant_to_interface : tnode;
  77. function typecheck_interface_to_variant : tnode;
  78. function typecheck_array_2_dynarray : tnode;
  79. protected
  80. function first_int_to_int : tnode;virtual;
  81. function first_cstring_to_pchar : tnode;virtual;
  82. function first_cstring_to_int : tnode;virtual;
  83. function first_string_to_chararray : tnode;virtual;
  84. function first_char_to_string : tnode;virtual;
  85. function first_nothing : tnode;virtual;
  86. function first_array_to_pointer : tnode;virtual;
  87. function first_int_to_real : tnode;virtual;
  88. function first_real_to_real : tnode;virtual;
  89. function first_pointer_to_array : tnode;virtual;
  90. function first_cchar_to_pchar : tnode;virtual;
  91. function first_bool_to_int : tnode;virtual;
  92. function first_int_to_bool : tnode;virtual;
  93. function first_bool_to_bool : tnode;virtual;
  94. function first_proc_to_procvar : tnode;virtual;
  95. function first_nil_to_methodprocvar : tnode;virtual;
  96. function first_set_to_set : tnode;virtual;
  97. function first_cord_to_pointer : tnode;virtual;
  98. function first_ansistring_to_pchar : tnode;virtual;
  99. function first_arrayconstructor_to_set : tnode;virtual;
  100. function first_class_to_intf : tnode;virtual;
  101. function first_char_to_char : tnode;virtual;
  102. function first_call_helper(c : tconverttype) : tnode;
  103. { these wrapper are necessary, because the first_* stuff is called }
  104. { through a table. Without the wrappers override wouldn't have }
  105. { any effect }
  106. function _first_int_to_int : tnode;
  107. function _first_cstring_to_pchar : tnode;
  108. function _first_cstring_to_int : tnode;
  109. function _first_string_to_chararray : tnode;
  110. function _first_char_to_string : tnode;
  111. function _first_nothing : tnode;
  112. function _first_array_to_pointer : tnode;
  113. function _first_int_to_real : tnode;
  114. function _first_real_to_real: tnode;
  115. function _first_pointer_to_array : tnode;
  116. function _first_cchar_to_pchar : tnode;
  117. function _first_bool_to_int : tnode;
  118. function _first_int_to_bool : tnode;
  119. function _first_bool_to_bool : tnode;
  120. function _first_proc_to_procvar : tnode;
  121. function _first_nil_to_methodprocvar : tnode;
  122. function _first_cord_to_pointer : tnode;
  123. function _first_ansistring_to_pchar : tnode;
  124. function _first_arrayconstructor_to_set : tnode;
  125. function _first_class_to_intf : tnode;
  126. function _first_char_to_char : tnode;
  127. function _first_set_to_set : tnode;
  128. procedure _second_int_to_int;virtual;
  129. procedure _second_string_to_string;virtual;
  130. procedure _second_cstring_to_pchar;virtual;
  131. procedure _second_cstring_to_int;virtual;
  132. procedure _second_string_to_chararray;virtual;
  133. procedure _second_array_to_pointer;virtual;
  134. procedure _second_pointer_to_array;virtual;
  135. procedure _second_chararray_to_string;virtual;
  136. procedure _second_char_to_string;virtual;
  137. procedure _second_int_to_real;virtual;
  138. procedure _second_real_to_real;virtual;
  139. procedure _second_cord_to_pointer;virtual;
  140. procedure _second_proc_to_procvar;virtual;
  141. procedure _second_nil_to_methodprocvar;virtual;
  142. procedure _second_bool_to_int;virtual;
  143. procedure _second_int_to_bool;virtual;
  144. procedure _second_bool_to_bool;virtual;
  145. procedure _second_set_to_set;virtual;
  146. procedure _second_ansistring_to_pchar;virtual;
  147. procedure _second_class_to_intf;virtual;
  148. procedure _second_char_to_char;virtual;
  149. procedure _second_nothing; virtual;
  150. procedure second_int_to_int;virtual;abstract;
  151. procedure second_string_to_string;virtual;abstract;
  152. procedure second_cstring_to_pchar;virtual;abstract;
  153. procedure second_cstring_to_int;virtual;abstract;
  154. procedure second_string_to_chararray;virtual;abstract;
  155. procedure second_array_to_pointer;virtual;abstract;
  156. procedure second_pointer_to_array;virtual;abstract;
  157. procedure second_chararray_to_string;virtual;abstract;
  158. procedure second_char_to_string;virtual;abstract;
  159. procedure second_int_to_real;virtual;abstract;
  160. procedure second_real_to_real;virtual;abstract;
  161. procedure second_cord_to_pointer;virtual;abstract;
  162. procedure second_proc_to_procvar;virtual;abstract;
  163. procedure second_nil_to_methodprocvar;virtual;abstract;
  164. procedure second_bool_to_int;virtual;abstract;
  165. procedure second_int_to_bool;virtual;abstract;
  166. procedure second_bool_to_bool;virtual;abstract;
  167. procedure second_set_to_set;virtual;abstract;
  168. procedure second_ansistring_to_pchar;virtual;abstract;
  169. procedure second_class_to_intf;virtual;abstract;
  170. procedure second_char_to_char;virtual;abstract;
  171. procedure second_nothing; virtual;abstract;
  172. end;
  173. ttypeconvnodeclass = class of ttypeconvnode;
  174. tasnode = class(tbinarynode)
  175. constructor create(l,r : tnode);virtual;
  176. function pass_1 : tnode;override;
  177. function pass_typecheck:tnode;override;
  178. function dogetcopy: tnode;override;
  179. destructor destroy; override;
  180. call: tnode;
  181. end;
  182. tasnodeclass = class of tasnode;
  183. tisnode = class(tbinarynode)
  184. constructor create(l,r : tnode);virtual;
  185. function pass_1 : tnode;override;
  186. function pass_typecheck:tnode;override;
  187. procedure pass_generate_code;override;
  188. end;
  189. tisnodeclass = class of tisnode;
  190. var
  191. ctypeconvnode : ttypeconvnodeclass;
  192. casnode : tasnodeclass;
  193. cisnode : tisnodeclass;
  194. procedure inserttypeconv(var p:tnode;def:tdef);
  195. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  196. procedure arrayconstructor_to_set(var p : tnode);
  197. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  198. implementation
  199. uses
  200. globtype,systems,
  201. cutils,verbose,globals,widestr,
  202. symconst,symdef,symsym,symbase,symtable,
  203. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  204. cgbase,procinfo,
  205. htypechk,pass_1,cpuinfo;
  206. {*****************************************************************************
  207. Helpers
  208. *****************************************************************************}
  209. procedure inserttypeconv(var p:tnode;def:tdef);
  210. begin
  211. if not assigned(p.resultdef) then
  212. begin
  213. typecheckpass(p);
  214. if codegenerror then
  215. exit;
  216. end;
  217. { don't insert obsolete type conversions }
  218. if equal_defs(p.resultdef,def) and
  219. not ((p.resultdef.typ=setdef) and
  220. (tsetdef(p.resultdef).settype <>
  221. tsetdef(def).settype)) then
  222. begin
  223. p.resultdef:=def;
  224. end
  225. else
  226. begin
  227. p:=ctypeconvnode.create(p,def);
  228. typecheckpass(p);
  229. end;
  230. end;
  231. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  232. begin
  233. if not assigned(p.resultdef) then
  234. begin
  235. typecheckpass(p);
  236. if codegenerror then
  237. exit;
  238. end;
  239. { don't insert obsolete type conversions }
  240. if equal_defs(p.resultdef,def) and
  241. not ((p.resultdef.typ=setdef) and
  242. (tsetdef(p.resultdef).settype <>
  243. tsetdef(def).settype)) then
  244. begin
  245. p.resultdef:=def;
  246. end
  247. else
  248. begin
  249. p:=ctypeconvnode.create_internal(p,def);
  250. typecheckpass(p);
  251. end;
  252. end;
  253. {*****************************************************************************
  254. Array constructor to Set Conversion
  255. *****************************************************************************}
  256. procedure arrayconstructor_to_set(var p : tnode);
  257. var
  258. constp : tsetconstnode;
  259. buildp,
  260. p2,p3,p4 : tnode;
  261. hdef : tdef;
  262. constset : Pconstset;
  263. constsetlo,
  264. constsethi : TConstExprInt;
  265. procedure update_constsethi(def:tdef);
  266. begin
  267. if (def.typ=orddef) and
  268. ((torddef(def).high>=constsethi) or
  269. (torddef(def).low <=constsetlo)) then
  270. begin
  271. if torddef(def).ordtype=uwidechar then
  272. begin
  273. constsethi:=255;
  274. constsetlo:=0;
  275. if hdef=nil then
  276. hdef:=def;
  277. end
  278. else
  279. begin
  280. if (torddef(def).high>=constsethi) then
  281. constsethi:=torddef(def).high;
  282. if (torddef(def).low<=constsetlo) then
  283. constsetlo:=torddef(def).low;
  284. if hdef=nil then
  285. begin
  286. if (constsethi>255) or
  287. (torddef(def).low<0) then
  288. hdef:=u8inttype
  289. else
  290. hdef:=def;
  291. end;
  292. if constsethi>255 then
  293. constsethi:=255;
  294. if constsetlo<0 then
  295. constsetlo:=0;
  296. end;
  297. end
  298. else if (def.typ=enumdef) and
  299. ((tenumdef(def).max>=constsethi) or
  300. (tenumdef(def).min<=constsetlo)) then
  301. begin
  302. if hdef=nil then
  303. hdef:=def;
  304. if (tenumdef(def).max>=constsethi) then
  305. constsethi:=tenumdef(def).max;
  306. if (tenumdef(def).min<=constsetlo) then
  307. constsetlo:=tenumdef(def).min;
  308. end;
  309. end;
  310. procedure do_set(pos : longint);
  311. begin
  312. if (pos and not $ff)<>0 then
  313. Message(parser_e_illegal_set_expr);
  314. if pos>constsethi then
  315. constsethi:=pos;
  316. if pos<constsetlo then
  317. constsetlo:=pos;
  318. if pos in constset^ then
  319. Message(parser_e_illegal_set_expr);
  320. include(constset^,pos);
  321. end;
  322. var
  323. l : Longint;
  324. lr,hr : TConstExprInt;
  325. hp : tarrayconstructornode;
  326. begin
  327. if p.nodetype<>arrayconstructorn then
  328. internalerror(200205105);
  329. new(constset);
  330. constset^:=[];
  331. hdef:=nil;
  332. { make sure to set constsetlo correctly for empty sets }
  333. if assigned(tarrayconstructornode(p).left) then
  334. constsetlo:=high(aint)
  335. else
  336. constsetlo:=0;
  337. constsethi:=0;
  338. constp:=csetconstnode.create(nil,hdef);
  339. constp.value_set:=constset;
  340. buildp:=constp;
  341. hp:=tarrayconstructornode(p);
  342. if assigned(hp.left) then
  343. begin
  344. while assigned(hp) do
  345. begin
  346. p4:=nil; { will contain the tree to create the set }
  347. {split a range into p2 and p3 }
  348. if hp.left.nodetype=arrayconstructorrangen then
  349. begin
  350. p2:=tarrayconstructorrangenode(hp.left).left;
  351. p3:=tarrayconstructorrangenode(hp.left).right;
  352. tarrayconstructorrangenode(hp.left).left:=nil;
  353. tarrayconstructorrangenode(hp.left).right:=nil;
  354. end
  355. else
  356. begin
  357. p2:=hp.left;
  358. hp.left:=nil;
  359. p3:=nil;
  360. end;
  361. typecheckpass(p2);
  362. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  363. if assigned(p3) then
  364. begin
  365. typecheckpass(p3);
  366. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  367. end;
  368. if codegenerror then
  369. break;
  370. current_filepos:=p2.fileinfo;
  371. case p2.resultdef.typ of
  372. enumdef,
  373. orddef:
  374. begin
  375. { widechars are not yet supported }
  376. if is_widechar(p2.resultdef) then
  377. begin
  378. inserttypeconv(p2,cchartype);
  379. if (p2.nodetype<>ordconstn) then
  380. incompatibletypes(cwidechartype,cchartype);
  381. end;
  382. getrange(p2.resultdef,lr,hr);
  383. if assigned(p3) then
  384. begin
  385. if is_widechar(p3.resultdef) then
  386. begin
  387. inserttypeconv(p3,cchartype);
  388. if (p3.nodetype<>ordconstn) then
  389. begin
  390. current_filepos:=p3.fileinfo;
  391. incompatibletypes(cwidechartype,cchartype);
  392. end;
  393. end;
  394. { this isn't good, you'll get problems with
  395. type t010 = 0..10;
  396. ts = set of t010;
  397. var s : ts;b : t010
  398. begin s:=[1,2,b]; end.
  399. if is_integer(p3^.resultdef) then
  400. begin
  401. inserttypeconv(p3,u8bitdef);
  402. end;
  403. }
  404. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  405. begin
  406. CGMessagePos(p3.fileinfo,type_e_typeconflict_in_set);
  407. end
  408. else
  409. begin
  410. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  411. begin
  412. if not(is_integer(p3.resultdef)) then
  413. hdef:=p3.resultdef
  414. else
  415. begin
  416. inserttypeconv(p3,u8inttype);
  417. inserttypeconv(p2,u8inttype);
  418. end;
  419. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  420. do_set(l);
  421. p2.free;
  422. p3.free;
  423. end
  424. else
  425. begin
  426. update_constsethi(p2.resultdef);
  427. inserttypeconv(p2,hdef);
  428. update_constsethi(p3.resultdef);
  429. inserttypeconv(p3,hdef);
  430. if assigned(hdef) then
  431. inserttypeconv(p3,hdef)
  432. else
  433. inserttypeconv(p3,u8inttype);
  434. p4:=csetelementnode.create(p2,p3);
  435. end;
  436. end;
  437. end
  438. else
  439. begin
  440. { Single value }
  441. if p2.nodetype=ordconstn then
  442. begin
  443. if not(is_integer(p2.resultdef)) then
  444. begin
  445. { for constant set elements, delphi allows the usage of elements of enumerations which
  446. have value>255 if there is no element with a value > 255 used }
  447. if (m_delphi in current_settings.modeswitches) and (p2.resultdef.typ=enumdef) then
  448. begin
  449. if tordconstnode(p2).value>constsethi then
  450. constsethi:=tordconstnode(p2).value;
  451. if hdef=nil then
  452. hdef:=p2.resultdef;
  453. end
  454. else
  455. update_constsethi(p2.resultdef);
  456. end;
  457. if assigned(hdef) then
  458. inserttypeconv(p2,hdef)
  459. else
  460. inserttypeconv(p2,u8inttype);
  461. do_set(tordconstnode(p2).value);
  462. p2.free;
  463. end
  464. else
  465. begin
  466. update_constsethi(p2.resultdef);
  467. if assigned(hdef) then
  468. inserttypeconv(p2,hdef)
  469. else
  470. inserttypeconv(p2,u8inttype);
  471. p4:=csetelementnode.create(p2,nil);
  472. end;
  473. end;
  474. end;
  475. stringdef :
  476. begin
  477. { if we've already set elements which are constants }
  478. { throw an error }
  479. if ((hdef=nil) and assigned(buildp)) or
  480. not(is_char(hdef)) then
  481. CGMessage(type_e_typeconflict_in_set)
  482. else
  483. for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
  484. do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
  485. if hdef=nil then
  486. hdef:=cchartype;
  487. p2.free;
  488. end;
  489. else
  490. CGMessage(type_e_ordinal_expr_expected);
  491. end;
  492. { insert the set creation tree }
  493. if assigned(p4) then
  494. buildp:=caddnode.create(addn,buildp,p4);
  495. { load next and dispose current node }
  496. p2:=hp;
  497. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  498. tarrayconstructornode(p2).right:=nil;
  499. p2.free;
  500. end;
  501. if (hdef=nil) then
  502. hdef:=u8inttype;
  503. end
  504. else
  505. begin
  506. { empty set [], only remove node }
  507. p.free;
  508. end;
  509. { set the initial set type }
  510. constp.resultdef:=tsetdef.create(hdef,constsetlo,constsethi);
  511. { determine the resultdef for the tree }
  512. typecheckpass(buildp);
  513. { set the new tree }
  514. p:=buildp;
  515. end;
  516. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  517. begin
  518. if not(iscvarargs) and
  519. (p.nodetype=stringconstn) then
  520. p:=ctypeconvnode.create_internal(p,cansistringtype)
  521. else
  522. case p.resultdef.typ of
  523. enumdef :
  524. p:=ctypeconvnode.create_internal(p,s32inttype);
  525. arraydef :
  526. begin
  527. if is_chararray(p.resultdef) then
  528. p:=ctypeconvnode.create_internal(p,charpointertype)
  529. else
  530. if is_widechararray(p.resultdef) then
  531. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  532. else
  533. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  534. end;
  535. orddef :
  536. begin
  537. if is_integer(p.resultdef) and
  538. not(is_64bitint(p.resultdef)) then
  539. p:=ctypeconvnode.create(p,s32inttype)
  540. else if is_void(p.resultdef) then
  541. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  542. else if iscvarargs and
  543. is_currency(p.resultdef) then
  544. p:=ctypeconvnode.create(p,s64floattype);
  545. end;
  546. floatdef :
  547. if not(iscvarargs) then
  548. begin
  549. if not(is_currency(p.resultdef)) then
  550. p:=ctypeconvnode.create(p,pbestrealtype^);
  551. end
  552. else
  553. begin
  554. if is_constrealnode(p) and
  555. not(nf_explicit in p.flags) then
  556. MessagePos(p.fileinfo,type_w_double_c_varargs);
  557. if (tfloatdef(p.resultdef).floattype in [s32real,s64currency]) or
  558. (is_constrealnode(p) and
  559. not(nf_explicit in p.flags)) then
  560. p:=ctypeconvnode.create(p,s64floattype);
  561. end;
  562. procvardef :
  563. p:=ctypeconvnode.create(p,voidpointertype);
  564. stringdef:
  565. if iscvarargs then
  566. p:=ctypeconvnode.create(p,charpointertype);
  567. variantdef:
  568. if iscvarargs then
  569. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  570. pointerdef:
  571. ;
  572. classrefdef:
  573. if iscvarargs then
  574. p:=ctypeconvnode.create(p,voidpointertype);
  575. objectdef :
  576. if iscvarargs or
  577. is_object(p.resultdef) then
  578. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  579. else
  580. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  581. end;
  582. typecheckpass(p);
  583. end;
  584. {*****************************************************************************
  585. TTYPECONVNODE
  586. *****************************************************************************}
  587. constructor ttypeconvnode.create(node : tnode;def:tdef);
  588. begin
  589. inherited create(typeconvn,node);
  590. convtype:=tc_none;
  591. totypedef:=def;
  592. if def=nil then
  593. internalerror(200103281);
  594. fileinfo:=node.fileinfo;
  595. {An attempt to convert the result of a floating point division
  596. (with the / operator) to an integer type will fail. Give a hint
  597. to use the div operator.}
  598. if (node.nodetype=slashn) and (def.typ=orddef) then
  599. cgmessage(type_h_use_div_for_int);
  600. {In expressions like int64:=longint+longint, an integer overflow could be avoided
  601. by simply converting the operands to int64 first. Give a hint to do this.}
  602. if (node.nodetype in [addn,subn,muln]) and
  603. (def.typ=orddef) and (node.resultdef<>nil) and (node.resultdef.typ=orddef) and
  604. ((Torddef(node.resultdef).low>=Torddef(def).low) and (Torddef(node.resultdef).high<=Torddef(def).high)) and
  605. ((Torddef(node.resultdef).low>Torddef(def).low) or (Torddef(node.resultdef).high<Torddef(def).high)) then
  606. case node.nodetype of
  607. addn:
  608. cgmessage1(type_h_convert_add_operands_to_prevent_overflow,def.gettypename);
  609. subn:
  610. cgmessage1(type_h_convert_sub_operands_to_prevent_overflow,def.gettypename);
  611. muln:
  612. cgmessage1(type_h_convert_mul_operands_to_prevent_overflow,def.gettypename);
  613. end;
  614. end;
  615. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  616. begin
  617. self.create(node,def);
  618. include(flags,nf_explicit);
  619. end;
  620. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  621. begin
  622. self.create(node,def);
  623. { handle like explicit conversions }
  624. include(flags,nf_explicit);
  625. include(flags,nf_internal);
  626. end;
  627. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  628. begin
  629. self.create(node,voidtype);
  630. convtype:=tc_proc_2_procvar;
  631. end;
  632. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  633. begin
  634. inherited ppuload(t,ppufile);
  635. ppufile.getderef(totypedefderef);
  636. convtype:=tconverttype(ppufile.getbyte);
  637. end;
  638. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  639. begin
  640. inherited ppuwrite(ppufile);
  641. ppufile.putderef(totypedefderef);
  642. ppufile.putbyte(byte(convtype));
  643. end;
  644. procedure ttypeconvnode.buildderefimpl;
  645. begin
  646. inherited buildderefimpl;
  647. totypedefderef.build(totypedef);
  648. end;
  649. procedure ttypeconvnode.derefimpl;
  650. begin
  651. inherited derefimpl;
  652. totypedef:=tdef(totypedefderef.resolve);
  653. end;
  654. function ttypeconvnode.dogetcopy : tnode;
  655. var
  656. n : ttypeconvnode;
  657. begin
  658. n:=ttypeconvnode(inherited dogetcopy);
  659. n.convtype:=convtype;
  660. n.totypedef:=totypedef;
  661. dogetcopy:=n;
  662. end;
  663. procedure ttypeconvnode.printnodeinfo(var t : text);
  664. const
  665. convtyp2str : array[tconverttype] of pchar = (
  666. 'tc_none',
  667. 'tc_equal',
  668. 'tc_not_possible',
  669. 'tc_string_2_string',
  670. 'tc_char_2_string',
  671. 'tc_char_2_chararray',
  672. 'tc_pchar_2_string',
  673. 'tc_cchar_2_pchar',
  674. 'tc_cstring_2_pchar',
  675. 'tc_cstring_2_int',
  676. 'tc_ansistring_2_pchar',
  677. 'tc_string_2_chararray',
  678. 'tc_chararray_2_string',
  679. 'tc_array_2_pointer',
  680. 'tc_pointer_2_array',
  681. 'tc_int_2_int',
  682. 'tc_int_2_bool',
  683. 'tc_bool_2_bool',
  684. 'tc_bool_2_int',
  685. 'tc_real_2_real',
  686. 'tc_int_2_real',
  687. 'tc_real_2_currency',
  688. 'tc_proc_2_procvar',
  689. 'tc_nil_2_methodprocvar',
  690. 'tc_arrayconstructor_2_set',
  691. 'tc_set_2_set',
  692. 'tc_cord_2_pointer',
  693. 'tc_intf_2_string',
  694. 'tc_intf_2_guid',
  695. 'tc_class_2_intf',
  696. 'tc_char_2_char',
  697. 'tc_dynarray_2_openarray',
  698. 'tc_pwchar_2_string',
  699. 'tc_variant_2_dynarray',
  700. 'tc_dynarray_2_variant',
  701. 'tc_variant_2_enum',
  702. 'tc_enum_2_variant',
  703. 'tc_interface_2_variant',
  704. 'tc_variant_2_interface',
  705. 'tc_array_2_dynarray'
  706. );
  707. begin
  708. inherited printnodeinfo(t);
  709. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  710. end;
  711. function ttypeconvnode.typecheck_cord_to_pointer : tnode;
  712. var
  713. t : tnode;
  714. begin
  715. result:=nil;
  716. if left.nodetype=ordconstn then
  717. begin
  718. { check if we have a valid pointer constant (JM) }
  719. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  720. if (sizeof(TConstPtrUInt) = 4) then
  721. begin
  722. if (tordconstnode(left).value < low(longint)) or
  723. (tordconstnode(left).value > high(cardinal)) then
  724. CGMessage(parser_e_range_check_error);
  725. end
  726. else if (sizeof(TConstPtrUInt) = 8) then
  727. begin
  728. if (tordconstnode(left).value < low(int64)) or
  729. (tordconstnode(left).value > high(qword)) then
  730. CGMessage(parser_e_range_check_error);
  731. end
  732. else
  733. internalerror(2001020801);
  734. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resultdef);
  735. result:=t;
  736. end
  737. else
  738. internalerror(200104023);
  739. end;
  740. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  741. var
  742. chartype : string[8];
  743. newblock : tblocknode;
  744. newstat : tstatementnode;
  745. restemp : ttempcreatenode;
  746. begin
  747. if is_widechar(tarraydef(left.resultdef).elementdef) then
  748. chartype:='widechar'
  749. else
  750. chartype:='char';
  751. if tstringdef(resultdef).stringtype=st_shortstring then
  752. begin
  753. newblock:=internalstatements(newstat);
  754. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  755. addstatement(newstat,restemp);
  756. addstatement(newstat,ccallnode.createintern('fpc_'+chartype+'array_to_shortstr',
  757. ccallparanode.create(cordconstnode.create(
  758. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  759. ccallparanode.create(left,ccallparanode.create(
  760. ctemprefnode.create(restemp),nil)))));
  761. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  762. addstatement(newstat,ctemprefnode.create(restemp));
  763. result:=newblock;
  764. end
  765. else
  766. result:=ccallnode.createinternres(
  767. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  768. ccallparanode.create(cordconstnode.create(
  769. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  770. ccallparanode.create(left,nil)),resultdef);
  771. left:=nil;
  772. end;
  773. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  774. var
  775. newblock : tblocknode;
  776. newstat : tstatementnode;
  777. restemp : ttempcreatenode;
  778. pchtemp : pchar;
  779. arrsize : aint;
  780. chartype : string[8];
  781. begin
  782. result := nil;
  783. with tarraydef(resultdef) do
  784. begin
  785. if highrange<lowrange then
  786. internalerror(200501051);
  787. arrsize := highrange-lowrange+1;
  788. end;
  789. if (left.nodetype = stringconstn) and
  790. (tstringconstnode(left).cst_type=cst_conststring) then
  791. begin
  792. { if the array of char is large enough we can use the string
  793. constant directly. This is handled in ncgcnv }
  794. if (arrsize>=tstringconstnode(left).len) and
  795. is_char(tarraydef(resultdef).elementdef) then
  796. begin
  797. { pad the constant string with #0 to the array len }
  798. { (2.0.x compatible) }
  799. if (arrsize>tstringconstnode(left).len) then
  800. begin
  801. pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  802. left.free;
  803. left:=cstringconstnode.createpchar(pchtemp,arrsize);
  804. typecheckpass(left);
  805. end;
  806. exit;
  807. end;
  808. { Convert to wide/short/ansistring and call default helper }
  809. if is_widechar(tarraydef(resultdef).elementdef) then
  810. inserttypeconv(left,cwidestringtype)
  811. else
  812. begin
  813. if tstringconstnode(left).len>255 then
  814. inserttypeconv(left,cansistringtype)
  815. else
  816. inserttypeconv(left,cshortstringtype);
  817. end;
  818. end;
  819. if is_widechar(tarraydef(resultdef).elementdef) then
  820. chartype:='widechar'
  821. else
  822. chartype:='char';
  823. newblock:=internalstatements(newstat);
  824. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  825. addstatement(newstat,restemp);
  826. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  827. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  828. ctemprefnode.create(restemp),nil))));
  829. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  830. addstatement(newstat,ctemprefnode.create(restemp));
  831. result:=newblock;
  832. left:=nil;
  833. end;
  834. function ttypeconvnode.typecheck_string_to_string : tnode;
  835. var
  836. procname: string[31];
  837. newblock : tblocknode;
  838. newstat : tstatementnode;
  839. restemp : ttempcreatenode;
  840. begin
  841. result:=nil;
  842. if (left.nodetype=stringconstn) and
  843. ((not is_widechararray(left.resultdef) and
  844. not is_widestring(left.resultdef)) or
  845. (tstringdef(resultdef).stringtype=st_widestring) or
  846. { non-ascii chars would be replaced with '?' -> loses info }
  847. not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str))) then
  848. begin
  849. tstringconstnode(left).changestringtype(resultdef);
  850. result:=left;
  851. left:=nil;
  852. end
  853. else
  854. begin
  855. { get the correct procedure name }
  856. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  857. '_to_'+tstringdef(resultdef).stringtypname;
  858. if tstringdef(resultdef).stringtype=st_shortstring then
  859. begin
  860. newblock:=internalstatements(newstat);
  861. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  862. addstatement(newstat,restemp);
  863. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  864. ctemprefnode.create(restemp),nil))));
  865. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  866. addstatement(newstat,ctemprefnode.create(restemp));
  867. result:=newblock;
  868. end
  869. else
  870. result := ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  871. left:=nil;
  872. end;
  873. end;
  874. function ttypeconvnode.typecheck_char_to_string : tnode;
  875. var
  876. procname: string[31];
  877. para : tcallparanode;
  878. hp : tstringconstnode;
  879. ws : pcompilerwidestring;
  880. newblock : tblocknode;
  881. newstat : tstatementnode;
  882. restemp : ttempcreatenode;
  883. begin
  884. result:=nil;
  885. { we can't do widechar to ansichar conversions at compile time, since }
  886. { this maps all non-ascii chars to '?' -> loses information }
  887. if (left.nodetype=ordconstn) and
  888. ((tstringdef(resultdef).stringtype=st_widestring) or
  889. (torddef(left.resultdef).ordtype=uchar) or
  890. { >=128 is destroyed }
  891. (tordconstnode(left).value<128)) then
  892. begin
  893. if tstringdef(resultdef).stringtype=st_widestring then
  894. begin
  895. initwidestring(ws);
  896. if torddef(left.resultdef).ordtype=uwidechar then
  897. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  898. else
  899. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  900. hp:=cstringconstnode.createwstr(ws);
  901. donewidestring(ws);
  902. end
  903. else
  904. begin
  905. if torddef(left.resultdef).ordtype=uwidechar then
  906. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value)))
  907. else
  908. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value));
  909. tstringconstnode(hp).changestringtype(resultdef);
  910. end;
  911. result:=hp;
  912. end
  913. else
  914. { shortstrings are handled 'inline' (except for widechars) }
  915. if (tstringdef(resultdef).stringtype <> st_shortstring) or
  916. (torddef(left.resultdef).ordtype = uwidechar) then
  917. begin
  918. if (tstringdef(resultdef).stringtype <> st_shortstring) then
  919. begin
  920. { create the procname }
  921. if torddef(left.resultdef).ordtype<>uwidechar then
  922. procname := 'fpc_char_to_'
  923. else
  924. procname := 'fpc_wchar_to_';
  925. procname:=procname+tstringdef(resultdef).stringtypname;
  926. { and the parameter }
  927. para := ccallparanode.create(left,nil);
  928. { and finally the call }
  929. result := ccallnode.createinternres(procname,para,resultdef);
  930. end
  931. else
  932. begin
  933. newblock:=internalstatements(newstat);
  934. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  935. addstatement(newstat,restemp);
  936. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  937. ctemprefnode.create(restemp),nil))));
  938. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  939. addstatement(newstat,ctemprefnode.create(restemp));
  940. result:=newblock;
  941. end;
  942. left := nil;
  943. end
  944. else
  945. begin
  946. { create word(byte(char) shl 8 or 1) for litte endian machines }
  947. { and word(byte(char) or 256) for big endian machines }
  948. left := ctypeconvnode.create_internal(left,u8inttype);
  949. if (target_info.endian = endian_little) then
  950. left := caddnode.create(orn,
  951. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  952. cordconstnode.create(1,s32inttype,false))
  953. else
  954. left := caddnode.create(orn,left,
  955. cordconstnode.create(1 shl 8,s32inttype,false));
  956. left := ctypeconvnode.create_internal(left,u16inttype);
  957. typecheckpass(left);
  958. end;
  959. end;
  960. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  961. begin
  962. if resultdef.size <> 1 then
  963. begin
  964. { convert first to string, then to chararray }
  965. inserttypeconv(left,cshortstringtype);
  966. inserttypeconv(left,resultdef);
  967. result:=left;
  968. left := nil;
  969. exit;
  970. end;
  971. result := nil;
  972. end;
  973. function ttypeconvnode.typecheck_char_to_char : tnode;
  974. var
  975. hp : tordconstnode;
  976. begin
  977. result:=nil;
  978. if (left.nodetype=ordconstn) and
  979. ((torddef(resultdef).ordtype<>uchar) or
  980. (torddef(left.resultdef).ordtype<>uwidechar) or
  981. { >= 128 is replaced by '?' currently -> loses information }
  982. (tordconstnode(left).value<128)) then
  983. begin
  984. if (torddef(resultdef).ordtype=uchar) and
  985. (torddef(left.resultdef).ordtype=uwidechar) then
  986. begin
  987. hp:=cordconstnode.create(
  988. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  989. cchartype,true);
  990. result:=hp;
  991. end
  992. else if (torddef(resultdef).ordtype=uwidechar) and
  993. (torddef(left.resultdef).ordtype=uchar) then
  994. begin
  995. hp:=cordconstnode.create(
  996. asciichar2unicode(chr(tordconstnode(left).value)),
  997. cwidechartype,true);
  998. result:=hp;
  999. end
  1000. else
  1001. internalerror(200105131);
  1002. exit;
  1003. end;
  1004. end;
  1005. function ttypeconvnode.typecheck_int_to_int : tnode;
  1006. var
  1007. v : TConstExprInt;
  1008. begin
  1009. result:=nil;
  1010. if left.nodetype=ordconstn then
  1011. begin
  1012. v:=tordconstnode(left).value;
  1013. if is_currency(resultdef) then
  1014. v:=v*10000;
  1015. if (resultdef.typ=pointerdef) then
  1016. result:=cpointerconstnode.create(TConstPtrUInt(v),resultdef)
  1017. else
  1018. begin
  1019. if is_currency(left.resultdef) then
  1020. v:=v div 10000;
  1021. result:=cordconstnode.create(v,resultdef,false);
  1022. end;
  1023. end
  1024. else if left.nodetype=pointerconstn then
  1025. begin
  1026. v:=tpointerconstnode(left).value;
  1027. if (resultdef.typ=pointerdef) then
  1028. result:=cpointerconstnode.create(v,resultdef)
  1029. else
  1030. begin
  1031. if is_currency(resultdef) then
  1032. v:=v*10000;
  1033. result:=cordconstnode.create(v,resultdef,false);
  1034. end;
  1035. end
  1036. else
  1037. begin
  1038. { multiply by 10000 for currency. We need to use getcopy to pass
  1039. the argument because the current node is always disposed. Only
  1040. inserting the multiply in the left node is not possible because
  1041. it'll get in an infinite loop to convert int->currency }
  1042. if is_currency(resultdef) then
  1043. begin
  1044. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1045. include(result.flags,nf_is_currency);
  1046. end
  1047. else if is_currency(left.resultdef) then
  1048. begin
  1049. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1050. include(result.flags,nf_is_currency);
  1051. end;
  1052. end;
  1053. end;
  1054. function ttypeconvnode.typecheck_int_to_real : tnode;
  1055. var
  1056. rv : bestreal;
  1057. begin
  1058. result:=nil;
  1059. if left.nodetype=ordconstn then
  1060. begin
  1061. rv:=tordconstnode(left).value;
  1062. if is_currency(resultdef) then
  1063. rv:=rv*10000.0
  1064. else if is_currency(left.resultdef) then
  1065. rv:=rv/10000.0;
  1066. result:=crealconstnode.create(rv,resultdef);
  1067. end
  1068. else
  1069. begin
  1070. { multiply by 10000 for currency. We need to use getcopy to pass
  1071. the argument because the current node is always disposed. Only
  1072. inserting the multiply in the left node is not possible because
  1073. it'll get in an infinite loop to convert int->currency }
  1074. if is_currency(resultdef) then
  1075. begin
  1076. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1077. include(result.flags,nf_is_currency);
  1078. end
  1079. else if is_currency(left.resultdef) then
  1080. begin
  1081. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1082. include(result.flags,nf_is_currency);
  1083. end;
  1084. end;
  1085. end;
  1086. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1087. begin
  1088. if not is_currency(resultdef) then
  1089. internalerror(200304221);
  1090. result:=nil;
  1091. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1092. include(left.flags,nf_is_currency);
  1093. typecheckpass(left);
  1094. { Convert constants directly, else call Round() }
  1095. if left.nodetype=realconstn then
  1096. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1097. else
  1098. begin
  1099. result:=ccallnode.createinternres('fpc_round_real',
  1100. ccallparanode.create(left,nil),resultdef);
  1101. left:=nil;
  1102. end;
  1103. end;
  1104. function ttypeconvnode.typecheck_real_to_real : tnode;
  1105. begin
  1106. result:=nil;
  1107. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1108. begin
  1109. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1110. include(left.flags,nf_is_currency);
  1111. typecheckpass(left);
  1112. end
  1113. else
  1114. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1115. begin
  1116. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1117. include(left.flags,nf_is_currency);
  1118. typecheckpass(left);
  1119. end;
  1120. end;
  1121. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1122. begin
  1123. result:=nil;
  1124. if is_pwidechar(resultdef) then
  1125. inserttypeconv(left,cwidestringtype)
  1126. else
  1127. inserttypeconv(left,cshortstringtype);
  1128. { evaluate again, reset resultdef so the convert_typ
  1129. will be calculated again and cstring_to_pchar will
  1130. be used for futher conversion }
  1131. convtype:=tc_none;
  1132. result:=pass_typecheck;
  1133. end;
  1134. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1135. begin
  1136. result:=nil;
  1137. if is_pwidechar(resultdef) then
  1138. inserttypeconv(left,cwidestringtype)
  1139. else
  1140. if is_pchar(resultdef) and
  1141. is_widestring(left.resultdef) then
  1142. begin
  1143. inserttypeconv(left,cansistringtype);
  1144. { the second pass of second_cstring_to_pchar expects a }
  1145. { strinconstn, but this may become a call to the }
  1146. { widestring manager in case left contains "high ascii" }
  1147. if (left.nodetype<>stringconstn) then
  1148. begin
  1149. result:=left;
  1150. left:=nil;
  1151. end;
  1152. end;
  1153. end;
  1154. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1155. var
  1156. fcc : cardinal;
  1157. pb : pbyte;
  1158. begin
  1159. result:=nil;
  1160. if left.nodetype<>stringconstn then
  1161. internalerror(200510012);
  1162. if tstringconstnode(left).len=4 then
  1163. begin
  1164. pb:=pbyte(tstringconstnode(left).value_str);
  1165. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1166. result:=cordconstnode.create(fcc,u32inttype,false);
  1167. end
  1168. else
  1169. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1170. end;
  1171. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1172. var
  1173. hp : tnode;
  1174. begin
  1175. result:=nil;
  1176. if left.nodetype<>arrayconstructorn then
  1177. internalerror(5546);
  1178. { remove typeconv node }
  1179. hp:=left;
  1180. left:=nil;
  1181. { create a set constructor tree }
  1182. arrayconstructor_to_set(hp);
  1183. result:=hp;
  1184. end;
  1185. function ttypeconvnode.typecheck_set_to_set : tnode;
  1186. begin
  1187. result:=nil;
  1188. { constant sets can be converted by changing the type only }
  1189. if (left.nodetype=setconstn) then
  1190. begin
  1191. left.resultdef:=resultdef;
  1192. result:=left;
  1193. left:=nil;
  1194. exit;
  1195. end;
  1196. end;
  1197. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1198. var
  1199. newblock : tblocknode;
  1200. newstat : tstatementnode;
  1201. restemp : ttempcreatenode;
  1202. begin
  1203. if tstringdef(resultdef).stringtype=st_shortstring then
  1204. begin
  1205. newblock:=internalstatements(newstat);
  1206. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1207. addstatement(newstat,restemp);
  1208. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1209. ctemprefnode.create(restemp),nil))));
  1210. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1211. addstatement(newstat,ctemprefnode.create(restemp));
  1212. result:=newblock;
  1213. end
  1214. else
  1215. result := ccallnode.createinternres(
  1216. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1217. ccallparanode.create(left,nil),resultdef);
  1218. left:=nil;
  1219. end;
  1220. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1221. begin
  1222. if assigned(tobjectdef(left.resultdef).iidguid) then
  1223. begin
  1224. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1225. CGMessage1(type_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1226. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1227. end;
  1228. end;
  1229. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1230. begin
  1231. { a dynamic array is a pointer to an array, so to convert it to }
  1232. { an open array, we have to dereference it (JM) }
  1233. result := ctypeconvnode.create_internal(left,voidpointertype);
  1234. typecheckpass(result);
  1235. { left is reused }
  1236. left := nil;
  1237. result := cderefnode.create(result);
  1238. include(result.flags,nf_no_checkpointer);
  1239. result.resultdef := resultdef;
  1240. end;
  1241. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1242. var
  1243. newblock : tblocknode;
  1244. newstat : tstatementnode;
  1245. restemp : ttempcreatenode;
  1246. begin
  1247. if tstringdef(resultdef).stringtype=st_shortstring then
  1248. begin
  1249. newblock:=internalstatements(newstat);
  1250. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1251. addstatement(newstat,restemp);
  1252. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1253. ctemprefnode.create(restemp),nil))));
  1254. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1255. addstatement(newstat,ctemprefnode.create(restemp));
  1256. result:=newblock;
  1257. end
  1258. else
  1259. result := ccallnode.createinternres(
  1260. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1261. ccallparanode.create(left,nil),resultdef);
  1262. left:=nil;
  1263. end;
  1264. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1265. begin
  1266. result := ccallnode.createinternres(
  1267. 'fpc_variant_to_dynarray',
  1268. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti)),
  1269. ccallparanode.create(left,nil)
  1270. ),resultdef);
  1271. typecheckpass(result);
  1272. left:=nil;
  1273. end;
  1274. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1275. begin
  1276. result := ccallnode.createinternres(
  1277. 'fpc_dynarray_to_variant',
  1278. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti)),
  1279. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1280. ),resultdef);
  1281. typecheckpass(result);
  1282. left:=nil;
  1283. end;
  1284. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1285. begin
  1286. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1287. result := ccallnode.createinternres(
  1288. 'fpc_variant_to_idispatch',
  1289. ccallparanode.create(left,nil)
  1290. ,resultdef)
  1291. else
  1292. result := ccallnode.createinternres(
  1293. 'fpc_variant_to_interface',
  1294. ccallparanode.create(left,nil)
  1295. ,resultdef);
  1296. typecheckpass(result);
  1297. left:=nil;
  1298. end;
  1299. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1300. begin
  1301. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1302. result := ccallnode.createinternres(
  1303. 'fpc_idispatch_to_variant',
  1304. ccallparanode.create(left,nil)
  1305. ,resultdef)
  1306. else
  1307. result := ccallnode.createinternres(
  1308. 'fpc_interface_to_variant',
  1309. ccallparanode.create(left,nil)
  1310. ,resultdef);
  1311. typecheckpass(result);
  1312. left:=nil;
  1313. end;
  1314. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1315. begin
  1316. result := ctypeconvnode.create_internal(left,sinttype);
  1317. result := ctypeconvnode.create_internal(result,resultdef);
  1318. typecheckpass(result);
  1319. { left is reused }
  1320. left := nil;
  1321. end;
  1322. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1323. begin
  1324. result := ctypeconvnode.create_internal(left,sinttype);
  1325. result := ctypeconvnode.create_internal(result,cvarianttype);
  1326. typecheckpass(result);
  1327. { left is reused }
  1328. left := nil;
  1329. end;
  1330. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1331. var
  1332. newstatement : tstatementnode;
  1333. temp : ttempcreatenode;
  1334. temp2 : ttempcreatenode;
  1335. begin
  1336. { create statements with call to getmem+initialize }
  1337. result:=internalstatements(newstatement);
  1338. { create temp for result }
  1339. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1340. addstatement(newstatement,temp);
  1341. { get temp for array of lengths }
  1342. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1343. addstatement(newstatement,temp2);
  1344. { one dimensional }
  1345. addstatement(newstatement,cassignmentnode.create(
  1346. ctemprefnode.create_offset(temp2,0),
  1347. cordconstnode.create
  1348. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1349. { create call to fpc_dynarr_setlength }
  1350. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1351. ccallparanode.create(caddrnode.create_internal
  1352. (ctemprefnode.create(temp2)),
  1353. ccallparanode.create(cordconstnode.create
  1354. (1,s32inttype,true),
  1355. ccallparanode.create(caddrnode.create_internal
  1356. (crttinode.create(tstoreddef(resultdef),initrtti)),
  1357. ccallparanode.create(
  1358. ctypeconvnode.create_internal(
  1359. ctemprefnode.create(temp),voidpointertype),
  1360. nil))))
  1361. ));
  1362. addstatement(newstatement,ctempdeletenode.create(temp2));
  1363. { copy ... }
  1364. addstatement(newstatement,cassignmentnode.create(
  1365. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1366. left
  1367. ));
  1368. { left is reused }
  1369. left:=nil;
  1370. { the last statement should return the value as
  1371. location and type, this is done be referencing the
  1372. temp and converting it first from a persistent temp to
  1373. normal temp }
  1374. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1375. addstatement(newstatement,ctemprefnode.create(temp));
  1376. end;
  1377. procedure copyparasym(p:TObject;arg:pointer);
  1378. var
  1379. newparast : TSymtable absolute arg;
  1380. vs : tparavarsym;
  1381. begin
  1382. if tsym(p).typ<>paravarsym then
  1383. exit;
  1384. with tparavarsym(p) do
  1385. begin
  1386. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1387. vs.defaultconstsym:=defaultconstsym;
  1388. newparast.insert(vs);
  1389. end;
  1390. end;
  1391. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1392. var
  1393. pd : tabstractprocdef;
  1394. begin
  1395. result:=nil;
  1396. pd:=tabstractprocdef(left.resultdef);
  1397. { create procvardef }
  1398. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1399. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1400. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1401. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1402. tprocvardef(resultdef).returndef:=pd.returndef;
  1403. { method ? then set the methodpointer flag }
  1404. if (pd.owner.symtabletype=ObjectSymtable) then
  1405. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1406. { was it a local procedure? }
  1407. if (pd.owner.symtabletype=localsymtable) then
  1408. include(tprocvardef(resultdef).procoptions,po_local);
  1409. { only need the address of the method? this is needed
  1410. for @tobject.create. In this case there will be a loadn without
  1411. a methodpointer. }
  1412. if (left.nodetype=loadn) and
  1413. not assigned(tloadnode(left).left) then
  1414. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1415. { Add parameters use only references, we don't need to keep the
  1416. parast. We use the parast from the original function to calculate
  1417. our parameter data and reset it afterwards }
  1418. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1419. tprocvardef(resultdef).calcparas;
  1420. end;
  1421. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1422. const
  1423. resultdefconvert : array[tconverttype] of pointer = (
  1424. {none} nil,
  1425. {equal} nil,
  1426. {not_possible} nil,
  1427. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1428. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1429. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1430. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1431. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1432. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1433. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1434. { ansistring_2_pchar } nil,
  1435. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1436. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1437. { array_2_pointer } nil,
  1438. { pointer_2_array } nil,
  1439. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1440. { int_2_bool } nil,
  1441. { bool_2_bool } nil,
  1442. { bool_2_int } nil,
  1443. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1444. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1445. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1446. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1447. { nil_2_methodprocvar } nil,
  1448. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1449. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1450. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1451. { intf_2_string } nil,
  1452. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1453. { class_2_intf } nil,
  1454. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1455. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1456. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1457. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1458. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1459. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1460. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1461. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1462. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1463. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1464. );
  1465. type
  1466. tprocedureofobject = function : tnode of object;
  1467. var
  1468. r : packed record
  1469. proc : pointer;
  1470. obj : pointer;
  1471. end;
  1472. begin
  1473. result:=nil;
  1474. { this is a little bit dirty but it works }
  1475. { and should be quite portable too }
  1476. r.proc:=resultdefconvert[c];
  1477. r.obj:=self;
  1478. if assigned(r.proc) then
  1479. result:=tprocedureofobject(r)();
  1480. end;
  1481. function ttypeconvnode.pass_typecheck:tnode;
  1482. var
  1483. hdef : tdef;
  1484. hp : tnode;
  1485. currprocdef : tabstractprocdef;
  1486. aprocdef : tprocdef;
  1487. eq : tequaltype;
  1488. cdoptions : tcompare_defs_options;
  1489. newblock: tblocknode;
  1490. newstatement: tstatementnode;
  1491. tempnode: ttempcreatenode;
  1492. begin
  1493. result:=nil;
  1494. resultdef:=totypedef;
  1495. typecheckpass(left);
  1496. if codegenerror then
  1497. exit;
  1498. { When absolute force tc_equal }
  1499. if (nf_absolute in flags) then
  1500. begin
  1501. convtype:=tc_equal;
  1502. if not(tstoreddef(resultdef).is_intregable) and
  1503. not(tstoreddef(resultdef).is_fpuregable) then
  1504. make_not_regable(left,[ra_addr_regable]);
  1505. exit;
  1506. end;
  1507. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1508. convert on the procvar value. This is used to access the
  1509. fields of a methodpointer }
  1510. if not(nf_load_procvar in flags) and
  1511. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1512. maybe_call_procvar(left,true);
  1513. { convert array constructors to sets, because there is no conversion
  1514. possible for array constructors }
  1515. if (resultdef.typ<>arraydef) and
  1516. is_array_constructor(left.resultdef) then
  1517. begin
  1518. arrayconstructor_to_set(left);
  1519. typecheckpass(left);
  1520. end;
  1521. if convtype=tc_none then
  1522. begin
  1523. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1524. if nf_explicit in flags then
  1525. include(cdoptions,cdo_explicit);
  1526. if nf_internal in flags then
  1527. include(cdoptions,cdo_internal);
  1528. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1529. case eq of
  1530. te_exact,
  1531. te_equal :
  1532. begin
  1533. result := simplify;
  1534. if assigned(result) then
  1535. exit;
  1536. { Only leave when there is no conversion to do.
  1537. We can still need to call a conversion routine,
  1538. like the routine to convert a stringconstnode }
  1539. if convtype in [tc_equal,tc_not_possible] then
  1540. begin
  1541. left.resultdef:=resultdef;
  1542. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1543. include(left.flags, nf_typedaddr);
  1544. result:=left;
  1545. left:=nil;
  1546. exit;
  1547. end;
  1548. end;
  1549. te_convert_l1,
  1550. te_convert_l2,
  1551. te_convert_l3,
  1552. te_convert_l4,
  1553. te_convert_l5:
  1554. begin
  1555. result := simplify;
  1556. if assigned(result) then
  1557. exit;
  1558. { nothing to do }
  1559. end;
  1560. te_convert_operator :
  1561. begin
  1562. include(current_procinfo.flags,pi_do_call);
  1563. aprocdef.procsym.IncRefCountBy(1);
  1564. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1565. { tell explicitly which def we must use !! (PM) }
  1566. tcallnode(hp).procdefinition:=aprocdef;
  1567. left:=nil;
  1568. result:=hp;
  1569. exit;
  1570. end;
  1571. te_incompatible :
  1572. begin
  1573. { Procedures have a resultdef of voiddef and functions of their
  1574. own resultdef. They will therefore always be incompatible with
  1575. a procvar. Because isconvertable cannot check for procedures we
  1576. use an extra check for them.}
  1577. if (left.nodetype=calln) and
  1578. (tcallnode(left).para_count=0) and
  1579. (resultdef.typ=procvardef) and
  1580. (
  1581. (m_tp_procvar in current_settings.modeswitches) or
  1582. (m_mac_procvar in current_settings.modeswitches)
  1583. ) then
  1584. begin
  1585. if assigned(tcallnode(left).right) then
  1586. begin
  1587. { this is already a procvar, if it is really equal
  1588. is checked below }
  1589. convtype:=tc_equal;
  1590. hp:=tcallnode(left).right.getcopy;
  1591. currprocdef:=tabstractprocdef(hp.resultdef);
  1592. end
  1593. else
  1594. begin
  1595. convtype:=tc_proc_2_procvar;
  1596. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1597. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1598. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1599. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1600. begin
  1601. if assigned(tcallnode(left).methodpointer) then
  1602. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1603. else
  1604. tloadnode(hp).set_mp(load_self_node);
  1605. end;
  1606. typecheckpass(hp);
  1607. end;
  1608. left.free;
  1609. left:=hp;
  1610. { Now check if the procedure we are going to assign to
  1611. the procvar, is compatible with the procvar's type }
  1612. if not(nf_explicit in flags) and
  1613. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1614. IncompatibleTypes(left.resultdef,resultdef);
  1615. exit;
  1616. end;
  1617. { Handle explicit type conversions }
  1618. if nf_explicit in flags then
  1619. begin
  1620. { do common tc_equal cast }
  1621. convtype:=tc_equal;
  1622. { ordinal constants can be resized to 1,2,4,8 bytes }
  1623. if (left.nodetype=ordconstn) then
  1624. begin
  1625. { Insert typeconv for ordinal to the correct size first on left, after
  1626. that the other conversion can be done }
  1627. hdef:=nil;
  1628. case longint(resultdef.size) of
  1629. 1 :
  1630. hdef:=s8inttype;
  1631. 2 :
  1632. hdef:=s16inttype;
  1633. 4 :
  1634. hdef:=s32inttype;
  1635. 8 :
  1636. hdef:=s64inttype;
  1637. end;
  1638. { we need explicit, because it can also be an enum }
  1639. if assigned(hdef) then
  1640. inserttypeconv_internal(left,hdef)
  1641. else
  1642. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1643. end;
  1644. { check if the result could be in a register }
  1645. if (not(tstoreddef(resultdef).is_intregable) and
  1646. not(tstoreddef(resultdef).is_fpuregable)) or
  1647. ((left.resultdef.typ = floatdef) and
  1648. (resultdef.typ <> floatdef)) then
  1649. make_not_regable(left,[ra_addr_regable]);
  1650. { class/interface to class/interface, with checkobject support }
  1651. if is_class_or_interface(resultdef) and
  1652. is_class_or_interface(left.resultdef) then
  1653. begin
  1654. { check if the types are related }
  1655. if not(nf_internal in flags) and
  1656. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1657. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1658. begin
  1659. { Give an error when typecasting class to interface, this is compatible
  1660. with delphi }
  1661. if is_interface(resultdef) and
  1662. not is_interface(left.resultdef) then
  1663. CGMessage2(type_e_classes_not_related,
  1664. FullTypeName(left.resultdef,resultdef),
  1665. FullTypeName(resultdef,left.resultdef))
  1666. else
  1667. CGMessage2(type_w_classes_not_related,
  1668. FullTypeName(left.resultdef,resultdef),
  1669. FullTypeName(resultdef,left.resultdef))
  1670. end;
  1671. { Add runtime check? }
  1672. if (cs_check_object in current_settings.localswitches) and
  1673. not(nf_internal in flags) then
  1674. begin
  1675. { we can translate the typeconvnode to 'as' when
  1676. typecasting to a class or interface }
  1677. { we need to make sure the result can still be
  1678. passed as a var parameter }
  1679. newblock:=internalstatements(newstatement);
  1680. if (valid_for_var(left,false)) then
  1681. begin
  1682. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1683. addstatement(newstatement,tempnode);
  1684. addstatement(newstatement,cassignmentnode.create(
  1685. ctemprefnode.create(tempnode),
  1686. caddrnode.create_internal(left)));
  1687. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1688. end
  1689. else
  1690. begin
  1691. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1692. addstatement(newstatement,tempnode);
  1693. addstatement(newstatement,cassignmentnode.create(
  1694. ctemprefnode.create(tempnode),
  1695. left));
  1696. left:=ctemprefnode.create(tempnode);
  1697. end;
  1698. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1699. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1700. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1701. left:=nil;
  1702. result:=newblock;
  1703. exit;
  1704. end;
  1705. end
  1706. else
  1707. begin
  1708. { only if the same size or formal def, and }
  1709. { don't allow type casting of constants to }
  1710. { structured types }
  1711. if not(
  1712. (left.resultdef.typ=formaldef) or
  1713. (
  1714. not(is_open_array(left.resultdef)) and
  1715. not(is_array_constructor(left.resultdef)) and
  1716. (left.resultdef.size=resultdef.size) and
  1717. { disallow casts of const nodes }
  1718. (not is_constnode(left) or
  1719. { however, there are some exceptions }
  1720. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1721. filedef,variantdef,objectdef]) or
  1722. is_class_or_interface(resultdef) or
  1723. { the softfloat code generates casts <const. float> to record }
  1724. (nf_internal in flags)
  1725. ))
  1726. ) or
  1727. (
  1728. is_void(left.resultdef) and
  1729. (left.nodetype=derefn)
  1730. )
  1731. ) then
  1732. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1733. end;
  1734. end
  1735. else
  1736. IncompatibleTypes(left.resultdef,resultdef);
  1737. end;
  1738. else
  1739. internalerror(200211231);
  1740. end;
  1741. end;
  1742. { Give hint or warning for unportable code, exceptions are
  1743. - typecasts from constants
  1744. - void }
  1745. if not(nf_internal in flags) and
  1746. (left.nodetype<>ordconstn) and
  1747. not(is_void(left.resultdef)) and
  1748. (((left.resultdef.typ=orddef) and
  1749. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1750. ((resultdef.typ=orddef) and
  1751. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1752. begin
  1753. {Converting pointers to signed integers is a bad idea. Warn.}
  1754. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1755. { Give a warning when sizes don't match, because then info will be lost }
  1756. if left.resultdef.size=resultdef.size then
  1757. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1758. else
  1759. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1760. end;
  1761. result := simplify;
  1762. if assigned(result) then
  1763. exit;
  1764. { now call the resultdef helper to do constant folding }
  1765. result:=typecheck_call_helper(convtype);
  1766. end;
  1767. function ttypeconvnode.simplify: tnode;
  1768. var
  1769. hp: tnode;
  1770. begin
  1771. result := nil;
  1772. { Constant folding and other node transitions to
  1773. remove the typeconv node }
  1774. case left.nodetype of
  1775. realconstn :
  1776. begin
  1777. if (convtype = tc_real_2_currency) then
  1778. result := typecheck_real_to_currency
  1779. else if (convtype = tc_real_2_real) then
  1780. result := typecheck_real_to_real
  1781. else
  1782. exit;
  1783. if not(assigned(result)) then
  1784. begin
  1785. result := left;
  1786. left := nil;
  1787. end;
  1788. if (result.nodetype = realconstn) then
  1789. begin
  1790. hp:=result;
  1791. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1792. if ([nf_explicit,nf_internal] * flags <> []) then
  1793. include(result.flags, nf_explicit);
  1794. hp.free;
  1795. end;
  1796. end;
  1797. niln :
  1798. begin
  1799. { nil to ordinal node }
  1800. if (resultdef.typ=orddef) then
  1801. begin
  1802. hp:=cordconstnode.create(0,resultdef,true);
  1803. if ([nf_explicit,nf_internal] * flags <> []) then
  1804. include(hp.flags, nf_explicit);
  1805. result:=hp;
  1806. exit;
  1807. end
  1808. else
  1809. { fold nil to any pointer type }
  1810. if (resultdef.typ=pointerdef) then
  1811. begin
  1812. hp:=cnilnode.create;
  1813. hp.resultdef:=resultdef;
  1814. if ([nf_explicit,nf_internal] * flags <> []) then
  1815. include(hp.flags, nf_explicit);
  1816. result:=hp;
  1817. exit;
  1818. end
  1819. else
  1820. { remove typeconv after niln, but not when the result is a
  1821. methodpointer. The typeconv of the methodpointer will then
  1822. take care of updateing size of niln to OS_64 }
  1823. if not((resultdef.typ=procvardef) and
  1824. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1825. begin
  1826. left.resultdef:=resultdef;
  1827. if ([nf_explicit,nf_internal] * flags <> []) then
  1828. include(left.flags, nf_explicit);
  1829. result:=left;
  1830. left:=nil;
  1831. exit;
  1832. end;
  1833. end;
  1834. ordconstn :
  1835. begin
  1836. { ordinal contants can be directly converted }
  1837. { but not char to char because it is a widechar to char or via versa }
  1838. { which needs extra code to do the code page transistion }
  1839. { constant ordinal to pointer }
  1840. if (resultdef.typ=pointerdef) and
  1841. (convtype<>tc_cchar_2_pchar) then
  1842. begin
  1843. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resultdef);
  1844. if ([nf_explicit,nf_internal] * flags <> []) then
  1845. include(hp.flags, nf_explicit);
  1846. result:=hp;
  1847. exit;
  1848. end
  1849. else if is_ordinal(resultdef) and
  1850. not(convtype=tc_char_2_char) then
  1851. begin
  1852. { replace the resultdef and recheck the range }
  1853. if ([nf_explicit,nf_internal] * flags <> []) then
  1854. include(left.flags, nf_explicit);
  1855. testrange(left.resultdef,resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1856. left.resultdef:=resultdef;
  1857. result:=left;
  1858. left:=nil;
  1859. exit;
  1860. end;
  1861. end;
  1862. pointerconstn :
  1863. begin
  1864. { pointerconstn to any pointer is folded too }
  1865. if (resultdef.typ=pointerdef) then
  1866. begin
  1867. left.resultdef:=resultdef;
  1868. if ([nf_explicit,nf_internal] * flags <> []) then
  1869. include(left.flags, nf_explicit);
  1870. result:=left;
  1871. left:=nil;
  1872. exit;
  1873. end
  1874. { constant pointer to ordinal }
  1875. else if is_ordinal(resultdef) then
  1876. begin
  1877. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1878. resultdef,not(nf_explicit in flags));
  1879. if ([nf_explicit,nf_internal] * flags <> []) then
  1880. include(hp.flags, nf_explicit);
  1881. result:=hp;
  1882. exit;
  1883. end;
  1884. end;
  1885. end;
  1886. end;
  1887. procedure Ttypeconvnode.mark_write;
  1888. begin
  1889. left.mark_write;
  1890. end;
  1891. function ttypeconvnode.first_cord_to_pointer : tnode;
  1892. begin
  1893. result:=nil;
  1894. internalerror(200104043);
  1895. end;
  1896. function ttypeconvnode.first_int_to_int : tnode;
  1897. begin
  1898. first_int_to_int:=nil;
  1899. expectloc:=left.expectloc;
  1900. if not is_void(left.resultdef) then
  1901. begin
  1902. if (left.expectloc<>LOC_REGISTER) and
  1903. (resultdef.size>left.resultdef.size) then
  1904. expectloc:=LOC_REGISTER
  1905. else
  1906. if (left.expectloc=LOC_CREGISTER) and
  1907. (resultdef.size<left.resultdef.size) then
  1908. expectloc:=LOC_REGISTER;
  1909. end;
  1910. {$ifndef cpu64bit}
  1911. if is_64bit(resultdef) then
  1912. registersint:=max(registersint,2)
  1913. else
  1914. {$endif cpu64bit}
  1915. registersint:=max(registersint,1);
  1916. end;
  1917. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1918. begin
  1919. result:=nil;
  1920. registersint:=1;
  1921. expectloc:=LOC_REGISTER;
  1922. end;
  1923. function ttypeconvnode.first_cstring_to_int : tnode;
  1924. begin
  1925. result:=nil;
  1926. internalerror(200510014);
  1927. end;
  1928. function ttypeconvnode.first_string_to_chararray : tnode;
  1929. begin
  1930. first_string_to_chararray:=nil;
  1931. expectloc:=left.expectloc;
  1932. end;
  1933. function ttypeconvnode.first_char_to_string : tnode;
  1934. begin
  1935. first_char_to_string:=nil;
  1936. expectloc:=LOC_REFERENCE;
  1937. end;
  1938. function ttypeconvnode.first_nothing : tnode;
  1939. begin
  1940. first_nothing:=nil;
  1941. end;
  1942. function ttypeconvnode.first_array_to_pointer : tnode;
  1943. begin
  1944. first_array_to_pointer:=nil;
  1945. if registersint<1 then
  1946. registersint:=1;
  1947. expectloc:=LOC_REGISTER;
  1948. end;
  1949. function ttypeconvnode.first_int_to_real: tnode;
  1950. var
  1951. fname: string[32];
  1952. begin
  1953. if target_info.system in system_wince then
  1954. begin
  1955. { converting a 64bit integer to a float requires a helper }
  1956. if is_64bitint(left.resultdef) or
  1957. is_currency(left.resultdef) then
  1958. begin
  1959. { hack to avoid double division by 10000, as it's
  1960. already done by typecheckpass.resultdef_int_to_real }
  1961. if is_currency(left.resultdef) then
  1962. left.resultdef := s64inttype;
  1963. if is_signed(left.resultdef) then
  1964. fname:='I64TO'
  1965. else
  1966. fname:='UI64TO';
  1967. end
  1968. else
  1969. { other integers are supposed to be 32 bit }
  1970. begin
  1971. if is_signed(left.resultdef) then
  1972. fname:='ITO'
  1973. else
  1974. fname:='UTO';
  1975. firstpass(left);
  1976. end;
  1977. if tfloatdef(resultdef).floattype=s64real then
  1978. fname:=fname+'D'
  1979. else
  1980. fname:=fname+'S';
  1981. result:=ccallnode.createintern(fname,ccallparanode.create(
  1982. left,nil));
  1983. left:=nil;
  1984. firstpass(result);
  1985. exit;
  1986. end
  1987. else
  1988. begin
  1989. { converting a 64bit integer to a float requires a helper }
  1990. if is_64bitint(left.resultdef) or
  1991. is_currency(left.resultdef) then
  1992. begin
  1993. { hack to avoid double division by 10000, as it's
  1994. already done by typecheckpass.resultdef_int_to_real }
  1995. if is_currency(left.resultdef) then
  1996. left.resultdef := s64inttype;
  1997. if is_signed(left.resultdef) then
  1998. fname:='int64_to_'
  1999. else
  2000. { we can't do better currently }
  2001. fname:='int64_to_';
  2002. end
  2003. else
  2004. { other integers are supposed to be 32 bit }
  2005. begin
  2006. if is_signed(left.resultdef) then
  2007. fname:='int32_to_'
  2008. else
  2009. { we can't do better currently }
  2010. fname:='int32_to_';
  2011. firstpass(left);
  2012. end;
  2013. if tfloatdef(resultdef).floattype=s64real then
  2014. fname:=fname+'float64'
  2015. else
  2016. fname:=fname+'float32';
  2017. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2018. left,nil)),resultdef);
  2019. left:=nil;
  2020. firstpass(result);
  2021. exit;
  2022. end;
  2023. end;
  2024. function ttypeconvnode.first_real_to_real : tnode;
  2025. begin
  2026. {$ifdef cpufpemu}
  2027. if cs_fp_emulation in current_settings.moduleswitches then
  2028. begin
  2029. if target_info.system in system_wince then
  2030. begin
  2031. case tfloatdef(left.resultdef).floattype of
  2032. s32real:
  2033. case tfloatdef(resultdef).floattype of
  2034. s64real:
  2035. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2036. s32real:
  2037. begin
  2038. result:=left;
  2039. left:=nil;
  2040. end;
  2041. else
  2042. internalerror(2005082704);
  2043. end;
  2044. s64real:
  2045. case tfloatdef(resultdef).floattype of
  2046. s32real:
  2047. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2048. s64real:
  2049. begin
  2050. result:=left;
  2051. left:=nil;
  2052. end;
  2053. else
  2054. internalerror(2005082703);
  2055. end;
  2056. else
  2057. internalerror(2005082702);
  2058. end;
  2059. left:=nil;
  2060. firstpass(result);
  2061. exit;
  2062. end
  2063. else
  2064. begin
  2065. case tfloatdef(left.resultdef).floattype of
  2066. s32real:
  2067. case tfloatdef(resultdef).floattype of
  2068. s64real:
  2069. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2070. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2071. s32real:
  2072. begin
  2073. result:=left;
  2074. left:=nil;
  2075. end;
  2076. else
  2077. internalerror(200610151);
  2078. end;
  2079. s64real:
  2080. case tfloatdef(resultdef).floattype of
  2081. s32real:
  2082. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2083. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2084. s64real:
  2085. begin
  2086. result:=left;
  2087. left:=nil;
  2088. end;
  2089. else
  2090. internalerror(200610152);
  2091. end;
  2092. else
  2093. internalerror(200610153);
  2094. end;
  2095. left:=nil;
  2096. firstpass(result);
  2097. exit;
  2098. end;
  2099. end
  2100. else
  2101. {$endif cpufpemu}
  2102. begin
  2103. first_real_to_real:=nil;
  2104. if registersfpu<1 then
  2105. registersfpu:=1;
  2106. expectloc:=LOC_FPUREGISTER;
  2107. end;
  2108. end;
  2109. function ttypeconvnode.first_pointer_to_array : tnode;
  2110. begin
  2111. first_pointer_to_array:=nil;
  2112. if registersint<1 then
  2113. registersint:=1;
  2114. expectloc:=LOC_REFERENCE;
  2115. end;
  2116. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2117. begin
  2118. first_cchar_to_pchar:=nil;
  2119. internalerror(200104021);
  2120. end;
  2121. function ttypeconvnode.first_bool_to_int : tnode;
  2122. begin
  2123. first_bool_to_int:=nil;
  2124. { byte(boolean) or word(wordbool) or longint(longbool) must
  2125. be accepted for var parameters }
  2126. if (nf_explicit in flags) and
  2127. (left.resultdef.size=resultdef.size) and
  2128. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2129. exit;
  2130. { when converting to 64bit, first convert to a 32bit int and then }
  2131. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2132. if resultdef.size > sizeof(aint) then
  2133. begin
  2134. result := ctypeconvnode.create_internal(left,u32inttype);
  2135. result := ctypeconvnode.create(result,resultdef);
  2136. left := nil;
  2137. firstpass(result);
  2138. exit;
  2139. end;
  2140. expectloc:=LOC_REGISTER;
  2141. if registersint<1 then
  2142. registersint:=1;
  2143. end;
  2144. function ttypeconvnode.first_int_to_bool : tnode;
  2145. begin
  2146. first_int_to_bool:=nil;
  2147. { byte(boolean) or word(wordbool) or longint(longbool) must
  2148. be accepted for var parameters }
  2149. if (nf_explicit in flags) and
  2150. (left.resultdef.size=resultdef.size) and
  2151. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2152. exit;
  2153. expectloc:=LOC_REGISTER;
  2154. { need if bool to bool !!
  2155. not very nice !!
  2156. insertypeconv(left,s32inttype);
  2157. left.explizit:=true;
  2158. firstpass(left); }
  2159. if registersint<1 then
  2160. registersint:=1;
  2161. end;
  2162. function ttypeconvnode.first_bool_to_bool : tnode;
  2163. begin
  2164. first_bool_to_bool:=nil;
  2165. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2166. expectloc := left.expectloc
  2167. else
  2168. begin
  2169. expectloc:=LOC_REGISTER;
  2170. if registersint<1 then
  2171. registersint:=1;
  2172. end;
  2173. end;
  2174. function ttypeconvnode.first_char_to_char : tnode;
  2175. var
  2176. fname: string[18];
  2177. begin
  2178. if (torddef(resultdef).ordtype=uchar) and
  2179. (torddef(left.resultdef).ordtype=uwidechar) then
  2180. fname := 'fpc_wchar_to_char'
  2181. else if (torddef(resultdef).ordtype=uwidechar) and
  2182. (torddef(left.resultdef).ordtype=uchar) then
  2183. fname := 'fpc_char_to_wchar'
  2184. else
  2185. internalerror(2007081201);
  2186. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2187. left:=nil;
  2188. firstpass(result);
  2189. end;
  2190. function ttypeconvnode.first_proc_to_procvar : tnode;
  2191. begin
  2192. first_proc_to_procvar:=nil;
  2193. { if we take the address of a nested function, it'll }
  2194. { probably be used in a foreach() construct and then }
  2195. { the parent needs a stackframe }
  2196. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  2197. include(current_procinfo.flags,pi_needs_stackframe);
  2198. if tabstractprocdef(resultdef).is_addressonly then
  2199. begin
  2200. registersint:=left.registersint;
  2201. if registersint<1 then
  2202. registersint:=1;
  2203. expectloc:=LOC_REGISTER;
  2204. end
  2205. else
  2206. begin
  2207. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2208. CGMessage(parser_e_illegal_expression);
  2209. registersint:=left.registersint;
  2210. expectloc:=left.expectloc
  2211. end
  2212. end;
  2213. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2214. begin
  2215. first_nil_to_methodprocvar:=nil;
  2216. registersint:=0;
  2217. expectloc:=LOC_REFERENCE;
  2218. end;
  2219. function ttypeconvnode.first_set_to_set : tnode;
  2220. var
  2221. newstatement : tstatementnode;
  2222. temp : ttempcreatenode;
  2223. begin
  2224. { in theory, we should do range checking here,
  2225. but Delphi doesn't do it either (FK) }
  2226. if left.nodetype=setconstn then
  2227. begin
  2228. left.resultdef:=resultdef;
  2229. result:=left;
  2230. end
  2231. { equal sets for the code generator? }
  2232. else if (left.resultdef.size=resultdef.size) and
  2233. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2234. {$warning This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2235. { e.g., this prints true because of this:
  2236. var
  2237. sa: set of 1..2;
  2238. sb: set of 5..6;
  2239. b: byte;
  2240. begin
  2241. b:=1;
  2242. sa:=[1..2];
  2243. sb:=sa;
  2244. writeln(b in sb);
  2245. end.
  2246. }
  2247. result:=left
  2248. else
  2249. // if is_varset(resultdef) then
  2250. begin
  2251. result:=internalstatements(newstatement);
  2252. { in case left is a smallset expression, it can be an addn or so. }
  2253. { fpc_varset_load expects a formal const parameter, which doesn't }
  2254. { accept set addn's -> assign to a temp first and pass the temp }
  2255. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2256. begin
  2257. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2258. addstatement(newstatement,temp);
  2259. { temp := left }
  2260. addstatement(newstatement,cassignmentnode.create(
  2261. ctemprefnode.create(temp),left));
  2262. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2263. addstatement(newstatement,ctemprefnode.create(temp));
  2264. left:=result;
  2265. firstpass(left);
  2266. { recreate the result's internalstatements list }
  2267. result:=internalstatements(newstatement);
  2268. end;
  2269. { create temp for result }
  2270. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2271. addstatement(newstatement,temp);
  2272. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2273. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2274. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2275. ccallparanode.create(ctemprefnode.create(temp),
  2276. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2277. ccallparanode.create(left,nil))))))
  2278. );
  2279. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2280. addstatement(newstatement,ctemprefnode.create(temp));
  2281. end;
  2282. {
  2283. else
  2284. begin
  2285. srsym:=search_system_type('FPC_SMALL_SET');
  2286. result :=
  2287. ccallnode.createinternres('fpc_set_load_small',
  2288. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.typedef),nil),resultdef);
  2289. end;
  2290. }
  2291. { reused }
  2292. left:=nil;
  2293. end;
  2294. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2295. begin
  2296. first_ansistring_to_pchar:=nil;
  2297. expectloc:=LOC_REGISTER;
  2298. if registersint<1 then
  2299. registersint:=1;
  2300. end;
  2301. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2302. begin
  2303. first_arrayconstructor_to_set:=nil;
  2304. internalerror(200104022);
  2305. end;
  2306. function ttypeconvnode.first_class_to_intf : tnode;
  2307. begin
  2308. first_class_to_intf:=nil;
  2309. expectloc:=LOC_REGISTER;
  2310. if registersint<1 then
  2311. registersint:=1;
  2312. end;
  2313. function ttypeconvnode._first_int_to_int : tnode;
  2314. begin
  2315. result:=first_int_to_int;
  2316. end;
  2317. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2318. begin
  2319. result:=first_cstring_to_pchar;
  2320. end;
  2321. function ttypeconvnode._first_cstring_to_int : tnode;
  2322. begin
  2323. result:=first_cstring_to_int;
  2324. end;
  2325. function ttypeconvnode._first_string_to_chararray : tnode;
  2326. begin
  2327. result:=first_string_to_chararray;
  2328. end;
  2329. function ttypeconvnode._first_char_to_string : tnode;
  2330. begin
  2331. result:=first_char_to_string;
  2332. end;
  2333. function ttypeconvnode._first_nothing : tnode;
  2334. begin
  2335. result:=first_nothing;
  2336. end;
  2337. function ttypeconvnode._first_array_to_pointer : tnode;
  2338. begin
  2339. result:=first_array_to_pointer;
  2340. end;
  2341. function ttypeconvnode._first_int_to_real : tnode;
  2342. begin
  2343. result:=first_int_to_real;
  2344. end;
  2345. function ttypeconvnode._first_real_to_real : tnode;
  2346. begin
  2347. result:=first_real_to_real;
  2348. end;
  2349. function ttypeconvnode._first_pointer_to_array : tnode;
  2350. begin
  2351. result:=first_pointer_to_array;
  2352. end;
  2353. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2354. begin
  2355. result:=first_cchar_to_pchar;
  2356. end;
  2357. function ttypeconvnode._first_bool_to_int : tnode;
  2358. begin
  2359. result:=first_bool_to_int;
  2360. end;
  2361. function ttypeconvnode._first_int_to_bool : tnode;
  2362. begin
  2363. result:=first_int_to_bool;
  2364. end;
  2365. function ttypeconvnode._first_bool_to_bool : tnode;
  2366. begin
  2367. result:=first_bool_to_bool;
  2368. end;
  2369. function ttypeconvnode._first_proc_to_procvar : tnode;
  2370. begin
  2371. result:=first_proc_to_procvar;
  2372. end;
  2373. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2374. begin
  2375. result:=first_nil_to_methodprocvar;
  2376. end;
  2377. function ttypeconvnode._first_set_to_set : tnode;
  2378. begin
  2379. result:=first_set_to_set;
  2380. end;
  2381. function ttypeconvnode._first_cord_to_pointer : tnode;
  2382. begin
  2383. result:=first_cord_to_pointer;
  2384. end;
  2385. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2386. begin
  2387. result:=first_ansistring_to_pchar;
  2388. end;
  2389. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2390. begin
  2391. result:=first_arrayconstructor_to_set;
  2392. end;
  2393. function ttypeconvnode._first_class_to_intf : tnode;
  2394. begin
  2395. result:=first_class_to_intf;
  2396. end;
  2397. function ttypeconvnode._first_char_to_char : tnode;
  2398. begin
  2399. result:=first_char_to_char;
  2400. end;
  2401. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2402. const
  2403. firstconvert : array[tconverttype] of pointer = (
  2404. nil, { none }
  2405. @ttypeconvnode._first_nothing, {equal}
  2406. @ttypeconvnode._first_nothing, {not_possible}
  2407. nil, { removed in typecheck_string_to_string }
  2408. @ttypeconvnode._first_char_to_string,
  2409. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2410. nil, { removed in typecheck_chararray_to_string }
  2411. @ttypeconvnode._first_cchar_to_pchar,
  2412. @ttypeconvnode._first_cstring_to_pchar,
  2413. @ttypeconvnode._first_cstring_to_int,
  2414. @ttypeconvnode._first_ansistring_to_pchar,
  2415. @ttypeconvnode._first_string_to_chararray,
  2416. nil, { removed in typecheck_chararray_to_string }
  2417. @ttypeconvnode._first_array_to_pointer,
  2418. @ttypeconvnode._first_pointer_to_array,
  2419. @ttypeconvnode._first_int_to_int,
  2420. @ttypeconvnode._first_int_to_bool,
  2421. @ttypeconvnode._first_bool_to_bool,
  2422. @ttypeconvnode._first_bool_to_int,
  2423. @ttypeconvnode._first_real_to_real,
  2424. @ttypeconvnode._first_int_to_real,
  2425. nil, { removed in typecheck_real_to_currency }
  2426. @ttypeconvnode._first_proc_to_procvar,
  2427. @ttypeconvnode._first_nil_to_methodprocvar,
  2428. @ttypeconvnode._first_arrayconstructor_to_set,
  2429. @ttypeconvnode._first_set_to_set,
  2430. @ttypeconvnode._first_cord_to_pointer,
  2431. @ttypeconvnode._first_nothing,
  2432. @ttypeconvnode._first_nothing,
  2433. @ttypeconvnode._first_class_to_intf,
  2434. @ttypeconvnode._first_char_to_char,
  2435. @ttypeconvnode._first_nothing,
  2436. @ttypeconvnode._first_nothing,
  2437. nil,
  2438. nil,
  2439. nil,
  2440. nil,
  2441. nil,
  2442. nil,
  2443. nil
  2444. );
  2445. type
  2446. tprocedureofobject = function : tnode of object;
  2447. var
  2448. r : packed record
  2449. proc : pointer;
  2450. obj : pointer;
  2451. end;
  2452. begin
  2453. { this is a little bit dirty but it works }
  2454. { and should be quite portable too }
  2455. r.proc:=firstconvert[c];
  2456. r.obj:=self;
  2457. if not assigned(r.proc) then
  2458. internalerror(200312081);
  2459. first_call_helper:=tprocedureofobject(r)()
  2460. end;
  2461. function ttypeconvnode.pass_1 : tnode;
  2462. begin
  2463. if warn_pointer_to_signed then
  2464. cgmessage(type_w_pointer_to_signed);
  2465. result:=nil;
  2466. firstpass(left);
  2467. if codegenerror then
  2468. exit;
  2469. { load the value_str from the left part }
  2470. registersint:=left.registersint;
  2471. registersfpu:=left.registersfpu;
  2472. {$ifdef SUPPORT_MMX}
  2473. registersmmx:=left.registersmmx;
  2474. {$endif}
  2475. expectloc:=left.expectloc;
  2476. result:=first_call_helper(convtype);
  2477. end;
  2478. function ttypeconvnode.assign_allowed:boolean;
  2479. begin
  2480. result:=(convtype=tc_equal) or
  2481. { typecasting from void is always allowed }
  2482. is_void(left.resultdef) or
  2483. (left.resultdef.typ=formaldef) or
  2484. { int 2 int with same size reuses same location, or for
  2485. tp7 mode also allow size < orignal size }
  2486. (
  2487. (convtype=tc_int_2_int) and
  2488. (
  2489. (resultdef.size=left.resultdef.size) or
  2490. ((m_tp7 in current_settings.modeswitches) and
  2491. (resultdef.size<left.resultdef.size))
  2492. )
  2493. ) or
  2494. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2495. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2496. (nf_explicit in flags) and
  2497. (resultdef.size=left.resultdef.size));
  2498. { When using only a part of the value it can't be in a register since
  2499. that will load the value in a new register first }
  2500. if (resultdef.size<left.resultdef.size) then
  2501. make_not_regable(left,[ra_addr_regable]);
  2502. end;
  2503. function ttypeconvnode.docompare(p: tnode) : boolean;
  2504. begin
  2505. docompare :=
  2506. inherited docompare(p) and
  2507. (convtype = ttypeconvnode(p).convtype);
  2508. end;
  2509. procedure ttypeconvnode._second_int_to_int;
  2510. begin
  2511. second_int_to_int;
  2512. end;
  2513. procedure ttypeconvnode._second_string_to_string;
  2514. begin
  2515. second_string_to_string;
  2516. end;
  2517. procedure ttypeconvnode._second_cstring_to_pchar;
  2518. begin
  2519. second_cstring_to_pchar;
  2520. end;
  2521. procedure ttypeconvnode._second_cstring_to_int;
  2522. begin
  2523. second_cstring_to_int;
  2524. end;
  2525. procedure ttypeconvnode._second_string_to_chararray;
  2526. begin
  2527. second_string_to_chararray;
  2528. end;
  2529. procedure ttypeconvnode._second_array_to_pointer;
  2530. begin
  2531. second_array_to_pointer;
  2532. end;
  2533. procedure ttypeconvnode._second_pointer_to_array;
  2534. begin
  2535. second_pointer_to_array;
  2536. end;
  2537. procedure ttypeconvnode._second_chararray_to_string;
  2538. begin
  2539. second_chararray_to_string;
  2540. end;
  2541. procedure ttypeconvnode._second_char_to_string;
  2542. begin
  2543. second_char_to_string;
  2544. end;
  2545. procedure ttypeconvnode._second_int_to_real;
  2546. begin
  2547. second_int_to_real;
  2548. end;
  2549. procedure ttypeconvnode._second_real_to_real;
  2550. begin
  2551. second_real_to_real;
  2552. end;
  2553. procedure ttypeconvnode._second_cord_to_pointer;
  2554. begin
  2555. second_cord_to_pointer;
  2556. end;
  2557. procedure ttypeconvnode._second_proc_to_procvar;
  2558. begin
  2559. second_proc_to_procvar;
  2560. end;
  2561. procedure ttypeconvnode._second_nil_to_methodprocvar;
  2562. begin
  2563. second_nil_to_methodprocvar;
  2564. end;
  2565. procedure ttypeconvnode._second_bool_to_int;
  2566. begin
  2567. second_bool_to_int;
  2568. end;
  2569. procedure ttypeconvnode._second_int_to_bool;
  2570. begin
  2571. second_int_to_bool;
  2572. end;
  2573. procedure ttypeconvnode._second_bool_to_bool;
  2574. begin
  2575. second_bool_to_bool;
  2576. end;
  2577. procedure ttypeconvnode._second_set_to_set;
  2578. begin
  2579. second_set_to_set;
  2580. end;
  2581. procedure ttypeconvnode._second_ansistring_to_pchar;
  2582. begin
  2583. second_ansistring_to_pchar;
  2584. end;
  2585. procedure ttypeconvnode._second_class_to_intf;
  2586. begin
  2587. second_class_to_intf;
  2588. end;
  2589. procedure ttypeconvnode._second_char_to_char;
  2590. begin
  2591. second_char_to_char;
  2592. end;
  2593. procedure ttypeconvnode._second_nothing;
  2594. begin
  2595. second_nothing;
  2596. end;
  2597. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2598. const
  2599. secondconvert : array[tconverttype] of pointer = (
  2600. @ttypeconvnode._second_nothing, {none}
  2601. @ttypeconvnode._second_nothing, {equal}
  2602. @ttypeconvnode._second_nothing, {not_possible}
  2603. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2604. @ttypeconvnode._second_char_to_string,
  2605. @ttypeconvnode._second_nothing, {char_to_charray}
  2606. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2607. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2608. @ttypeconvnode._second_cstring_to_pchar,
  2609. @ttypeconvnode._second_cstring_to_int,
  2610. @ttypeconvnode._second_ansistring_to_pchar,
  2611. @ttypeconvnode._second_string_to_chararray,
  2612. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2613. @ttypeconvnode._second_array_to_pointer,
  2614. @ttypeconvnode._second_pointer_to_array,
  2615. @ttypeconvnode._second_int_to_int,
  2616. @ttypeconvnode._second_int_to_bool,
  2617. @ttypeconvnode._second_bool_to_bool,
  2618. @ttypeconvnode._second_bool_to_int,
  2619. @ttypeconvnode._second_real_to_real,
  2620. @ttypeconvnode._second_int_to_real,
  2621. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2622. @ttypeconvnode._second_proc_to_procvar,
  2623. @ttypeconvnode._second_nil_to_methodprocvar,
  2624. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2625. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  2626. @ttypeconvnode._second_cord_to_pointer,
  2627. @ttypeconvnode._second_nothing, { interface 2 string }
  2628. @ttypeconvnode._second_nothing, { interface 2 guid }
  2629. @ttypeconvnode._second_class_to_intf,
  2630. @ttypeconvnode._second_char_to_char,
  2631. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2632. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2633. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2634. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2635. @ttypeconvnode._second_nothing, { variant_2_enum }
  2636. @ttypeconvnode._second_nothing, { enum_2_variant }
  2637. @ttypeconvnode._second_nothing, { variant_2_interface }
  2638. @ttypeconvnode._second_nothing, { interface_2_variant }
  2639. @ttypeconvnode._second_nothing { array_2_dynarray }
  2640. );
  2641. type
  2642. tprocedureofobject = procedure of object;
  2643. var
  2644. r : packed record
  2645. proc : pointer;
  2646. obj : pointer;
  2647. end;
  2648. begin
  2649. { this is a little bit dirty but it works }
  2650. { and should be quite portable too }
  2651. r.proc:=secondconvert[c];
  2652. r.obj:=self;
  2653. tprocedureofobject(r)();
  2654. end;
  2655. {*****************************************************************************
  2656. TISNODE
  2657. *****************************************************************************}
  2658. constructor tisnode.create(l,r : tnode);
  2659. begin
  2660. inherited create(isn,l,r);
  2661. end;
  2662. function tisnode.pass_typecheck:tnode;
  2663. var
  2664. paras: tcallparanode;
  2665. begin
  2666. result:=nil;
  2667. typecheckpass(left);
  2668. typecheckpass(right);
  2669. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2670. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2671. if codegenerror then
  2672. exit;
  2673. if (right.resultdef.typ=classrefdef) then
  2674. begin
  2675. { left must be a class }
  2676. if is_class(left.resultdef) then
  2677. begin
  2678. { the operands must be related }
  2679. if (not(tobjectdef(left.resultdef).is_related(
  2680. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2681. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2682. tobjectdef(left.resultdef)))) then
  2683. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2684. tclassrefdef(right.resultdef).pointeddef.typename);
  2685. end
  2686. else
  2687. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2688. { call fpc_do_is helper }
  2689. paras := ccallparanode.create(
  2690. left,
  2691. ccallparanode.create(
  2692. right,nil));
  2693. result := ccallnode.createintern('fpc_do_is',paras);
  2694. left := nil;
  2695. right := nil;
  2696. end
  2697. else if is_interface(right.resultdef) then
  2698. begin
  2699. { left is a class }
  2700. if is_class(left.resultdef) then
  2701. begin
  2702. { the class must implement the interface }
  2703. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2704. CGMessage2(type_e_classes_not_related,
  2705. FullTypeName(left.resultdef,right.resultdef),
  2706. FullTypeName(right.resultdef,left.resultdef))
  2707. end
  2708. { left is an interface }
  2709. else if is_interface(left.resultdef) then
  2710. begin
  2711. { the operands must be related }
  2712. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2713. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2714. CGMessage2(type_e_classes_not_related,
  2715. FullTypeName(left.resultdef,right.resultdef),
  2716. FullTypeName(right.resultdef,left.resultdef));
  2717. end
  2718. else
  2719. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2720. { call fpc_do_is helper }
  2721. paras := ccallparanode.create(
  2722. left,
  2723. ccallparanode.create(
  2724. right,nil));
  2725. result := ccallnode.createintern('fpc_do_is',paras);
  2726. left := nil;
  2727. right := nil;
  2728. end
  2729. else
  2730. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2731. resultdef:=booltype;
  2732. end;
  2733. function tisnode.pass_1 : tnode;
  2734. begin
  2735. internalerror(200204254);
  2736. result:=nil;
  2737. end;
  2738. { dummy pass_2, it will never be called, but we need one since }
  2739. { you can't instantiate an abstract class }
  2740. procedure tisnode.pass_generate_code;
  2741. begin
  2742. end;
  2743. {*****************************************************************************
  2744. TASNODE
  2745. *****************************************************************************}
  2746. constructor tasnode.create(l,r : tnode);
  2747. begin
  2748. inherited create(asn,l,r);
  2749. call := nil;
  2750. end;
  2751. destructor tasnode.destroy;
  2752. begin
  2753. call.free;
  2754. inherited destroy;
  2755. end;
  2756. function tasnode.pass_typecheck:tnode;
  2757. var
  2758. hp : tnode;
  2759. begin
  2760. result:=nil;
  2761. typecheckpass(right);
  2762. typecheckpass(left);
  2763. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2764. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2765. if codegenerror then
  2766. exit;
  2767. if (right.resultdef.typ=classrefdef) then
  2768. begin
  2769. { left must be a class }
  2770. if is_class(left.resultdef) then
  2771. begin
  2772. { the operands must be related }
  2773. if (not(tobjectdef(left.resultdef).is_related(
  2774. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2775. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2776. tobjectdef(left.resultdef)))) then
  2777. CGMessage2(type_e_classes_not_related,
  2778. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2779. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2780. end
  2781. else
  2782. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2783. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2784. end
  2785. else if is_interface(right.resultdef) then
  2786. begin
  2787. { left is a class }
  2788. if not(is_class(left.resultdef) or
  2789. is_interfacecom(left.resultdef)) then
  2790. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2791. resultdef:=right.resultdef;
  2792. { load the GUID of the interface }
  2793. if (right.nodetype=typen) then
  2794. begin
  2795. if assigned(tobjectdef(right.resultdef).iidguid) then
  2796. begin
  2797. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  2798. CGMessage1(type_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  2799. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  2800. right.free;
  2801. right:=hp;
  2802. end
  2803. else
  2804. internalerror(200206282);
  2805. typecheckpass(right);
  2806. end;
  2807. end
  2808. else
  2809. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2810. end;
  2811. function tasnode.dogetcopy: tnode;
  2812. begin
  2813. result := inherited dogetcopy;
  2814. if assigned(call) then
  2815. tasnode(result).call := call.getcopy
  2816. else
  2817. tasnode(result).call := nil;
  2818. end;
  2819. function tasnode.pass_1 : tnode;
  2820. var
  2821. procname: string;
  2822. begin
  2823. result:=nil;
  2824. if not assigned(call) then
  2825. begin
  2826. if is_class(left.resultdef) and
  2827. (right.resultdef.typ=classrefdef) then
  2828. call := ccallnode.createinternres('fpc_do_as',
  2829. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2830. resultdef)
  2831. else
  2832. begin
  2833. if is_class(left.resultdef) then
  2834. procname := 'fpc_class_as_intf'
  2835. else
  2836. procname := 'fpc_intf_as';
  2837. call := ccallnode.createintern(procname,
  2838. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2839. call := ctypeconvnode.create_internal(call,resultdef);
  2840. end;
  2841. left := nil;
  2842. right := nil;
  2843. firstpass(call);
  2844. if codegenerror then
  2845. exit;
  2846. expectloc:=call.expectloc;
  2847. registersint:=call.registersint;
  2848. registersfpu:=call.registersfpu;
  2849. {$ifdef SUPPORT_MMX}
  2850. registersmmx:=call.registersmmx;
  2851. {$endif SUPPORT_MMX}
  2852. end;
  2853. end;
  2854. begin
  2855. ctypeconvnode:=ttypeconvnode;
  2856. casnode:=tasnode;
  2857. cisnode:=tisnode;
  2858. end.