ncnv.pas 152 KB

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