ncnv.pas 143 KB

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