ncnv.pas 136 KB

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