ncnv.pas 129 KB

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