ncnv.pas 176 KB

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