ncnv.pas 176 KB

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