ncnv.pas 140 KB

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