ncnv.pas 175 KB

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