ncnv.pas 138 KB

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