ncnv.pas 173 KB

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