ncnv.pas 167 KB

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