ncnv.pas 153 KB

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