ncnv.pas 175 KB

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