ncnv.pas 169 KB

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