ncnv.pas 137 KB

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