2
0

ncnv.pas 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152
  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,blockutl,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(caddrnode.create_internal(crttinode.create(tstoreddef(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(caddrnode.create_internal(crttinode.create(tstoreddef(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(caddrnode.create_internal
  1623. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1624. ccallparanode.create(
  1625. ctypeconvnode.create_internal(
  1626. ctemprefnode.create(temp),voidpointertype),
  1627. nil))))
  1628. ));
  1629. addstatement(newstatement,ctempdeletenode.create(temp2));
  1630. { copy ... }
  1631. addstatement(newstatement,cassignmentnode.create(
  1632. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1633. left
  1634. ));
  1635. { left is reused }
  1636. left:=nil;
  1637. { the last statement should return the value as
  1638. location and type, this is done be referencing the
  1639. temp and converting it first from a persistent temp to
  1640. normal temp }
  1641. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1642. addstatement(newstatement,ctemprefnode.create(temp));
  1643. end;
  1644. function ttypeconvnode.typecheck_elem_2_openarray : tnode;
  1645. begin
  1646. result:=nil;
  1647. end;
  1648. function ttypeconvnode._typecheck_int_to_int : tnode;
  1649. begin
  1650. result := typecheck_int_to_int;
  1651. end;
  1652. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  1653. begin
  1654. result := typecheck_cord_to_pointer;
  1655. end;
  1656. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  1657. begin
  1658. result := typecheck_chararray_to_string;
  1659. end;
  1660. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  1661. begin
  1662. result := typecheck_string_to_chararray;
  1663. end;
  1664. function ttypeconvnode._typecheck_string_to_string: tnode;
  1665. begin
  1666. result := typecheck_string_to_string;
  1667. end;
  1668. function ttypeconvnode._typecheck_char_to_string : tnode;
  1669. begin
  1670. result := typecheck_char_to_string;
  1671. end;
  1672. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  1673. begin
  1674. result := typecheck_char_to_chararray;
  1675. end;
  1676. function ttypeconvnode._typecheck_int_to_real : tnode;
  1677. begin
  1678. result := typecheck_int_to_real;
  1679. end;
  1680. function ttypeconvnode._typecheck_real_to_real : tnode;
  1681. begin
  1682. result := typecheck_real_to_real;
  1683. end;
  1684. function ttypeconvnode._typecheck_real_to_currency : tnode;
  1685. begin
  1686. result := typecheck_real_to_currency;
  1687. end;
  1688. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  1689. begin
  1690. result := typecheck_cchar_to_pchar;
  1691. end;
  1692. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  1693. begin
  1694. result := typecheck_cstring_to_pchar;
  1695. end;
  1696. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  1697. begin
  1698. result := typecheck_cstring_to_int;
  1699. end;
  1700. function ttypeconvnode._typecheck_char_to_char : tnode;
  1701. begin
  1702. result := typecheck_char_to_char;
  1703. end;
  1704. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  1705. begin
  1706. result := typecheck_arrayconstructor_to_set;
  1707. end;
  1708. function ttypeconvnode._typecheck_set_to_set : tnode;
  1709. begin
  1710. result := typecheck_set_to_set;
  1711. end;
  1712. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  1713. begin
  1714. result := typecheck_pchar_to_string;
  1715. end;
  1716. function ttypeconvnode._typecheck_interface_to_string : tnode;
  1717. begin
  1718. result := typecheck_interface_to_string;
  1719. end;
  1720. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  1721. begin
  1722. result := typecheck_interface_to_guid;
  1723. end;
  1724. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  1725. begin
  1726. result := typecheck_dynarray_to_openarray;
  1727. end;
  1728. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  1729. begin
  1730. result := typecheck_pwchar_to_string;
  1731. end;
  1732. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  1733. begin
  1734. result := typecheck_variant_to_dynarray;
  1735. end;
  1736. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  1737. begin
  1738. result := typecheck_dynarray_to_variant;
  1739. end;
  1740. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  1741. begin
  1742. result := typecheck_variant_to_enum;
  1743. end;
  1744. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  1745. begin
  1746. result := typecheck_enum_to_variant;
  1747. end;
  1748. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  1749. begin
  1750. result := typecheck_proc_to_procvar;
  1751. end;
  1752. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  1753. begin
  1754. result := typecheck_variant_to_interface;
  1755. end;
  1756. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  1757. begin
  1758. result := typecheck_interface_to_variant;
  1759. end;
  1760. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  1761. begin
  1762. result := typecheck_array_2_dynarray;
  1763. end;
  1764. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  1765. begin
  1766. result := typecheck_elem_2_openarray;
  1767. end;
  1768. function ttypeconvnode.target_specific_general_typeconv: boolean;
  1769. begin
  1770. result:=false;
  1771. end;
  1772. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  1773. begin
  1774. result:=false;
  1775. end;
  1776. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1777. var
  1778. pd : tabstractprocdef;
  1779. copytype : tproccopytyp;
  1780. source: pnode;
  1781. begin
  1782. result:=nil;
  1783. pd:=tabstractprocdef(left.resultdef);
  1784. { create procvardef (default for create_proc_to_procvar is voiddef,
  1785. but if later a regular inserttypeconvnode() is used to insert a type
  1786. conversion to the actual procvardef, totypedef will be set to the
  1787. real procvartype that we are converting to) }
  1788. if assigned(totypedef) and
  1789. (totypedef.typ=procvardef) then
  1790. begin
  1791. { have to do this in typecheckpass so that it's triggered for
  1792. typed constant declarations }
  1793. if po_is_block in tprocvardef(totypedef).procoptions then
  1794. begin
  1795. { can only convert from procdef to procvardef, but in the mean
  1796. time other type conversions may have been inserted (pointers,
  1797. proc2procvar, ...) }
  1798. source:=actualtargetnode(@left);
  1799. while (source^.nodetype=typeconvn) and
  1800. (ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
  1801. (is_void(source^.resultdef) or
  1802. (source^.resultdef.typ=procvardef)) do
  1803. begin
  1804. { won't skip proc2procvar }
  1805. source:=actualtargetnode(@ttypeconvnode(source^).left);
  1806. end;
  1807. if (source^.nodetype=loadn) and
  1808. (source^.resultdef.typ=procdef) and
  1809. not is_nested_pd(tprocdef(source^.resultdef)) and
  1810. not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
  1811. begin
  1812. result:=generate_block_for_procaddr(tloadnode(source^));
  1813. exit;
  1814. end
  1815. else
  1816. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1817. end;
  1818. resultdef:=totypedef;
  1819. end
  1820. else
  1821. begin
  1822. { only need the address of the method? this is needed
  1823. for @tobject.create. In this case there will be a loadn without
  1824. a methodpointer. }
  1825. if (left.nodetype=loadn) and
  1826. not assigned(tloadnode(left).left) and
  1827. (not(m_nested_procvars in current_settings.modeswitches) or
  1828. not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
  1829. copytype:=pc_address_only
  1830. else
  1831. copytype:=pc_normal;
  1832. resultdef:=pd.getcopyas(procvardef,copytype);
  1833. end;
  1834. end;
  1835. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1836. const
  1837. resultdefconvert : array[tconverttype] of pointer = (
  1838. {none} nil,
  1839. {equal} nil,
  1840. {not_possible} nil,
  1841. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  1842. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  1843. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  1844. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  1845. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  1846. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  1847. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  1848. { ansistring_2_pchar } nil,
  1849. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  1850. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  1851. { array_2_pointer } nil,
  1852. { pointer_2_array } nil,
  1853. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  1854. { int_2_bool } nil,
  1855. { bool_2_bool } nil,
  1856. { bool_2_int } nil,
  1857. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  1858. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  1859. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  1860. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  1861. { nil_2_methodprocvar } nil,
  1862. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  1863. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  1864. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  1865. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  1866. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  1867. { class_2_intf } nil,
  1868. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  1869. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  1870. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  1871. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  1872. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  1873. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  1874. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  1875. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  1876. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  1877. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  1878. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray
  1879. );
  1880. type
  1881. tprocedureofobject = function : tnode of object;
  1882. var
  1883. r : packed record
  1884. proc : pointer;
  1885. obj : pointer;
  1886. end;
  1887. begin
  1888. result:=nil;
  1889. { this is a little bit dirty but it works }
  1890. { and should be quite portable too }
  1891. r.proc:=resultdefconvert[c];
  1892. r.obj:=self;
  1893. if assigned(r.proc) then
  1894. result:=tprocedureofobject(r)();
  1895. end;
  1896. function ttypeconvnode.pass_typecheck:tnode;
  1897. var
  1898. hdef : tdef;
  1899. hp : tnode;
  1900. currprocdef : tabstractprocdef;
  1901. aprocdef : tprocdef;
  1902. eq : tequaltype;
  1903. cdoptions : tcompare_defs_options;
  1904. newblock: tblocknode;
  1905. newstatement: tstatementnode;
  1906. tempnode: ttempcreatenode;
  1907. begin
  1908. result:=nil;
  1909. resultdef:=totypedef;
  1910. typecheckpass(left);
  1911. if codegenerror then
  1912. exit;
  1913. { When absolute force tc_equal }
  1914. if (nf_absolute in flags) then
  1915. begin
  1916. convtype:=tc_equal;
  1917. if (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  1918. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) then
  1919. make_not_regable(left,[ra_addr_regable]);
  1920. exit;
  1921. end;
  1922. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1923. convert on the procvar value. This is used to access the
  1924. fields of a methodpointer }
  1925. if not(nf_load_procvar in flags) and
  1926. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1927. maybe_call_procvar(left,true);
  1928. { convert array constructors to sets, because there is no conversion
  1929. possible for array constructors }
  1930. if (resultdef.typ<>arraydef) and
  1931. is_array_constructor(left.resultdef) then
  1932. begin
  1933. arrayconstructor_to_set(left);
  1934. typecheckpass(left);
  1935. end;
  1936. if target_specific_general_typeconv then
  1937. exit;
  1938. if convtype=tc_none then
  1939. begin
  1940. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  1941. { overloaded operators require calls, which is not possible inside
  1942. a constant declaration }
  1943. if (block_type<>bt_const) and
  1944. not(nf_internal in flags) then
  1945. include(cdoptions,cdo_check_operator);
  1946. if nf_explicit in flags then
  1947. include(cdoptions,cdo_explicit);
  1948. if nf_internal in flags then
  1949. include(cdoptions,cdo_internal);
  1950. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1951. case eq of
  1952. te_exact,
  1953. te_equal :
  1954. begin
  1955. result := simplify(false);
  1956. if assigned(result) then
  1957. exit;
  1958. { in case of bitpacked accesses, the original type must
  1959. remain so that not too many/few bits are laoded }
  1960. if is_bitpacked_access(left) then
  1961. convtype:=tc_int_2_int;
  1962. { Only leave when there is no conversion to do.
  1963. We can still need to call a conversion routine,
  1964. like the routine to convert a stringconstnode }
  1965. if (convtype in [tc_equal,tc_not_possible]) and
  1966. { some conversions, like dynarray to pointer in Delphi
  1967. mode, must not be removed, because then we get memory
  1968. leaks due to missing temp finalization }
  1969. (not is_managed_type(left.resultdef) or
  1970. { different kinds of refcounted types may need calls
  1971. to different kinds of refcounting helpers }
  1972. (resultdef=left.resultdef)) then
  1973. begin
  1974. {$ifndef llvm}
  1975. left.resultdef:=resultdef;
  1976. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1977. include(left.flags, nf_typedaddr);
  1978. result:=left;
  1979. left:=nil;
  1980. {$else llvm}
  1981. { we still may have to insert a type conversion at the
  1982. llvm level }
  1983. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1984. include(flags, nf_typedaddr);
  1985. result:=nil;
  1986. {$endif llvm}
  1987. exit;
  1988. end;
  1989. end;
  1990. te_convert_l1,
  1991. te_convert_l2,
  1992. te_convert_l3,
  1993. te_convert_l4,
  1994. te_convert_l5,
  1995. te_convert_l6:
  1996. { nothing to do }
  1997. ;
  1998. te_convert_operator :
  1999. begin
  2000. include(current_procinfo.flags,pi_do_call);
  2001. addsymref(aprocdef.procsym);
  2002. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  2003. { tell explicitly which def we must use !! (PM) }
  2004. tcallnode(hp).procdefinition:=aprocdef;
  2005. left:=nil;
  2006. result:=hp;
  2007. exit;
  2008. end;
  2009. te_incompatible :
  2010. begin
  2011. { Procedures have a resultdef of voiddef and functions of their
  2012. own resultdef. They will therefore always be incompatible with
  2013. a procvar. Because isconvertable cannot check for procedures we
  2014. use an extra check for them.}
  2015. if (left.nodetype=calln) and
  2016. (tcallnode(left).required_para_count=0) and
  2017. (resultdef.typ=procvardef) and
  2018. (
  2019. (m_tp_procvar in current_settings.modeswitches) or
  2020. (m_mac_procvar in current_settings.modeswitches)
  2021. ) then
  2022. begin
  2023. if assigned(tcallnode(left).right) then
  2024. begin
  2025. { this is already a procvar, if it is really equal
  2026. is checked below }
  2027. convtype:=tc_equal;
  2028. hp:=tcallnode(left).right.getcopy;
  2029. currprocdef:=tabstractprocdef(hp.resultdef);
  2030. end
  2031. else
  2032. begin
  2033. convtype:=tc_proc_2_procvar;
  2034. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  2035. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  2036. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2037. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2038. begin
  2039. if assigned(tcallnode(left).methodpointer) then
  2040. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  2041. else
  2042. tloadnode(hp).set_mp(load_self_node);
  2043. end;
  2044. typecheckpass(hp);
  2045. end;
  2046. left.free;
  2047. left:=hp;
  2048. { Now check if the procedure we are going to assign to
  2049. the procvar, is compatible with the procvar's type }
  2050. if not(nf_explicit in flags) and
  2051. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
  2052. IncompatibleTypes(left.resultdef,resultdef)
  2053. else
  2054. result:=typecheck_call_helper(convtype);
  2055. exit;
  2056. end
  2057. else if maybe_global_proc_to_nested(left,resultdef) then
  2058. begin
  2059. result:=left;
  2060. left:=nil;
  2061. exit;
  2062. end;
  2063. { Handle explicit type conversions }
  2064. if nf_explicit in flags then
  2065. begin
  2066. { do common tc_equal cast, except when dealing with proc -> procvar
  2067. (may have to get rid of method pointer) }
  2068. if (left.resultdef.typ<>procdef) or
  2069. (resultdef.typ<>procvardef) then
  2070. convtype:=tc_equal
  2071. else
  2072. convtype:=tc_proc_2_procvar;
  2073. { ordinal constants can be resized to 1,2,4,8 bytes }
  2074. if (left.nodetype=ordconstn) then
  2075. begin
  2076. { Insert typeconv for ordinal to the correct size first on left, after
  2077. that the other conversion can be done }
  2078. hdef:=nil;
  2079. case longint(resultdef.size) of
  2080. 1 :
  2081. hdef:=s8inttype;
  2082. 2 :
  2083. hdef:=s16inttype;
  2084. 4 :
  2085. hdef:=s32inttype;
  2086. 8 :
  2087. hdef:=s64inttype;
  2088. end;
  2089. { we need explicit, because it can also be an enum }
  2090. if assigned(hdef) then
  2091. inserttypeconv_internal(left,hdef)
  2092. else
  2093. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2094. end;
  2095. { class/interface to class/interface, with checkobject support }
  2096. if is_class_or_interface_or_objc(resultdef) and
  2097. is_class_or_interface_or_objc(left.resultdef) then
  2098. begin
  2099. { check if the types are related }
  2100. if not(nf_internal in flags) and
  2101. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2102. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2103. begin
  2104. { Give an error when typecasting class to interface, this is compatible
  2105. with delphi }
  2106. if is_interface(resultdef) and
  2107. not is_interface(left.resultdef) then
  2108. CGMessage2(type_e_classes_not_related,
  2109. FullTypeName(left.resultdef,resultdef),
  2110. FullTypeName(resultdef,left.resultdef))
  2111. else
  2112. CGMessage2(type_w_classes_not_related,
  2113. FullTypeName(left.resultdef,resultdef),
  2114. FullTypeName(resultdef,left.resultdef))
  2115. end;
  2116. { Add runtime check? }
  2117. if not is_objc_class_or_protocol(resultdef) and
  2118. not is_objc_class_or_protocol(left.resultdef) and
  2119. (cs_check_object in current_settings.localswitches) and
  2120. not(nf_internal in flags) then
  2121. begin
  2122. { we can translate the typeconvnode to 'as' when
  2123. typecasting to a class or interface }
  2124. { we need to make sure the result can still be
  2125. passed as a var parameter }
  2126. newblock:=internalstatements(newstatement);
  2127. if (valid_for_var(left,false)) then
  2128. begin
  2129. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2130. addstatement(newstatement,tempnode);
  2131. addstatement(newstatement,cassignmentnode.create(
  2132. ctemprefnode.create(tempnode),
  2133. caddrnode.create_internal(left)));
  2134. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2135. end
  2136. else
  2137. begin
  2138. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2139. addstatement(newstatement,tempnode);
  2140. addstatement(newstatement,cassignmentnode.create(
  2141. ctemprefnode.create(tempnode),
  2142. left));
  2143. left:=ctemprefnode.create(tempnode);
  2144. end;
  2145. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2146. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2147. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2148. left:=nil;
  2149. result:=newblock;
  2150. exit;
  2151. end;
  2152. end
  2153. else
  2154. begin
  2155. { only if the same size or formal def, and }
  2156. { don't allow type casting of constants to }
  2157. { structured types }
  2158. if not(
  2159. (left.resultdef.typ=formaldef) or
  2160. {$ifdef jvm}
  2161. { enums /are/ class instances on the JVM
  2162. platform }
  2163. (((left.resultdef.typ=enumdef) and
  2164. (resultdef.typ=objectdef)) or
  2165. ((resultdef.typ=enumdef) and
  2166. (left.resultdef.typ=objectdef))) or
  2167. {$endif}
  2168. (
  2169. is_void(left.resultdef) and
  2170. (left.nodetype=derefn)
  2171. ) or
  2172. (
  2173. not(is_open_array(left.resultdef)) and
  2174. not(is_array_constructor(left.resultdef)) and
  2175. not(is_array_of_const(left.resultdef)) and
  2176. (left.resultdef.size=resultdef.size) and
  2177. { disallow casts of const nodes }
  2178. (not is_constnode(left) or
  2179. { however, there are some exceptions }
  2180. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2181. filedef,variantdef,objectdef]) or
  2182. is_class_or_interface_or_objc(resultdef) or
  2183. { the softfloat code generates casts <const. float> to record }
  2184. (nf_internal in flags)
  2185. ))
  2186. )
  2187. ) then
  2188. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2189. else
  2190. begin
  2191. { perform target-specific explicit typecast
  2192. checks }
  2193. if target_specific_explicit_typeconv then
  2194. begin
  2195. result:=simplify(false);
  2196. exit;
  2197. end;
  2198. end;
  2199. end;
  2200. end
  2201. else
  2202. IncompatibleTypes(left.resultdef,resultdef);
  2203. end;
  2204. else
  2205. internalerror(200211231);
  2206. end;
  2207. end;
  2208. { Give hint or warning for unportable code, exceptions are
  2209. - typecasts from constants
  2210. - void }
  2211. if not(nf_internal in flags) and
  2212. (left.nodetype<>ordconstn) and
  2213. not(is_void(left.resultdef)) and
  2214. (((left.resultdef.typ=orddef) and
  2215. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2216. ((resultdef.typ=orddef) and
  2217. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2218. begin
  2219. {Converting pointers to signed integers is a bad idea. Warn.}
  2220. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2221. { Give a warning when sizes don't match, because then info will be lost }
  2222. if left.resultdef.size=resultdef.size then
  2223. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2224. else
  2225. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2226. end;
  2227. { tc_cord_2_pointer still requires a type check, which
  2228. simplify does not do }
  2229. if (convtype<>tc_cord_2_pointer) then
  2230. begin
  2231. result := simplify(false);
  2232. if assigned(result) then
  2233. exit;
  2234. end;
  2235. { now call the resultdef helper to do constant folding }
  2236. result:=typecheck_call_helper(convtype);
  2237. end;
  2238. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2239. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2240. this applies is powerpc64
  2241. }
  2242. {$ifndef CPUNO32BITOPS}
  2243. { checks whether we can safely remove typeconversions to bigger types
  2244. in case range and overflow checking are off, and in case
  2245. the result of this node tree is downcasted again to a
  2246. smaller type value afterwards,
  2247. the smaller types being allowed are described by validints, ordinal constants must fit into l..h
  2248. We do this on 64 bit CPUs as well, they benefit from it as well }
  2249. function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;l,h : Tconstexprint): boolean;
  2250. var
  2251. gotdivmod: boolean;
  2252. { checks whether a node has an accepted resultdef, or originally
  2253. had one but was implicitly converted to s64bit }
  2254. function wasoriginallysmallerint(n: tnode): boolean;
  2255. begin
  2256. if (n.resultdef.typ<>orddef) then
  2257. exit(false);
  2258. if (torddef(n.resultdef).ordtype in validints) then
  2259. begin
  2260. if is_signed(n.resultdef) then
  2261. gotsint:=true;
  2262. exit(true);
  2263. end;
  2264. { type conv to a bigger int, we do not like to use? }
  2265. if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
  2266. { nf_explicit is also set for explicitly typecasted }
  2267. { ordconstn's }
  2268. ([nf_internal,nf_explicit]*n.flags=[]) and
  2269. { either a typeconversion node coming from a smaller type }
  2270. (((n.nodetype=typeconvn) and
  2271. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2272. (torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
  2273. { or an ordconstnode which has a smaller type}
  2274. ((n.nodetype=ordconstn) and
  2275. (tordconstnode(n).value>=l) and
  2276. (tordconstnode(n).value<=h))) then
  2277. begin
  2278. if ((n.nodetype=typeconvn) and
  2279. is_signed(ttypeconvnode(n).left.resultdef)) or
  2280. ((n.nodetype=ordconstn) and
  2281. (tordconstnode(n).value<0)) then
  2282. gotsint:=true;
  2283. exit(true);
  2284. end;
  2285. result:=false;
  2286. end;
  2287. function docheckremoveinttypeconvs(n: tnode): boolean;
  2288. begin
  2289. result:=false;
  2290. if wasoriginallysmallerint(n) then
  2291. exit(true);
  2292. case n.nodetype of
  2293. subn,orn,xorn:
  2294. begin
  2295. { the result could become negative in this case }
  2296. if n.nodetype=subn then
  2297. gotsint:=true;
  2298. result:=
  2299. docheckremoveinttypeconvs(tbinarynode(n).left) and
  2300. docheckremoveinttypeconvs(tbinarynode(n).right);
  2301. end;
  2302. addn,muln,divn,modn,andn:
  2303. begin
  2304. if n.nodetype in [divn,modn] then
  2305. gotdivmod:=true;
  2306. result:=
  2307. (docheckremoveinttypeconvs(tbinarynode(n).left) and
  2308. docheckremoveinttypeconvs(tbinarynode(n).right)) or
  2309. ((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).left)) or
  2310. ((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).right));
  2311. end;
  2312. end;
  2313. end;
  2314. begin { checkremove64bittypeconvs }
  2315. gotdivmod:=false;
  2316. gotsint:=false;
  2317. result:=
  2318. docheckremoveinttypeconvs(n) and
  2319. not(gotdivmod and gotsint);
  2320. end;
  2321. { remove int type conversions and set the result to the given type }
  2322. procedure doremoveinttypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
  2323. begin
  2324. case n.nodetype of
  2325. subn,addn,muln,divn,modn,xorn,andn,orn:
  2326. begin
  2327. exclude(n.flags,nf_internal);
  2328. if not forceunsigned and
  2329. is_signed(n.resultdef) then
  2330. begin
  2331. doremoveinttypeconvs(tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
  2332. doremoveinttypeconvs(tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
  2333. n.resultdef:=signedtype;
  2334. end
  2335. else
  2336. begin
  2337. doremoveinttypeconvs(tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  2338. doremoveinttypeconvs(tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
  2339. n.resultdef:=unsignedtype;
  2340. end;
  2341. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  2342. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  2343. // ) then
  2344. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  2345. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  2346. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  2347. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  2348. end;
  2349. typeconvn:
  2350. begin
  2351. n.resultdef:=todef;
  2352. ttypeconvnode(n).totypedef:=todef;
  2353. end;
  2354. else
  2355. inserttypeconv_internal(n,todef);
  2356. end;
  2357. end;
  2358. {$endif not CPUNO32BITOPS}
  2359. procedure swap_const_value (var val : TConstExprInt; size : longint);
  2360. begin
  2361. case size of
  2362. 1 : {do nothing };
  2363. 2 : if val.signed then
  2364. val.svalue:=swapendian(smallint(val.svalue))
  2365. else
  2366. val.uvalue:=swapendian(word(val.uvalue));
  2367. 4 : if val.signed then
  2368. val.svalue:=swapendian(longint(val.svalue))
  2369. else
  2370. val.uvalue:=swapendian(qword(val.uvalue));
  2371. 8 : if val.signed then
  2372. val.svalue:=swapendian(int64(val.svalue))
  2373. else
  2374. val.uvalue:=swapendian(qword(val.uvalue));
  2375. else
  2376. internalerror(2014111201);
  2377. end;
  2378. end;
  2379. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2380. var
  2381. hp: tnode;
  2382. {$ifndef CPUNO32BITOPS}
  2383. foundsint: boolean;
  2384. {$endif not CPUNO32BITOPS}
  2385. begin
  2386. result := nil;
  2387. { Constant folding and other node transitions to
  2388. remove the typeconv node }
  2389. case left.nodetype of
  2390. stringconstn :
  2391. if (convtype=tc_string_2_string) and
  2392. (resultdef.typ=stringdef) and
  2393. (
  2394. ((not is_widechararray(left.resultdef) and
  2395. not is_wide_or_unicode_string(left.resultdef)) or
  2396. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  2397. )
  2398. ) then
  2399. begin
  2400. { output string consts in local ansistring encoding }
  2401. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0)or(tstringdef(resultdef).encoding=globals.CP_NONE)) then
  2402. tstringconstnode(left).changestringtype(getansistringdef)
  2403. else
  2404. tstringconstnode(left).changestringtype(resultdef);
  2405. result:=left;
  2406. resultdef:=left.resultdef;
  2407. left:=nil;
  2408. exit;
  2409. end;
  2410. realconstn :
  2411. begin
  2412. if (convtype = tc_real_2_currency) then
  2413. result := typecheck_real_to_currency
  2414. else if (convtype = tc_real_2_real) then
  2415. result := typecheck_real_to_real
  2416. else
  2417. exit;
  2418. if not(assigned(result)) then
  2419. begin
  2420. result := left;
  2421. left := nil;
  2422. end;
  2423. if (result.nodetype = realconstn) then
  2424. begin
  2425. hp:=result;
  2426. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2427. if ([nf_explicit,nf_internal] * flags <> []) then
  2428. include(result.flags, nf_explicit);
  2429. hp.free;
  2430. end;
  2431. end;
  2432. niln :
  2433. begin
  2434. { nil to ordinal node }
  2435. if (resultdef.typ=orddef) then
  2436. begin
  2437. hp:=cordconstnode.create(0,resultdef,true);
  2438. if ([nf_explicit,nf_internal] * flags <> []) then
  2439. include(hp.flags, nf_explicit);
  2440. result:=hp;
  2441. exit;
  2442. end
  2443. else
  2444. { fold nil to any pointer type }
  2445. if (resultdef.typ=pointerdef) then
  2446. begin
  2447. hp:=cnilnode.create;
  2448. hp.resultdef:=resultdef;
  2449. if ([nf_explicit,nf_internal] * flags <> []) then
  2450. include(hp.flags, nf_explicit);
  2451. result:=hp;
  2452. exit;
  2453. end
  2454. else
  2455. { remove typeconv after niln, but not when the result is a
  2456. methodpointer. The typeconv of the methodpointer will then
  2457. take care of updateing size of niln to OS_64 }
  2458. if not((resultdef.typ=procvardef) and
  2459. not(tprocvardef(resultdef).is_addressonly)) then
  2460. begin
  2461. left.resultdef:=resultdef;
  2462. if ([nf_explicit,nf_internal] * flags <> []) then
  2463. include(left.flags, nf_explicit);
  2464. result:=left;
  2465. left:=nil;
  2466. exit;
  2467. end;
  2468. end;
  2469. ordconstn :
  2470. begin
  2471. { ordinal contants can be directly converted }
  2472. { but not char to char because it is a widechar to char or via versa }
  2473. { which needs extra code to do the code page transistion }
  2474. { constant ordinal to pointer }
  2475. if (resultdef.typ=pointerdef) and
  2476. (convtype<>tc_cchar_2_pchar) then
  2477. begin
  2478. if (target_info.system in systems_managed_vm) and
  2479. (tordconstnode(left).value<>0) then
  2480. message(parser_e_feature_unsupported_for_vm);
  2481. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2482. if ([nf_explicit,nf_internal] * flags <> []) then
  2483. include(hp.flags, nf_explicit);
  2484. result:=hp;
  2485. exit;
  2486. end
  2487. else if is_ordinal(resultdef) and
  2488. not(convtype=tc_char_2_char) then
  2489. begin
  2490. { replace the resultdef and recheck the range }
  2491. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  2492. include(left.flags, nf_explicit)
  2493. else
  2494. { no longer an ordconst with an explicit typecast }
  2495. exclude(left.flags, nf_explicit);
  2496. { when converting from one boolean type to another, force }
  2497. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2498. { (Delphi-compatibile) }
  2499. if is_boolean(left.resultdef) and
  2500. is_boolean(resultdef) and
  2501. (is_cbool(left.resultdef) or
  2502. is_cbool(resultdef)) then
  2503. begin
  2504. if is_pasbool(resultdef) then
  2505. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2506. else
  2507. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2508. end
  2509. else
  2510. begin
  2511. { for constant values on absolute variables, swaping is required }
  2512. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2513. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  2514. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags)
  2515. or (nf_absolute in flags),false);
  2516. { swap value back, but according to new type }
  2517. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2518. swap_const_value(tordconstnode(left).value,resultdef.size);
  2519. end;
  2520. left.resultdef:=resultdef;
  2521. tordconstnode(left).typedef:=resultdef;
  2522. if is_signed(resultdef) then
  2523. tordconstnode(left).value.signed:=true
  2524. else
  2525. tordconstnode(left).value.signed:=false;
  2526. result:=left;
  2527. left:=nil;
  2528. exit;
  2529. end;
  2530. end;
  2531. pointerconstn :
  2532. begin
  2533. { pointerconstn to any pointer is folded too }
  2534. if (resultdef.typ=pointerdef) then
  2535. begin
  2536. left.resultdef:=resultdef;
  2537. if ([nf_explicit,nf_internal] * flags <> []) then
  2538. include(left.flags, nf_explicit)
  2539. else
  2540. { no longer an ordconst with an explicit typecast }
  2541. exclude(left.flags, nf_explicit);
  2542. result:=left;
  2543. left:=nil;
  2544. exit;
  2545. end
  2546. { constant pointer to ordinal }
  2547. else if is_ordinal(resultdef) then
  2548. begin
  2549. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2550. resultdef,not(nf_explicit in flags));
  2551. if ([nf_explicit,nf_internal] * flags <> []) then
  2552. include(hp.flags, nf_explicit);
  2553. result:=hp;
  2554. exit;
  2555. end;
  2556. end;
  2557. end;
  2558. {$ifndef CPUNO32BITOPS}
  2559. { must be done before code below, because we need the
  2560. typeconversions for ordconstn's as well }
  2561. case convtype of
  2562. tc_int_2_int:
  2563. begin
  2564. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2565. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  2566. begin
  2567. { avoid unnecessary widening of intermediary calculations
  2568. to 64 bit }
  2569. if (resultdef.size <= 4) and
  2570. is_64bitint(left.resultdef) and
  2571. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2572. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  2573. doremoveinttypeconvs(left,generrordef,not foundsint,s32inttype,u32inttype);
  2574. {$if defined(cpu8bitalu)}
  2575. if (resultdef.size<left.resultdef.size) and
  2576. is_integer(left.resultdef) and
  2577. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2578. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  2579. doremoveinttypeconvs(left,generrordef,not foundsint,s8inttype,u8inttype);
  2580. {$endif defined(cpu8bitalu)}
  2581. end;
  2582. end;
  2583. end;
  2584. {$endif not CPUNO32BITOPS}
  2585. end;
  2586. procedure Ttypeconvnode.mark_write;
  2587. begin
  2588. left.mark_write;
  2589. end;
  2590. function ttypeconvnode.first_cord_to_pointer : tnode;
  2591. begin
  2592. result:=nil;
  2593. internalerror(200104043);
  2594. end;
  2595. function ttypeconvnode.first_int_to_int : tnode;
  2596. begin
  2597. first_int_to_int:=nil;
  2598. expectloc:=left.expectloc;
  2599. if not is_void(left.resultdef) then
  2600. begin
  2601. if (left.expectloc<>LOC_REGISTER) and
  2602. ((resultdef.size>left.resultdef.size) or
  2603. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2604. expectloc:=LOC_REGISTER
  2605. else
  2606. if (left.expectloc=LOC_CREGISTER) and
  2607. (resultdef.size<left.resultdef.size) then
  2608. expectloc:=LOC_REGISTER;
  2609. end;
  2610. end;
  2611. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2612. begin
  2613. result:=nil;
  2614. expectloc:=LOC_REGISTER;
  2615. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2616. if (cs_create_pic in current_settings.moduleswitches) then
  2617. include(current_procinfo.flags,pi_needs_got);
  2618. end;
  2619. function ttypeconvnode.first_cstring_to_int : tnode;
  2620. begin
  2621. result:=nil;
  2622. internalerror(200510014);
  2623. end;
  2624. function ttypeconvnode.first_string_to_chararray : tnode;
  2625. begin
  2626. first_string_to_chararray:=nil;
  2627. expectloc:=left.expectloc;
  2628. end;
  2629. function ttypeconvnode.first_char_to_string : tnode;
  2630. begin
  2631. first_char_to_string:=nil;
  2632. if tstringdef(resultdef).stringtype=st_shortstring then
  2633. inc(current_procinfo.estimatedtempsize,256);
  2634. expectloc:=LOC_REFERENCE;
  2635. end;
  2636. function ttypeconvnode.first_char_to_chararray : tnode;
  2637. begin
  2638. if resultdef.size <> 1 then
  2639. begin
  2640. { convert first to string, then to chararray }
  2641. inserttypeconv(left,cshortstringtype);
  2642. inserttypeconv(left,resultdef);
  2643. result:=left;
  2644. left := nil;
  2645. exit;
  2646. end;
  2647. result := nil;
  2648. end;
  2649. function ttypeconvnode.first_nothing : tnode;
  2650. begin
  2651. first_nothing:=nil;
  2652. end;
  2653. function ttypeconvnode.first_array_to_pointer : tnode;
  2654. begin
  2655. first_array_to_pointer:=nil;
  2656. expectloc:=LOC_REGISTER;
  2657. end;
  2658. function ttypeconvnode.first_int_to_real: tnode;
  2659. var
  2660. fname: string[32];
  2661. begin
  2662. if target_info.system in systems_wince then
  2663. begin
  2664. { converting a 64bit integer to a float requires a helper }
  2665. if is_64bitint(left.resultdef) or
  2666. is_currency(left.resultdef) then
  2667. begin
  2668. { hack to avoid double division by 10000, as it's
  2669. already done by typecheckpass.resultdef_int_to_real }
  2670. if is_currency(left.resultdef) then
  2671. left.resultdef := s64inttype;
  2672. if is_signed(left.resultdef) then
  2673. fname:='i64to'
  2674. else
  2675. fname:='ui64to';
  2676. end
  2677. else
  2678. { other integers are supposed to be 32 bit }
  2679. begin
  2680. if is_signed(left.resultdef) then
  2681. fname:='ito'
  2682. else
  2683. fname:='uto';
  2684. firstpass(left);
  2685. end;
  2686. if tfloatdef(resultdef).floattype=s64real then
  2687. fname:=fname+'d'
  2688. else
  2689. fname:=fname+'s';
  2690. result:=ccallnode.createintern(fname,ccallparanode.create(
  2691. left,nil));
  2692. left:=nil;
  2693. firstpass(result);
  2694. exit;
  2695. end
  2696. else
  2697. begin
  2698. { converting a 64bit integer to a float requires a helper }
  2699. if is_64bitint(left.resultdef) or
  2700. is_currency(left.resultdef) then
  2701. begin
  2702. { hack to avoid double division by 10000, as it's
  2703. already done by typecheckpass.resultdef_int_to_real }
  2704. if is_currency(left.resultdef) then
  2705. left.resultdef := s64inttype;
  2706. if is_signed(left.resultdef) then
  2707. fname:='int64_to_'
  2708. else
  2709. { we can't do better currently }
  2710. fname:='qword_to_';
  2711. end
  2712. else
  2713. { other integers are supposed to be 32 bit }
  2714. begin
  2715. if is_signed(left.resultdef) then
  2716. fname:='int32_to_'
  2717. else
  2718. fname:='int64_to_';
  2719. firstpass(left);
  2720. end;
  2721. if tfloatdef(resultdef).floattype=s64real then
  2722. fname:=fname+'float64'
  2723. else
  2724. fname:=fname+'float32';
  2725. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2726. left,nil)),resultdef);
  2727. left:=nil;
  2728. firstpass(result);
  2729. exit;
  2730. end;
  2731. end;
  2732. function ttypeconvnode.first_real_to_real : tnode;
  2733. begin
  2734. {$ifdef cpufpemu}
  2735. if cs_fp_emulation in current_settings.moduleswitches then
  2736. begin
  2737. if target_info.system in systems_wince then
  2738. begin
  2739. case tfloatdef(left.resultdef).floattype of
  2740. s32real:
  2741. case tfloatdef(resultdef).floattype of
  2742. s64real:
  2743. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  2744. s32real:
  2745. begin
  2746. result:=left;
  2747. left:=nil;
  2748. end;
  2749. else
  2750. internalerror(2005082704);
  2751. end;
  2752. s64real:
  2753. case tfloatdef(resultdef).floattype of
  2754. s32real:
  2755. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  2756. s64real:
  2757. begin
  2758. result:=left;
  2759. left:=nil;
  2760. end;
  2761. else
  2762. internalerror(2005082703);
  2763. end;
  2764. else
  2765. internalerror(2005082702);
  2766. end;
  2767. left:=nil;
  2768. firstpass(result);
  2769. exit;
  2770. end
  2771. else
  2772. begin
  2773. case tfloatdef(left.resultdef).floattype of
  2774. s32real:
  2775. case tfloatdef(resultdef).floattype of
  2776. s64real:
  2777. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2778. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2779. s32real:
  2780. begin
  2781. result:=left;
  2782. left:=nil;
  2783. end;
  2784. else
  2785. internalerror(200610151);
  2786. end;
  2787. s64real:
  2788. case tfloatdef(resultdef).floattype of
  2789. s32real:
  2790. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2791. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2792. s64real:
  2793. begin
  2794. result:=left;
  2795. left:=nil;
  2796. end;
  2797. else
  2798. internalerror(200610152);
  2799. end;
  2800. else
  2801. internalerror(200610153);
  2802. end;
  2803. left:=nil;
  2804. firstpass(result);
  2805. exit;
  2806. end;
  2807. end
  2808. else
  2809. {$endif cpufpemu}
  2810. begin
  2811. first_real_to_real:=nil;
  2812. if not use_vectorfpu(resultdef) then
  2813. expectloc:=LOC_FPUREGISTER
  2814. else
  2815. expectloc:=LOC_MMREGISTER;
  2816. end;
  2817. end;
  2818. function ttypeconvnode.first_pointer_to_array : tnode;
  2819. begin
  2820. first_pointer_to_array:=nil;
  2821. expectloc:=LOC_REFERENCE;
  2822. end;
  2823. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2824. begin
  2825. first_cchar_to_pchar:=nil;
  2826. internalerror(200104021);
  2827. end;
  2828. function ttypeconvnode.first_bool_to_int : tnode;
  2829. begin
  2830. first_bool_to_int:=nil;
  2831. { byte(boolean) or word(wordbool) or longint(longbool) must
  2832. be accepted for var parameters }
  2833. if (nf_explicit in flags) and
  2834. (left.resultdef.size=resultdef.size) and
  2835. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2836. begin
  2837. expectloc:=left.expectloc;
  2838. exit;
  2839. end;
  2840. expectloc:=LOC_REGISTER;
  2841. end;
  2842. function ttypeconvnode.first_int_to_bool : tnode;
  2843. begin
  2844. first_int_to_bool:=nil;
  2845. { byte(boolean) or word(wordbool) or longint(longbool) must
  2846. be accepted for var parameters }
  2847. if (nf_explicit in flags) and
  2848. (left.resultdef.size=resultdef.size) and
  2849. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2850. begin
  2851. expectloc:=left.expectloc;
  2852. exit;
  2853. end;
  2854. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2855. { convert to a boolean (only necessary for 32bit processors) }
  2856. { note: not if left is already a bool (qwordbool that is true, even if
  2857. only because the highest bit is set, must remain true if it is
  2858. --implicitly, unlike integers-- converted to another type of bool),
  2859. Left can already be a bool because this routine can also be called
  2860. from first_bool_to_bool }
  2861. if not is_boolean(left.resultdef) and
  2862. (left.resultdef.size > sizeof(aint)) and
  2863. (left.resultdef.size<>resultdef.size)
  2864. and is_cbool(resultdef) then
  2865. begin
  2866. left:=ctypeconvnode.create_internal(left,s32inttype);
  2867. firstpass(left);
  2868. exit;
  2869. end;
  2870. expectloc:=LOC_REGISTER;
  2871. end;
  2872. function ttypeconvnode.first_bool_to_bool : tnode;
  2873. begin
  2874. first_bool_to_bool:=nil;
  2875. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  2876. not is_cbool(resultdef) then
  2877. expectloc := left.expectloc
  2878. { the following cases use the code generation for bool_to_int/
  2879. int_to_bool -> also set their expectlocs }
  2880. else if (resultdef.size=left.resultdef.size) and
  2881. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  2882. result:=first_bool_to_int
  2883. else
  2884. result:=first_int_to_bool
  2885. end;
  2886. function ttypeconvnode.first_char_to_char : tnode;
  2887. var
  2888. fname: string[18];
  2889. begin
  2890. if (torddef(resultdef).ordtype=uchar) and
  2891. (torddef(left.resultdef).ordtype=uwidechar) then
  2892. fname := 'fpc_uchar_to_char'
  2893. else if (torddef(resultdef).ordtype=uwidechar) and
  2894. (torddef(left.resultdef).ordtype=uchar) then
  2895. fname := 'fpc_char_to_uchar'
  2896. else
  2897. internalerror(2007081201);
  2898. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2899. left:=nil;
  2900. firstpass(result);
  2901. end;
  2902. function ttypeconvnode.first_proc_to_procvar : tnode;
  2903. begin
  2904. first_proc_to_procvar:=nil;
  2905. { if we take the address of a nested function, the current function/
  2906. procedure needs a stack frame since it's required to construct
  2907. the nested procvar }
  2908. if is_nested_pd(tprocvardef(resultdef)) then
  2909. include(current_procinfo.flags,pi_needs_stackframe);
  2910. if tabstractprocdef(resultdef).is_addressonly then
  2911. expectloc:=LOC_REGISTER
  2912. else
  2913. expectloc:=left.expectloc;
  2914. end;
  2915. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2916. begin
  2917. first_nil_to_methodprocvar:=nil;
  2918. expectloc:=LOC_REGISTER;
  2919. end;
  2920. function ttypeconvnode.first_set_to_set : tnode;
  2921. var
  2922. newstatement : tstatementnode;
  2923. temp : ttempcreatenode;
  2924. begin
  2925. { in theory, we should do range checking here,
  2926. but Delphi doesn't do it either (FK) }
  2927. if left.nodetype=setconstn then
  2928. begin
  2929. left.resultdef:=resultdef;
  2930. result:=left;
  2931. left:=nil;
  2932. end
  2933. { equal sets for the code generator? }
  2934. else if (left.resultdef.size=resultdef.size) and
  2935. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2936. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2937. { e.g., this prints true because of this:
  2938. var
  2939. sa: set of 1..2;
  2940. sb: set of 5..6;
  2941. b: byte;
  2942. begin
  2943. b:=1;
  2944. sa:=[1..2];
  2945. sb:=sa;
  2946. writeln(b in sb);
  2947. end.
  2948. }
  2949. begin
  2950. result:=left;
  2951. left:=nil;
  2952. end
  2953. else
  2954. begin
  2955. result:=internalstatements(newstatement);
  2956. { in case left is a smallset expression, it can be an addn or so. }
  2957. { fpc_varset_load expects a formal const parameter, which doesn't }
  2958. { accept set addn's -> assign to a temp first and pass the temp }
  2959. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2960. begin
  2961. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2962. addstatement(newstatement,temp);
  2963. { temp := left }
  2964. addstatement(newstatement,cassignmentnode.create(
  2965. ctemprefnode.create(temp),left));
  2966. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2967. addstatement(newstatement,ctemprefnode.create(temp));
  2968. left:=result;
  2969. firstpass(left);
  2970. { recreate the result's internalstatements list }
  2971. result:=internalstatements(newstatement);
  2972. end;
  2973. { create temp for result }
  2974. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2975. addstatement(newstatement,temp);
  2976. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2977. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2978. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2979. ccallparanode.create(ctemprefnode.create(temp),
  2980. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2981. ccallparanode.create(left,nil))))))
  2982. );
  2983. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2984. addstatement(newstatement,ctemprefnode.create(temp));
  2985. left:=nil;
  2986. end;
  2987. end;
  2988. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2989. begin
  2990. first_ansistring_to_pchar:=nil;
  2991. expectloc:=LOC_REGISTER;
  2992. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2993. if (cs_create_pic in current_settings.moduleswitches) then
  2994. include(current_procinfo.flags,pi_needs_got);
  2995. end;
  2996. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2997. begin
  2998. first_arrayconstructor_to_set:=nil;
  2999. internalerror(200104022);
  3000. end;
  3001. function ttypeconvnode.first_class_to_intf : tnode;
  3002. var
  3003. hd : tobjectdef;
  3004. ImplIntf : TImplementedInterface;
  3005. begin
  3006. result:=nil;
  3007. expectloc:=LOC_REGISTER;
  3008. hd:=tobjectdef(left.resultdef);
  3009. while assigned(hd) do
  3010. begin
  3011. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3012. if assigned(ImplIntf) then
  3013. begin
  3014. case ImplIntf.IType of
  3015. etStandard:
  3016. { handle in pass 2 }
  3017. ;
  3018. etFieldValue, etFieldValueClass:
  3019. if is_interface(tobjectdef(resultdef)) then
  3020. begin
  3021. result:=left;
  3022. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3023. { this ensures proper refcounting when field is of class type }
  3024. if not is_interface(result.resultdef) then
  3025. inserttypeconv(result, resultdef);
  3026. left:=nil;
  3027. end
  3028. else
  3029. begin
  3030. internalerror(200802213);
  3031. end;
  3032. etStaticMethodResult, etStaticMethodClass,
  3033. etVirtualMethodResult, etVirtualMethodClass:
  3034. if is_interface(tobjectdef(resultdef)) then
  3035. begin
  3036. { TODO: generating a call to TObject.GetInterface instead could yield
  3037. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3038. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3039. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3040. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3041. left,[]);
  3042. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3043. { if it is a class, process it further in a similar way }
  3044. if not is_interface(result.resultdef) then
  3045. inserttypeconv(result, resultdef);
  3046. left:=nil;
  3047. end
  3048. else if is_class(tobjectdef(resultdef)) then
  3049. begin
  3050. internalerror(200802211);
  3051. end
  3052. else
  3053. internalerror(200802231);
  3054. else
  3055. internalerror(200802165);
  3056. end;
  3057. break;
  3058. end;
  3059. hd:=hd.childof;
  3060. end;
  3061. if hd=nil then
  3062. internalerror(200802164);
  3063. end;
  3064. function ttypeconvnode.first_string_to_string : tnode;
  3065. var
  3066. procname: string[31];
  3067. newblock : tblocknode;
  3068. newstat : tstatementnode;
  3069. restemp : ttempcreatenode;
  3070. begin
  3071. { get the correct procedure name }
  3072. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3073. '_to_'+tstringdef(resultdef).stringtypname;
  3074. if tstringdef(resultdef).stringtype=st_shortstring then
  3075. begin
  3076. newblock:=internalstatements(newstat);
  3077. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3078. addstatement(newstat,restemp);
  3079. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3080. ctemprefnode.create(restemp),nil))));
  3081. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  3082. addstatement(newstat,ctemprefnode.create(restemp));
  3083. result:=newblock;
  3084. end
  3085. { encoding parameter required? }
  3086. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  3087. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  3088. result:=ccallnode.createinternres(procname,
  3089. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  3090. ccallparanode.create(left,nil)),resultdef)
  3091. else
  3092. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  3093. left:=nil;
  3094. end;
  3095. function ttypeconvnode._first_int_to_int : tnode;
  3096. begin
  3097. result:=first_int_to_int;
  3098. end;
  3099. function ttypeconvnode._first_cstring_to_pchar : tnode;
  3100. begin
  3101. result:=first_cstring_to_pchar;
  3102. end;
  3103. function ttypeconvnode._first_cstring_to_int : tnode;
  3104. begin
  3105. result:=first_cstring_to_int;
  3106. end;
  3107. function ttypeconvnode._first_string_to_chararray : tnode;
  3108. begin
  3109. result:=first_string_to_chararray;
  3110. end;
  3111. function ttypeconvnode._first_char_to_string : tnode;
  3112. begin
  3113. result:=first_char_to_string;
  3114. end;
  3115. function ttypeconvnode._first_char_to_chararray: tnode;
  3116. begin
  3117. result:=first_char_to_chararray;
  3118. end;
  3119. function ttypeconvnode._first_nothing : tnode;
  3120. begin
  3121. result:=first_nothing;
  3122. end;
  3123. function ttypeconvnode._first_array_to_pointer : tnode;
  3124. begin
  3125. result:=first_array_to_pointer;
  3126. end;
  3127. function ttypeconvnode._first_int_to_real : tnode;
  3128. begin
  3129. result:=first_int_to_real;
  3130. end;
  3131. function ttypeconvnode._first_real_to_real : tnode;
  3132. begin
  3133. result:=first_real_to_real;
  3134. end;
  3135. function ttypeconvnode._first_pointer_to_array : tnode;
  3136. begin
  3137. result:=first_pointer_to_array;
  3138. end;
  3139. function ttypeconvnode._first_cchar_to_pchar : tnode;
  3140. begin
  3141. result:=first_cchar_to_pchar;
  3142. end;
  3143. function ttypeconvnode._first_bool_to_int : tnode;
  3144. begin
  3145. result:=first_bool_to_int;
  3146. end;
  3147. function ttypeconvnode._first_int_to_bool : tnode;
  3148. begin
  3149. result:=first_int_to_bool;
  3150. end;
  3151. function ttypeconvnode._first_bool_to_bool : tnode;
  3152. begin
  3153. result:=first_bool_to_bool;
  3154. end;
  3155. function ttypeconvnode._first_proc_to_procvar : tnode;
  3156. begin
  3157. result:=first_proc_to_procvar;
  3158. end;
  3159. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  3160. begin
  3161. result:=first_nil_to_methodprocvar;
  3162. end;
  3163. function ttypeconvnode._first_set_to_set : tnode;
  3164. begin
  3165. result:=first_set_to_set;
  3166. end;
  3167. function ttypeconvnode._first_cord_to_pointer : tnode;
  3168. begin
  3169. result:=first_cord_to_pointer;
  3170. end;
  3171. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  3172. begin
  3173. result:=first_ansistring_to_pchar;
  3174. end;
  3175. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  3176. begin
  3177. result:=first_arrayconstructor_to_set;
  3178. end;
  3179. function ttypeconvnode._first_class_to_intf : tnode;
  3180. begin
  3181. result:=first_class_to_intf;
  3182. end;
  3183. function ttypeconvnode._first_char_to_char : tnode;
  3184. begin
  3185. result:=first_char_to_char;
  3186. end;
  3187. function ttypeconvnode._first_string_to_string : tnode;
  3188. begin
  3189. result:=first_string_to_string;
  3190. end;
  3191. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  3192. const
  3193. firstconvert : array[tconverttype] of pointer = (
  3194. nil, { none }
  3195. @ttypeconvnode._first_nothing, {equal}
  3196. @ttypeconvnode._first_nothing, {not_possible}
  3197. @ttypeconvnode._first_string_to_string,
  3198. @ttypeconvnode._first_char_to_string,
  3199. @ttypeconvnode._first_char_to_chararray,
  3200. nil, { removed in typecheck_chararray_to_string }
  3201. @ttypeconvnode._first_cchar_to_pchar,
  3202. @ttypeconvnode._first_cstring_to_pchar,
  3203. @ttypeconvnode._first_cstring_to_int,
  3204. @ttypeconvnode._first_ansistring_to_pchar,
  3205. @ttypeconvnode._first_string_to_chararray,
  3206. nil, { removed in typecheck_chararray_to_string }
  3207. @ttypeconvnode._first_array_to_pointer,
  3208. @ttypeconvnode._first_pointer_to_array,
  3209. @ttypeconvnode._first_int_to_int,
  3210. @ttypeconvnode._first_int_to_bool,
  3211. @ttypeconvnode._first_bool_to_bool,
  3212. @ttypeconvnode._first_bool_to_int,
  3213. @ttypeconvnode._first_real_to_real,
  3214. @ttypeconvnode._first_int_to_real,
  3215. nil, { removed in typecheck_real_to_currency }
  3216. @ttypeconvnode._first_proc_to_procvar,
  3217. @ttypeconvnode._first_nil_to_methodprocvar,
  3218. @ttypeconvnode._first_arrayconstructor_to_set,
  3219. @ttypeconvnode._first_set_to_set,
  3220. @ttypeconvnode._first_cord_to_pointer,
  3221. @ttypeconvnode._first_nothing,
  3222. @ttypeconvnode._first_nothing,
  3223. @ttypeconvnode._first_class_to_intf,
  3224. @ttypeconvnode._first_char_to_char,
  3225. @ttypeconvnode._first_nothing,
  3226. @ttypeconvnode._first_nothing,
  3227. nil,
  3228. nil,
  3229. nil,
  3230. nil,
  3231. nil,
  3232. nil,
  3233. nil,
  3234. @ttypeconvnode._first_nothing
  3235. );
  3236. type
  3237. tprocedureofobject = function : tnode of object;
  3238. var
  3239. r : packed record
  3240. proc : pointer;
  3241. obj : pointer;
  3242. end;
  3243. begin
  3244. { this is a little bit dirty but it works }
  3245. { and should be quite portable too }
  3246. r.proc:=firstconvert[c];
  3247. r.obj:=self;
  3248. if not assigned(r.proc) then
  3249. internalerror(200312081);
  3250. first_call_helper:=tprocedureofobject(r)()
  3251. end;
  3252. function ttypeconvnode.pass_1 : tnode;
  3253. begin
  3254. if warn_pointer_to_signed then
  3255. cgmessage(type_w_pointer_to_signed);
  3256. result:=nil;
  3257. firstpass(left);
  3258. if codegenerror then
  3259. exit;
  3260. expectloc:=left.expectloc;
  3261. if nf_explicit in flags then
  3262. { check if the result could be in a register }
  3263. if (not(tstoreddef(resultdef).is_intregable) and
  3264. not(tstoreddef(resultdef).is_fpuregable)) or
  3265. ((left.resultdef.typ = floatdef) and
  3266. (resultdef.typ <> floatdef)) then
  3267. make_not_regable(left,[ra_addr_regable]);
  3268. result:=first_call_helper(convtype);
  3269. end;
  3270. function ttypeconvnode.retains_value_location:boolean;
  3271. begin
  3272. result:=(convtype=tc_equal) or
  3273. { typecasting from void is always allowed }
  3274. is_void(left.resultdef) or
  3275. (left.resultdef.typ=formaldef) or
  3276. { int 2 int with same size reuses same location, or for
  3277. tp7 mode also allow size < orignal size }
  3278. (
  3279. (convtype=tc_int_2_int) and
  3280. (
  3281. not is_bitpacked_access(left) and
  3282. (resultdef.size=left.resultdef.size) or
  3283. ((m_tp7 in current_settings.modeswitches) and
  3284. (resultdef.size<left.resultdef.size))
  3285. )
  3286. ) or
  3287. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  3288. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  3289. (nf_explicit in flags) and
  3290. (resultdef.size=left.resultdef.size)) or
  3291. { on managed platforms, converting an element to an open array
  3292. involves creating an actual array -> value location changes }
  3293. ((convtype=tc_elem_2_openarray) and
  3294. not(target_info.system in systems_managed_vm));
  3295. end;
  3296. function ttypeconvnode.assign_allowed:boolean;
  3297. begin
  3298. result:=retains_value_location;
  3299. { When using only a part of the value it can't be in a register since
  3300. that will load the value in a new register first }
  3301. { the same goes for changing the sign of equal-sized values which
  3302. are smaller than an entire register }
  3303. if result and
  3304. { don't try to check the size of an open array }
  3305. (is_open_array(resultdef) or
  3306. (resultdef.size<left.resultdef.size) or
  3307. ((resultdef.size=left.resultdef.size) and
  3308. (left.resultdef.size<sizeof(aint)) and
  3309. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  3310. make_not_regable(left,[ra_addr_regable]);
  3311. end;
  3312. function ttypeconvnode.docompare(p: tnode) : boolean;
  3313. begin
  3314. docompare :=
  3315. inherited docompare(p) and
  3316. (convtype = ttypeconvnode(p).convtype) and
  3317. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  3318. end;
  3319. procedure ttypeconvnode._second_int_to_int;
  3320. begin
  3321. second_int_to_int;
  3322. end;
  3323. procedure ttypeconvnode._second_string_to_string;
  3324. begin
  3325. second_string_to_string;
  3326. end;
  3327. procedure ttypeconvnode._second_cstring_to_pchar;
  3328. begin
  3329. second_cstring_to_pchar;
  3330. end;
  3331. procedure ttypeconvnode._second_cstring_to_int;
  3332. begin
  3333. second_cstring_to_int;
  3334. end;
  3335. procedure ttypeconvnode._second_string_to_chararray;
  3336. begin
  3337. second_string_to_chararray;
  3338. end;
  3339. procedure ttypeconvnode._second_array_to_pointer;
  3340. begin
  3341. second_array_to_pointer;
  3342. end;
  3343. procedure ttypeconvnode._second_pointer_to_array;
  3344. begin
  3345. second_pointer_to_array;
  3346. end;
  3347. procedure ttypeconvnode._second_chararray_to_string;
  3348. begin
  3349. second_chararray_to_string;
  3350. end;
  3351. procedure ttypeconvnode._second_char_to_string;
  3352. begin
  3353. second_char_to_string;
  3354. end;
  3355. procedure ttypeconvnode._second_int_to_real;
  3356. begin
  3357. second_int_to_real;
  3358. end;
  3359. procedure ttypeconvnode._second_real_to_real;
  3360. begin
  3361. second_real_to_real;
  3362. end;
  3363. procedure ttypeconvnode._second_cord_to_pointer;
  3364. begin
  3365. second_cord_to_pointer;
  3366. end;
  3367. procedure ttypeconvnode._second_proc_to_procvar;
  3368. begin
  3369. second_proc_to_procvar;
  3370. end;
  3371. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3372. begin
  3373. second_nil_to_methodprocvar;
  3374. end;
  3375. procedure ttypeconvnode._second_bool_to_int;
  3376. begin
  3377. second_bool_to_int;
  3378. end;
  3379. procedure ttypeconvnode._second_int_to_bool;
  3380. begin
  3381. second_int_to_bool;
  3382. end;
  3383. procedure ttypeconvnode._second_bool_to_bool;
  3384. begin
  3385. second_bool_to_bool;
  3386. end;
  3387. procedure ttypeconvnode._second_set_to_set;
  3388. begin
  3389. second_set_to_set;
  3390. end;
  3391. procedure ttypeconvnode._second_ansistring_to_pchar;
  3392. begin
  3393. second_ansistring_to_pchar;
  3394. end;
  3395. procedure ttypeconvnode._second_class_to_intf;
  3396. begin
  3397. second_class_to_intf;
  3398. end;
  3399. procedure ttypeconvnode._second_char_to_char;
  3400. begin
  3401. second_char_to_char;
  3402. end;
  3403. procedure ttypeconvnode._second_elem_to_openarray;
  3404. begin
  3405. second_elem_to_openarray;
  3406. end;
  3407. procedure ttypeconvnode._second_nothing;
  3408. begin
  3409. second_nothing;
  3410. end;
  3411. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3412. const
  3413. secondconvert : array[tconverttype] of pointer = (
  3414. @ttypeconvnode._second_nothing, {none}
  3415. @ttypeconvnode._second_nothing, {equal}
  3416. @ttypeconvnode._second_nothing, {not_possible}
  3417. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3418. @ttypeconvnode._second_char_to_string,
  3419. @ttypeconvnode._second_nothing, {char_to_charray}
  3420. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3421. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3422. @ttypeconvnode._second_cstring_to_pchar,
  3423. @ttypeconvnode._second_cstring_to_int,
  3424. @ttypeconvnode._second_ansistring_to_pchar,
  3425. @ttypeconvnode._second_string_to_chararray,
  3426. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3427. @ttypeconvnode._second_array_to_pointer,
  3428. @ttypeconvnode._second_pointer_to_array,
  3429. @ttypeconvnode._second_int_to_int,
  3430. @ttypeconvnode._second_int_to_bool,
  3431. @ttypeconvnode._second_bool_to_bool,
  3432. @ttypeconvnode._second_bool_to_int,
  3433. @ttypeconvnode._second_real_to_real,
  3434. @ttypeconvnode._second_int_to_real,
  3435. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3436. @ttypeconvnode._second_proc_to_procvar,
  3437. @ttypeconvnode._second_nil_to_methodprocvar,
  3438. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3439. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3440. @ttypeconvnode._second_cord_to_pointer,
  3441. @ttypeconvnode._second_nothing, { interface 2 string }
  3442. @ttypeconvnode._second_nothing, { interface 2 guid }
  3443. @ttypeconvnode._second_class_to_intf,
  3444. @ttypeconvnode._second_char_to_char,
  3445. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3446. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3447. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3448. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3449. @ttypeconvnode._second_nothing, { variant_2_enum }
  3450. @ttypeconvnode._second_nothing, { enum_2_variant }
  3451. @ttypeconvnode._second_nothing, { variant_2_interface }
  3452. @ttypeconvnode._second_nothing, { interface_2_variant }
  3453. @ttypeconvnode._second_nothing, { array_2_dynarray }
  3454. @ttypeconvnode._second_elem_to_openarray { elem_2_openarray }
  3455. );
  3456. type
  3457. tprocedureofobject = procedure of object;
  3458. var
  3459. r : packed record
  3460. proc : pointer;
  3461. obj : pointer;
  3462. end;
  3463. begin
  3464. { this is a little bit dirty but it works }
  3465. { and should be quite portable too }
  3466. r.proc:=secondconvert[c];
  3467. r.obj:=self;
  3468. tprocedureofobject(r)();
  3469. end;
  3470. {*****************************************************************************
  3471. TASNODE
  3472. *****************************************************************************}
  3473. function tasisnode.target_specific_typecheck: boolean;
  3474. begin
  3475. result:=false;
  3476. end;
  3477. function tasisnode.pass_typecheck: tnode;
  3478. var
  3479. hp : tnode;
  3480. begin
  3481. result:=nil;
  3482. typecheckpass(right);
  3483. typecheckpass(left);
  3484. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3485. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3486. if codegenerror then
  3487. exit;
  3488. if target_specific_typecheck then
  3489. begin
  3490. // ok
  3491. end
  3492. else if (right.resultdef.typ=classrefdef) then
  3493. begin
  3494. { left maybe an interface reference }
  3495. if is_interfacecom(left.resultdef) or
  3496. is_javainterface(left.resultdef) then
  3497. begin
  3498. { relation checks are not possible }
  3499. end
  3500. { or left must be a class }
  3501. else if is_class(left.resultdef) or
  3502. is_javaclass(left.resultdef) then
  3503. begin
  3504. { the operands must be related }
  3505. if (not(def_is_related(tobjectdef(left.resultdef),
  3506. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3507. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  3508. tobjectdef(left.resultdef)))) then
  3509. CGMessage2(type_e_classes_not_related,
  3510. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3511. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3512. end
  3513. else
  3514. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3515. case nodetype of
  3516. isn:
  3517. resultdef:=pasbool8type;
  3518. asn:
  3519. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3520. end;
  3521. end
  3522. else if is_interface(right.resultdef) or
  3523. is_dispinterface(right.resultdef) or
  3524. is_javainterface(right.resultdef) then
  3525. begin
  3526. case nodetype of
  3527. isn:
  3528. resultdef:=pasbool8type;
  3529. asn:
  3530. resultdef:=right.resultdef;
  3531. end;
  3532. { left is a class or interface }
  3533. if is_javainterface(right.resultdef) then
  3534. begin
  3535. if not is_java_class_or_interface(left.resultdef) then
  3536. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3537. end
  3538. else if not(is_class(left.resultdef) or
  3539. is_interfacecom(left.resultdef)) then
  3540. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  3541. else
  3542. begin
  3543. { load the GUID of the interface }
  3544. if (right.nodetype=typen) then
  3545. begin
  3546. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3547. begin
  3548. if assigned(tobjectdef(right.resultdef).iidstr) then
  3549. begin
  3550. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3551. tstringconstnode(hp).changestringtype(cshortstringtype);
  3552. right.free;
  3553. right:=hp;
  3554. end
  3555. else
  3556. internalerror(201006131);
  3557. end
  3558. else
  3559. begin
  3560. if assigned(tobjectdef(right.resultdef).iidguid) then
  3561. begin
  3562. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3563. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3564. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3565. right.free;
  3566. right:=hp;
  3567. end
  3568. else
  3569. internalerror(201006132);
  3570. end;
  3571. typecheckpass(right);
  3572. end;
  3573. end;
  3574. end
  3575. else
  3576. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3577. end;
  3578. {*****************************************************************************
  3579. TISNODE
  3580. *****************************************************************************}
  3581. constructor tisnode.create(l,r : tnode);
  3582. begin
  3583. inherited create(isn,l,r);
  3584. end;
  3585. constructor tisnode.create_internal(l, r: tnode);
  3586. begin
  3587. create(l,r);
  3588. include(flags,nf_internal);
  3589. end;
  3590. function tisnode.pass_1 : tnode;
  3591. var
  3592. procname: string;
  3593. begin
  3594. result:=nil;
  3595. { Passing a class type to an "is" expression cannot result in a class
  3596. of that type to be constructed.
  3597. }
  3598. include(right.flags,nf_ignore_for_wpo);
  3599. if is_class(left.resultdef) and
  3600. (right.resultdef.typ=classrefdef) then
  3601. result := ccallnode.createinternres('fpc_do_is',
  3602. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3603. resultdef)
  3604. else
  3605. begin
  3606. if is_class(left.resultdef) then
  3607. if is_shortstring(right.resultdef) then
  3608. procname := 'fpc_class_is_corbaintf'
  3609. else
  3610. procname := 'fpc_class_is_intf'
  3611. else
  3612. if right.resultdef.typ=classrefdef then
  3613. procname := 'fpc_intf_is_class'
  3614. else
  3615. procname := 'fpc_intf_is';
  3616. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3617. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3618. end;
  3619. left := nil;
  3620. right := nil;
  3621. //firstpass(call);
  3622. if codegenerror then
  3623. exit;
  3624. end;
  3625. { dummy pass_2, it will never be called, but we need one since }
  3626. { you can't instantiate an abstract class }
  3627. procedure tisnode.pass_generate_code;
  3628. begin
  3629. end;
  3630. {*****************************************************************************
  3631. TASNODE
  3632. *****************************************************************************}
  3633. constructor tasnode.create(l,r : tnode);
  3634. begin
  3635. inherited create(asn,l,r);
  3636. call := nil;
  3637. end;
  3638. constructor tasnode.create_internal(l,r : tnode);
  3639. begin
  3640. create(l,r);
  3641. include(flags,nf_internal);
  3642. end;
  3643. destructor tasnode.destroy;
  3644. begin
  3645. call.free;
  3646. inherited destroy;
  3647. end;
  3648. function tasnode.dogetcopy: tnode;
  3649. begin
  3650. result := inherited dogetcopy;
  3651. if assigned(call) then
  3652. tasnode(result).call := call.getcopy
  3653. else
  3654. tasnode(result).call := nil;
  3655. end;
  3656. function tasnode.docompare(p: tnode): boolean;
  3657. begin
  3658. result:=
  3659. inherited docompare(p) and
  3660. tasnode(p).call.isequal(call);
  3661. end;
  3662. function tasnode.pass_1 : tnode;
  3663. var
  3664. procname: string;
  3665. begin
  3666. result:=nil;
  3667. { Passing a class type to an "as" expression cannot result in a class
  3668. of that type to be constructed.
  3669. We could put this inside the if-block below, but this way it is
  3670. safe for sure even if the code below changes
  3671. }
  3672. if assigned(right) then
  3673. include(right.flags,nf_ignore_for_wpo);
  3674. if not assigned(call) then
  3675. begin
  3676. if is_class(left.resultdef) and
  3677. (right.resultdef.typ=classrefdef) then
  3678. call := ccallnode.createinternres('fpc_do_as',
  3679. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3680. resultdef)
  3681. else
  3682. begin
  3683. if is_class(left.resultdef) then
  3684. if is_shortstring(right.resultdef) then
  3685. procname := 'fpc_class_as_corbaintf'
  3686. else
  3687. procname := 'fpc_class_as_intf'
  3688. else
  3689. if right.resultdef.typ=classrefdef then
  3690. procname := 'fpc_intf_as_class'
  3691. else
  3692. procname := 'fpc_intf_as';
  3693. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3694. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3695. end;
  3696. left := nil;
  3697. right := nil;
  3698. firstpass(call);
  3699. if codegenerror then
  3700. exit;
  3701. expectloc:=call.expectloc;
  3702. end;
  3703. end;
  3704. end.