ncnv.pas 141 KB

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