ncnv.pas 143 KB

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