ncnv.pas 163 KB

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