ncnv.pas 143 KB

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