ncnv.pas 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205
  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. if (cs_check_range in current_settings.localswitches) and (tsetconstnode(left).elements>0) and ((tsetconstnode(left).low<tsetdef(resultdef).setlow) or (tsetconstnode(left).high>tsetdef(resultdef).setmax)) then
  1638. Message(parser_e_range_check_error);
  1639. left.resultdef:=resultdef;
  1640. result:=left;
  1641. left:=nil;
  1642. exit;
  1643. end;
  1644. end;
  1645. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1646. var
  1647. newblock : tblocknode;
  1648. newstat : tstatementnode;
  1649. restemp : ttempcreatenode;
  1650. begin
  1651. if tstringdef(resultdef).stringtype=st_shortstring then
  1652. begin
  1653. newblock:=internalstatements(newstat);
  1654. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1655. addstatement(newstat,restemp);
  1656. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1657. ctemprefnode.create(restemp),nil))));
  1658. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1659. addstatement(newstat,ctemprefnode.create(restemp));
  1660. result:=newblock;
  1661. end
  1662. else if tstringdef(resultdef).stringtype=st_ansistring then
  1663. result := ccallnode.createinternres(
  1664. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1665. ccallparanode.create(
  1666. cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  1667. ccallparanode.create(left,nil)
  1668. ),
  1669. resultdef
  1670. )
  1671. else
  1672. result := ccallnode.createinternres(
  1673. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1674. ccallparanode.create(left,nil),resultdef);
  1675. left:=nil;
  1676. end;
  1677. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1678. begin
  1679. if assigned(tobjectdef(left.resultdef).iidstr) then
  1680. begin
  1681. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1682. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1683. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1684. tstringconstnode(result).changestringtype(cshortstringtype);
  1685. end
  1686. else
  1687. internalerror(2013112913);
  1688. end;
  1689. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1690. begin
  1691. if assigned(tobjectdef(left.resultdef).iidguid) then
  1692. begin
  1693. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1694. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1695. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1696. end
  1697. else
  1698. internalerror(2013112914);
  1699. end;
  1700. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1701. begin
  1702. if (actualtargetnode(@left)^.nodetype in [pointerconstn,niln]) then
  1703. CGMessage(type_e_no_addr_of_constant);
  1704. { a dynamic array is a pointer to an array, so to convert it to }
  1705. { an open array, we have to dereference it (JM) }
  1706. result:=ctypeconvnode.create_internal(left,cpointerdef.getreusable(resultdef));
  1707. typecheckpass(result);
  1708. { left is reused }
  1709. left:=nil;
  1710. result:=cderefnode.create(result);
  1711. include(TDerefNode(result).derefnodeflags,drnf_no_checkpointer);
  1712. end;
  1713. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1714. var
  1715. newblock : tblocknode;
  1716. newstat : tstatementnode;
  1717. restemp : ttempcreatenode;
  1718. begin
  1719. if tstringdef(resultdef).stringtype=st_shortstring then
  1720. begin
  1721. newblock:=internalstatements(newstat);
  1722. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1723. addstatement(newstat,restemp);
  1724. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1725. ctemprefnode.create(restemp),nil))));
  1726. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1727. addstatement(newstat,ctemprefnode.create(restemp));
  1728. result:=newblock;
  1729. end
  1730. else if tstringdef(resultdef).stringtype=st_ansistring then
  1731. begin
  1732. result:=ccallnode.createinternres(
  1733. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1734. ccallparanode.create(
  1735. cordconstnode.create(
  1736. getparaencoding(resultdef),
  1737. u16inttype,
  1738. true
  1739. ),
  1740. ccallparanode.create(left,nil)
  1741. ),
  1742. resultdef
  1743. );
  1744. end
  1745. else
  1746. result := ccallnode.createinternres(
  1747. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1748. ccallparanode.create(left,nil),resultdef);
  1749. left:=nil;
  1750. end;
  1751. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1752. begin
  1753. result := ccallnode.createinternres(
  1754. 'fpc_variant_to_dynarray',
  1755. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1756. ccallparanode.create(left,nil)
  1757. ),resultdef);
  1758. typecheckpass(result);
  1759. left:=nil;
  1760. end;
  1761. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1762. begin
  1763. result := ccallnode.createinternres(
  1764. 'fpc_dynarray_to_variant',
  1765. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1766. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1767. ),resultdef);
  1768. typecheckpass(result);
  1769. left:=nil;
  1770. end;
  1771. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1772. begin
  1773. if def_is_related(tobjectdef(resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1774. result := ccallnode.createinternres(
  1775. 'fpc_variant_to_idispatch',
  1776. ccallparanode.create(left,nil)
  1777. ,resultdef)
  1778. else
  1779. result := ccallnode.createinternres(
  1780. 'fpc_variant_to_interface',
  1781. ccallparanode.create(left,nil)
  1782. ,resultdef);
  1783. typecheckpass(result);
  1784. left:=nil;
  1785. end;
  1786. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1787. begin
  1788. if def_is_related(tobjectdef(left.resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1789. result := ccallnode.createinternres(
  1790. 'fpc_idispatch_to_variant',
  1791. ccallparanode.create(left,nil)
  1792. ,resultdef)
  1793. else
  1794. result := ccallnode.createinternres(
  1795. 'fpc_interface_to_variant',
  1796. ccallparanode.create(left,nil)
  1797. ,resultdef);
  1798. typecheckpass(result);
  1799. left:=nil;
  1800. end;
  1801. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1802. begin
  1803. result := ctypeconvnode.create_internal(left,sinttype);
  1804. result := ctypeconvnode.create_internal(result,resultdef);
  1805. typecheckpass(result);
  1806. { left is reused }
  1807. left := nil;
  1808. end;
  1809. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1810. begin
  1811. result := ctypeconvnode.create_internal(left,sinttype);
  1812. result := ctypeconvnode.create_internal(result,cvarianttype);
  1813. typecheckpass(result);
  1814. { left is reused }
  1815. left := nil;
  1816. end;
  1817. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1818. var
  1819. newstatement : tstatementnode;
  1820. temp : ttempcreatenode;
  1821. temp2 : ttempcreatenode;
  1822. begin
  1823. { create statements with call to getmem+initialize }
  1824. result:=internalstatements(newstatement);
  1825. { create temp for result }
  1826. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1827. addstatement(newstatement,temp);
  1828. { get temp for array of lengths }
  1829. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1830. addstatement(newstatement,temp2);
  1831. { one dimensional }
  1832. addstatement(newstatement,cassignmentnode.create(
  1833. ctemprefnode.create(temp2),
  1834. cordconstnode.create
  1835. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1836. { create call to fpc_dynarr_setlength }
  1837. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1838. ccallparanode.create(caddrnode.create_internal
  1839. (ctemprefnode.create(temp2)),
  1840. ccallparanode.create(cordconstnode.create
  1841. (1,s32inttype,true),
  1842. ccallparanode.create(caddrnode.create_internal
  1843. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1844. ccallparanode.create(
  1845. ctypeconvnode.create_internal(
  1846. ctemprefnode.create(temp),voidpointertype),
  1847. nil))))
  1848. ));
  1849. addstatement(newstatement,ctempdeletenode.create(temp2));
  1850. { copy ... }
  1851. addstatement(newstatement,cassignmentnode.create(
  1852. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1853. left
  1854. ));
  1855. { left is reused }
  1856. left:=nil;
  1857. { the last statement should return the value as
  1858. location and type, this is done be referencing the
  1859. temp and converting it first from a persistent temp to
  1860. normal temp }
  1861. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1862. addstatement(newstatement,ctemprefnode.create(temp));
  1863. end;
  1864. function ttypeconvnode.typecheck_elem_2_openarray : tnode;
  1865. begin
  1866. result:=nil;
  1867. end;
  1868. function ttypeconvnode.typecheck_arrayconstructor_to_dynarray : tnode;
  1869. var
  1870. newstatement,
  1871. assstatement : tstatementnode;
  1872. arrnode : ttempcreatenode;
  1873. temp2 : ttempcreatenode;
  1874. assnode : tnode;
  1875. paracount : integer;
  1876. elemnode : tarrayconstructornode;
  1877. begin
  1878. { assignment of []? }
  1879. if (
  1880. (left.nodetype=arrayconstructorn) and
  1881. not assigned(tarrayconstructornode(left).left)
  1882. ) or
  1883. is_emptyset(left)
  1884. then
  1885. begin
  1886. result:=cnilnode.create;
  1887. exit;
  1888. end;
  1889. if resultdef.typ<>arraydef then
  1890. internalerror(2017050102);
  1891. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1892. result:=internalstatements(newstatement);
  1893. { create temp for result }
  1894. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1895. addstatement(newstatement,arrnode);
  1896. paracount:=0;
  1897. { create an assignment call for each element }
  1898. assnode:=internalstatements(assstatement);
  1899. if left.nodetype=arrayconstructorrangen then
  1900. internalerror(2016021902);
  1901. elemnode:=tarrayconstructornode(left);
  1902. while assigned(elemnode) do
  1903. begin
  1904. { arr[i] := param_i }
  1905. if not assigned(elemnode.left) then
  1906. internalerror(2017050103);
  1907. addstatement(assstatement,
  1908. cassignmentnode.create(
  1909. cvecnode.create(
  1910. ctemprefnode.create(arrnode),
  1911. cordconstnode.create(paracount,tarraydef(totypedef).rangedef,false)),
  1912. elemnode.left));
  1913. elemnode.left:=nil;
  1914. inc(paracount);
  1915. elemnode:=tarrayconstructornode(elemnode.right);
  1916. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1917. internalerror(2016021903);
  1918. end;
  1919. { get temp for array of lengths }
  1920. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1921. addstatement(newstatement,temp2);
  1922. { create call to fpc_dynarr_setlength }
  1923. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1924. ccallparanode.create(caddrnode.create_internal
  1925. (ctemprefnode.create(temp2)),
  1926. ccallparanode.create(cordconstnode.create
  1927. (1,s32inttype,true),
  1928. ccallparanode.create(caddrnode.create_internal
  1929. (crttinode.create(tstoreddef(totypedef),initrtti,rdt_normal)),
  1930. ccallparanode.create(
  1931. ctypeconvnode.create_internal(
  1932. ctemprefnode.create(arrnode),voidpointertype),
  1933. nil))))
  1934. ));
  1935. { add assignment statememnts }
  1936. addstatement(newstatement,ctempdeletenode.create(temp2));
  1937. addstatement(newstatement,assnode);
  1938. { the last statement should return the value as
  1939. location and type, this is done be referencing the
  1940. temp and converting it first from a persistent temp to
  1941. normal temp }
  1942. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1943. addstatement(newstatement,ctemprefnode.create(arrnode));
  1944. end;
  1945. function ttypeconvnode.typecheck_arrayconstructor_to_array : tnode;
  1946. var
  1947. newstatement,
  1948. assstatement : tstatementnode;
  1949. arrnode : ttempcreatenode;
  1950. temp2 : ttempcreatenode;
  1951. assnode : tnode;
  1952. paracount : integer;
  1953. elemnode : tarrayconstructornode;
  1954. begin
  1955. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1956. result:=internalstatements(newstatement);
  1957. { create temp for result }
  1958. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1959. addstatement(newstatement,arrnode);
  1960. paracount:=0;
  1961. { create an assignment call for each element }
  1962. assnode:=internalstatements(assstatement);
  1963. if left.nodetype=arrayconstructorrangen then
  1964. internalerror(2020041402);
  1965. elemnode:=tarrayconstructornode(left);
  1966. while assigned(elemnode) do
  1967. begin
  1968. { arr[i] := param_i }
  1969. if not assigned(elemnode.left) then
  1970. internalerror(2020041403);
  1971. addstatement(assstatement,
  1972. cassignmentnode.create(
  1973. cvecnode.create(
  1974. ctemprefnode.create(arrnode),
  1975. cordconstnode.create(paracount+tarraydef(totypedef).lowrange,tarraydef(totypedef).rangedef,false)),
  1976. elemnode.left));
  1977. elemnode.left:=nil;
  1978. inc(paracount);
  1979. elemnode:=tarrayconstructornode(elemnode.right);
  1980. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1981. internalerror(2020041404);
  1982. end;
  1983. { get temp for array of lengths }
  1984. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1985. addstatement(newstatement,temp2);
  1986. { add assignment statememnts }
  1987. addstatement(newstatement,ctempdeletenode.create(temp2));
  1988. addstatement(newstatement,assnode);
  1989. { the last statement should return the value as
  1990. location and type, this is done be referencing the
  1991. temp and converting it first from a persistent temp to
  1992. normal temp }
  1993. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1994. addstatement(newstatement,ctemprefnode.create(arrnode));
  1995. end;
  1996. function ttypeconvnode._typecheck_int_to_int : tnode;
  1997. begin
  1998. result := typecheck_int_to_int;
  1999. end;
  2000. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  2001. begin
  2002. result := typecheck_cord_to_pointer;
  2003. end;
  2004. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  2005. begin
  2006. result := typecheck_chararray_to_string;
  2007. end;
  2008. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  2009. begin
  2010. result := typecheck_string_to_chararray;
  2011. end;
  2012. function ttypeconvnode._typecheck_string_to_string: tnode;
  2013. begin
  2014. result := typecheck_string_to_string;
  2015. end;
  2016. function ttypeconvnode._typecheck_char_to_string : tnode;
  2017. begin
  2018. result := typecheck_char_to_string;
  2019. end;
  2020. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  2021. begin
  2022. result := typecheck_char_to_chararray;
  2023. end;
  2024. function ttypeconvnode._typecheck_int_to_real : tnode;
  2025. begin
  2026. result := typecheck_int_to_real;
  2027. end;
  2028. function ttypeconvnode._typecheck_real_to_real : tnode;
  2029. begin
  2030. result := typecheck_real_to_real;
  2031. end;
  2032. function ttypeconvnode._typecheck_real_to_currency : tnode;
  2033. begin
  2034. result := typecheck_real_to_currency;
  2035. end;
  2036. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  2037. begin
  2038. result := typecheck_cchar_to_pchar;
  2039. end;
  2040. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  2041. begin
  2042. result := typecheck_cstring_to_pchar;
  2043. end;
  2044. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  2045. begin
  2046. result := typecheck_cstring_to_int;
  2047. end;
  2048. function ttypeconvnode._typecheck_char_to_char : tnode;
  2049. begin
  2050. result := typecheck_char_to_char;
  2051. end;
  2052. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  2053. begin
  2054. result := typecheck_arrayconstructor_to_set;
  2055. end;
  2056. function ttypeconvnode._typecheck_set_to_set : tnode;
  2057. begin
  2058. result := typecheck_set_to_set;
  2059. end;
  2060. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  2061. begin
  2062. result := typecheck_pchar_to_string;
  2063. end;
  2064. function ttypeconvnode._typecheck_interface_to_string : tnode;
  2065. begin
  2066. result := typecheck_interface_to_string;
  2067. end;
  2068. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  2069. begin
  2070. result := typecheck_interface_to_guid;
  2071. end;
  2072. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  2073. begin
  2074. result := typecheck_dynarray_to_openarray;
  2075. end;
  2076. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  2077. begin
  2078. result := typecheck_pwchar_to_string;
  2079. end;
  2080. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  2081. begin
  2082. result := typecheck_variant_to_dynarray;
  2083. end;
  2084. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  2085. begin
  2086. result := typecheck_dynarray_to_variant;
  2087. end;
  2088. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  2089. begin
  2090. result := typecheck_variant_to_enum;
  2091. end;
  2092. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  2093. begin
  2094. result := typecheck_enum_to_variant;
  2095. end;
  2096. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  2097. begin
  2098. result := typecheck_proc_to_procvar;
  2099. end;
  2100. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  2101. begin
  2102. result := typecheck_variant_to_interface;
  2103. end;
  2104. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  2105. begin
  2106. result := typecheck_interface_to_variant;
  2107. end;
  2108. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  2109. begin
  2110. result := typecheck_array_2_dynarray;
  2111. end;
  2112. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  2113. begin
  2114. result := typecheck_elem_2_openarray;
  2115. end;
  2116. function ttypeconvnode._typecheck_arrayconstructor_to_dynarray : tnode;
  2117. begin
  2118. result:=typecheck_arrayconstructor_to_dynarray;
  2119. end;
  2120. function ttypeconvnode._typecheck_arrayconstructor_to_array : tnode;
  2121. begin
  2122. result:=typecheck_arrayconstructor_to_array;
  2123. end;
  2124. function ttypeconvnode._typecheck_procvar_to_funcref : tnode;
  2125. begin
  2126. result:=typecheck_procvar_2_funcref;
  2127. end;
  2128. function ttypeconvnode._typecheck_anonproc_to_funcref : tnode;
  2129. begin
  2130. result:=typecheck_anonproc_2_funcref;
  2131. end;
  2132. function ttypeconvnode.target_specific_general_typeconv: boolean;
  2133. begin
  2134. result:=false;
  2135. end;
  2136. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  2137. begin
  2138. result:=false;
  2139. end;
  2140. class function ttypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  2141. begin
  2142. result:=false;
  2143. end;
  2144. type
  2145. tsym_mapping = record
  2146. oldsym:tsym;
  2147. newsym:tsym;
  2148. end;
  2149. psym_mapping = ^tsym_mapping;
  2150. function replace_self_sym(var n:tnode;arg:pointer):foreachnoderesult;
  2151. var
  2152. mapping : psym_mapping absolute arg;
  2153. ld : tloadnode;
  2154. begin
  2155. if n.nodetype=loadn then
  2156. begin
  2157. ld:=tloadnode(n);
  2158. if ld.symtableentry=mapping^.oldsym then
  2159. begin
  2160. ld.symtableentry:=mapping^.newsym;
  2161. { make sure that the node is processed again }
  2162. ld.resultdef:=nil;
  2163. if assigned(ld.left) then
  2164. begin
  2165. { no longer loaded through the frame pointer }
  2166. ld.left.free;
  2167. ld.left:=nil;
  2168. end;
  2169. typecheckpass(n);
  2170. end;
  2171. end;
  2172. result:=fen_true;
  2173. end;
  2174. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  2175. function is_self_sym(sym:tsym):boolean;
  2176. begin
  2177. result:=(sym.typ in [localvarsym,paravarsym]) and
  2178. (vo_is_self in tabstractvarsym(sym).varoptions);
  2179. end;
  2180. var
  2181. pd : tabstractprocdef;
  2182. copytype : tproccopytyp;
  2183. source: pnode;
  2184. fpsym,
  2185. selfsym,
  2186. sym : tsym;
  2187. mapping : tsym_mapping;
  2188. pi : tprocinfo;
  2189. i : longint;
  2190. begin
  2191. result:=nil;
  2192. pd:=tabstractprocdef(left.resultdef);
  2193. { create procvardef (default for create_proc_to_procvar is voiddef,
  2194. but if later a regular inserttypeconvnode() is used to insert a type
  2195. conversion to the actual procvardef, totypedef will be set to the
  2196. real procvartype that we are converting to) }
  2197. if assigned(totypedef) and
  2198. (totypedef.typ=procvardef) then
  2199. begin
  2200. { have to do this in typecheckpass so that it's triggered for
  2201. typed constant declarations }
  2202. if po_is_block in tprocvardef(totypedef).procoptions then
  2203. begin
  2204. { can only convert from procdef to procvardef, but in the mean
  2205. time other type conversions may have been inserted (pointers,
  2206. proc2procvar, ...) }
  2207. source:=actualtargetnode(@left);
  2208. while (source^.nodetype=typeconvn) and
  2209. (ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
  2210. (is_void(source^.resultdef) or
  2211. (source^.resultdef.typ=procvardef)) do
  2212. begin
  2213. { won't skip proc2procvar }
  2214. source:=actualtargetnode(@ttypeconvnode(source^).left);
  2215. end;
  2216. if (source^.nodetype=loadn) and
  2217. (source^.resultdef.typ=procdef) and
  2218. not is_nested_pd(tprocdef(source^.resultdef)) and
  2219. not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
  2220. begin
  2221. result:=generate_block_for_procaddr(tloadnode(source^));
  2222. exit;
  2223. end
  2224. else
  2225. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2226. end
  2227. else if (pd.typ=procdef) and
  2228. (po_anonymous in pd.procoptions) then
  2229. begin
  2230. if left.nodetype<>loadn then
  2231. internalerror(2021062402);
  2232. { get rid of any potential framepointer loading; if it's necessary
  2233. (for a nested procvar for example) it will be added again }
  2234. if assigned(tloadnode(left).left) and (tloadnode(left).left.nodetype=loadparentfpn) then
  2235. begin
  2236. tloadnode(left).left.free;
  2237. tloadnode(left).left:=nil;
  2238. tloadnode(left).resultdef:=nil;
  2239. end;
  2240. if tprocvardef(totypedef).is_methodpointer then
  2241. begin
  2242. if assigned(tprocdef(pd).capturedsyms) and
  2243. (
  2244. (tprocdef(pd).capturedsyms.count>1) or
  2245. (
  2246. (tprocdef(pd).capturedsyms.count=1) and
  2247. not is_self_sym(tsym(pcapturedsyminfo(tprocdef(pd).capturedsyms[0])^.sym))
  2248. )
  2249. ) then
  2250. begin
  2251. result:=cerrornode.create;
  2252. exit;
  2253. end;
  2254. { so that insert_self_and_vmt_para correctly inserts the
  2255. Self, cause it otherwise skips that for anonymous functions }
  2256. include(pd.procoptions,po_methodpointer);
  2257. { we know this only captures Self, so we can move the
  2258. anonymous function to normal function level }
  2259. pd.parast.symtablelevel:=normal_function_level;
  2260. tprocdef(pd).localst.symtablelevel:=normal_function_level;
  2261. selfsym:=nil;
  2262. fpsym:=nil;
  2263. { find the framepointer parameter and an eventual self }
  2264. for i:=0 to tprocdef(pd).parast.symlist.count-1 do
  2265. begin
  2266. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2267. if sym.typ<>paravarsym then
  2268. continue;
  2269. if vo_is_parentfp in tparavarsym(sym).varoptions then
  2270. fpsym:=sym;
  2271. if vo_is_self in tparavarsym(sym).varoptions then
  2272. selfsym:=sym;
  2273. if assigned(fpsym) and assigned(selfsym) then
  2274. break;
  2275. end;
  2276. if assigned(fpsym) then
  2277. tprocdef(pd).parast.symlist.remove(fpsym);
  2278. { if we don't have a self parameter already we need to
  2279. insert a suitable one }
  2280. if not assigned(selfsym) then
  2281. begin
  2282. { replace the self symbol by the new parameter if it was
  2283. captured }
  2284. if assigned(tprocdef(pd).capturedsyms) and
  2285. (tprocdef(pd).capturedsyms.count>0) then
  2286. begin
  2287. if not assigned(tprocdef(pd).struct) then
  2288. { we can't use the captured symbol for the struct as that
  2289. might be the self of a type helper, thus we need to find
  2290. the parent procinfo that provides the Self }
  2291. tprocdef(pd).struct:=current_procinfo.get_normal_proc.procdef.struct;
  2292. if not assigned(tprocdef(pd).struct) then
  2293. internalerror(2021062204);
  2294. insert_self_and_vmt_para(pd);
  2295. mapping.oldsym:=tsym(pcapturedsyminfo(tprocdef(pd).capturedsyms[0])^.sym);
  2296. mapping.newsym:=nil;
  2297. { find the new self parameter }
  2298. for i:=0 to tprocdef(pd).parast.symlist.count-1 do
  2299. begin
  2300. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2301. if (sym.typ=paravarsym) and (vo_is_self in tparavarsym(sym).varoptions) then
  2302. begin
  2303. mapping.newsym:=sym;
  2304. break;
  2305. end;
  2306. end;
  2307. if not assigned(mapping.newsym) then
  2308. internalerror(2021062202);
  2309. { the anonymous function can only be a direct child of the
  2310. current_procinfo }
  2311. pi:=current_procinfo.get_first_nestedproc;
  2312. while assigned(pi) do
  2313. begin
  2314. if pi.procdef=pd then
  2315. break;
  2316. pi:=tprocinfo(pi.next);
  2317. end;
  2318. if not assigned(pi) then
  2319. internalerror(2021062203);
  2320. { replace all uses of the captured Self by the new Self
  2321. parameter }
  2322. foreachnodestatic(pm_preprocess,tcgprocinfo(pi).code,@replace_self_sym,@mapping);
  2323. end
  2324. else
  2325. begin
  2326. { for a nested function of a method struct is already
  2327. set }
  2328. if not assigned(tprocdef(pd).struct) then
  2329. { simply add a TObject as Self parameter }
  2330. tprocdef(pd).struct:=class_tobject;
  2331. insert_self_and_vmt_para(pd);
  2332. { there is no self, so load a nil value }
  2333. tloadnode(left).set_mp(cnilnode.create);
  2334. end;
  2335. end;
  2336. { the anonymous function no longer adheres to the nested
  2337. calling convention }
  2338. exclude(pd.procoptions,po_delphi_nested_cc);
  2339. tprocdef(pd).calcparas;
  2340. if not assigned(tloadnode(left).left) then
  2341. tloadnode(left).set_mp(load_self_node);
  2342. end
  2343. else if tprocvardef(totypedef).is_addressonly then
  2344. begin
  2345. if assigned(tprocdef(pd).capturedsyms) and (tprocdef(pd).capturedsyms.count>0) then
  2346. begin
  2347. result:=cerrornode.create;
  2348. exit;
  2349. end;
  2350. { remove framepointer and Self parameters }
  2351. for i:=tprocdef(pd).parast.symlist.count-1 downto 0 do
  2352. begin
  2353. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2354. if (sym.typ=paravarsym) and (tparavarsym(sym).varoptions*[vo_is_parentfp,vo_is_self]<>[]) then
  2355. tprocdef(pd).parast.symlist.delete(i);
  2356. end;
  2357. { the anonymous function no longer adheres to the nested
  2358. calling convention }
  2359. exclude(pd.procoptions,po_delphi_nested_cc);
  2360. { we don't need to look through the existing nodes, cause
  2361. the parameter was never used anyway }
  2362. tprocdef(pd).calcparas;
  2363. end
  2364. else
  2365. begin
  2366. { this is a nested function pointer, so ensure that the
  2367. anonymous function is handled as such }
  2368. if assigned(tprocdef(pd).capturedsyms) and
  2369. (tprocdef(pd).capturedsyms.count>0) and
  2370. (left.nodetype=loadn) then
  2371. begin
  2372. tloadnode(left).left:=cloadparentfpnode.create(tprocdef(tloadnode(left).symtable.defowner),lpf_forload);
  2373. typecheckpass(tloadnode(left).left);
  2374. pi:=current_procinfo.get_first_nestedproc;
  2375. while assigned(pi) do
  2376. begin
  2377. if pi.procdef=pd then
  2378. break;
  2379. pi:=tprocinfo(pi.next);
  2380. end;
  2381. pi.set_needs_parentfp(tprocdef(tloadnode(left).symtable.defowner).parast.symtablelevel);
  2382. end;
  2383. end;
  2384. end;
  2385. resultdef:=totypedef;
  2386. end
  2387. else
  2388. begin
  2389. { only need the address of the method? this is needed
  2390. for @tobject.create. In this case there will be a loadn without
  2391. a methodpointer. }
  2392. if (left.nodetype=loadn) and
  2393. not assigned(tloadnode(left).left) and
  2394. (not(m_nested_procvars in current_settings.modeswitches) or
  2395. not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
  2396. copytype:=pc_address_only
  2397. else
  2398. copytype:=pc_normal;
  2399. resultdef:=cprocvardef.getreusableprocaddr(pd,copytype);
  2400. end;
  2401. end;
  2402. function ttypeconvnode.typecheck_procvar_2_funcref : tnode;
  2403. var
  2404. capturer : tsym;
  2405. intfdef : tdef;
  2406. ld,blck,hp : tnode;
  2407. stmt : tstatementnode;
  2408. begin
  2409. result:=nil;
  2410. if not(m_tp_procvar in current_settings.modeswitches) and
  2411. is_invokable(resultdef) and
  2412. (left.nodetype=typeconvn) and
  2413. (ttypeconvnode(left).convtype=tc_proc_2_procvar) and
  2414. is_methodpointer(left.resultdef) and
  2415. (po_classmethod in tprocvardef(left.resultdef).procoptions) and
  2416. not(po_staticmethod in tprocvardef(left.resultdef).procoptions) and
  2417. (proc_to_funcref_equal(tprocdef(ttypeconvnode(left).left.resultdef),tobjectdef(resultdef))>=te_convert_l1) then
  2418. begin
  2419. hp:=left;
  2420. left:=ttypeconvnode(left).left;
  2421. if (left.nodetype=loadn) and
  2422. not assigned(tloadnode(left).left) then
  2423. tloadnode(left).set_mp(cloadvmtaddrnode.create(ctypenode.create(tdef(tloadnode(left).symtable.defowner))));
  2424. left:=ctypeconvnode.create_proc_to_procvar(left);
  2425. ttypeconvnode(left).totypedef:=resultdef;
  2426. typecheckpass(left);
  2427. ttypeconvnode(hp).left:=nil;
  2428. hp.free;
  2429. end;
  2430. intfdef:=capturer_add_procvar_or_proc(current_procinfo,left,capturer,hp);
  2431. if assigned(intfdef) then
  2432. begin
  2433. if assigned(capturer) then
  2434. ld:=cloadnode.create(capturer,capturer.owner)
  2435. else
  2436. ld:=cnilnode.create;
  2437. result:=ctypeconvnode.create_internal(
  2438. ctypeconvnode.create_internal(
  2439. ld,
  2440. intfdef),
  2441. totypedef);
  2442. if assigned(hp) then
  2443. begin
  2444. blck:=internalstatements(stmt);
  2445. addstatement(stmt,cassignmentnode.create(hp,left));
  2446. left:=nil;
  2447. addstatement(stmt,result);
  2448. result:=blck;
  2449. end;
  2450. end;
  2451. if not assigned(result) then
  2452. result:=cerrornode.create;
  2453. end;
  2454. function ttypeconvnode.typecheck_anonproc_2_funcref : tnode;
  2455. var
  2456. capturer : tsym;
  2457. intfdef : tdef;
  2458. ldnode : tnode;
  2459. begin
  2460. intfdef:=capturer_add_anonymous_proc(current_procinfo,tprocdef(left.resultdef),capturer);
  2461. if assigned(intfdef) then
  2462. begin
  2463. if assigned(capturer) then
  2464. ldnode:=cloadnode.create(capturer,capturer.owner)
  2465. else
  2466. ldnode:=cnilnode.create;
  2467. result:=ctypeconvnode.create_internal(
  2468. ctypeconvnode.create_internal(
  2469. ldnode,
  2470. intfdef),
  2471. totypedef);
  2472. end
  2473. else
  2474. result:=cerrornode.create;
  2475. end;
  2476. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  2477. const
  2478. resultdefconvert : array[tconverttype] of pointer = (
  2479. {none} nil,
  2480. {equal} nil,
  2481. {not_possible} nil,
  2482. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  2483. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  2484. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  2485. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  2486. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  2487. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  2488. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  2489. { ansistring_2_pchar } nil,
  2490. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  2491. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  2492. { array_2_pointer } nil,
  2493. { pointer_2_array } nil,
  2494. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  2495. { int_2_bool } nil,
  2496. { bool_2_bool } nil,
  2497. { bool_2_int } nil,
  2498. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  2499. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  2500. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  2501. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  2502. { nil_2_methodprocvar } nil,
  2503. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  2504. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  2505. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  2506. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  2507. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  2508. { class_2_intf } nil,
  2509. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  2510. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  2511. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  2512. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  2513. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  2514. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  2515. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  2516. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  2517. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  2518. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  2519. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray,
  2520. { arrayconstructor_2_dynarray } @ttypeconvnode._typecheck_arrayconstructor_to_dynarray,
  2521. { arrayconstructor_2_array } @ttypeconvnode._typecheck_arrayconstructor_to_array,
  2522. { anonproc_2_funcref } @ttypeconvnode._typecheck_anonproc_to_funcref,
  2523. { procvar_2_funcref } @ttypeconvnode._typecheck_procvar_to_funcref
  2524. );
  2525. type
  2526. tprocedureofobject = function : tnode of object;
  2527. var
  2528. r : TMethod;
  2529. begin
  2530. result:=nil;
  2531. { this is a little bit dirty but it works }
  2532. { and should be quite portable too }
  2533. r.Code:=resultdefconvert[c];
  2534. r.Data:=self;
  2535. if assigned(r.Code) then
  2536. result:=tprocedureofobject(r)();
  2537. end;
  2538. function ttypeconvnode.pass_typecheck:tnode;
  2539. var
  2540. hdef : tdef;
  2541. hp : tnode;
  2542. currprocdef : tabstractprocdef;
  2543. aprocdef : tprocdef;
  2544. eq : tequaltype;
  2545. cdoptions : tcompare_defs_options;
  2546. selfnode : tnode;
  2547. newblock: tblocknode;
  2548. newstatement: tstatementnode;
  2549. tempnode: ttempcreatenode;
  2550. begin
  2551. result:=nil;
  2552. resultdef:=totypedef;
  2553. typecheckpass(left);
  2554. if codegenerror then
  2555. exit;
  2556. { When absolute force tc_equal }
  2557. if (nf_absolute in flags) then
  2558. begin
  2559. convtype:=tc_equal;
  2560. { we need to check regability only if something is really regable }
  2561. if ((tstoreddef(left.resultdef).is_intregable) or
  2562. (tstoreddef(resultdef).is_fpuregable)) and
  2563. (
  2564. (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  2565. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) or
  2566. { like in pdecvar.read_absolute(): if the size changes, the
  2567. register size would also have to change (but second_nothing
  2568. does not handle this) }
  2569. (tstoreddef(resultdef).size<>tstoreddef(left.resultdef).size)) then
  2570. make_not_regable(left,[ra_addr_regable]);
  2571. exit;
  2572. end;
  2573. { tp procvar support. Skip typecasts to procvar, record or set. Those
  2574. convert on the procvar value. This is used to access the
  2575. fields of a methodpointer }
  2576. if not(nf_load_procvar in flags) and
  2577. not(resultdef.typ in [procvardef,recorddef,setdef]) and
  2578. not is_invokable(resultdef) and
  2579. { in case of interface assignments of invokables they'll be converted
  2580. to voidpointertype using an internal conversions; we must not call
  2581. the invokable in that case }
  2582. not (
  2583. (nf_internal in flags) and
  2584. is_invokable(left.resultdef)
  2585. ) then
  2586. maybe_call_procvar(left,true);
  2587. if target_specific_general_typeconv then
  2588. exit;
  2589. if convtype=tc_none then
  2590. begin
  2591. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  2592. { overloaded operators require calls, which is not possible inside
  2593. a constant declaration }
  2594. if (block_type<>bt_const) and
  2595. not(nf_internal in flags) then
  2596. include(cdoptions,cdo_check_operator);
  2597. if nf_explicit in flags then
  2598. include(cdoptions,cdo_explicit);
  2599. if nf_internal in flags then
  2600. include(cdoptions,cdo_internal);
  2601. aprocdef:=nil;
  2602. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  2603. case eq of
  2604. te_exact,
  2605. te_equal :
  2606. begin
  2607. result := simplify(false);
  2608. if assigned(result) then
  2609. exit;
  2610. { in case of bitpacked accesses, the original type must
  2611. remain so that not too many/few bits are laoded }
  2612. if is_bitpacked_access(left) then
  2613. convtype:=tc_int_2_int;
  2614. { Only leave when there is no conversion to do.
  2615. We can still need to call a conversion routine,
  2616. like the routine to convert a stringconstnode }
  2617. if (convtype in [tc_equal,tc_not_possible]) and
  2618. { some conversions, like dynarray to pointer in Delphi
  2619. mode, must not be removed, because then we get memory
  2620. leaks due to missing temp finalization }
  2621. (not is_managed_type(left.resultdef) or
  2622. { different kinds of refcounted types may need calls
  2623. to different kinds of refcounting helpers }
  2624. (resultdef=left.resultdef)) then
  2625. begin
  2626. {$ifdef llvm}
  2627. { we still may have to insert a type conversion at the
  2628. llvm level }
  2629. if (blocktype<>bt_const) and
  2630. (left.resultdef<>resultdef) and
  2631. { if unspecialised generic -> we won't generate any code
  2632. for this, and keeping the type conversion node will
  2633. cause valid_for_assign to fail because the typecast will be from/to something of 0
  2634. bytes to/from something with a non-zero size }
  2635. not is_typeparam(left.resultdef) and
  2636. not is_typeparam(resultdef) then
  2637. result:=nil
  2638. else
  2639. {$endif llvm}
  2640. begin
  2641. left.resultdef:=resultdef;
  2642. if (nf_explicit in flags) and (left.nodetype = addrn) then
  2643. include(taddrnode(left).addrnodeflags,anf_typedaddr);
  2644. result:=left;
  2645. left:=nil;
  2646. end;
  2647. exit;
  2648. end;
  2649. end;
  2650. te_convert_l1,
  2651. te_convert_l2,
  2652. te_convert_l3,
  2653. te_convert_l4,
  2654. te_convert_l5,
  2655. te_convert_l6:
  2656. { nothing to do }
  2657. ;
  2658. te_convert_operator :
  2659. begin
  2660. include(current_procinfo.flags,pi_do_call);
  2661. addsymref(aprocdef.procsym,aprocdef);
  2662. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[],nil);
  2663. { tell explicitly which def we must use !! (PM) }
  2664. tcallnode(hp).procdefinition:=aprocdef;
  2665. left:=nil;
  2666. result:=hp;
  2667. exit;
  2668. end;
  2669. te_incompatible :
  2670. begin
  2671. { convert an array constructor to a set so that we still get
  2672. the error "set of Y incompatible to Z" instead of "array of
  2673. X incompatible to Z" }
  2674. if (resultdef.typ<>arraydef) and
  2675. is_array_constructor(left.resultdef) then
  2676. begin
  2677. arrayconstructor_to_set(left);
  2678. typecheckpass(left);
  2679. end;
  2680. { Procedures have a resultdef of voiddef and functions of their
  2681. own resultdef. They will therefore always be incompatible with
  2682. a procvar. Because isconvertable cannot check for procedures we
  2683. use an extra check for them.}
  2684. if (left.nodetype=calln) and
  2685. (tcallnode(left).required_para_count=0) and
  2686. (
  2687. (resultdef.typ=procvardef) or
  2688. is_invokable(resultdef)
  2689. ) and
  2690. (
  2691. (m_tp_procvar in current_settings.modeswitches) or
  2692. (m_mac_procvar in current_settings.modeswitches)
  2693. ) then
  2694. begin
  2695. if assigned(tcallnode(left).right) then
  2696. begin
  2697. { this is already a procvar, if it is really equal
  2698. is checked below }
  2699. convtype:=tc_equal;
  2700. hp:=tcallnode(left).right.getcopy;
  2701. currprocdef:=tabstractprocdef(hp.resultdef);
  2702. end
  2703. else
  2704. begin
  2705. if resultdef.typ=procvardef then
  2706. begin
  2707. convtype:=tc_proc_2_procvar;
  2708. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  2709. end
  2710. else
  2711. begin
  2712. convtype:=tc_procvar_2_funcref;
  2713. currprocdef:=tprocsym(tcallnode(left).symtableprocentry).find_procdef_byfuncrefdef(tobjectdef(resultdef));
  2714. end;
  2715. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  2716. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2717. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2718. begin
  2719. selfnode:=tcallnode(left).methodpointer;
  2720. if assigned(selfnode) then
  2721. begin
  2722. { in case the nodetype is a typen, avoid the internal error
  2723. in set_mp and instead let the code error out normally }
  2724. if selfnode.nodetype<>typen then
  2725. tloadnode(hp).set_mp(selfnode.getcopy)
  2726. end
  2727. else
  2728. tloadnode(hp).set_mp(load_self_node);
  2729. end;
  2730. typecheckpass(hp);
  2731. end;
  2732. left.free;
  2733. left:=hp;
  2734. { Now check if the procedure we are going to assign to
  2735. the procvar, is compatible with the procvar's type }
  2736. if not(nf_explicit in flags) and
  2737. (
  2738. (
  2739. (resultdef.typ=procvardef) and
  2740. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible)
  2741. ) or (
  2742. is_invokable(resultdef) and
  2743. (proc_to_funcref_equal(currprocdef,tobjectdef(resultdef))=te_incompatible)
  2744. )
  2745. ) then
  2746. IncompatibleTypes(left.resultdef,resultdef)
  2747. else
  2748. result:=typecheck_call_helper(convtype);
  2749. exit;
  2750. end
  2751. else if maybe_global_proc_to_nested(left,resultdef) or
  2752. maybe_classmethod_to_methodprocvar(left,resultdef) then
  2753. begin
  2754. result:=left;
  2755. left:=nil;
  2756. exit;
  2757. end;
  2758. { Handle explicit type conversions }
  2759. if nf_explicit in flags then
  2760. begin
  2761. { do common tc_equal cast, except when dealing with proc -> procvar
  2762. (may have to get rid of method pointer) }
  2763. if (left.resultdef.typ<>procdef) or
  2764. (resultdef.typ<>procvardef) then
  2765. convtype:=tc_equal
  2766. else
  2767. convtype:=tc_proc_2_procvar;
  2768. { ordinal constants can be resized to 1,2,4,8 bytes }
  2769. if (left.nodetype=ordconstn) then
  2770. begin
  2771. { Insert typeconv for ordinal to the correct size first on left, after
  2772. that the other conversion can be done }
  2773. hdef:=nil;
  2774. case longint(resultdef.size) of
  2775. 1 :
  2776. hdef:=s8inttype;
  2777. 2 :
  2778. hdef:=s16inttype;
  2779. 4 :
  2780. hdef:=s32inttype;
  2781. 8 :
  2782. hdef:=s64inttype;
  2783. end;
  2784. { we need explicit, because it can also be an enum }
  2785. if assigned(hdef) then
  2786. inserttypeconv_internal(left,hdef)
  2787. else
  2788. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2789. end;
  2790. { class/interface to class/interface, with checkobject support }
  2791. if is_class_or_interface_or_objc(resultdef) and
  2792. is_class_or_interface_or_objc(left.resultdef) then
  2793. begin
  2794. { check if the types are related }
  2795. if not(nf_internal in flags) and
  2796. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2797. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2798. begin
  2799. { Give an error when typecasting class to interface, this is compatible
  2800. with delphi }
  2801. if is_interface(resultdef) and
  2802. not is_interface(left.resultdef) then
  2803. CGMessage2(type_e_classes_not_related,
  2804. FullTypeName(left.resultdef,resultdef),
  2805. FullTypeName(resultdef,left.resultdef))
  2806. else
  2807. CGMessage2(type_w_classes_not_related,
  2808. FullTypeName(left.resultdef,resultdef),
  2809. FullTypeName(resultdef,left.resultdef))
  2810. end;
  2811. { Add runtime check? }
  2812. if not is_objc_class_or_protocol(resultdef) and
  2813. not is_objc_class_or_protocol(left.resultdef) and
  2814. (cs_check_object in current_settings.localswitches) and
  2815. not(nf_internal in flags) then
  2816. begin
  2817. { we can translate the typeconvnode to 'as' when
  2818. typecasting to a class or interface }
  2819. { we need to make sure the result can still be
  2820. passed as a var parameter }
  2821. newblock:=internalstatements(newstatement);
  2822. if (valid_for_var(left,false)) then
  2823. begin
  2824. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2825. addstatement(newstatement,tempnode);
  2826. addstatement(newstatement,cassignmentnode.create(
  2827. ctemprefnode.create(tempnode),
  2828. caddrnode.create_internal(left)));
  2829. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2830. end
  2831. else
  2832. begin
  2833. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2834. addstatement(newstatement,tempnode);
  2835. addstatement(newstatement,cassignmentnode.create(
  2836. ctemprefnode.create(tempnode),
  2837. left));
  2838. left:=ctemprefnode.create(tempnode);
  2839. end;
  2840. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2841. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2842. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2843. left:=nil;
  2844. result:=newblock;
  2845. exit;
  2846. end;
  2847. end
  2848. else
  2849. begin
  2850. { only if the same size or formal def, and }
  2851. { don't allow type casting of constants to }
  2852. { structured types }
  2853. if not(
  2854. (left.resultdef.typ=formaldef) or
  2855. {$ifdef jvm}
  2856. { enums /are/ class instances on the JVM
  2857. platform }
  2858. (((left.resultdef.typ=enumdef) and
  2859. (resultdef.typ=objectdef)) or
  2860. ((resultdef.typ=enumdef) and
  2861. (left.resultdef.typ=objectdef))) or
  2862. {$endif}
  2863. (
  2864. is_void(left.resultdef) and
  2865. (left.nodetype=derefn)
  2866. ) or
  2867. (
  2868. not(is_open_array(left.resultdef)) and
  2869. not(is_array_constructor(left.resultdef)) and
  2870. not(is_array_of_const(left.resultdef)) and
  2871. { if the from type is an anonymous function then
  2872. don't blindly convert it if the size is the same
  2873. as compare_defs_ext already determined that the
  2874. anonymous function is not compatible }
  2875. not(
  2876. (left.resultdef.typ=procdef) and
  2877. (po_anonymous in tprocdef(left.resultdef).procoptions)
  2878. ) and
  2879. (left.resultdef.size=resultdef.size) and
  2880. { disallow casts of const nodes }
  2881. (not is_constnode(left) or
  2882. { however, there are some exceptions }
  2883. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2884. filedef,variantdef,objectdef]) or
  2885. is_class_or_interface_or_objc(resultdef) or
  2886. { the softfloat code generates casts <const. float> to record }
  2887. (nf_internal in flags)
  2888. ))
  2889. )
  2890. ) then
  2891. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2892. else
  2893. begin
  2894. { perform target-specific explicit typecast
  2895. checks }
  2896. if target_specific_explicit_typeconv then
  2897. begin
  2898. result:=simplify(false);
  2899. exit;
  2900. end;
  2901. end;
  2902. end;
  2903. end
  2904. else
  2905. IncompatibleTypes(left.resultdef,resultdef);
  2906. end;
  2907. end;
  2908. end;
  2909. { Give hint or warning for unportable code, exceptions are
  2910. - typecasts from constants
  2911. - void }
  2912. if not(nf_internal in flags) and
  2913. (left.nodetype<>ordconstn) and
  2914. not(is_void(left.resultdef)) and
  2915. (((left.resultdef.typ=orddef) and
  2916. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2917. ((resultdef.typ=orddef) and
  2918. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2919. begin
  2920. {Converting pointers to signed integers is a bad idea. Warn.}
  2921. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2922. { Give a warning when sizes don't match, because then info will be lost }
  2923. if left.resultdef.size=resultdef.size then
  2924. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2925. else
  2926. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2927. end;
  2928. { tc_cord_2_pointer still requires a type check, which
  2929. simplify does not do }
  2930. if (convtype<>tc_cord_2_pointer) then
  2931. begin
  2932. result := simplify(false);
  2933. if assigned(result) then
  2934. exit;
  2935. end;
  2936. { now call the resultdef helper to do constant folding }
  2937. result:=typecheck_call_helper(convtype);
  2938. end;
  2939. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2940. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2941. this applies is powerpc64
  2942. }
  2943. {$ifndef CPUNO32BITOPS}
  2944. { checks whether we can safely remove typeconversions to bigger types
  2945. in case range and overflow checking are off, and in case
  2946. the result of this node tree is downcasted again to a
  2947. smaller type value afterwards,
  2948. the smaller types being allowed are described by validints, ordinal constants must fit into l..h
  2949. We do this on 64 bit CPUs as well, they benefit from it as well }
  2950. function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;const l,h : Tconstexprint): boolean;
  2951. var
  2952. gotminus1,
  2953. gotsigned,
  2954. gotunsigned,
  2955. gotdivmod: boolean;
  2956. { checks whether a node has an accepted resultdef, or originally
  2957. had one but was implicitly converted to s64bit }
  2958. function wasoriginallysmallerint(n: tnode): boolean;
  2959. begin
  2960. if (n.resultdef.typ<>orddef) then
  2961. exit(false);
  2962. gotsigned:=gotsigned or is_signed(n.resultdef);
  2963. gotunsigned:=gotunsigned or not(is_signed(n.resultdef));
  2964. { actually, we should only check right (denominator) nodes here, but
  2965. setting it always is a safe approximation }
  2966. if ((n.nodetype=ordconstn) and
  2967. (tordconstnode(n).value=-1)) then
  2968. gotminus1:=true;
  2969. if (torddef(n.resultdef).ordtype in validints) then
  2970. begin
  2971. if is_signed(n.resultdef) then
  2972. gotsint:=true;
  2973. exit(true);
  2974. end;
  2975. { type conv to a bigger int, we do not like to use? }
  2976. if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
  2977. { nf_explicit is also set for explicitly typecasted }
  2978. { ordconstn's }
  2979. ([nf_internal,nf_explicit]*n.flags=[]) and
  2980. { either a typeconversion node coming from a smaller type }
  2981. (((n.nodetype=typeconvn) and
  2982. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2983. (torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
  2984. { or an ordconstnode which has a smaller type}
  2985. ((n.nodetype=ordconstn) and
  2986. (tordconstnode(n).value>=l) and
  2987. (tordconstnode(n).value<=h))) then
  2988. begin
  2989. if ((n.nodetype=typeconvn) and
  2990. is_signed(ttypeconvnode(n).left.resultdef)) or
  2991. ((n.nodetype=ordconstn) and
  2992. (tordconstnode(n).value<0)) then
  2993. begin
  2994. gotsint:=true;
  2995. gotsigned:=true;
  2996. end
  2997. else
  2998. gotunsigned:=true;
  2999. exit(true);
  3000. end;
  3001. result:=false;
  3002. end;
  3003. function docheckremoveinttypeconvs(n: tnode): boolean;
  3004. begin
  3005. if wasoriginallysmallerint(n) then
  3006. exit(true);
  3007. case n.nodetype of
  3008. subn,orn,xorn:
  3009. begin
  3010. { the result could become negative in this case }
  3011. if n.nodetype=subn then
  3012. gotsint:=true;
  3013. result:=
  3014. docheckremoveinttypeconvs(tbinarynode(n).left) and
  3015. docheckremoveinttypeconvs(tbinarynode(n).right);
  3016. end;
  3017. unaryminusn:
  3018. begin
  3019. gotsint:=true;
  3020. result:=docheckremoveinttypeconvs(tunarynode(n).left);
  3021. end;
  3022. shrn:
  3023. begin
  3024. result:=wasoriginallysmallerint(tbinarynode(n).left) and
  3025. docheckremoveinttypeconvs(tbinarynode(n).right);
  3026. end;
  3027. notn:
  3028. result:=docheckremoveinttypeconvs(tunarynode(n).left);
  3029. addn,muln,divn,modn,andn,shln:
  3030. begin
  3031. if n.nodetype in [divn,modn] then
  3032. gotdivmod:=true;
  3033. result:=
  3034. (docheckremoveinttypeconvs(tbinarynode(n).left) and
  3035. docheckremoveinttypeconvs(tbinarynode(n).right) and
  3036. (not(n.nodetype in [modn,divn]) or (not(gotminus1)))
  3037. ) or
  3038. (
  3039. (n.nodetype=andn) and
  3040. (
  3041. { Right node is more likely to be a constant, so check
  3042. this one first }
  3043. wasoriginallysmallerint(tbinarynode(n).right) or
  3044. wasoriginallysmallerint(tbinarynode(n).left)
  3045. )
  3046. );
  3047. end;
  3048. else
  3049. result:=false;
  3050. end;
  3051. end;
  3052. begin { checkremove64bittypeconvs }
  3053. gotdivmod:=false;
  3054. gotsint:=false;
  3055. gotminus1:=false;
  3056. gotsigned:=false;
  3057. gotunsigned:=false;
  3058. result:=
  3059. docheckremoveinttypeconvs(n) and
  3060. (not(gotdivmod) or (gotsigned xor gotunsigned));
  3061. end;
  3062. { remove int type conversions and set the result to the given type }
  3063. procedure doremoveinttypeconvs(level : dword;var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
  3064. function SmallerOrSigned(def: tdef): Boolean;
  3065. begin
  3066. Result := (def.size < signedtype.size) or
  3067. (
  3068. (def.size = signedtype.size) and
  3069. is_signed(def)
  3070. )
  3071. end;
  3072. var
  3073. newblock: tblocknode;
  3074. newstatements: tstatementnode;
  3075. originaldivtree: tnode;
  3076. tempnode: ttempcreatenode;
  3077. NeedMinus1Check: Boolean;
  3078. begin
  3079. { we may not recurse into shr nodes:
  3080. dword1:=dword1+((dword2+dword3) shr 2);
  3081. while we can remove an extension on the outermost addition, we cannot
  3082. remove it from the shr
  3083. }
  3084. { Don't downsize into a division operation either, as the numerator can
  3085. be much larger than the result and non-linear properties prevent
  3086. accurate truncation; fixes #39646 [Kit] }
  3087. if (n.nodetype in [shrn,divn,modn]) and (level<>0) then
  3088. begin
  3089. inserttypeconv_internal(n,todef);
  3090. exit;
  3091. end;
  3092. case n.nodetype of
  3093. subn,addn,muln,divn,modn,xorn,andn,orn,shln,shrn:
  3094. begin
  3095. exclude(n.flags,nf_internal);
  3096. if not forceunsigned and
  3097. is_signed(n.resultdef) then
  3098. begin
  3099. originaldivtree:=nil;
  3100. NeedMinus1Check:=False;
  3101. if n.nodetype in [divn,modn] then
  3102. begin
  3103. { If the DIV operation is being downsized, we must explicitly check for a divisor of -1 }
  3104. NeedMinus1Check := True;
  3105. { If the operand size is equal or smaller, the -1 check isn't necessary }
  3106. if (
  3107. SmallerOrSigned(tbinarynode(n).left.resultdef) or
  3108. (
  3109. (tbinarynode(n).left.nodetype = typeconvn) and
  3110. SmallerOrSigned(ttypeconvnode(tbinarynode(n).left).left.resultdef)
  3111. )
  3112. ) and
  3113. (
  3114. SmallerOrSigned(tbinarynode(n).right.resultdef) or
  3115. (
  3116. (tbinarynode(n).right.nodetype = typeconvn) and
  3117. SmallerOrSigned(ttypeconvnode(tbinarynode(n).right).left.resultdef)
  3118. )
  3119. ) then
  3120. NeedMinus1Check := False;
  3121. end;
  3122. if NeedMinus1Check then
  3123. originaldivtree:=n.getcopy;
  3124. doremoveinttypeconvs(level+1,tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
  3125. doremoveinttypeconvs(level+1,tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
  3126. n.resultdef:=signedtype;
  3127. if NeedMinus1Check then
  3128. begin
  3129. newblock:=internalstatements(newstatements);
  3130. tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,true);
  3131. addstatement(newstatements,tempnode);
  3132. addstatement(newstatements,cifnode.create_internal(
  3133. caddnode.create_internal(equaln,tbinarynode(n).right.getcopy,cordconstnode.create(-1,n.resultdef,false)),
  3134. cassignmentnode.create_internal(
  3135. ctemprefnode.create(tempnode),
  3136. cmoddivnode.create(n.nodetype,tbinarynode(originaldivtree).left.getcopy,cordconstnode.create(-1,tbinarynode(originaldivtree).right.resultdef,false))
  3137. ),
  3138. cassignmentnode.create_internal(
  3139. ctemprefnode.create(tempnode),n
  3140. )
  3141. )
  3142. );
  3143. addstatement(newstatements,ctempdeletenode.create_normal_temp(tempnode));
  3144. addstatement(newstatements,ctemprefnode.create(tempnode));
  3145. n:=newblock;
  3146. do_typecheckpass(n);
  3147. originaldivtree.free;
  3148. end;
  3149. end
  3150. else
  3151. begin
  3152. doremoveinttypeconvs(level+1,tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3153. doremoveinttypeconvs(level+1,tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3154. n.resultdef:=unsignedtype;
  3155. end;
  3156. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  3157. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  3158. // ) then
  3159. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  3160. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  3161. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  3162. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  3163. end;
  3164. unaryminusn,notn:
  3165. begin
  3166. exclude(n.flags,nf_internal);
  3167. if not forceunsigned and
  3168. is_signed(n.resultdef) then
  3169. begin
  3170. doremoveinttypeconvs(level+1,tunarynode(n).left,signedtype,false,signedtype,unsignedtype);
  3171. n.resultdef:=signedtype;
  3172. end
  3173. else
  3174. begin
  3175. doremoveinttypeconvs(level+1,tunarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3176. n.resultdef:=unsignedtype;
  3177. end;
  3178. end;
  3179. typeconvn:
  3180. begin
  3181. ttypeconvnode(n).totypedef:=todef;
  3182. { may change the type conversion, e.g. if the old conversion was
  3183. from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
  3184. n.resultdef:=nil;
  3185. ttypeconvnode(n).convtype:=tc_none;
  3186. typecheckpass(n);
  3187. end;
  3188. else
  3189. inserttypeconv_internal(n,todef);
  3190. end;
  3191. end;
  3192. {$endif not CPUNO32BITOPS}
  3193. procedure swap_const_value (var val : TConstExprInt; size : longint);
  3194. begin
  3195. case size of
  3196. 1 : {do nothing };
  3197. 2 : if val.signed then
  3198. val.svalue:=swapendian(smallint(val.svalue))
  3199. else
  3200. val.uvalue:=swapendian(word(val.uvalue));
  3201. 4 : if val.signed then
  3202. val.svalue:=swapendian(longint(val.svalue))
  3203. else
  3204. val.uvalue:=swapendian(qword(val.uvalue));
  3205. 8 : if val.signed then
  3206. val.svalue:=swapendian(int64(val.svalue))
  3207. else
  3208. val.uvalue:=swapendian(qword(val.uvalue));
  3209. else
  3210. internalerror(2014111201);
  3211. end;
  3212. end;
  3213. function ttypeconvnode.simplify(forinline : boolean): tnode;
  3214. var
  3215. hp: tnode;
  3216. v: Tconstexprint;
  3217. {$ifndef CPUNO32BITOPS}
  3218. foundsint: boolean;
  3219. {$endif not CPUNO32BITOPS}
  3220. begin
  3221. result := nil;
  3222. { Constant folding and other node transitions to
  3223. remove the typeconv node }
  3224. case left.nodetype of
  3225. stringconstn :
  3226. if (resultdef.typ=stringdef) and
  3227. ((convtype=tc_equal) or
  3228. ((convtype=tc_string_2_string) and
  3229. (
  3230. ((not is_widechararray(left.resultdef) and
  3231. not is_wide_or_unicode_string(left.resultdef)) or
  3232. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  3233. )
  3234. )
  3235. )
  3236. ) then
  3237. begin
  3238. { output string consts in local ansistring encoding }
  3239. if is_ansistring(resultdef) and
  3240. { do not mess with the result type for internally created nodes }
  3241. not(nf_internal in flags) and
  3242. ((tstringdef(resultdef).encoding=0) or (tstringdef(resultdef).encoding=globals.CP_NONE)) then
  3243. tstringconstnode(left).changestringtype(getansistringdef)
  3244. else
  3245. tstringconstnode(left).changestringtype(resultdef);
  3246. result:=left;
  3247. left:=nil;
  3248. exit;
  3249. end
  3250. else if
  3251. (convtype<>tc_cstring_2_pchar) and
  3252. is_dynamicstring(left.resultdef) and
  3253. (tstringconstnode(left).len=0) and
  3254. (resultdef.typ=pointerdef) and
  3255. cstringconstnode.emptydynstrnil then
  3256. begin
  3257. result:=cnilnode.create;
  3258. exit;
  3259. end;
  3260. realconstn :
  3261. begin
  3262. if (convtype = tc_real_2_currency) then
  3263. result := typecheck_real_to_currency
  3264. else if (convtype = tc_real_2_real) then
  3265. result := typecheck_real_to_real
  3266. else
  3267. exit;
  3268. if not(assigned(result)) then
  3269. begin
  3270. result := left;
  3271. left := nil;
  3272. end;
  3273. if (result.nodetype = realconstn) then
  3274. begin
  3275. hp:=result;
  3276. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  3277. if nf_is_currency in hp.flags then
  3278. include(result.flags,nf_is_currency);
  3279. if ([nf_explicit,nf_internal] * flags <> []) then
  3280. include(result.flags, nf_explicit);
  3281. hp.free;
  3282. end;
  3283. end;
  3284. niln :
  3285. begin
  3286. { nil to ordinal node }
  3287. if (resultdef.typ=orddef) then
  3288. begin
  3289. hp:=cordconstnode.create(0,resultdef,true);
  3290. if ([nf_explicit,nf_internal] * flags <> []) then
  3291. include(hp.flags, nf_explicit);
  3292. result:=hp;
  3293. exit;
  3294. end
  3295. else
  3296. { fold nil to any pointer type }
  3297. if (resultdef.typ=pointerdef) then
  3298. begin
  3299. hp:=cnilnode.create;
  3300. hp.resultdef:=resultdef;
  3301. if ([nf_explicit,nf_internal] * flags <> []) then
  3302. include(hp.flags, nf_explicit);
  3303. result:=hp;
  3304. exit;
  3305. end
  3306. else
  3307. { remove typeconv after niln, but not when the result is a
  3308. methodpointer. The typeconv of the methodpointer will then
  3309. take care of updateing size of niln to OS_64 }
  3310. if not((resultdef.typ=procvardef) and
  3311. not(tprocvardef(resultdef).is_addressonly)) and
  3312. { converting (dynamic array) nil to a an open array is not allowed }
  3313. not is_open_array(resultdef) then
  3314. begin
  3315. left.resultdef:=resultdef;
  3316. if ([nf_explicit,nf_internal] * flags <> []) then
  3317. include(left.flags, nf_explicit);
  3318. result:=left;
  3319. left:=nil;
  3320. exit;
  3321. end;
  3322. end;
  3323. ordconstn :
  3324. begin
  3325. { ordinal contants can be directly converted }
  3326. { but not char to char because it is a widechar to char or via versa }
  3327. { which needs extra code to do the code page transistion }
  3328. { constant ordinal to pointer }
  3329. if (resultdef.typ=pointerdef) and
  3330. (convtype<>tc_cchar_2_pchar) then
  3331. begin
  3332. if (target_info.system in systems_managed_vm) and
  3333. (tordconstnode(left).value<>0) then
  3334. message(parser_e_feature_unsupported_for_vm);
  3335. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  3336. if ([nf_explicit,nf_internal] * flags <> []) then
  3337. include(hp.flags, nf_explicit);
  3338. result:=hp;
  3339. exit;
  3340. end
  3341. else if is_ordinal(resultdef) and
  3342. not(convtype=tc_char_2_char) then
  3343. begin
  3344. { replace the resultdef and recheck the range }
  3345. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  3346. include(left.flags, nf_explicit)
  3347. else
  3348. { no longer an ordconst with an explicit typecast }
  3349. exclude(left.flags, nf_explicit);
  3350. { when converting from one boolean type to another, force }
  3351. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  3352. { (Delphi-compatibile) }
  3353. if is_boolean(left.resultdef) and
  3354. is_boolean(resultdef) and
  3355. (is_cbool(left.resultdef) or
  3356. is_cbool(resultdef)) then
  3357. begin
  3358. if is_pasbool(resultdef) then
  3359. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  3360. else
  3361. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  3362. end
  3363. else
  3364. begin
  3365. { for constant values on absolute variables, swapping is required }
  3366. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  3367. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  3368. if not(nf_generic_para in flags) then
  3369. adaptrange(
  3370. resultdef,tordconstnode(left).value,
  3371. { when evaluating an explicit typecast during inlining, don't warn about
  3372. lost bits; only warn if someone literally typed e.g. byte($1ff) }
  3373. (([nf_internal,nf_absolute]*flags)<>[]) or (forinline and (nf_explicit in flags)),
  3374. nf_explicit in flags,
  3375. cs_check_range in localswitches);
  3376. { swap value back, but according to new type }
  3377. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  3378. swap_const_value(tordconstnode(left).value,resultdef.size);
  3379. { cut off the new value? }
  3380. if resultdef.size<left.resultdef.size then
  3381. case resultdef.size of
  3382. 1:
  3383. if is_signed(resultdef) then
  3384. tordconstnode(left).value:=tordconstnode(left).value and shortint($ff)
  3385. else
  3386. tordconstnode(left).value:=tordconstnode(left).value and byte($ff);
  3387. 2:
  3388. if is_signed(resultdef) then
  3389. tordconstnode(left).value:=tordconstnode(left).value and smallint($ffff)
  3390. else
  3391. tordconstnode(left).value:=tordconstnode(left).value and word($ffff);
  3392. 4:
  3393. if is_signed(resultdef) then
  3394. tordconstnode(left).value:=tordconstnode(left).value and longint($ffffffff)
  3395. else
  3396. tordconstnode(left).value:=tordconstnode(left).value and dword($ffffffff);
  3397. end;
  3398. end;
  3399. left.resultdef:=resultdef;
  3400. tordconstnode(left).typedef:=resultdef;
  3401. if is_signed(resultdef) then
  3402. tordconstnode(left).value.signed:=true
  3403. else
  3404. tordconstnode(left).value.signed:=false;
  3405. result:=left;
  3406. left:=nil;
  3407. exit;
  3408. end
  3409. else if (convtype=tc_int_2_int) and
  3410. is_currency(resultdef) then
  3411. begin
  3412. v:=tordconstnode(left).value;
  3413. if not(nf_internal in flags) and not(is_currency(left.resultdef)) then
  3414. v:=v*10000;
  3415. result:=cordconstnode.create(v,resultdef,false);
  3416. exit;
  3417. end;
  3418. end;
  3419. pointerconstn :
  3420. begin
  3421. { pointerconstn to any pointer is folded too }
  3422. if (resultdef.typ=pointerdef) then
  3423. begin
  3424. left.resultdef:=resultdef;
  3425. if ([nf_explicit,nf_internal] * flags <> []) then
  3426. include(left.flags, nf_explicit)
  3427. else
  3428. { no longer an ordconst with an explicit typecast }
  3429. exclude(left.flags, nf_explicit);
  3430. result:=left;
  3431. left:=nil;
  3432. exit;
  3433. end
  3434. { constant pointer to ordinal }
  3435. else if is_ordinal(resultdef) then
  3436. begin
  3437. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  3438. resultdef,not(nf_explicit in flags));
  3439. if ([nf_explicit,nf_internal] * flags <> []) then
  3440. include(hp.flags, nf_explicit);
  3441. result:=hp;
  3442. exit;
  3443. end;
  3444. end;
  3445. else
  3446. ;
  3447. end;
  3448. {$ifndef CPUNO32BITOPS}
  3449. { must be done before code below, because we need the
  3450. typeconversions for ordconstn's as well }
  3451. case convtype of
  3452. tc_bool_2_int,
  3453. tc_int_2_bool,
  3454. tc_int_2_int:
  3455. begin
  3456. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  3457. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  3458. begin
  3459. { avoid unnecessary widening of intermediary calculations
  3460. to 64 bit }
  3461. if (resultdef.size <= 4) and
  3462. is_64bitint(left.resultdef) and
  3463. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3464. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  3465. doremoveinttypeconvs(0,left,generrordef,not foundsint,s32inttype,u32inttype);
  3466. {$if defined(cpu16bitalu)}
  3467. if (resultdef.size <= 2) and
  3468. (is_32bitint(left.resultdef) or is_64bitint(left.resultdef)) and
  3469. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3470. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit],int64(low(smallint)),high(word)) then
  3471. doremoveinttypeconvs(0,left,generrordef,not foundsint,s16inttype,u16inttype);
  3472. {$endif defined(cpu16bitalu)}
  3473. {$if defined(cpu8bitalu)}
  3474. if (resultdef.size<left.resultdef.size) and
  3475. is_integer(left.resultdef) and
  3476. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3477. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  3478. doremoveinttypeconvs(0,left,generrordef,not foundsint,s8inttype,u8inttype);
  3479. {$endif defined(cpu8bitalu)}
  3480. { the above simplification may have left a redundant equal
  3481. typeconv (e.g. int32 to int32). If that's the case, we remove it }
  3482. if equal_defs(left.resultdef,resultdef) then
  3483. begin
  3484. result:=left;
  3485. left:=nil;
  3486. exit;
  3487. end;
  3488. if (convtype=tc_int_2_int) and (left.nodetype=typeconvn) and (ttypeconvnode(left).convtype=tc_bool_2_int) then
  3489. begin
  3490. ttypeconvnode(left).totypedef:=resultdef;
  3491. ttypeconvnode(left).resultdef:=resultdef;
  3492. result:=left;
  3493. left:=nil;
  3494. exit;
  3495. end;
  3496. end;
  3497. end;
  3498. else
  3499. ;
  3500. end;
  3501. {$endif not CPUNO32BITOPS}
  3502. end;
  3503. procedure Ttypeconvnode.mark_write;
  3504. begin
  3505. if not(convtype=tc_pointer_2_array) then
  3506. left.mark_write;
  3507. end;
  3508. function ttypeconvnode.first_cord_to_pointer : tnode;
  3509. begin
  3510. result:=nil;
  3511. internalerror(200104043);
  3512. end;
  3513. function ttypeconvnode.first_int_to_int : tnode;
  3514. begin
  3515. first_int_to_int:=nil;
  3516. expectloc:=left.expectloc;
  3517. if not is_void(left.resultdef) then
  3518. begin
  3519. if (left.expectloc<>LOC_REGISTER) and
  3520. ((resultdef.size>left.resultdef.size) or
  3521. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  3522. expectloc:=LOC_REGISTER
  3523. else
  3524. if (left.expectloc=LOC_CREGISTER) and
  3525. (resultdef.size<left.resultdef.size) then
  3526. expectloc:=LOC_REGISTER;
  3527. end;
  3528. end;
  3529. function ttypeconvnode.first_cstring_to_pchar : tnode;
  3530. begin
  3531. result:=nil;
  3532. expectloc:=LOC_REGISTER;
  3533. end;
  3534. function ttypeconvnode.first_cstring_to_int : tnode;
  3535. begin
  3536. result:=nil;
  3537. internalerror(200510014);
  3538. end;
  3539. function ttypeconvnode.first_string_to_chararray : tnode;
  3540. begin
  3541. first_string_to_chararray:=nil;
  3542. expectloc:=left.expectloc;
  3543. end;
  3544. function ttypeconvnode.first_char_to_string : tnode;
  3545. begin
  3546. first_char_to_string:=nil;
  3547. if tstringdef(resultdef).stringtype=st_shortstring then
  3548. inc(current_procinfo.estimatedtempsize,256);
  3549. expectloc:=LOC_REFERENCE;
  3550. end;
  3551. function ttypeconvnode.first_char_to_chararray : tnode;
  3552. begin
  3553. if resultdef.size <> 1 then
  3554. begin
  3555. { convert first to string, then to chararray }
  3556. inserttypeconv(left,cshortstringtype);
  3557. inserttypeconv(left,resultdef);
  3558. result:=left;
  3559. left := nil;
  3560. exit;
  3561. end;
  3562. result := nil;
  3563. end;
  3564. function ttypeconvnode.first_nothing : tnode;
  3565. begin
  3566. first_nothing:=nil;
  3567. end;
  3568. function ttypeconvnode.first_array_to_pointer : tnode;
  3569. begin
  3570. first_array_to_pointer:=nil;
  3571. make_not_regable(left,[ra_addr_regable]);
  3572. expectloc:=LOC_REGISTER;
  3573. end;
  3574. function ttypeconvnode.first_int_to_real: tnode;
  3575. var
  3576. fname: string[32];
  3577. begin
  3578. if target_info.system in systems_wince then
  3579. begin
  3580. { converting a 64bit integer to a float requires a helper }
  3581. if is_64bitint(left.resultdef) or
  3582. is_currency(left.resultdef) then
  3583. begin
  3584. { hack to avoid double division by 10000, as it's
  3585. already done by typecheckpass.resultdef_int_to_real }
  3586. if is_currency(left.resultdef) then
  3587. left.resultdef := s64inttype;
  3588. if is_signed(left.resultdef) then
  3589. fname:='i64to'
  3590. else
  3591. fname:='ui64to';
  3592. end
  3593. else
  3594. { other integers are supposed to be 32 bit }
  3595. begin
  3596. if is_signed(left.resultdef) then
  3597. fname:='ito'
  3598. else
  3599. fname:='uto';
  3600. firstpass(left);
  3601. end;
  3602. if tfloatdef(resultdef).floattype=s64real then
  3603. fname:=fname+'d'
  3604. else
  3605. fname:=fname+'s';
  3606. result:=ccallnode.createintern(fname,ccallparanode.create(
  3607. left,nil));
  3608. left:=nil;
  3609. firstpass(result);
  3610. exit;
  3611. end
  3612. else
  3613. begin
  3614. { converting a 64bit integer to a float requires a helper }
  3615. if is_64bitint(left.resultdef) or
  3616. is_currency(left.resultdef) then
  3617. begin
  3618. { hack to avoid double division by 10000, as it's
  3619. already done by typecheckpass.resultdef_int_to_real }
  3620. if is_currency(left.resultdef) then
  3621. left.resultdef := s64inttype;
  3622. if is_signed(left.resultdef) then
  3623. fname:='int64_to_'
  3624. else
  3625. { we can't do better currently }
  3626. fname:='qword_to_';
  3627. end
  3628. else
  3629. { other integers are supposed to be 32 bit }
  3630. begin
  3631. if is_signed(left.resultdef) then
  3632. fname:='int32_to_'
  3633. else
  3634. fname:='int64_to_';
  3635. firstpass(left);
  3636. end;
  3637. if tfloatdef(resultdef).floattype=s64real then
  3638. fname:=fname+'float64'
  3639. else
  3640. fname:=fname+'float32';
  3641. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  3642. left,nil)),resultdef);
  3643. left:=nil;
  3644. firstpass(result);
  3645. exit;
  3646. end;
  3647. end;
  3648. function ttypeconvnode.first_real_to_real : tnode;
  3649. begin
  3650. {$ifdef cpufpemu}
  3651. if cs_fp_emulation in current_settings.moduleswitches then
  3652. begin
  3653. if target_info.system in systems_wince then
  3654. begin
  3655. case tfloatdef(left.resultdef).floattype of
  3656. s32real:
  3657. case tfloatdef(resultdef).floattype of
  3658. s64real:
  3659. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  3660. s32real:
  3661. begin
  3662. result:=left;
  3663. left:=nil;
  3664. end;
  3665. else
  3666. internalerror(2005082704);
  3667. end;
  3668. s64real:
  3669. case tfloatdef(resultdef).floattype of
  3670. s32real:
  3671. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  3672. s64real:
  3673. begin
  3674. result:=left;
  3675. left:=nil;
  3676. end;
  3677. else
  3678. internalerror(2005082703);
  3679. end;
  3680. else
  3681. internalerror(2005082702);
  3682. end;
  3683. left:=nil;
  3684. firstpass(result);
  3685. exit;
  3686. end
  3687. else
  3688. begin
  3689. case tfloatdef(left.resultdef).floattype of
  3690. s32real:
  3691. case tfloatdef(resultdef).floattype of
  3692. s64real:
  3693. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  3694. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  3695. s32real:
  3696. begin
  3697. result:=left;
  3698. left:=nil;
  3699. end;
  3700. else
  3701. internalerror(200610151);
  3702. end;
  3703. s64real:
  3704. case tfloatdef(resultdef).floattype of
  3705. s32real:
  3706. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  3707. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  3708. s64real:
  3709. begin
  3710. result:=left;
  3711. left:=nil;
  3712. end;
  3713. else
  3714. internalerror(200610152);
  3715. end;
  3716. else
  3717. internalerror(200610153);
  3718. end;
  3719. left:=nil;
  3720. firstpass(result);
  3721. exit;
  3722. end;
  3723. end
  3724. else
  3725. {$endif cpufpemu}
  3726. begin
  3727. first_real_to_real:=nil;
  3728. if not use_vectorfpu(resultdef) then
  3729. expectloc:=LOC_FPUREGISTER
  3730. else
  3731. expectloc:=LOC_MMREGISTER;
  3732. end;
  3733. end;
  3734. function ttypeconvnode.first_pointer_to_array : tnode;
  3735. begin
  3736. first_pointer_to_array:=nil;
  3737. expectloc:=LOC_REFERENCE;
  3738. end;
  3739. function ttypeconvnode.first_cchar_to_pchar : tnode;
  3740. begin
  3741. first_cchar_to_pchar:=nil;
  3742. internalerror(200104021);
  3743. end;
  3744. function ttypeconvnode.first_bool_to_int : tnode;
  3745. begin
  3746. first_bool_to_int:=nil;
  3747. { byte(boolean) or word(wordbool) or longint(longbool) must
  3748. be accepted for var parameters }
  3749. if (nf_explicit in flags) and
  3750. (left.resultdef.size=resultdef.size) and
  3751. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3752. begin
  3753. expectloc:=left.expectloc;
  3754. exit;
  3755. end;
  3756. expectloc:=LOC_REGISTER;
  3757. end;
  3758. function ttypeconvnode.first_int_to_bool : tnode;
  3759. begin
  3760. first_int_to_bool:=nil;
  3761. { byte(boolean) or word(wordbool) or longint(longbool) must
  3762. be accepted for var parameters }
  3763. if (nf_explicit in flags) and
  3764. (left.resultdef.size=resultdef.size) and
  3765. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3766. begin
  3767. {$ifdef xtensa}
  3768. expectloc:=LOC_REGISTER;
  3769. {$else xtensa}
  3770. expectloc:=left.expectloc;
  3771. {$endif xtensa}
  3772. exit;
  3773. end;
  3774. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  3775. { convert to a boolean (only necessary for 32bit processors) }
  3776. { note: not if left is already a bool (qwordbool that is true, even if
  3777. only because the highest bit is set, must remain true if it is
  3778. --implicitly, unlike integers-- converted to another type of bool),
  3779. Left can already be a bool because this routine can also be called
  3780. from first_bool_to_bool }
  3781. if not is_boolean(left.resultdef) and
  3782. (left.resultdef.size > sizeof(aint)) and
  3783. (left.resultdef.size<>resultdef.size)
  3784. and is_cbool(resultdef) then
  3785. begin
  3786. left:=ctypeconvnode.create_internal(left,s32inttype);
  3787. firstpass(left);
  3788. exit;
  3789. end;
  3790. expectloc:=LOC_REGISTER;
  3791. end;
  3792. function ttypeconvnode.first_bool_to_bool : tnode;
  3793. begin
  3794. first_bool_to_bool:=nil;
  3795. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  3796. not is_cbool(resultdef) then
  3797. expectloc := left.expectloc
  3798. { the following cases use the code generation for bool_to_int/
  3799. int_to_bool -> also set their expectlocs }
  3800. else if (resultdef.size=left.resultdef.size) and
  3801. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  3802. result:=first_bool_to_int
  3803. else
  3804. result:=first_int_to_bool
  3805. end;
  3806. function ttypeconvnode.first_char_to_char : tnode;
  3807. var
  3808. fname: string[18];
  3809. begin
  3810. if (torddef(resultdef).ordtype=uchar) and
  3811. (torddef(left.resultdef).ordtype=uwidechar) then
  3812. fname := 'fpc_uchar_to_char'
  3813. else if (torddef(resultdef).ordtype=uwidechar) and
  3814. (torddef(left.resultdef).ordtype=uchar) then
  3815. fname := 'fpc_char_to_uchar'
  3816. else
  3817. internalerror(2007081201);
  3818. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  3819. left:=nil;
  3820. firstpass(result);
  3821. end;
  3822. function ttypeconvnode.first_proc_to_procvar : tnode;
  3823. begin
  3824. first_proc_to_procvar:=nil;
  3825. { if we take the address of a nested function, the current function/
  3826. procedure needs a stack frame since it's required to construct
  3827. the nested procvar }
  3828. if is_nested_pd(tprocvardef(resultdef)) and
  3829. (
  3830. not (po_anonymous in tprocdef(left.resultdef).procoptions) or
  3831. (po_delphi_nested_cc in tprocvardef(resultdef).procoptions)
  3832. ) then
  3833. include(current_procinfo.flags,pi_needs_stackframe);
  3834. if tabstractprocdef(resultdef).is_addressonly then
  3835. expectloc:=LOC_REGISTER
  3836. else
  3837. expectloc:=left.expectloc;
  3838. end;
  3839. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  3840. begin
  3841. first_nil_to_methodprocvar:=nil;
  3842. expectloc:=LOC_REGISTER;
  3843. end;
  3844. function ttypeconvnode.first_set_to_set : tnode;
  3845. var
  3846. newstatement : tstatementnode;
  3847. temp : ttempcreatenode;
  3848. begin
  3849. { in theory, we should do range checking here,
  3850. but Delphi doesn't do it either (FK) }
  3851. if left.nodetype=setconstn then
  3852. begin
  3853. left.resultdef:=resultdef;
  3854. result:=left;
  3855. left:=nil;
  3856. end
  3857. { equal sets for the code generator? }
  3858. else if (left.resultdef.size=resultdef.size) and
  3859. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  3860. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  3861. { e.g., this prints true because of this:
  3862. var
  3863. sa: set of 1..2;
  3864. sb: set of 5..6;
  3865. b: byte;
  3866. begin
  3867. b:=1;
  3868. sa:=[1..2];
  3869. sb:=sa;
  3870. writeln(b in sb);
  3871. end.
  3872. }
  3873. begin
  3874. result:=left;
  3875. left:=nil;
  3876. end
  3877. else
  3878. begin
  3879. result:=internalstatements(newstatement);
  3880. { in case left is a smallset expression, it can be an addn or so. }
  3881. { fpc_varset_load expects a formal const parameter, which doesn't }
  3882. { accept set addn's -> assign to a temp first and pass the temp }
  3883. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  3884. begin
  3885. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  3886. addstatement(newstatement,temp);
  3887. { temp := left }
  3888. addstatement(newstatement,cassignmentnode.create(
  3889. ctemprefnode.create(temp),left));
  3890. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3891. addstatement(newstatement,ctemprefnode.create(temp));
  3892. left:=result;
  3893. firstpass(left);
  3894. { recreate the result's internalstatements list }
  3895. result:=internalstatements(newstatement);
  3896. end;
  3897. { create temp for result }
  3898. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3899. addstatement(newstatement,temp);
  3900. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  3901. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  3902. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3903. ccallparanode.create(ctemprefnode.create(temp),
  3904. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3905. ccallparanode.create(left,nil))))))
  3906. );
  3907. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3908. addstatement(newstatement,ctemprefnode.create(temp));
  3909. left:=nil;
  3910. end;
  3911. end;
  3912. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  3913. begin
  3914. first_ansistring_to_pchar:=nil;
  3915. expectloc:=LOC_REGISTER;
  3916. end;
  3917. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  3918. begin
  3919. first_arrayconstructor_to_set:=nil;
  3920. internalerror(200104022);
  3921. end;
  3922. function ttypeconvnode.first_class_to_intf : tnode;
  3923. var
  3924. hd : tobjectdef;
  3925. ImplIntf : TImplementedInterface;
  3926. begin
  3927. result:=nil;
  3928. expectloc:=LOC_REGISTER;
  3929. hd:=tobjectdef(left.resultdef);
  3930. while assigned(hd) do
  3931. begin
  3932. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3933. if assigned(ImplIntf) then
  3934. begin
  3935. case ImplIntf.IType of
  3936. etStandard:
  3937. { handle in pass 2 }
  3938. ;
  3939. etFieldValue, etFieldValueClass:
  3940. if is_interface(tobjectdef(resultdef)) then
  3941. begin
  3942. result:=left;
  3943. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3944. { this ensures proper refcounting when field is of class type }
  3945. if not is_interface(result.resultdef) then
  3946. inserttypeconv(result, resultdef);
  3947. left:=nil;
  3948. end
  3949. else
  3950. begin
  3951. internalerror(200802213);
  3952. end;
  3953. etStaticMethodResult, etStaticMethodClass,
  3954. etVirtualMethodResult, etVirtualMethodClass:
  3955. if is_interface(tobjectdef(resultdef)) then
  3956. begin
  3957. { TODO: generating a call to TObject.GetInterface instead could yield
  3958. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3959. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3960. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3961. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3962. left,[],nil);
  3963. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3964. { if it is a class, process it further in a similar way }
  3965. if not is_interface(result.resultdef) then
  3966. inserttypeconv(result, resultdef);
  3967. left:=nil;
  3968. end
  3969. else if is_class(tobjectdef(resultdef)) then
  3970. begin
  3971. internalerror(200802211);
  3972. end
  3973. else
  3974. internalerror(200802231);
  3975. end;
  3976. break;
  3977. end;
  3978. hd:=hd.childof;
  3979. end;
  3980. if hd=nil then
  3981. internalerror(200802164);
  3982. end;
  3983. function ttypeconvnode.first_string_to_string : tnode;
  3984. var
  3985. procname: string[31];
  3986. newblock : tblocknode;
  3987. newstat : tstatementnode;
  3988. restemp : ttempcreatenode;
  3989. begin
  3990. { get the correct procedure name }
  3991. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3992. '_to_'+tstringdef(resultdef).stringtypname;
  3993. if tstringdef(resultdef).stringtype=st_shortstring then
  3994. begin
  3995. newblock:=internalstatements(newstat);
  3996. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3997. addstatement(newstat,restemp);
  3998. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3999. ctemprefnode.create(restemp),nil))));
  4000. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  4001. addstatement(newstat,ctemprefnode.create(restemp));
  4002. result:=newblock;
  4003. end
  4004. { encoding parameter required? }
  4005. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  4006. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  4007. result:=ccallnode.createinternres(procname,
  4008. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  4009. ccallparanode.create(left,nil)),resultdef)
  4010. else
  4011. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  4012. left:=nil;
  4013. end;
  4014. function ttypeconvnode._first_int_to_int : tnode;
  4015. begin
  4016. result:=first_int_to_int;
  4017. end;
  4018. function ttypeconvnode._first_cstring_to_pchar : tnode;
  4019. begin
  4020. result:=first_cstring_to_pchar;
  4021. end;
  4022. function ttypeconvnode._first_cstring_to_int : tnode;
  4023. begin
  4024. result:=first_cstring_to_int;
  4025. end;
  4026. function ttypeconvnode._first_string_to_chararray : tnode;
  4027. begin
  4028. result:=first_string_to_chararray;
  4029. end;
  4030. function ttypeconvnode._first_char_to_string : tnode;
  4031. begin
  4032. result:=first_char_to_string;
  4033. end;
  4034. function ttypeconvnode._first_char_to_chararray: tnode;
  4035. begin
  4036. result:=first_char_to_chararray;
  4037. end;
  4038. function ttypeconvnode._first_nothing : tnode;
  4039. begin
  4040. result:=first_nothing;
  4041. end;
  4042. function ttypeconvnode._first_array_to_pointer : tnode;
  4043. begin
  4044. result:=first_array_to_pointer;
  4045. end;
  4046. function ttypeconvnode._first_int_to_real : tnode;
  4047. begin
  4048. result:=first_int_to_real;
  4049. end;
  4050. function ttypeconvnode._first_real_to_real : tnode;
  4051. begin
  4052. result:=first_real_to_real;
  4053. end;
  4054. function ttypeconvnode._first_pointer_to_array : tnode;
  4055. begin
  4056. result:=first_pointer_to_array;
  4057. end;
  4058. function ttypeconvnode._first_cchar_to_pchar : tnode;
  4059. begin
  4060. result:=first_cchar_to_pchar;
  4061. end;
  4062. function ttypeconvnode._first_bool_to_int : tnode;
  4063. begin
  4064. result:=first_bool_to_int;
  4065. end;
  4066. function ttypeconvnode._first_int_to_bool : tnode;
  4067. begin
  4068. result:=first_int_to_bool;
  4069. end;
  4070. function ttypeconvnode._first_bool_to_bool : tnode;
  4071. begin
  4072. result:=first_bool_to_bool;
  4073. end;
  4074. function ttypeconvnode._first_proc_to_procvar : tnode;
  4075. begin
  4076. result:=first_proc_to_procvar;
  4077. end;
  4078. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  4079. begin
  4080. result:=first_nil_to_methodprocvar;
  4081. end;
  4082. function ttypeconvnode._first_set_to_set : tnode;
  4083. begin
  4084. result:=first_set_to_set;
  4085. end;
  4086. function ttypeconvnode._first_cord_to_pointer : tnode;
  4087. begin
  4088. result:=first_cord_to_pointer;
  4089. end;
  4090. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  4091. begin
  4092. result:=first_ansistring_to_pchar;
  4093. end;
  4094. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  4095. begin
  4096. result:=first_arrayconstructor_to_set;
  4097. end;
  4098. function ttypeconvnode._first_class_to_intf : tnode;
  4099. begin
  4100. result:=first_class_to_intf;
  4101. end;
  4102. function ttypeconvnode._first_char_to_char : tnode;
  4103. begin
  4104. result:=first_char_to_char;
  4105. end;
  4106. function ttypeconvnode._first_string_to_string : tnode;
  4107. begin
  4108. result:=first_string_to_string;
  4109. end;
  4110. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  4111. const
  4112. firstconvert : array[tconverttype] of pointer = (
  4113. nil, { none }
  4114. @ttypeconvnode._first_nothing, {equal}
  4115. @ttypeconvnode._first_nothing, {not_possible}
  4116. @ttypeconvnode._first_string_to_string,
  4117. @ttypeconvnode._first_char_to_string,
  4118. @ttypeconvnode._first_char_to_chararray,
  4119. nil, { removed in typecheck_chararray_to_string }
  4120. @ttypeconvnode._first_cchar_to_pchar,
  4121. @ttypeconvnode._first_cstring_to_pchar,
  4122. @ttypeconvnode._first_cstring_to_int,
  4123. @ttypeconvnode._first_ansistring_to_pchar,
  4124. @ttypeconvnode._first_string_to_chararray,
  4125. nil, { removed in typecheck_chararray_to_string }
  4126. @ttypeconvnode._first_array_to_pointer,
  4127. @ttypeconvnode._first_pointer_to_array,
  4128. @ttypeconvnode._first_int_to_int,
  4129. @ttypeconvnode._first_int_to_bool,
  4130. @ttypeconvnode._first_bool_to_bool,
  4131. @ttypeconvnode._first_bool_to_int,
  4132. @ttypeconvnode._first_real_to_real,
  4133. @ttypeconvnode._first_int_to_real,
  4134. nil, { removed in typecheck_real_to_currency }
  4135. @ttypeconvnode._first_proc_to_procvar,
  4136. @ttypeconvnode._first_nil_to_methodprocvar,
  4137. @ttypeconvnode._first_arrayconstructor_to_set,
  4138. @ttypeconvnode._first_set_to_set,
  4139. @ttypeconvnode._first_cord_to_pointer,
  4140. @ttypeconvnode._first_nothing,
  4141. @ttypeconvnode._first_nothing,
  4142. @ttypeconvnode._first_class_to_intf,
  4143. @ttypeconvnode._first_char_to_char,
  4144. @ttypeconvnode._first_nothing,
  4145. @ttypeconvnode._first_nothing,
  4146. nil,
  4147. nil,
  4148. nil,
  4149. nil,
  4150. nil,
  4151. nil,
  4152. nil,
  4153. @ttypeconvnode._first_nothing,
  4154. @ttypeconvnode._first_nothing,
  4155. @ttypeconvnode._first_nothing,
  4156. nil,
  4157. nil
  4158. );
  4159. type
  4160. tprocedureofobject = function : tnode of object;
  4161. var
  4162. r : TMethod;
  4163. begin
  4164. { this is a little bit dirty but it works }
  4165. { and should be quite portable too }
  4166. r.Code:=firstconvert[c];
  4167. r.Data:=self;
  4168. if not assigned(r.Code) then
  4169. internalerror(200312081);
  4170. first_call_helper:=tprocedureofobject(r)()
  4171. end;
  4172. function ttypeconvnode.pass_1 : tnode;
  4173. begin
  4174. if warn_pointer_to_signed then
  4175. cgmessage(type_w_pointer_to_signed);
  4176. result:=nil;
  4177. firstpass(left);
  4178. if codegenerror then
  4179. exit;
  4180. expectloc:=left.expectloc;
  4181. if nf_explicit in flags then
  4182. { check if the result could be in a register }
  4183. if (not(tstoreddef(resultdef).is_intregable) and
  4184. not(tstoreddef(resultdef).is_const_intregable) and
  4185. not(tstoreddef(resultdef).is_fpuregable)) or
  4186. ((left.resultdef.typ = floatdef) and
  4187. (resultdef.typ <> floatdef)) then
  4188. make_not_regable(left,[ra_addr_regable]);
  4189. result:=first_call_helper(convtype);
  4190. end;
  4191. function ttypeconvnode.retains_value_location:boolean;
  4192. begin
  4193. result:=assigned(left.resultdef) and
  4194. (
  4195. (convtype=tc_equal) or
  4196. { typecasting from void is always allowed }
  4197. is_void(left.resultdef) or
  4198. (left.resultdef.typ=formaldef) or
  4199. { int 2 int with same size reuses same location, or for
  4200. tp7 mode also allow size < orignal size }
  4201. (
  4202. (convtype=tc_int_2_int) and
  4203. (
  4204. not is_bitpacked_access(left) and
  4205. (resultdef.size=left.resultdef.size) or
  4206. ((m_tp7 in current_settings.modeswitches) and
  4207. (resultdef.size<left.resultdef.size))
  4208. )
  4209. ) or
  4210. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  4211. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  4212. (nf_explicit in flags) and
  4213. (resultdef.size=left.resultdef.size)) or
  4214. { on managed platforms, converting an element to an open array
  4215. involves creating an actual array -> value location changes }
  4216. ((convtype=tc_elem_2_openarray) and
  4217. not(target_info.system in systems_managed_vm))
  4218. );
  4219. end;
  4220. function ttypeconvnode.assign_allowed:boolean;
  4221. begin
  4222. result:=retains_value_location;
  4223. { When using only a part of the value it can't be in a register since
  4224. that will load the value in a new register first }
  4225. { the same goes for changing the sign of equal-sized values which
  4226. are smaller than an entire register }
  4227. if result and
  4228. { don't try to check the size of an open array }
  4229. (is_open_array(resultdef) or
  4230. is_open_array(left.resultdef) or
  4231. (resultdef.size<left.resultdef.size) or
  4232. ((resultdef.size=left.resultdef.size) and
  4233. (left.resultdef.size<sizeof(aint)) and
  4234. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  4235. make_not_regable(left,[ra_addr_regable]);
  4236. end;
  4237. function ttypeconvnode.docompare(p: tnode) : boolean;
  4238. begin
  4239. docompare :=
  4240. inherited docompare(p) and
  4241. (convtype = ttypeconvnode(p).convtype) and
  4242. (convnodeflags = ttypeconvnode(p).convnodeflags) and
  4243. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  4244. end;
  4245. procedure ttypeconvnode._second_int_to_int;
  4246. begin
  4247. second_int_to_int;
  4248. end;
  4249. procedure ttypeconvnode._second_string_to_string;
  4250. begin
  4251. second_string_to_string;
  4252. end;
  4253. procedure ttypeconvnode._second_cstring_to_pchar;
  4254. begin
  4255. second_cstring_to_pchar;
  4256. end;
  4257. procedure ttypeconvnode._second_cstring_to_int;
  4258. begin
  4259. second_cstring_to_int;
  4260. end;
  4261. procedure ttypeconvnode._second_string_to_chararray;
  4262. begin
  4263. second_string_to_chararray;
  4264. end;
  4265. procedure ttypeconvnode._second_array_to_pointer;
  4266. begin
  4267. second_array_to_pointer;
  4268. end;
  4269. procedure ttypeconvnode._second_pointer_to_array;
  4270. begin
  4271. second_pointer_to_array;
  4272. end;
  4273. procedure ttypeconvnode._second_chararray_to_string;
  4274. begin
  4275. second_chararray_to_string;
  4276. end;
  4277. procedure ttypeconvnode._second_char_to_string;
  4278. begin
  4279. second_char_to_string;
  4280. end;
  4281. procedure ttypeconvnode._second_int_to_real;
  4282. begin
  4283. second_int_to_real;
  4284. end;
  4285. procedure ttypeconvnode._second_real_to_real;
  4286. begin
  4287. second_real_to_real;
  4288. end;
  4289. procedure ttypeconvnode._second_cord_to_pointer;
  4290. begin
  4291. second_cord_to_pointer;
  4292. end;
  4293. procedure ttypeconvnode._second_proc_to_procvar;
  4294. begin
  4295. second_proc_to_procvar;
  4296. end;
  4297. procedure ttypeconvnode._second_nil_to_methodprocvar;
  4298. begin
  4299. second_nil_to_methodprocvar;
  4300. end;
  4301. procedure ttypeconvnode._second_bool_to_int;
  4302. begin
  4303. second_bool_to_int;
  4304. end;
  4305. procedure ttypeconvnode._second_int_to_bool;
  4306. begin
  4307. second_int_to_bool;
  4308. end;
  4309. procedure ttypeconvnode._second_bool_to_bool;
  4310. begin
  4311. second_bool_to_bool;
  4312. end;
  4313. procedure ttypeconvnode._second_set_to_set;
  4314. begin
  4315. second_set_to_set;
  4316. end;
  4317. procedure ttypeconvnode._second_ansistring_to_pchar;
  4318. begin
  4319. second_ansistring_to_pchar;
  4320. end;
  4321. procedure ttypeconvnode._second_class_to_intf;
  4322. begin
  4323. second_class_to_intf;
  4324. end;
  4325. procedure ttypeconvnode._second_char_to_char;
  4326. begin
  4327. second_char_to_char;
  4328. end;
  4329. procedure ttypeconvnode._second_elem_to_openarray;
  4330. begin
  4331. second_elem_to_openarray;
  4332. end;
  4333. procedure ttypeconvnode._second_nothing;
  4334. begin
  4335. second_nothing;
  4336. end;
  4337. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  4338. const
  4339. secondconvert : array[tconverttype] of pointer = (
  4340. @ttypeconvnode._second_nothing, {none}
  4341. @ttypeconvnode._second_nothing, {equal}
  4342. @ttypeconvnode._second_nothing, {not_possible}
  4343. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  4344. @ttypeconvnode._second_char_to_string,
  4345. @ttypeconvnode._second_nothing, {char_to_charray}
  4346. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  4347. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  4348. @ttypeconvnode._second_cstring_to_pchar,
  4349. @ttypeconvnode._second_cstring_to_int,
  4350. @ttypeconvnode._second_ansistring_to_pchar,
  4351. @ttypeconvnode._second_string_to_chararray,
  4352. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  4353. @ttypeconvnode._second_array_to_pointer,
  4354. @ttypeconvnode._second_pointer_to_array,
  4355. @ttypeconvnode._second_int_to_int,
  4356. @ttypeconvnode._second_int_to_bool,
  4357. @ttypeconvnode._second_bool_to_bool,
  4358. @ttypeconvnode._second_bool_to_int,
  4359. @ttypeconvnode._second_real_to_real,
  4360. @ttypeconvnode._second_int_to_real,
  4361. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  4362. @ttypeconvnode._second_proc_to_procvar,
  4363. @ttypeconvnode._second_nil_to_methodprocvar,
  4364. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  4365. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  4366. @ttypeconvnode._second_cord_to_pointer,
  4367. @ttypeconvnode._second_nothing, { interface 2 string }
  4368. @ttypeconvnode._second_nothing, { interface 2 guid }
  4369. @ttypeconvnode._second_class_to_intf,
  4370. @ttypeconvnode._second_char_to_char,
  4371. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  4372. @ttypeconvnode._second_nothing, { pwchar_2_string }
  4373. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  4374. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  4375. @ttypeconvnode._second_nothing, { variant_2_enum }
  4376. @ttypeconvnode._second_nothing, { enum_2_variant }
  4377. @ttypeconvnode._second_nothing, { variant_2_interface }
  4378. @ttypeconvnode._second_nothing, { interface_2_variant }
  4379. @ttypeconvnode._second_nothing, { array_2_dynarray }
  4380. @ttypeconvnode._second_elem_to_openarray, { elem_2_openarray }
  4381. @ttypeconvnode._second_nothing, { arrayconstructor_2_dynarray }
  4382. @ttypeconvnode._second_nothing, { arrayconstructor_2_array }
  4383. @ttypeconvnode._second_nothing, { anonproc_2_funcref }
  4384. @ttypeconvnode._second_nothing { procvar_2_funcref }
  4385. );
  4386. type
  4387. tprocedureofobject = procedure of object;
  4388. var
  4389. r : TMethod;
  4390. begin
  4391. { this is a little bit dirty but it works }
  4392. { and should be quite portable too }
  4393. r.Code:=secondconvert[c];
  4394. r.Data:=self;
  4395. tprocedureofobject(r)();
  4396. end;
  4397. {*****************************************************************************
  4398. TASNODE
  4399. *****************************************************************************}
  4400. function tasisnode.target_specific_typecheck: boolean;
  4401. begin
  4402. result:=false;
  4403. end;
  4404. function tasisnode.pass_typecheck: tnode;
  4405. var
  4406. hp : tnode;
  4407. begin
  4408. result:=nil;
  4409. typecheckpass(right);
  4410. typecheckpass(left);
  4411. set_varstate(right,vs_read,[vsf_must_be_valid]);
  4412. set_varstate(left,vs_read,[vsf_must_be_valid]);
  4413. if codegenerror then
  4414. exit;
  4415. if target_specific_typecheck then
  4416. begin
  4417. // ok
  4418. end
  4419. else if (right.resultdef.typ=classrefdef) then
  4420. begin
  4421. { left maybe an interface reference }
  4422. if is_interfacecom(left.resultdef) or
  4423. is_javainterface(left.resultdef) then
  4424. begin
  4425. { relation checks are not possible }
  4426. end
  4427. { or left must be a class }
  4428. else if is_class(left.resultdef) or
  4429. is_javaclass(left.resultdef) then
  4430. begin
  4431. { the operands must be related }
  4432. if (not(def_is_related(tobjectdef(left.resultdef),
  4433. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  4434. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  4435. tobjectdef(left.resultdef)))) then
  4436. CGMessage2(type_e_classes_not_related,
  4437. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  4438. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  4439. end
  4440. else
  4441. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  4442. case nodetype of
  4443. isn:
  4444. resultdef:=pasbool1type;
  4445. asn:
  4446. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  4447. else
  4448. ;
  4449. end;
  4450. end
  4451. else if is_interface(right.resultdef) or
  4452. is_dispinterface(right.resultdef) or
  4453. is_javainterface(right.resultdef) then
  4454. begin
  4455. case nodetype of
  4456. isn:
  4457. resultdef:=pasbool1type;
  4458. asn:
  4459. resultdef:=right.resultdef;
  4460. else
  4461. ;
  4462. end;
  4463. { left is a class or interface }
  4464. if is_javainterface(right.resultdef) then
  4465. begin
  4466. if not is_java_class_or_interface(left.resultdef) then
  4467. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  4468. end
  4469. else if not(is_class(left.resultdef) or
  4470. is_interfacecom(left.resultdef)) then
  4471. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  4472. else
  4473. begin
  4474. { load the GUID of the interface }
  4475. if (right.nodetype=typen) then
  4476. begin
  4477. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  4478. begin
  4479. if assigned(tobjectdef(right.resultdef).iidstr) then
  4480. begin
  4481. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  4482. tstringconstnode(hp).changestringtype(cshortstringtype);
  4483. right.free;
  4484. right:=hp;
  4485. end
  4486. else
  4487. internalerror(201006131);
  4488. end
  4489. else
  4490. begin
  4491. if assigned(tobjectdef(right.resultdef).iidguid) then
  4492. begin
  4493. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  4494. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  4495. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  4496. right.free;
  4497. right:=hp;
  4498. end
  4499. else
  4500. internalerror(201006132);
  4501. end;
  4502. typecheckpass(right);
  4503. end;
  4504. end;
  4505. end
  4506. else
  4507. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  4508. end;
  4509. {*****************************************************************************
  4510. TISNODE
  4511. *****************************************************************************}
  4512. constructor tisnode.create(l,r : tnode);
  4513. begin
  4514. inherited create(isn,l,r);
  4515. end;
  4516. constructor tisnode.create_internal(l, r: tnode);
  4517. begin
  4518. create(l,r);
  4519. include(flags,nf_internal);
  4520. end;
  4521. function tisnode.pass_1 : tnode;
  4522. var
  4523. procname: string;
  4524. statement : tstatementnode;
  4525. tempnode : ttempcreatenode;
  4526. begin
  4527. result:=nil;
  4528. { Passing a class type to an "is" expression cannot result in a class
  4529. of that type to be constructed.
  4530. }
  4531. include(right.flags,nf_ignore_for_wpo);
  4532. if is_class(left.resultdef) and
  4533. (right.resultdef.typ=classrefdef) then
  4534. begin
  4535. if (right.nodetype=loadvmtaddrn) and
  4536. (tloadvmtaddrnode(right).left.nodetype=typen) and
  4537. (oo_is_sealed in tobjectdef(tloadvmtaddrnode(right).left.resultdef).objectoptions) and
  4538. equal_defs(left.resultdef,tclassrefdef(right.resultdef).pointeddef) then
  4539. begin
  4540. if might_have_sideeffects(left) or
  4541. (node_complexity(left)>2) then
  4542. begin
  4543. result:=internalstatements(statement);
  4544. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  4545. addstatement(statement,tempnode);
  4546. addstatement(statement,cassignmentnode.create_internal(ctemprefnode.create(tempnode),left));
  4547. addstatement(statement,caddnode.create_internal(andn,
  4548. caddnode.create_internal(unequaln,ctemprefnode.create(tempnode),cnilnode.create),
  4549. caddnode.create_internal(equaln,cloadvmtaddrnode.create(ctemprefnode.create(tempnode)),right)
  4550. )
  4551. );
  4552. left:=nil;
  4553. right:=nil;
  4554. end
  4555. else
  4556. begin
  4557. result:=caddnode.create_internal(andn,
  4558. caddnode.create_internal(unequaln,left.getcopy,cnilnode.create),
  4559. caddnode.create_internal(equaln,cloadvmtaddrnode.create(left.getcopy),right)
  4560. );
  4561. right:=nil;
  4562. end;
  4563. end
  4564. else
  4565. result := ccallnode.createinternres('fpc_do_is',
  4566. ccallparanode.create(left,ccallparanode.create(right,nil)),
  4567. resultdef);
  4568. end
  4569. else
  4570. begin
  4571. if is_class(left.resultdef) then
  4572. if is_shortstring(right.resultdef) then
  4573. procname := 'fpc_class_is_corbaintf'
  4574. else
  4575. procname := 'fpc_class_is_intf'
  4576. else
  4577. if right.resultdef.typ=classrefdef then
  4578. procname := 'fpc_intf_is_class'
  4579. else
  4580. procname := 'fpc_intf_is';
  4581. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4582. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4583. end;
  4584. left := nil;
  4585. right := nil;
  4586. //firstpass(call);
  4587. if codegenerror then
  4588. exit;
  4589. end;
  4590. { dummy pass_2, it will never be called, but we need one since }
  4591. { you can't instantiate an abstract class }
  4592. procedure tisnode.pass_generate_code;
  4593. begin
  4594. end;
  4595. {*****************************************************************************
  4596. TASNODE
  4597. *****************************************************************************}
  4598. constructor tasnode.create(l,r : tnode);
  4599. begin
  4600. inherited create(asn,l,r);
  4601. call := nil;
  4602. end;
  4603. constructor tasnode.create_internal(l,r : tnode);
  4604. begin
  4605. create(l,r);
  4606. include(flags,nf_internal);
  4607. end;
  4608. destructor tasnode.destroy;
  4609. begin
  4610. call.free;
  4611. inherited destroy;
  4612. end;
  4613. function tasnode.dogetcopy: tnode;
  4614. begin
  4615. result := inherited dogetcopy;
  4616. if assigned(call) then
  4617. tasnode(result).call := call.getcopy
  4618. else
  4619. tasnode(result).call := nil;
  4620. end;
  4621. function tasnode.docompare(p: tnode): boolean;
  4622. begin
  4623. result:=
  4624. inherited docompare(p) and
  4625. tasnode(p).call.isequal(call);
  4626. end;
  4627. function tasnode.pass_1 : tnode;
  4628. var
  4629. procname: string;
  4630. begin
  4631. result:=nil;
  4632. { Passing a class type to an "as" expression cannot result in a class
  4633. of that type to be constructed.
  4634. We could put this inside the if-block below, but this way it is
  4635. safe for sure even if the code below changes
  4636. }
  4637. if assigned(right) then
  4638. include(right.flags,nf_ignore_for_wpo);
  4639. if not assigned(call) then
  4640. begin
  4641. if is_class(left.resultdef) and
  4642. (right.resultdef.typ=classrefdef) then
  4643. call := ccallnode.createinternres('fpc_do_as',
  4644. ccallparanode.create(left,ccallparanode.create(right,nil)),
  4645. resultdef)
  4646. else
  4647. begin
  4648. if is_class(left.resultdef) then
  4649. if is_shortstring(right.resultdef) then
  4650. procname := 'fpc_class_as_corbaintf'
  4651. else
  4652. procname := 'fpc_class_as_intf'
  4653. else
  4654. if right.resultdef.typ=classrefdef then
  4655. procname := 'fpc_intf_as_class'
  4656. else
  4657. procname := 'fpc_intf_as';
  4658. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4659. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4660. end;
  4661. left := nil;
  4662. right := nil;
  4663. firstpass(call);
  4664. if codegenerror then
  4665. exit;
  4666. expectloc:=call.expectloc;
  4667. end;
  4668. end;
  4669. end.