ncnv.pas 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837
  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. {$ifndef cpu64bitalu}
  2036. { checks whether we can safely remove 64 bit typeconversions }
  2037. { in case range and overflow checking are off, and in case }
  2038. { the result of this node tree is downcasted again to a }
  2039. { 8/16/32 bit value afterwards }
  2040. function checkremove64bittypeconvs(n: tnode; out gotsint: boolean): boolean;
  2041. var
  2042. gotmuldivmod: boolean;
  2043. { checks whether a node is either an u32bit, or originally }
  2044. { was one but was implicitly converted to s64bit }
  2045. function wasoriginallyint32(n: tnode): boolean;
  2046. begin
  2047. if (n.resultdef.typ<>orddef) then
  2048. exit(false);
  2049. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  2050. begin
  2051. if (torddef(n.resultdef).ordtype=s32bit) then
  2052. gotsint:=true;
  2053. exit(true);
  2054. end;
  2055. if (torddef(n.resultdef).ordtype=s64bit) and
  2056. { nf_explicit is also set for explicitly typecasted }
  2057. { ordconstn's }
  2058. ([nf_internal,nf_explicit]*n.flags=[]) and
  2059. { either a typeconversion node coming from u32bit }
  2060. (((n.nodetype=typeconvn) and
  2061. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2062. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  2063. { or an ordconstnode which was/is a valid cardinal }
  2064. ((n.nodetype=ordconstn) and
  2065. (tordconstnode(n).value>=int64(low(longint))) and
  2066. (tordconstnode(n).value<=high(cardinal)))) then
  2067. begin
  2068. if ((n.nodetype=typeconvn) and
  2069. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  2070. ((n.nodetype=ordconstn) and
  2071. (tordconstnode(n).value<0)) then
  2072. gotsint:=true;
  2073. exit(true);
  2074. end;
  2075. result:=false;
  2076. end;
  2077. function docheckremove64bittypeconvs(n: tnode): boolean;
  2078. begin
  2079. result:=false;
  2080. if wasoriginallyint32(n) then
  2081. exit(true);
  2082. case n.nodetype of
  2083. subn,orn,xorn:
  2084. begin
  2085. { nf_internal is set by taddnode.typecheckpass in }
  2086. { case the arguments of this subn were u32bit, but }
  2087. { upcasted to s64bit for calculation correctness }
  2088. { (normally only needed when range checking, but }
  2089. { also done otherwise so there is no difference }
  2090. { in overload choosing etc between $r+ and $r-) }
  2091. if (nf_internal in n.flags) then
  2092. result:=true
  2093. else
  2094. result:=
  2095. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2096. docheckremove64bittypeconvs(tbinarynode(n).right);
  2097. end;
  2098. addn,muln,divn,modn,andn:
  2099. begin
  2100. if n.nodetype in [muln,divn,modn] then
  2101. gotmuldivmod:=true;
  2102. result:=
  2103. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2104. docheckremove64bittypeconvs(tbinarynode(n).right);
  2105. end;
  2106. end;
  2107. end;
  2108. begin { checkremove64bittypeconvs }
  2109. gotmuldivmod:=false;
  2110. gotsint:=false;
  2111. result:=
  2112. docheckremove64bittypeconvs(n) and
  2113. not(gotmuldivmod and gotsint);
  2114. end;
  2115. procedure doremove64bittypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean);
  2116. begin
  2117. case n.nodetype of
  2118. subn,addn,muln,divn,modn,xorn,andn,orn:
  2119. begin
  2120. exclude(n.flags,nf_internal);
  2121. if not forceunsigned and
  2122. is_signed(n.resultdef) then
  2123. begin
  2124. doremove64bittypeconvs(tbinarynode(n).left,s32inttype,false);
  2125. doremove64bittypeconvs(tbinarynode(n).right,s32inttype,false);
  2126. n.resultdef:=s32inttype
  2127. end
  2128. else
  2129. begin
  2130. doremove64bittypeconvs(tbinarynode(n).left,u32inttype,forceunsigned);
  2131. doremove64bittypeconvs(tbinarynode(n).right,u32inttype,forceunsigned);
  2132. n.resultdef:=u32inttype
  2133. end;
  2134. end;
  2135. ordconstn:
  2136. inserttypeconv_internal(n,todef);
  2137. typeconvn:
  2138. begin
  2139. n.resultdef:=todef;
  2140. ttypeconvnode(n).totypedef:=todef;
  2141. end;
  2142. end;
  2143. end;
  2144. {$endif not cpu64bitalu}
  2145. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2146. var
  2147. hp: tnode;
  2148. {$ifndef cpu64bitalu}
  2149. foundsint: boolean;
  2150. {$endif not cpu64bitalu}
  2151. begin
  2152. result := nil;
  2153. { Constant folding and other node transitions to
  2154. remove the typeconv node }
  2155. case left.nodetype of
  2156. stringconstn :
  2157. if (convtype=tc_string_2_string) and
  2158. (
  2159. ((not is_widechararray(left.resultdef) and
  2160. not is_wide_or_unicode_string(left.resultdef)) or
  2161. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) or
  2162. { non-ascii chars would be replaced with '?' -> loses info }
  2163. not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)))
  2164. ) then
  2165. begin
  2166. tstringconstnode(left).changestringtype(resultdef);
  2167. result:=left;
  2168. left:=nil;
  2169. exit;
  2170. end;
  2171. realconstn :
  2172. begin
  2173. if (convtype = tc_real_2_currency) then
  2174. result := typecheck_real_to_currency
  2175. else if (convtype = tc_real_2_real) then
  2176. result := typecheck_real_to_real
  2177. else
  2178. exit;
  2179. if not(assigned(result)) then
  2180. begin
  2181. result := left;
  2182. left := nil;
  2183. end;
  2184. if (result.nodetype = realconstn) then
  2185. begin
  2186. hp:=result;
  2187. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2188. if ([nf_explicit,nf_internal] * flags <> []) then
  2189. include(result.flags, nf_explicit);
  2190. hp.free;
  2191. end;
  2192. end;
  2193. niln :
  2194. begin
  2195. { nil to ordinal node }
  2196. if (resultdef.typ=orddef) then
  2197. begin
  2198. hp:=cordconstnode.create(0,resultdef,true);
  2199. if ([nf_explicit,nf_internal] * flags <> []) then
  2200. include(hp.flags, nf_explicit);
  2201. result:=hp;
  2202. exit;
  2203. end
  2204. else
  2205. { fold nil to any pointer type }
  2206. if (resultdef.typ=pointerdef) then
  2207. begin
  2208. hp:=cnilnode.create;
  2209. hp.resultdef:=resultdef;
  2210. if ([nf_explicit,nf_internal] * flags <> []) then
  2211. include(hp.flags, nf_explicit);
  2212. result:=hp;
  2213. exit;
  2214. end
  2215. else
  2216. { remove typeconv after niln, but not when the result is a
  2217. methodpointer. The typeconv of the methodpointer will then
  2218. take care of updateing size of niln to OS_64 }
  2219. if not((resultdef.typ=procvardef) and
  2220. not(tprocvardef(resultdef).is_addressonly)) then
  2221. begin
  2222. left.resultdef:=resultdef;
  2223. if ([nf_explicit,nf_internal] * flags <> []) then
  2224. include(left.flags, nf_explicit);
  2225. result:=left;
  2226. left:=nil;
  2227. exit;
  2228. end;
  2229. end;
  2230. ordconstn :
  2231. begin
  2232. { ordinal contants can be directly converted }
  2233. { but not char to char because it is a widechar to char or via versa }
  2234. { which needs extra code to do the code page transistion }
  2235. { constant ordinal to pointer }
  2236. if (resultdef.typ=pointerdef) and
  2237. (convtype<>tc_cchar_2_pchar) then
  2238. begin
  2239. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2240. if ([nf_explicit,nf_internal] * flags <> []) then
  2241. include(hp.flags, nf_explicit);
  2242. result:=hp;
  2243. exit;
  2244. end
  2245. else if is_ordinal(resultdef) and
  2246. not(convtype=tc_char_2_char) then
  2247. begin
  2248. { replace the resultdef and recheck the range }
  2249. if ([nf_explicit,nf_internal] * flags <> []) then
  2250. include(left.flags, nf_explicit)
  2251. else
  2252. { no longer an ordconst with an explicit typecast }
  2253. exclude(left.flags, nf_explicit);
  2254. { when converting from one boolean type to another, force }
  2255. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2256. { (Delphi-compatibile) }
  2257. if is_boolean(left.resultdef) and
  2258. is_boolean(resultdef) and
  2259. (is_cbool(left.resultdef) or
  2260. is_cbool(resultdef)) then
  2261. begin
  2262. if is_pasbool(resultdef) then
  2263. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2264. else
  2265. {$ifdef VER2_2}
  2266. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  2267. tordconstnode(left).value:=-tordconstnode(left).value;
  2268. {$else}
  2269. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2270. {$endif VER2_2}
  2271. end
  2272. else
  2273. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags),false);
  2274. left.resultdef:=resultdef;
  2275. tordconstnode(left).typedef:=resultdef;
  2276. result:=left;
  2277. left:=nil;
  2278. exit;
  2279. end;
  2280. end;
  2281. pointerconstn :
  2282. begin
  2283. { pointerconstn to any pointer is folded too }
  2284. if (resultdef.typ=pointerdef) then
  2285. begin
  2286. left.resultdef:=resultdef;
  2287. if ([nf_explicit,nf_internal] * flags <> []) then
  2288. include(left.flags, nf_explicit)
  2289. else
  2290. { no longer an ordconst with an explicit typecast }
  2291. exclude(left.flags, nf_explicit);
  2292. result:=left;
  2293. left:=nil;
  2294. exit;
  2295. end
  2296. { constant pointer to ordinal }
  2297. else if is_ordinal(resultdef) then
  2298. begin
  2299. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2300. resultdef,not(nf_explicit in flags));
  2301. if ([nf_explicit,nf_internal] * flags <> []) then
  2302. include(hp.flags, nf_explicit);
  2303. result:=hp;
  2304. exit;
  2305. end;
  2306. end;
  2307. end;
  2308. {$ifndef cpu64bitalu}
  2309. { must be done before code below, because we need the
  2310. typeconversions for ordconstn's as well }
  2311. case convtype of
  2312. tc_int_2_int:
  2313. begin
  2314. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2315. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2316. (resultdef.size <= 4) and
  2317. is_64bitint(left.resultdef) and
  2318. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2319. checkremove64bittypeconvs(left,foundsint) then
  2320. begin
  2321. { avoid unnecessary widening of intermediary calculations }
  2322. { to 64 bit }
  2323. doremove64bittypeconvs(left,generrordef,not foundsint);
  2324. end;
  2325. end;
  2326. end;
  2327. {$endif not cpu64bitalu}
  2328. end;
  2329. procedure Ttypeconvnode.mark_write;
  2330. begin
  2331. left.mark_write;
  2332. end;
  2333. function ttypeconvnode.first_cord_to_pointer : tnode;
  2334. begin
  2335. result:=nil;
  2336. internalerror(200104043);
  2337. end;
  2338. function ttypeconvnode.first_int_to_int : tnode;
  2339. begin
  2340. first_int_to_int:=nil;
  2341. expectloc:=left.expectloc;
  2342. if not is_void(left.resultdef) then
  2343. begin
  2344. if (left.expectloc<>LOC_REGISTER) and
  2345. ((resultdef.size>left.resultdef.size) or
  2346. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2347. expectloc:=LOC_REGISTER
  2348. else
  2349. if (left.expectloc=LOC_CREGISTER) and
  2350. (resultdef.size<left.resultdef.size) then
  2351. expectloc:=LOC_REGISTER;
  2352. end;
  2353. end;
  2354. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2355. begin
  2356. result:=nil;
  2357. expectloc:=LOC_REGISTER;
  2358. end;
  2359. function ttypeconvnode.first_cstring_to_int : tnode;
  2360. begin
  2361. result:=nil;
  2362. internalerror(200510014);
  2363. end;
  2364. function ttypeconvnode.first_string_to_chararray : tnode;
  2365. begin
  2366. first_string_to_chararray:=nil;
  2367. expectloc:=left.expectloc;
  2368. end;
  2369. function ttypeconvnode.first_char_to_string : tnode;
  2370. begin
  2371. first_char_to_string:=nil;
  2372. expectloc:=LOC_REFERENCE;
  2373. end;
  2374. function ttypeconvnode.first_nothing : tnode;
  2375. begin
  2376. first_nothing:=nil;
  2377. end;
  2378. function ttypeconvnode.first_array_to_pointer : tnode;
  2379. begin
  2380. first_array_to_pointer:=nil;
  2381. expectloc:=LOC_REGISTER;
  2382. end;
  2383. function ttypeconvnode.first_int_to_real: tnode;
  2384. var
  2385. fname: string[32];
  2386. begin
  2387. if target_info.system in systems_wince then
  2388. begin
  2389. { converting a 64bit integer to a float requires a helper }
  2390. if is_64bitint(left.resultdef) or
  2391. is_currency(left.resultdef) then
  2392. begin
  2393. { hack to avoid double division by 10000, as it's
  2394. already done by typecheckpass.resultdef_int_to_real }
  2395. if is_currency(left.resultdef) then
  2396. left.resultdef := s64inttype;
  2397. if is_signed(left.resultdef) then
  2398. fname:='I64TO'
  2399. else
  2400. fname:='UI64TO';
  2401. end
  2402. else
  2403. { other integers are supposed to be 32 bit }
  2404. begin
  2405. if is_signed(left.resultdef) then
  2406. fname:='ITO'
  2407. else
  2408. fname:='UTO';
  2409. firstpass(left);
  2410. end;
  2411. if tfloatdef(resultdef).floattype=s64real then
  2412. fname:=fname+'D'
  2413. else
  2414. fname:=fname+'S';
  2415. result:=ccallnode.createintern(fname,ccallparanode.create(
  2416. left,nil));
  2417. left:=nil;
  2418. firstpass(result);
  2419. exit;
  2420. end
  2421. else
  2422. begin
  2423. { converting a 64bit integer to a float requires a helper }
  2424. if is_64bitint(left.resultdef) or
  2425. is_currency(left.resultdef) then
  2426. begin
  2427. { hack to avoid double division by 10000, as it's
  2428. already done by typecheckpass.resultdef_int_to_real }
  2429. if is_currency(left.resultdef) then
  2430. left.resultdef := s64inttype;
  2431. if is_signed(left.resultdef) then
  2432. fname:='int64_to_'
  2433. else
  2434. { we can't do better currently }
  2435. fname:='qword_to_';
  2436. end
  2437. else
  2438. { other integers are supposed to be 32 bit }
  2439. begin
  2440. if is_signed(left.resultdef) then
  2441. fname:='int32_to_'
  2442. else
  2443. fname:='int64_to_';
  2444. firstpass(left);
  2445. end;
  2446. if tfloatdef(resultdef).floattype=s64real then
  2447. fname:=fname+'float64'
  2448. else
  2449. fname:=fname+'float32';
  2450. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2451. left,nil)),resultdef);
  2452. left:=nil;
  2453. firstpass(result);
  2454. exit;
  2455. end;
  2456. end;
  2457. function ttypeconvnode.first_real_to_real : tnode;
  2458. begin
  2459. {$ifdef cpufpemu}
  2460. if cs_fp_emulation in current_settings.moduleswitches then
  2461. begin
  2462. if target_info.system in systems_wince then
  2463. begin
  2464. case tfloatdef(left.resultdef).floattype of
  2465. s32real:
  2466. case tfloatdef(resultdef).floattype of
  2467. s64real:
  2468. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2469. s32real:
  2470. begin
  2471. result:=left;
  2472. left:=nil;
  2473. end;
  2474. else
  2475. internalerror(2005082704);
  2476. end;
  2477. s64real:
  2478. case tfloatdef(resultdef).floattype of
  2479. s32real:
  2480. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2481. s64real:
  2482. begin
  2483. result:=left;
  2484. left:=nil;
  2485. end;
  2486. else
  2487. internalerror(2005082703);
  2488. end;
  2489. else
  2490. internalerror(2005082702);
  2491. end;
  2492. left:=nil;
  2493. firstpass(result);
  2494. exit;
  2495. end
  2496. else
  2497. begin
  2498. case tfloatdef(left.resultdef).floattype of
  2499. s32real:
  2500. case tfloatdef(resultdef).floattype of
  2501. s64real:
  2502. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2503. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2504. s32real:
  2505. begin
  2506. result:=left;
  2507. left:=nil;
  2508. end;
  2509. else
  2510. internalerror(200610151);
  2511. end;
  2512. s64real:
  2513. case tfloatdef(resultdef).floattype of
  2514. s32real:
  2515. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2516. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2517. s64real:
  2518. begin
  2519. result:=left;
  2520. left:=nil;
  2521. end;
  2522. else
  2523. internalerror(200610152);
  2524. end;
  2525. else
  2526. internalerror(200610153);
  2527. end;
  2528. left:=nil;
  2529. firstpass(result);
  2530. exit;
  2531. end;
  2532. end
  2533. else
  2534. {$endif cpufpemu}
  2535. begin
  2536. first_real_to_real:=nil;
  2537. if not use_vectorfpu(resultdef) then
  2538. expectloc:=LOC_FPUREGISTER
  2539. else
  2540. expectloc:=LOC_MMREGISTER;
  2541. end;
  2542. end;
  2543. function ttypeconvnode.first_pointer_to_array : tnode;
  2544. begin
  2545. first_pointer_to_array:=nil;
  2546. expectloc:=LOC_REFERENCE;
  2547. end;
  2548. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2549. begin
  2550. first_cchar_to_pchar:=nil;
  2551. internalerror(200104021);
  2552. end;
  2553. function ttypeconvnode.first_bool_to_int : tnode;
  2554. begin
  2555. first_bool_to_int:=nil;
  2556. { byte(boolean) or word(wordbool) or longint(longbool) must
  2557. be accepted for var parameters }
  2558. if (nf_explicit in flags) and
  2559. (left.resultdef.size=resultdef.size) and
  2560. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2561. exit;
  2562. { when converting to 64bit, first convert to a 32bit int and then }
  2563. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2564. if resultdef.size > sizeof(aint) then
  2565. begin
  2566. result := ctypeconvnode.create_internal(left,s32inttype);
  2567. result := ctypeconvnode.create(result,resultdef);
  2568. left := nil;
  2569. firstpass(result);
  2570. exit;
  2571. end;
  2572. expectloc:=LOC_REGISTER;
  2573. end;
  2574. function ttypeconvnode.first_int_to_bool : tnode;
  2575. begin
  2576. first_int_to_bool:=nil;
  2577. { byte(boolean) or word(wordbool) or longint(longbool) must
  2578. be accepted for var parameters }
  2579. if (nf_explicit in flags) and
  2580. (left.resultdef.size=resultdef.size) and
  2581. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2582. exit;
  2583. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2584. { convert to a boolean (only necessary for 32bit processors) }
  2585. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2586. and is_cbool(resultdef) then
  2587. begin
  2588. left:=ctypeconvnode.create_internal(left,s32inttype);
  2589. firstpass(left);
  2590. exit;
  2591. end;
  2592. expectloc:=LOC_REGISTER;
  2593. end;
  2594. function ttypeconvnode.first_bool_to_bool : tnode;
  2595. begin
  2596. first_bool_to_bool:=nil;
  2597. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2598. expectloc := left.expectloc
  2599. else
  2600. expectloc:=LOC_REGISTER;
  2601. end;
  2602. function ttypeconvnode.first_char_to_char : tnode;
  2603. var
  2604. fname: string[18];
  2605. begin
  2606. if (torddef(resultdef).ordtype=uchar) and
  2607. (torddef(left.resultdef).ordtype=uwidechar) then
  2608. fname := 'fpc_uchar_to_char'
  2609. else if (torddef(resultdef).ordtype=uwidechar) and
  2610. (torddef(left.resultdef).ordtype=uchar) then
  2611. fname := 'fpc_char_to_uchar'
  2612. else
  2613. internalerror(2007081201);
  2614. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2615. left:=nil;
  2616. firstpass(result);
  2617. end;
  2618. function ttypeconvnode.first_proc_to_procvar : tnode;
  2619. begin
  2620. first_proc_to_procvar:=nil;
  2621. { if we take the address of a nested function, the current function/
  2622. procedure needs a stack frame since it's required to construct
  2623. the nested procvar }
  2624. if is_nested_pd(tprocvardef(resultdef)) then
  2625. include(current_procinfo.flags,pi_needs_stackframe);
  2626. if tabstractprocdef(resultdef).is_addressonly then
  2627. expectloc:=LOC_REGISTER
  2628. else
  2629. begin
  2630. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2631. CGMessage(parser_e_illegal_expression);
  2632. expectloc:=left.expectloc;
  2633. end;
  2634. end;
  2635. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2636. begin
  2637. first_nil_to_methodprocvar:=nil;
  2638. expectloc:=LOC_REFERENCE;
  2639. end;
  2640. function ttypeconvnode.first_set_to_set : tnode;
  2641. var
  2642. newstatement : tstatementnode;
  2643. temp : ttempcreatenode;
  2644. begin
  2645. { in theory, we should do range checking here,
  2646. but Delphi doesn't do it either (FK) }
  2647. if left.nodetype=setconstn then
  2648. begin
  2649. left.resultdef:=resultdef;
  2650. result:=left;
  2651. left:=nil;
  2652. end
  2653. { equal sets for the code generator? }
  2654. else if (left.resultdef.size=resultdef.size) and
  2655. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2656. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2657. { e.g., this prints true because of this:
  2658. var
  2659. sa: set of 1..2;
  2660. sb: set of 5..6;
  2661. b: byte;
  2662. begin
  2663. b:=1;
  2664. sa:=[1..2];
  2665. sb:=sa;
  2666. writeln(b in sb);
  2667. end.
  2668. }
  2669. begin
  2670. result:=left;
  2671. left:=nil;
  2672. end
  2673. else
  2674. begin
  2675. result:=internalstatements(newstatement);
  2676. { in case left is a smallset expression, it can be an addn or so. }
  2677. { fpc_varset_load expects a formal const parameter, which doesn't }
  2678. { accept set addn's -> assign to a temp first and pass the temp }
  2679. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2680. begin
  2681. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2682. addstatement(newstatement,temp);
  2683. { temp := left }
  2684. addstatement(newstatement,cassignmentnode.create(
  2685. ctemprefnode.create(temp),left));
  2686. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2687. addstatement(newstatement,ctemprefnode.create(temp));
  2688. left:=result;
  2689. firstpass(left);
  2690. { recreate the result's internalstatements list }
  2691. result:=internalstatements(newstatement);
  2692. end;
  2693. { create temp for result }
  2694. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2695. addstatement(newstatement,temp);
  2696. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2697. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2698. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2699. ccallparanode.create(ctemprefnode.create(temp),
  2700. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2701. ccallparanode.create(left,nil))))))
  2702. );
  2703. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2704. addstatement(newstatement,ctemprefnode.create(temp));
  2705. left:=nil;
  2706. end;
  2707. end;
  2708. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2709. begin
  2710. first_ansistring_to_pchar:=nil;
  2711. expectloc:=LOC_REGISTER;
  2712. end;
  2713. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2714. begin
  2715. first_arrayconstructor_to_set:=nil;
  2716. internalerror(200104022);
  2717. end;
  2718. function ttypeconvnode.first_class_to_intf : tnode;
  2719. var
  2720. hd : tobjectdef;
  2721. ImplIntf : TImplementedInterface;
  2722. begin
  2723. result:=nil;
  2724. expectloc:=LOC_REGISTER;
  2725. hd:=tobjectdef(left.resultdef);
  2726. while assigned(hd) do
  2727. begin
  2728. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2729. if assigned(ImplIntf) then
  2730. begin
  2731. case ImplIntf.IType of
  2732. etStandard:
  2733. { handle in pass 2 }
  2734. ;
  2735. etFieldValue, etFieldValueClass:
  2736. if is_interface(tobjectdef(resultdef)) then
  2737. begin
  2738. result:=left;
  2739. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2740. { this ensures proper refcounting when field is of class type }
  2741. if not is_interface(result.resultdef) then
  2742. inserttypeconv(result, resultdef);
  2743. left:=nil;
  2744. end
  2745. else
  2746. begin
  2747. internalerror(200802213);
  2748. end;
  2749. etStaticMethodResult, etStaticMethodClass,
  2750. etVirtualMethodResult, etVirtualMethodClass:
  2751. if is_interface(tobjectdef(resultdef)) then
  2752. begin
  2753. { TODO: generating a call to TObject.GetInterface instead could yield
  2754. smaller code size. OTOH, refcounting gotchas are possible that way. }
  2755. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2756. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2757. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2758. left,[]);
  2759. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2760. { if it is a class, process it further in a similar way }
  2761. if not is_interface(result.resultdef) then
  2762. inserttypeconv(result, resultdef);
  2763. left:=nil;
  2764. end
  2765. else if is_class(tobjectdef(resultdef)) then
  2766. begin
  2767. internalerror(200802211);
  2768. end
  2769. else
  2770. internalerror(200802231);
  2771. else
  2772. internalerror(200802165);
  2773. end;
  2774. break;
  2775. end;
  2776. hd:=hd.childof;
  2777. end;
  2778. if hd=nil then
  2779. internalerror(200802164);
  2780. end;
  2781. function ttypeconvnode.first_string_to_string : tnode;
  2782. var
  2783. procname: string[31];
  2784. newblock : tblocknode;
  2785. newstat : tstatementnode;
  2786. restemp : ttempcreatenode;
  2787. begin
  2788. { get the correct procedure name }
  2789. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  2790. '_to_'+tstringdef(resultdef).stringtypname;
  2791. if tstringdef(resultdef).stringtype=st_shortstring then
  2792. begin
  2793. newblock:=internalstatements(newstat);
  2794. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  2795. addstatement(newstat,restemp);
  2796. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  2797. ctemprefnode.create(restemp),nil))));
  2798. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  2799. addstatement(newstat,ctemprefnode.create(restemp));
  2800. result:=newblock;
  2801. end
  2802. else
  2803. result := ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  2804. left:=nil;
  2805. end;
  2806. function ttypeconvnode._first_int_to_int : tnode;
  2807. begin
  2808. result:=first_int_to_int;
  2809. end;
  2810. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2811. begin
  2812. result:=first_cstring_to_pchar;
  2813. end;
  2814. function ttypeconvnode._first_cstring_to_int : tnode;
  2815. begin
  2816. result:=first_cstring_to_int;
  2817. end;
  2818. function ttypeconvnode._first_string_to_chararray : tnode;
  2819. begin
  2820. result:=first_string_to_chararray;
  2821. end;
  2822. function ttypeconvnode._first_char_to_string : tnode;
  2823. begin
  2824. result:=first_char_to_string;
  2825. end;
  2826. function ttypeconvnode._first_nothing : tnode;
  2827. begin
  2828. result:=first_nothing;
  2829. end;
  2830. function ttypeconvnode._first_array_to_pointer : tnode;
  2831. begin
  2832. result:=first_array_to_pointer;
  2833. end;
  2834. function ttypeconvnode._first_int_to_real : tnode;
  2835. begin
  2836. result:=first_int_to_real;
  2837. end;
  2838. function ttypeconvnode._first_real_to_real : tnode;
  2839. begin
  2840. result:=first_real_to_real;
  2841. end;
  2842. function ttypeconvnode._first_pointer_to_array : tnode;
  2843. begin
  2844. result:=first_pointer_to_array;
  2845. end;
  2846. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2847. begin
  2848. result:=first_cchar_to_pchar;
  2849. end;
  2850. function ttypeconvnode._first_bool_to_int : tnode;
  2851. begin
  2852. result:=first_bool_to_int;
  2853. end;
  2854. function ttypeconvnode._first_int_to_bool : tnode;
  2855. begin
  2856. result:=first_int_to_bool;
  2857. end;
  2858. function ttypeconvnode._first_bool_to_bool : tnode;
  2859. begin
  2860. result:=first_bool_to_bool;
  2861. end;
  2862. function ttypeconvnode._first_proc_to_procvar : tnode;
  2863. begin
  2864. result:=first_proc_to_procvar;
  2865. end;
  2866. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2867. begin
  2868. result:=first_nil_to_methodprocvar;
  2869. end;
  2870. function ttypeconvnode._first_set_to_set : tnode;
  2871. begin
  2872. result:=first_set_to_set;
  2873. end;
  2874. function ttypeconvnode._first_cord_to_pointer : tnode;
  2875. begin
  2876. result:=first_cord_to_pointer;
  2877. end;
  2878. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2879. begin
  2880. result:=first_ansistring_to_pchar;
  2881. end;
  2882. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2883. begin
  2884. result:=first_arrayconstructor_to_set;
  2885. end;
  2886. function ttypeconvnode._first_class_to_intf : tnode;
  2887. begin
  2888. result:=first_class_to_intf;
  2889. end;
  2890. function ttypeconvnode._first_char_to_char : tnode;
  2891. begin
  2892. result:=first_char_to_char;
  2893. end;
  2894. function ttypeconvnode._first_string_to_string : tnode;
  2895. begin
  2896. result:=first_string_to_string;
  2897. end;
  2898. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2899. const
  2900. firstconvert : array[tconverttype] of pointer = (
  2901. nil, { none }
  2902. @ttypeconvnode._first_nothing, {equal}
  2903. @ttypeconvnode._first_nothing, {not_possible}
  2904. @ttypeconvnode._first_string_to_string,
  2905. @ttypeconvnode._first_char_to_string,
  2906. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2907. nil, { removed in typecheck_chararray_to_string }
  2908. @ttypeconvnode._first_cchar_to_pchar,
  2909. @ttypeconvnode._first_cstring_to_pchar,
  2910. @ttypeconvnode._first_cstring_to_int,
  2911. @ttypeconvnode._first_ansistring_to_pchar,
  2912. @ttypeconvnode._first_string_to_chararray,
  2913. nil, { removed in typecheck_chararray_to_string }
  2914. @ttypeconvnode._first_array_to_pointer,
  2915. @ttypeconvnode._first_pointer_to_array,
  2916. @ttypeconvnode._first_int_to_int,
  2917. @ttypeconvnode._first_int_to_bool,
  2918. @ttypeconvnode._first_bool_to_bool,
  2919. @ttypeconvnode._first_bool_to_int,
  2920. @ttypeconvnode._first_real_to_real,
  2921. @ttypeconvnode._first_int_to_real,
  2922. nil, { removed in typecheck_real_to_currency }
  2923. @ttypeconvnode._first_proc_to_procvar,
  2924. @ttypeconvnode._first_nil_to_methodprocvar,
  2925. @ttypeconvnode._first_arrayconstructor_to_set,
  2926. @ttypeconvnode._first_set_to_set,
  2927. @ttypeconvnode._first_cord_to_pointer,
  2928. @ttypeconvnode._first_nothing,
  2929. @ttypeconvnode._first_nothing,
  2930. @ttypeconvnode._first_class_to_intf,
  2931. @ttypeconvnode._first_char_to_char,
  2932. @ttypeconvnode._first_nothing,
  2933. @ttypeconvnode._first_nothing,
  2934. nil,
  2935. nil,
  2936. nil,
  2937. nil,
  2938. nil,
  2939. nil,
  2940. nil,
  2941. @ttypeconvnode._first_nothing
  2942. );
  2943. type
  2944. tprocedureofobject = function : tnode of object;
  2945. var
  2946. r : packed record
  2947. proc : pointer;
  2948. obj : pointer;
  2949. end;
  2950. begin
  2951. { this is a little bit dirty but it works }
  2952. { and should be quite portable too }
  2953. r.proc:=firstconvert[c];
  2954. r.obj:=self;
  2955. if not assigned(r.proc) then
  2956. internalerror(200312081);
  2957. first_call_helper:=tprocedureofobject(r)()
  2958. end;
  2959. function ttypeconvnode.pass_1 : tnode;
  2960. begin
  2961. if warn_pointer_to_signed then
  2962. cgmessage(type_w_pointer_to_signed);
  2963. result:=nil;
  2964. firstpass(left);
  2965. if codegenerror then
  2966. exit;
  2967. expectloc:=left.expectloc;
  2968. result:=first_call_helper(convtype);
  2969. end;
  2970. function ttypeconvnode.retains_value_location:boolean;
  2971. begin
  2972. result:=(convtype=tc_equal) or
  2973. { typecasting from void is always allowed }
  2974. is_void(left.resultdef) or
  2975. (left.resultdef.typ=formaldef) or
  2976. { int 2 int with same size reuses same location, or for
  2977. tp7 mode also allow size < orignal size }
  2978. (
  2979. (convtype=tc_int_2_int) and
  2980. (
  2981. not is_bitpacked_access(left) and
  2982. (resultdef.size=left.resultdef.size) or
  2983. ((m_tp7 in current_settings.modeswitches) and
  2984. (resultdef.size<left.resultdef.size))
  2985. )
  2986. ) or
  2987. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2988. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2989. (nf_explicit in flags) and
  2990. (resultdef.size=left.resultdef.size));
  2991. end;
  2992. function ttypeconvnode.assign_allowed:boolean;
  2993. begin
  2994. result:=retains_value_location;
  2995. { When using only a part of the value it can't be in a register since
  2996. that will load the value in a new register first }
  2997. { the same goes for changing the sign of equal-sized values which
  2998. are smaller than an entire register }
  2999. if result and
  3000. { don't try to check the size of an open array }
  3001. is_open_array(resultdef) or
  3002. (resultdef.size<left.resultdef.size) or
  3003. ((resultdef.size=left.resultdef.size) and
  3004. (left.resultdef.size<sizeof(aint)) and
  3005. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  3006. make_not_regable(left,[ra_addr_regable]);
  3007. end;
  3008. function ttypeconvnode.docompare(p: tnode) : boolean;
  3009. begin
  3010. docompare :=
  3011. inherited docompare(p) and
  3012. (convtype = ttypeconvnode(p).convtype) and
  3013. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  3014. end;
  3015. procedure ttypeconvnode._second_int_to_int;
  3016. begin
  3017. second_int_to_int;
  3018. end;
  3019. procedure ttypeconvnode._second_string_to_string;
  3020. begin
  3021. second_string_to_string;
  3022. end;
  3023. procedure ttypeconvnode._second_cstring_to_pchar;
  3024. begin
  3025. second_cstring_to_pchar;
  3026. end;
  3027. procedure ttypeconvnode._second_cstring_to_int;
  3028. begin
  3029. second_cstring_to_int;
  3030. end;
  3031. procedure ttypeconvnode._second_string_to_chararray;
  3032. begin
  3033. second_string_to_chararray;
  3034. end;
  3035. procedure ttypeconvnode._second_array_to_pointer;
  3036. begin
  3037. second_array_to_pointer;
  3038. end;
  3039. procedure ttypeconvnode._second_pointer_to_array;
  3040. begin
  3041. second_pointer_to_array;
  3042. end;
  3043. procedure ttypeconvnode._second_chararray_to_string;
  3044. begin
  3045. second_chararray_to_string;
  3046. end;
  3047. procedure ttypeconvnode._second_char_to_string;
  3048. begin
  3049. second_char_to_string;
  3050. end;
  3051. procedure ttypeconvnode._second_int_to_real;
  3052. begin
  3053. second_int_to_real;
  3054. end;
  3055. procedure ttypeconvnode._second_real_to_real;
  3056. begin
  3057. second_real_to_real;
  3058. end;
  3059. procedure ttypeconvnode._second_cord_to_pointer;
  3060. begin
  3061. second_cord_to_pointer;
  3062. end;
  3063. procedure ttypeconvnode._second_proc_to_procvar;
  3064. begin
  3065. second_proc_to_procvar;
  3066. end;
  3067. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3068. begin
  3069. second_nil_to_methodprocvar;
  3070. end;
  3071. procedure ttypeconvnode._second_bool_to_int;
  3072. begin
  3073. second_bool_to_int;
  3074. end;
  3075. procedure ttypeconvnode._second_int_to_bool;
  3076. begin
  3077. second_int_to_bool;
  3078. end;
  3079. procedure ttypeconvnode._second_bool_to_bool;
  3080. begin
  3081. second_bool_to_bool;
  3082. end;
  3083. procedure ttypeconvnode._second_set_to_set;
  3084. begin
  3085. second_set_to_set;
  3086. end;
  3087. procedure ttypeconvnode._second_ansistring_to_pchar;
  3088. begin
  3089. second_ansistring_to_pchar;
  3090. end;
  3091. procedure ttypeconvnode._second_class_to_intf;
  3092. begin
  3093. second_class_to_intf;
  3094. end;
  3095. procedure ttypeconvnode._second_char_to_char;
  3096. begin
  3097. second_char_to_char;
  3098. end;
  3099. procedure ttypeconvnode._second_elem_to_openarray;
  3100. begin
  3101. second_elem_to_openarray;
  3102. end;
  3103. procedure ttypeconvnode._second_nothing;
  3104. begin
  3105. second_nothing;
  3106. end;
  3107. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3108. const
  3109. secondconvert : array[tconverttype] of pointer = (
  3110. @ttypeconvnode._second_nothing, {none}
  3111. @ttypeconvnode._second_nothing, {equal}
  3112. @ttypeconvnode._second_nothing, {not_possible}
  3113. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3114. @ttypeconvnode._second_char_to_string,
  3115. @ttypeconvnode._second_nothing, {char_to_charray}
  3116. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3117. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3118. @ttypeconvnode._second_cstring_to_pchar,
  3119. @ttypeconvnode._second_cstring_to_int,
  3120. @ttypeconvnode._second_ansistring_to_pchar,
  3121. @ttypeconvnode._second_string_to_chararray,
  3122. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3123. @ttypeconvnode._second_array_to_pointer,
  3124. @ttypeconvnode._second_pointer_to_array,
  3125. @ttypeconvnode._second_int_to_int,
  3126. @ttypeconvnode._second_int_to_bool,
  3127. @ttypeconvnode._second_bool_to_bool,
  3128. @ttypeconvnode._second_bool_to_int,
  3129. @ttypeconvnode._second_real_to_real,
  3130. @ttypeconvnode._second_int_to_real,
  3131. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3132. @ttypeconvnode._second_proc_to_procvar,
  3133. @ttypeconvnode._second_nil_to_methodprocvar,
  3134. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3135. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3136. @ttypeconvnode._second_cord_to_pointer,
  3137. @ttypeconvnode._second_nothing, { interface 2 string }
  3138. @ttypeconvnode._second_nothing, { interface 2 guid }
  3139. @ttypeconvnode._second_class_to_intf,
  3140. @ttypeconvnode._second_char_to_char,
  3141. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3142. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3143. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3144. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3145. @ttypeconvnode._second_nothing, { variant_2_enum }
  3146. @ttypeconvnode._second_nothing, { enum_2_variant }
  3147. @ttypeconvnode._second_nothing, { variant_2_interface }
  3148. @ttypeconvnode._second_nothing, { interface_2_variant }
  3149. @ttypeconvnode._second_nothing, { array_2_dynarray }
  3150. @ttypeconvnode._second_elem_to_openarray { elem_2_openarray }
  3151. );
  3152. type
  3153. tprocedureofobject = procedure of object;
  3154. var
  3155. r : packed record
  3156. proc : pointer;
  3157. obj : pointer;
  3158. end;
  3159. begin
  3160. { this is a little bit dirty but it works }
  3161. { and should be quite portable too }
  3162. r.proc:=secondconvert[c];
  3163. r.obj:=self;
  3164. tprocedureofobject(r)();
  3165. end;
  3166. {*****************************************************************************
  3167. TASNODE
  3168. *****************************************************************************}
  3169. function tasisnode.target_specific_typecheck: boolean;
  3170. begin
  3171. result:=false;
  3172. end;
  3173. function tasisnode.pass_typecheck: tnode;
  3174. var
  3175. hp : tnode;
  3176. begin
  3177. result:=nil;
  3178. typecheckpass(right);
  3179. typecheckpass(left);
  3180. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3181. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3182. if codegenerror then
  3183. exit;
  3184. if target_specific_typecheck then
  3185. begin
  3186. // ok
  3187. end
  3188. else if (right.resultdef.typ=classrefdef) then
  3189. begin
  3190. { left maybe an interface reference }
  3191. if is_interfacecom(left.resultdef) or
  3192. is_javainterface(left.resultdef) then
  3193. begin
  3194. { relation checks are not possible }
  3195. end
  3196. { or left must be a class }
  3197. else if is_class(left.resultdef) or
  3198. is_javaclass(left.resultdef) then
  3199. begin
  3200. { the operands must be related }
  3201. if (not(tobjectdef(left.resultdef).is_related(
  3202. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3203. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  3204. tobjectdef(left.resultdef)))) then
  3205. CGMessage2(type_e_classes_not_related,
  3206. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3207. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3208. end
  3209. else
  3210. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3211. case nodetype of
  3212. isn:
  3213. resultdef:=pasbool8type;
  3214. asn:
  3215. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3216. end;
  3217. end
  3218. else if is_interface(right.resultdef) or
  3219. is_dispinterface(right.resultdef) or
  3220. is_javainterface(right.resultdef) then
  3221. begin
  3222. case nodetype of
  3223. isn:
  3224. resultdef:=pasbool8type;
  3225. asn:
  3226. resultdef:=right.resultdef;
  3227. end;
  3228. { left is a class or interface }
  3229. if is_javainterface(right.resultdef) then
  3230. begin
  3231. if not is_java_class_or_interface(left.resultdef) then
  3232. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3233. end
  3234. else if not(is_class(left.resultdef) or
  3235. is_interfacecom(left.resultdef)) then
  3236. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  3237. else
  3238. begin
  3239. { load the GUID of the interface }
  3240. if (right.nodetype=typen) then
  3241. begin
  3242. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3243. begin
  3244. if assigned(tobjectdef(right.resultdef).iidstr) then
  3245. begin
  3246. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3247. tstringconstnode(hp).changestringtype(cshortstringtype);
  3248. right.free;
  3249. right:=hp;
  3250. end
  3251. else
  3252. internalerror(201006131);
  3253. end
  3254. else
  3255. begin
  3256. if assigned(tobjectdef(right.resultdef).iidguid) then
  3257. begin
  3258. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3259. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3260. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3261. right.free;
  3262. right:=hp;
  3263. end
  3264. else
  3265. internalerror(201006132);
  3266. end;
  3267. typecheckpass(right);
  3268. end;
  3269. end;
  3270. end
  3271. else
  3272. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3273. end;
  3274. {*****************************************************************************
  3275. TISNODE
  3276. *****************************************************************************}
  3277. constructor tisnode.create(l,r : tnode);
  3278. begin
  3279. inherited create(isn,l,r);
  3280. end;
  3281. constructor tisnode.create_internal(l, r: tnode);
  3282. begin
  3283. create(l,r);
  3284. include(flags,nf_internal);
  3285. end;
  3286. function tisnode.pass_1 : tnode;
  3287. var
  3288. procname: string;
  3289. begin
  3290. result:=nil;
  3291. { Passing a class type to an "is" expression cannot result in a class
  3292. of that type to be constructed.
  3293. }
  3294. include(right.flags,nf_ignore_for_wpo);
  3295. if is_class(left.resultdef) and
  3296. (right.resultdef.typ=classrefdef) then
  3297. result := ccallnode.createinternres('fpc_do_is',
  3298. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3299. resultdef)
  3300. else
  3301. begin
  3302. if is_class(left.resultdef) then
  3303. if is_shortstring(right.resultdef) then
  3304. procname := 'fpc_class_is_corbaintf'
  3305. else
  3306. procname := 'fpc_class_is_intf'
  3307. else
  3308. if right.resultdef.typ=classrefdef then
  3309. procname := 'fpc_intf_is_class'
  3310. else
  3311. procname := 'fpc_intf_is';
  3312. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3313. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3314. end;
  3315. left := nil;
  3316. right := nil;
  3317. //firstpass(call);
  3318. if codegenerror then
  3319. exit;
  3320. end;
  3321. { dummy pass_2, it will never be called, but we need one since }
  3322. { you can't instantiate an abstract class }
  3323. procedure tisnode.pass_generate_code;
  3324. begin
  3325. end;
  3326. {*****************************************************************************
  3327. TASNODE
  3328. *****************************************************************************}
  3329. constructor tasnode.create(l,r : tnode);
  3330. begin
  3331. inherited create(asn,l,r);
  3332. call := nil;
  3333. end;
  3334. constructor tasnode.create_internal(l,r : tnode);
  3335. begin
  3336. create(l,r);
  3337. include(flags,nf_internal);
  3338. end;
  3339. destructor tasnode.destroy;
  3340. begin
  3341. call.free;
  3342. inherited destroy;
  3343. end;
  3344. function tasnode.dogetcopy: tnode;
  3345. begin
  3346. result := inherited dogetcopy;
  3347. if assigned(call) then
  3348. tasnode(result).call := call.getcopy
  3349. else
  3350. tasnode(result).call := nil;
  3351. end;
  3352. function tasnode.docompare(p: tnode): boolean;
  3353. begin
  3354. result:=
  3355. inherited docompare(p) and
  3356. tasnode(p).call.isequal(call);
  3357. end;
  3358. function tasnode.pass_1 : tnode;
  3359. var
  3360. procname: string;
  3361. begin
  3362. result:=nil;
  3363. { Passing a class type to an "as" expression cannot result in a class
  3364. of that type to be constructed.
  3365. }
  3366. include(right.flags,nf_ignore_for_wpo);
  3367. if not assigned(call) then
  3368. begin
  3369. if is_class(left.resultdef) and
  3370. (right.resultdef.typ=classrefdef) then
  3371. call := ccallnode.createinternres('fpc_do_as',
  3372. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3373. resultdef)
  3374. else
  3375. begin
  3376. if is_class(left.resultdef) then
  3377. if is_shortstring(right.resultdef) then
  3378. procname := 'fpc_class_as_corbaintf'
  3379. else
  3380. procname := 'fpc_class_as_intf'
  3381. else
  3382. if right.resultdef.typ=classrefdef then
  3383. procname := 'fpc_intf_as_class'
  3384. else
  3385. procname := 'fpc_intf_as';
  3386. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3387. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3388. end;
  3389. left := nil;
  3390. right := nil;
  3391. firstpass(call);
  3392. if codegenerror then
  3393. exit;
  3394. expectloc:=call.expectloc;
  3395. end;
  3396. end;
  3397. end.