2
0

ncnv.pas 203 KB

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