ncnv.pas 179 KB

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