ncnv.pas 201 KB

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