ncnv.pas 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663
  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 (tstringdef(resultdef).stringtype=st_ansistring) then
  838. begin
  839. result:=ccallnode.createinternres(
  840. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  841. ccallparanode.create(
  842. cordconstnode.create(
  843. ord(tarraydef(left.resultdef).lowrange=0),
  844. pasbool8type,
  845. false
  846. ),
  847. ccallparanode.create(
  848. cordconstnode.create(
  849. tstringdef(resultdef).encoding,
  850. u16inttype,
  851. true
  852. ),
  853. ccallparanode.create(left,nil)
  854. )
  855. ),
  856. resultdef
  857. );
  858. end
  859. else
  860. result:=ccallnode.createinternres(
  861. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  862. ccallparanode.create(cordconstnode.create(
  863. ord(tarraydef(left.resultdef).lowrange=0),pasbool8type,false),
  864. ccallparanode.create(left,nil)),resultdef);
  865. left:=nil;
  866. end;
  867. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  868. var
  869. newblock : tblocknode;
  870. newstat : tstatementnode;
  871. restemp : ttempcreatenode;
  872. pchtemp : pchar;
  873. arrsize : aint;
  874. chartype : string[8];
  875. begin
  876. result := nil;
  877. with tarraydef(resultdef) do
  878. begin
  879. if highrange<lowrange then
  880. internalerror(200501051);
  881. arrsize := highrange-lowrange+1;
  882. end;
  883. if (left.nodetype = stringconstn) and
  884. (tstringconstnode(left).cst_type=cst_conststring) then
  885. begin
  886. { if the array of char is large enough we can use the string
  887. constant directly. This is handled in ncgcnv }
  888. if (arrsize>=tstringconstnode(left).len) and
  889. is_char(tarraydef(resultdef).elementdef) then
  890. begin
  891. { pad the constant string with #0 to the array len }
  892. { (2.0.x compatible) }
  893. if (arrsize>tstringconstnode(left).len) then
  894. begin
  895. pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  896. left.free;
  897. left:=cstringconstnode.createpchar(pchtemp,arrsize);
  898. typecheckpass(left);
  899. end;
  900. exit;
  901. end;
  902. { Convert to wide/short/ansistring and call default helper }
  903. if is_widechar(tarraydef(resultdef).elementdef) then
  904. inserttypeconv(left,cwidestringtype)
  905. else
  906. begin
  907. if tstringconstnode(left).len>255 then
  908. inserttypeconv(left,cansistringtype)
  909. else
  910. inserttypeconv(left,cshortstringtype);
  911. end;
  912. end;
  913. if is_widechar(tarraydef(resultdef).elementdef) then
  914. chartype:='widechar'
  915. else
  916. chartype:='char';
  917. newblock:=internalstatements(newstat);
  918. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  919. addstatement(newstat,restemp);
  920. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  921. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  922. ctemprefnode.create(restemp),nil))));
  923. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  924. addstatement(newstat,ctemprefnode.create(restemp));
  925. result:=newblock;
  926. left:=nil;
  927. end;
  928. function ttypeconvnode.typecheck_char_to_string : tnode;
  929. var
  930. procname: string[31];
  931. para : tcallparanode;
  932. hp : tstringconstnode;
  933. ws : pcompilerwidestring;
  934. newblock : tblocknode;
  935. newstat : tstatementnode;
  936. restemp : ttempcreatenode;
  937. sa : ansistring;
  938. cw : WideChar;
  939. l : SizeUInt;
  940. begin
  941. result:=nil;
  942. { we can't do widechar to ansichar conversions at compile time, since }
  943. { this maps all non-ascii chars to '?' -> loses information }
  944. if (left.nodetype=ordconstn) and
  945. ((tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) or
  946. (torddef(left.resultdef).ordtype=uchar) or
  947. ((torddef(left.resultdef).ordtype=uwidechar) and
  948. (current_settings.sourcecodepage<>CP_UTF8)
  949. )
  950. )
  951. { widechar >=128 is destroyed }
  952. {(tordconstnode(left).value.uvalue<128))} then
  953. begin
  954. if (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
  955. begin
  956. initwidestring(ws);
  957. if torddef(left.resultdef).ordtype=uwidechar then
  958. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  959. else
  960. concatwidestringchar(ws,asciichar2unicode(chr(tordconstnode(left).value.uvalue)));
  961. hp:=cstringconstnode.createwstr(ws);
  962. hp.changestringtype(resultdef);
  963. donewidestring(ws);
  964. end
  965. else
  966. begin
  967. if (torddef(left.resultdef).ordtype=uwidechar) then
  968. begin
  969. if (current_settings.sourcecodepage<>CP_UTF8) then
  970. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)))
  971. else
  972. begin
  973. exit;
  974. {Word(cw):=tcompilerwidechar(tordconstnode(left).value.uvalue);
  975. SetLength(sa,5);
  976. l:=UnicodeToUtf8(@(sa[1]),Length(sa),@cw,1);
  977. SetLength(sa,l-1);
  978. hp:=cstringconstnode.createstr(sa);}
  979. end
  980. end
  981. else
  982. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  983. tstringconstnode(hp).changestringtype(resultdef);
  984. end;
  985. result:=hp;
  986. end
  987. else
  988. { shortstrings are handled 'inline' (except for widechars) }
  989. if (tstringdef(resultdef).stringtype<>st_shortstring) or
  990. (torddef(left.resultdef).ordtype=uwidechar) then
  991. begin
  992. if (tstringdef(resultdef).stringtype<>st_shortstring) then
  993. begin
  994. { parameter }
  995. para:=ccallparanode.create(left,nil);
  996. { encoding required? }
  997. if tstringdef(resultdef).stringtype=st_ansistring then
  998. para:=ccallparanode.create(cordconstnode.create(tstringdef(resultdef).encoding,u16inttype,true),para);
  999. { create the procname }
  1000. if torddef(left.resultdef).ordtype<>uwidechar then
  1001. procname:='fpc_char_to_'
  1002. else
  1003. procname:='fpc_uchar_to_';
  1004. procname:=procname+tstringdef(resultdef).stringtypname;
  1005. { and finally the call }
  1006. result:=ccallnode.createinternres(procname,para,resultdef);
  1007. end
  1008. else
  1009. begin
  1010. newblock:=internalstatements(newstat);
  1011. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1012. addstatement(newstat,restemp);
  1013. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1014. ctemprefnode.create(restemp),nil))));
  1015. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1016. addstatement(newstat,ctemprefnode.create(restemp));
  1017. result:=newblock;
  1018. end;
  1019. left := nil;
  1020. end
  1021. else
  1022. begin
  1023. { create word(byte(char) shl 8 or 1) for litte endian machines }
  1024. { and word(byte(char) or 256) for big endian machines }
  1025. left := ctypeconvnode.create_internal(left,u8inttype);
  1026. if (target_info.endian = endian_little) then
  1027. left := caddnode.create(orn,
  1028. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  1029. cordconstnode.create(1,s32inttype,false))
  1030. else
  1031. left := caddnode.create(orn,left,
  1032. cordconstnode.create(1 shl 8,s32inttype,false));
  1033. left := ctypeconvnode.create_internal(left,u16inttype);
  1034. typecheckpass(left);
  1035. end;
  1036. end;
  1037. function ttypeconvnode.typecheck_string_to_string : tnode;
  1038. begin
  1039. result:=nil;
  1040. if (left.nodetype=stringconstn) and
  1041. ((tstringdef(resultdef).stringtype=st_shortstring) or
  1042. ((tstringdef(resultdef).stringtype=st_ansistring) and
  1043. (tstringdef(resultdef).encoding<>CP_NONE)
  1044. )
  1045. ) and
  1046. ((tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1047. (current_settings.sourcecodepage<>CP_UTF8)
  1048. ) then
  1049. begin
  1050. tstringconstnode(left).changestringtype(resultdef);
  1051. Result:=left;
  1052. left:=nil;
  1053. end
  1054. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  1055. (tstringdef(left.resultdef).stringtype=st_ansistring) and
  1056. (tstringdef(resultdef).encoding<>tstringdef(left.resultdef).encoding) then
  1057. begin
  1058. result:=ccallnode.createinternres(
  1059. 'fpc_ansistr_to_ansistr',
  1060. ccallparanode.create(
  1061. cordconstnode.create(
  1062. tstringdef(resultdef).encoding,
  1063. u16inttype,
  1064. true
  1065. ),
  1066. ccallparanode.create(left,nil)
  1067. ),
  1068. resultdef
  1069. );
  1070. left:=nil;
  1071. end;
  1072. end;
  1073. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  1074. begin
  1075. if resultdef.size <> 1 then
  1076. begin
  1077. { convert first to string, then to chararray }
  1078. inserttypeconv(left,cshortstringtype);
  1079. inserttypeconv(left,resultdef);
  1080. result:=left;
  1081. left := nil;
  1082. exit;
  1083. end;
  1084. result := nil;
  1085. end;
  1086. function ttypeconvnode.typecheck_char_to_char : tnode;
  1087. var
  1088. hp : tordconstnode;
  1089. cha : array[0..3] of ansichar;
  1090. cw : WideChar;
  1091. begin
  1092. result:=nil;
  1093. if (left.nodetype=ordconstn) and
  1094. ((torddef(resultdef).ordtype<>uchar) or
  1095. (torddef(left.resultdef).ordtype<>uwidechar) or
  1096. (current_settings.sourcecodepage<>CP_UTF8))
  1097. { >= 128 is replaced by '?' currently -> loses information }
  1098. {(tordconstnode(left).value.uvalue<128))} then
  1099. begin
  1100. if (torddef(resultdef).ordtype=uchar) and
  1101. (torddef(left.resultdef).ordtype=uwidechar) and
  1102. (current_settings.sourcecodepage<>CP_UTF8) then
  1103. begin
  1104. hp:=cordconstnode.create(
  1105. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  1106. cchartype,true);
  1107. result:=hp;
  1108. end
  1109. else if (torddef(resultdef).ordtype=uwidechar) and
  1110. (torddef(left.resultdef).ordtype=uchar) then
  1111. begin
  1112. hp:=cordconstnode.create(
  1113. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  1114. cwidechartype,true);
  1115. result:=hp;
  1116. end
  1117. else
  1118. internalerror(200105131);
  1119. exit;
  1120. end;
  1121. end;
  1122. function ttypeconvnode.typecheck_int_to_int : tnode;
  1123. var
  1124. v : TConstExprInt;
  1125. begin
  1126. result:=nil;
  1127. if left.nodetype=ordconstn then
  1128. begin
  1129. v:=tordconstnode(left).value;
  1130. if is_currency(resultdef) then
  1131. v:=v*10000;
  1132. if (resultdef.typ=pointerdef) then
  1133. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1134. else
  1135. begin
  1136. if is_currency(left.resultdef) then
  1137. v:=v div 10000;
  1138. result:=cordconstnode.create(v,resultdef,false);
  1139. end;
  1140. end
  1141. else if left.nodetype=pointerconstn then
  1142. begin
  1143. v:=tpointerconstnode(left).value;
  1144. if (resultdef.typ=pointerdef) then
  1145. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1146. else
  1147. begin
  1148. if is_currency(resultdef) then
  1149. v:=v*10000;
  1150. result:=cordconstnode.create(v,resultdef,false);
  1151. end;
  1152. end
  1153. else
  1154. begin
  1155. { multiply by 10000 for currency. We need to use getcopy to pass
  1156. the argument because the current node is always disposed. Only
  1157. inserting the multiply in the left node is not possible because
  1158. it'll get in an infinite loop to convert int->currency }
  1159. if is_currency(resultdef) then
  1160. begin
  1161. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1162. include(result.flags,nf_is_currency);
  1163. end
  1164. else if is_currency(left.resultdef) then
  1165. begin
  1166. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1167. include(result.flags,nf_is_currency);
  1168. end;
  1169. end;
  1170. end;
  1171. function ttypeconvnode.typecheck_int_to_real : tnode;
  1172. var
  1173. rv : bestreal;
  1174. begin
  1175. result:=nil;
  1176. if left.nodetype=ordconstn then
  1177. begin
  1178. rv:=tordconstnode(left).value;
  1179. if is_currency(resultdef) then
  1180. rv:=rv*10000.0
  1181. else if is_currency(left.resultdef) then
  1182. rv:=rv/10000.0;
  1183. result:=crealconstnode.create(rv,resultdef);
  1184. end
  1185. else
  1186. begin
  1187. { multiply by 10000 for currency. We need to use getcopy to pass
  1188. the argument because the current node is always disposed. Only
  1189. inserting the multiply in the left node is not possible because
  1190. it'll get in an infinite loop to convert int->currency }
  1191. if is_currency(resultdef) then
  1192. begin
  1193. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1194. include(result.flags,nf_is_currency);
  1195. end
  1196. else if is_currency(left.resultdef) then
  1197. begin
  1198. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1199. include(result.flags,nf_is_currency);
  1200. end;
  1201. end;
  1202. end;
  1203. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1204. begin
  1205. if not is_currency(resultdef) then
  1206. internalerror(200304221);
  1207. result:=nil;
  1208. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1209. include(left.flags,nf_is_currency);
  1210. typecheckpass(left);
  1211. { Convert constants directly, else call Round() }
  1212. if left.nodetype=realconstn then
  1213. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1214. else
  1215. begin
  1216. result:=ccallnode.createinternres('fpc_round_real',
  1217. ccallparanode.create(left,nil),resultdef);
  1218. left:=nil;
  1219. end;
  1220. end;
  1221. function ttypeconvnode.typecheck_real_to_real : tnode;
  1222. begin
  1223. result:=nil;
  1224. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1225. begin
  1226. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1227. include(left.flags,nf_is_currency);
  1228. typecheckpass(left);
  1229. end
  1230. else
  1231. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1232. begin
  1233. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1234. include(left.flags,nf_is_currency);
  1235. typecheckpass(left);
  1236. end;
  1237. end;
  1238. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1239. begin
  1240. result:=nil;
  1241. if is_pwidechar(resultdef) then
  1242. inserttypeconv(left,cwidestringtype)
  1243. else
  1244. inserttypeconv(left,cshortstringtype);
  1245. { evaluate again, reset resultdef so the convert_typ
  1246. will be calculated again and cstring_to_pchar will
  1247. be used for futher conversion }
  1248. convtype:=tc_none;
  1249. result:=pass_typecheck;
  1250. end;
  1251. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1252. begin
  1253. result:=nil;
  1254. if is_pwidechar(resultdef) then
  1255. inserttypeconv(left,cwidestringtype)
  1256. else
  1257. if is_pchar(resultdef) and
  1258. (is_widestring(left.resultdef) or
  1259. is_unicodestring(left.resultdef)) then
  1260. begin
  1261. inserttypeconv(left,cansistringtype);
  1262. { the second pass of second_cstring_to_pchar expects a }
  1263. { strinconstn, but this may become a call to the }
  1264. { widestring manager in case left contains "high ascii" }
  1265. if (left.nodetype<>stringconstn) then
  1266. begin
  1267. result:=left;
  1268. left:=nil;
  1269. end;
  1270. end;
  1271. end;
  1272. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1273. var
  1274. fcc : cardinal;
  1275. pb : pbyte;
  1276. begin
  1277. result:=nil;
  1278. if left.nodetype<>stringconstn then
  1279. internalerror(200510012);
  1280. if tstringconstnode(left).len=4 then
  1281. begin
  1282. pb:=pbyte(tstringconstnode(left).value_str);
  1283. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1284. result:=cordconstnode.create(fcc,u32inttype,false);
  1285. end
  1286. else
  1287. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1288. end;
  1289. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1290. var
  1291. hp : tnode;
  1292. begin
  1293. result:=nil;
  1294. if left.nodetype<>arrayconstructorn then
  1295. internalerror(5546);
  1296. { remove typeconv node }
  1297. hp:=left;
  1298. left:=nil;
  1299. { create a set constructor tree }
  1300. arrayconstructor_to_set(hp);
  1301. result:=hp;
  1302. end;
  1303. function ttypeconvnode.typecheck_set_to_set : tnode;
  1304. begin
  1305. result:=nil;
  1306. { constant sets can be converted by changing the type only }
  1307. if (left.nodetype=setconstn) then
  1308. begin
  1309. left.resultdef:=resultdef;
  1310. result:=left;
  1311. left:=nil;
  1312. exit;
  1313. end;
  1314. end;
  1315. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1316. var
  1317. newblock : tblocknode;
  1318. newstat : tstatementnode;
  1319. restemp : ttempcreatenode;
  1320. begin
  1321. if tstringdef(resultdef).stringtype=st_shortstring then
  1322. begin
  1323. newblock:=internalstatements(newstat);
  1324. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1325. addstatement(newstat,restemp);
  1326. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1327. ctemprefnode.create(restemp),nil))));
  1328. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1329. addstatement(newstat,ctemprefnode.create(restemp));
  1330. result:=newblock;
  1331. end
  1332. else if tstringdef(resultdef).stringtype=st_ansistring then
  1333. result := ccallnode.createinternres(
  1334. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1335. ccallparanode.create(
  1336. cordconstnode.create(tstringdef(resultdef).encoding,u16inttype,true),
  1337. ccallparanode.create(left,nil)
  1338. ),
  1339. resultdef
  1340. )
  1341. else
  1342. result := ccallnode.createinternres(
  1343. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1344. ccallparanode.create(left,nil),resultdef);
  1345. left:=nil;
  1346. end;
  1347. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1348. begin
  1349. if assigned(tobjectdef(left.resultdef).iidstr) then
  1350. begin
  1351. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1352. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1353. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1354. tstringconstnode(result).changestringtype(cshortstringtype);
  1355. end;
  1356. end;
  1357. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1358. begin
  1359. if assigned(tobjectdef(left.resultdef).iidguid) then
  1360. begin
  1361. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1362. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1363. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1364. end;
  1365. end;
  1366. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1367. begin
  1368. { a dynamic array is a pointer to an array, so to convert it to }
  1369. { an open array, we have to dereference it (JM) }
  1370. result := ctypeconvnode.create_internal(left,voidpointertype);
  1371. typecheckpass(result);
  1372. { left is reused }
  1373. left := nil;
  1374. result := cderefnode.create(result);
  1375. include(result.flags,nf_no_checkpointer);
  1376. result.resultdef := resultdef;
  1377. end;
  1378. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1379. var
  1380. newblock : tblocknode;
  1381. newstat : tstatementnode;
  1382. restemp : ttempcreatenode;
  1383. begin
  1384. if tstringdef(resultdef).stringtype=st_shortstring then
  1385. begin
  1386. newblock:=internalstatements(newstat);
  1387. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1388. addstatement(newstat,restemp);
  1389. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1390. ctemprefnode.create(restemp),nil))));
  1391. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1392. addstatement(newstat,ctemprefnode.create(restemp));
  1393. result:=newblock;
  1394. end
  1395. else if tstringdef(resultdef).stringtype=st_ansistring then
  1396. begin
  1397. result:=ccallnode.createinternres(
  1398. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1399. ccallparanode.create(
  1400. cordconstnode.create(
  1401. tstringdef(resultdef).encoding,
  1402. u16inttype,
  1403. true
  1404. ),
  1405. ccallparanode.create(left,nil)
  1406. ),
  1407. resultdef
  1408. );
  1409. end
  1410. else
  1411. result := ccallnode.createinternres(
  1412. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1413. ccallparanode.create(left,nil),resultdef);
  1414. left:=nil;
  1415. end;
  1416. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1417. begin
  1418. result := ccallnode.createinternres(
  1419. 'fpc_variant_to_dynarray',
  1420. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1421. ccallparanode.create(left,nil)
  1422. ),resultdef);
  1423. typecheckpass(result);
  1424. left:=nil;
  1425. end;
  1426. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1427. begin
  1428. result := ccallnode.createinternres(
  1429. 'fpc_dynarray_to_variant',
  1430. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1431. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1432. ),resultdef);
  1433. typecheckpass(result);
  1434. left:=nil;
  1435. end;
  1436. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1437. begin
  1438. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1439. result := ccallnode.createinternres(
  1440. 'fpc_variant_to_idispatch',
  1441. ccallparanode.create(left,nil)
  1442. ,resultdef)
  1443. else
  1444. result := ccallnode.createinternres(
  1445. 'fpc_variant_to_interface',
  1446. ccallparanode.create(left,nil)
  1447. ,resultdef);
  1448. typecheckpass(result);
  1449. left:=nil;
  1450. end;
  1451. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1452. begin
  1453. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1454. result := ccallnode.createinternres(
  1455. 'fpc_idispatch_to_variant',
  1456. ccallparanode.create(left,nil)
  1457. ,resultdef)
  1458. else
  1459. result := ccallnode.createinternres(
  1460. 'fpc_interface_to_variant',
  1461. ccallparanode.create(left,nil)
  1462. ,resultdef);
  1463. typecheckpass(result);
  1464. left:=nil;
  1465. end;
  1466. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1467. begin
  1468. result := ctypeconvnode.create_internal(left,sinttype);
  1469. result := ctypeconvnode.create_internal(result,resultdef);
  1470. typecheckpass(result);
  1471. { left is reused }
  1472. left := nil;
  1473. end;
  1474. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1475. begin
  1476. result := ctypeconvnode.create_internal(left,sinttype);
  1477. result := ctypeconvnode.create_internal(result,cvarianttype);
  1478. typecheckpass(result);
  1479. { left is reused }
  1480. left := nil;
  1481. end;
  1482. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1483. var
  1484. newstatement : tstatementnode;
  1485. temp : ttempcreatenode;
  1486. temp2 : ttempcreatenode;
  1487. begin
  1488. { create statements with call to getmem+initialize }
  1489. result:=internalstatements(newstatement);
  1490. { create temp for result }
  1491. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1492. addstatement(newstatement,temp);
  1493. { get temp for array of lengths }
  1494. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1495. addstatement(newstatement,temp2);
  1496. { one dimensional }
  1497. addstatement(newstatement,cassignmentnode.create(
  1498. ctemprefnode.create_offset(temp2,0),
  1499. cordconstnode.create
  1500. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1501. { create call to fpc_dynarr_setlength }
  1502. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1503. ccallparanode.create(caddrnode.create_internal
  1504. (ctemprefnode.create(temp2)),
  1505. ccallparanode.create(cordconstnode.create
  1506. (1,s32inttype,true),
  1507. ccallparanode.create(caddrnode.create_internal
  1508. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1509. ccallparanode.create(
  1510. ctypeconvnode.create_internal(
  1511. ctemprefnode.create(temp),voidpointertype),
  1512. nil))))
  1513. ));
  1514. addstatement(newstatement,ctempdeletenode.create(temp2));
  1515. { copy ... }
  1516. addstatement(newstatement,cassignmentnode.create(
  1517. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1518. left
  1519. ));
  1520. { left is reused }
  1521. left:=nil;
  1522. { the last statement should return the value as
  1523. location and type, this is done be referencing the
  1524. temp and converting it first from a persistent temp to
  1525. normal temp }
  1526. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1527. addstatement(newstatement,ctemprefnode.create(temp));
  1528. end;
  1529. procedure copyparasym(p:TObject;arg:pointer);
  1530. var
  1531. newparast : TSymtable absolute arg;
  1532. vs : tparavarsym;
  1533. begin
  1534. if tsym(p).typ<>paravarsym then
  1535. exit;
  1536. with tparavarsym(p) do
  1537. begin
  1538. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1539. vs.defaultconstsym:=defaultconstsym;
  1540. newparast.insert(vs);
  1541. end;
  1542. end;
  1543. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1544. var
  1545. pd : tabstractprocdef;
  1546. nestinglevel : byte;
  1547. begin
  1548. result:=nil;
  1549. pd:=tabstractprocdef(left.resultdef);
  1550. { create procvardef (default for create_proc_to_procvar is voiddef,
  1551. but if later a regular inserttypeconvnode() is used to insert a type
  1552. conversion to the actual procvardef, totypedef will be set to the
  1553. real procvartype that we are converting to) }
  1554. if assigned(totypedef) and
  1555. (totypedef.typ=procvardef) then
  1556. resultdef:=totypedef
  1557. else
  1558. begin
  1559. nestinglevel:=pd.parast.symtablelevel;
  1560. resultdef:=tprocvardef.create(nestinglevel);
  1561. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1562. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1563. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1564. tprocvardef(resultdef).returndef:=pd.returndef;
  1565. { method ? then set the methodpointer flag }
  1566. if (pd.owner.symtabletype=ObjectSymtable) then
  1567. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1568. { only need the address of the method? this is needed
  1569. for @tobject.create. In this case there will be a loadn without
  1570. a methodpointer. }
  1571. if (left.nodetype=loadn) and
  1572. not assigned(tloadnode(left).left) and
  1573. (not(m_nested_procvars in current_settings.modeswitches) or
  1574. not is_nested_pd(tprocvardef(resultdef))) then
  1575. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1576. { Add parameters use only references, we don't need to keep the
  1577. parast. We use the parast from the original function to calculate
  1578. our parameter data and reset it afterwards }
  1579. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1580. tprocvardef(resultdef).calcparas;
  1581. end;
  1582. end;
  1583. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1584. const
  1585. resultdefconvert : array[tconverttype] of pointer = (
  1586. {none} nil,
  1587. {equal} nil,
  1588. {not_possible} nil,
  1589. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1590. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1591. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1592. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1593. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1594. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1595. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1596. { ansistring_2_pchar } nil,
  1597. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1598. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1599. { array_2_pointer } nil,
  1600. { pointer_2_array } nil,
  1601. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1602. { int_2_bool } nil,
  1603. { bool_2_bool } nil,
  1604. { bool_2_int } nil,
  1605. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1606. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1607. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1608. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1609. { nil_2_methodprocvar } nil,
  1610. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1611. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1612. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1613. { intf_2_string } @ttypeconvnode.typecheck_interface_to_string,
  1614. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1615. { class_2_intf } nil,
  1616. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1617. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1618. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1619. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1620. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1621. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1622. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1623. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1624. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1625. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1626. );
  1627. type
  1628. tprocedureofobject = function : tnode of object;
  1629. var
  1630. r : packed record
  1631. proc : pointer;
  1632. obj : pointer;
  1633. end;
  1634. begin
  1635. result:=nil;
  1636. { this is a little bit dirty but it works }
  1637. { and should be quite portable too }
  1638. r.proc:=resultdefconvert[c];
  1639. r.obj:=self;
  1640. if assigned(r.proc) then
  1641. result:=tprocedureofobject(r)();
  1642. end;
  1643. function ttypeconvnode.actualtargetnode: tnode;
  1644. begin
  1645. result:=self;
  1646. while (result.nodetype=typeconvn) and
  1647. ttypeconvnode(result).retains_value_location do
  1648. result:=ttypeconvnode(result).left;
  1649. end;
  1650. function ttypeconvnode.pass_typecheck:tnode;
  1651. var
  1652. hdef : tdef;
  1653. hp : tnode;
  1654. currprocdef : tabstractprocdef;
  1655. aprocdef : tprocdef;
  1656. eq : tequaltype;
  1657. cdoptions : tcompare_defs_options;
  1658. newblock: tblocknode;
  1659. newstatement: tstatementnode;
  1660. tempnode: ttempcreatenode;
  1661. begin
  1662. result:=nil;
  1663. resultdef:=totypedef;
  1664. typecheckpass(left);
  1665. if codegenerror then
  1666. exit;
  1667. { When absolute force tc_equal }
  1668. if (nf_absolute in flags) then
  1669. begin
  1670. convtype:=tc_equal;
  1671. if not(tstoreddef(resultdef).is_intregable) and
  1672. not(tstoreddef(resultdef).is_fpuregable) then
  1673. make_not_regable(left,[ra_addr_regable]);
  1674. exit;
  1675. end;
  1676. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1677. convert on the procvar value. This is used to access the
  1678. fields of a methodpointer }
  1679. if not(nf_load_procvar in flags) and
  1680. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1681. maybe_call_procvar(left,true);
  1682. { convert array constructors to sets, because there is no conversion
  1683. possible for array constructors }
  1684. if (resultdef.typ<>arraydef) and
  1685. is_array_constructor(left.resultdef) then
  1686. begin
  1687. arrayconstructor_to_set(left);
  1688. typecheckpass(left);
  1689. end;
  1690. if convtype=tc_none then
  1691. begin
  1692. cdoptions:=[cdo_check_operator,cdo_allow_variant,cdo_warn_incompatible_univ];
  1693. if nf_explicit in flags then
  1694. include(cdoptions,cdo_explicit);
  1695. if nf_internal in flags then
  1696. include(cdoptions,cdo_internal);
  1697. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1698. case eq of
  1699. te_exact,
  1700. te_equal :
  1701. begin
  1702. result := simplify(false);
  1703. if assigned(result) then
  1704. exit;
  1705. { in case of bitpacked accesses, the original type must
  1706. remain so that not too many/few bits are laoded }
  1707. if is_bitpacked_access(left) then
  1708. convtype:=tc_int_2_int;
  1709. { Only leave when there is no conversion to do.
  1710. We can still need to call a conversion routine,
  1711. like the routine to convert a stringconstnode }
  1712. if (convtype in [tc_equal,tc_not_possible]) and
  1713. { some conversions, like dynarray to pointer in Delphi
  1714. mode, must not be removed, because then we get memory
  1715. leaks due to missing temp finalization }
  1716. (not is_managed_type(left.resultdef) or
  1717. { different kinds of refcounted types may need calls
  1718. to different kinds of refcounting helpers }
  1719. (resultdef=left.resultdef)) then
  1720. begin
  1721. left.resultdef:=resultdef;
  1722. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1723. include(left.flags, nf_typedaddr);
  1724. result:=left;
  1725. left:=nil;
  1726. exit;
  1727. end;
  1728. end;
  1729. te_convert_l1,
  1730. te_convert_l2,
  1731. te_convert_l3,
  1732. te_convert_l4,
  1733. te_convert_l5:
  1734. { nothing to do }
  1735. ;
  1736. te_convert_operator :
  1737. begin
  1738. include(current_procinfo.flags,pi_do_call);
  1739. addsymref(aprocdef.procsym);
  1740. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1741. { tell explicitly which def we must use !! (PM) }
  1742. tcallnode(hp).procdefinition:=aprocdef;
  1743. left:=nil;
  1744. result:=hp;
  1745. exit;
  1746. end;
  1747. te_incompatible :
  1748. begin
  1749. { Procedures have a resultdef of voiddef and functions of their
  1750. own resultdef. They will therefore always be incompatible with
  1751. a procvar. Because isconvertable cannot check for procedures we
  1752. use an extra check for them.}
  1753. if (left.nodetype=calln) and
  1754. (tcallnode(left).required_para_count=0) and
  1755. (resultdef.typ=procvardef) and
  1756. (
  1757. (m_tp_procvar in current_settings.modeswitches) or
  1758. (m_mac_procvar in current_settings.modeswitches)
  1759. ) then
  1760. begin
  1761. if assigned(tcallnode(left).right) then
  1762. begin
  1763. { this is already a procvar, if it is really equal
  1764. is checked below }
  1765. convtype:=tc_equal;
  1766. hp:=tcallnode(left).right.getcopy;
  1767. currprocdef:=tabstractprocdef(hp.resultdef);
  1768. end
  1769. else
  1770. begin
  1771. convtype:=tc_proc_2_procvar;
  1772. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1773. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1774. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1775. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1776. begin
  1777. if assigned(tcallnode(left).methodpointer) then
  1778. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  1779. else
  1780. tloadnode(hp).set_mp(load_self_node);
  1781. end;
  1782. typecheckpass(hp);
  1783. end;
  1784. left.free;
  1785. left:=hp;
  1786. { Now check if the procedure we are going to assign to
  1787. the procvar, is compatible with the procvar's type }
  1788. if not(nf_explicit in flags) and
  1789. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
  1790. IncompatibleTypes(left.resultdef,resultdef);
  1791. exit;
  1792. end
  1793. else if maybe_global_proc_to_nested(left,resultdef) then
  1794. begin
  1795. result:=left;
  1796. left:=nil;
  1797. exit;
  1798. end;
  1799. { Handle explicit type conversions }
  1800. if nf_explicit in flags then
  1801. begin
  1802. { do common tc_equal cast }
  1803. convtype:=tc_equal;
  1804. { ordinal constants can be resized to 1,2,4,8 bytes }
  1805. if (left.nodetype=ordconstn) then
  1806. begin
  1807. { Insert typeconv for ordinal to the correct size first on left, after
  1808. that the other conversion can be done }
  1809. hdef:=nil;
  1810. case longint(resultdef.size) of
  1811. 1 :
  1812. hdef:=s8inttype;
  1813. 2 :
  1814. hdef:=s16inttype;
  1815. 4 :
  1816. hdef:=s32inttype;
  1817. 8 :
  1818. hdef:=s64inttype;
  1819. end;
  1820. { we need explicit, because it can also be an enum }
  1821. if assigned(hdef) then
  1822. inserttypeconv_internal(left,hdef)
  1823. else
  1824. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1825. end;
  1826. { check if the result could be in a register }
  1827. if (not(tstoreddef(resultdef).is_intregable) and
  1828. not(tstoreddef(resultdef).is_fpuregable)) or
  1829. ((left.resultdef.typ = floatdef) and
  1830. (resultdef.typ <> floatdef)) then
  1831. make_not_regable(left,[ra_addr_regable]);
  1832. { class/interface to class/interface, with checkobject support }
  1833. if is_class_or_interface_or_objc(resultdef) and
  1834. is_class_or_interface_or_objc(left.resultdef) then
  1835. begin
  1836. { check if the types are related }
  1837. if not(nf_internal in flags) and
  1838. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1839. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1840. begin
  1841. { Give an error when typecasting class to interface, this is compatible
  1842. with delphi }
  1843. if is_interface(resultdef) and
  1844. not is_interface(left.resultdef) then
  1845. CGMessage2(type_e_classes_not_related,
  1846. FullTypeName(left.resultdef,resultdef),
  1847. FullTypeName(resultdef,left.resultdef))
  1848. else
  1849. CGMessage2(type_w_classes_not_related,
  1850. FullTypeName(left.resultdef,resultdef),
  1851. FullTypeName(resultdef,left.resultdef))
  1852. end;
  1853. { Add runtime check? }
  1854. if not is_objc_class_or_protocol(resultdef) and
  1855. not is_objc_class_or_protocol(left.resultdef) and
  1856. (cs_check_object in current_settings.localswitches) and
  1857. not(nf_internal in flags) then
  1858. begin
  1859. { we can translate the typeconvnode to 'as' when
  1860. typecasting to a class or interface }
  1861. { we need to make sure the result can still be
  1862. passed as a var parameter }
  1863. newblock:=internalstatements(newstatement);
  1864. if (valid_for_var(left,false)) then
  1865. begin
  1866. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1867. addstatement(newstatement,tempnode);
  1868. addstatement(newstatement,cassignmentnode.create(
  1869. ctemprefnode.create(tempnode),
  1870. caddrnode.create_internal(left)));
  1871. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1872. end
  1873. else
  1874. begin
  1875. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1876. addstatement(newstatement,tempnode);
  1877. addstatement(newstatement,cassignmentnode.create(
  1878. ctemprefnode.create(tempnode),
  1879. left));
  1880. left:=ctemprefnode.create(tempnode);
  1881. end;
  1882. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1883. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1884. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1885. left:=nil;
  1886. result:=newblock;
  1887. exit;
  1888. end;
  1889. end
  1890. else
  1891. begin
  1892. { only if the same size or formal def, and }
  1893. { don't allow type casting of constants to }
  1894. { structured types }
  1895. if not(
  1896. (left.resultdef.typ=formaldef) or
  1897. (
  1898. not(is_open_array(left.resultdef)) and
  1899. not(is_array_constructor(left.resultdef)) and
  1900. not(is_array_of_const(left.resultdef)) and
  1901. (left.resultdef.size=resultdef.size) and
  1902. { disallow casts of const nodes }
  1903. (not is_constnode(left) or
  1904. { however, there are some exceptions }
  1905. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1906. filedef,variantdef,objectdef]) or
  1907. is_class_or_interface_or_objc(resultdef) or
  1908. { the softfloat code generates casts <const. float> to record }
  1909. (nf_internal in flags)
  1910. ))
  1911. ) or
  1912. (
  1913. is_void(left.resultdef) and
  1914. (left.nodetype=derefn)
  1915. )
  1916. ) then
  1917. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1918. end;
  1919. end
  1920. else
  1921. IncompatibleTypes(left.resultdef,resultdef);
  1922. end;
  1923. else
  1924. internalerror(200211231);
  1925. end;
  1926. end;
  1927. { Give hint or warning for unportable code, exceptions are
  1928. - typecasts from constants
  1929. - void }
  1930. if not(nf_internal in flags) and
  1931. (left.nodetype<>ordconstn) and
  1932. not(is_void(left.resultdef)) and
  1933. (((left.resultdef.typ=orddef) and
  1934. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1935. ((resultdef.typ=orddef) and
  1936. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1937. begin
  1938. {Converting pointers to signed integers is a bad idea. Warn.}
  1939. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1940. { Give a warning when sizes don't match, because then info will be lost }
  1941. if left.resultdef.size=resultdef.size then
  1942. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1943. else
  1944. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1945. end;
  1946. { tc_cord_2_pointer still requires a type check, which
  1947. simplify does not do }
  1948. if (convtype<>tc_cord_2_pointer) then
  1949. begin
  1950. result := simplify(false);
  1951. if assigned(result) then
  1952. exit;
  1953. end;
  1954. { now call the resultdef helper to do constant folding }
  1955. result:=typecheck_call_helper(convtype);
  1956. end;
  1957. {$ifndef cpu64bitalu}
  1958. { checks whether we can safely remove 64 bit typeconversions }
  1959. { in case range and overflow checking are off, and in case }
  1960. { the result of this node tree is downcasted again to a }
  1961. { 8/16/32 bit value afterwards }
  1962. function checkremove64bittypeconvs(n: tnode; out gotsint: boolean): boolean;
  1963. var
  1964. gotmuldivmod: boolean;
  1965. { checks whether a node is either an u32bit, or originally }
  1966. { was one but was implicitly converted to s64bit }
  1967. function wasoriginallyint32(n: tnode): boolean;
  1968. begin
  1969. if (n.resultdef.typ<>orddef) then
  1970. exit(false);
  1971. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  1972. begin
  1973. if (torddef(n.resultdef).ordtype=s32bit) then
  1974. gotsint:=true;
  1975. exit(true);
  1976. end;
  1977. if (torddef(n.resultdef).ordtype=s64bit) and
  1978. { nf_explicit is also set for explicitly typecasted }
  1979. { ordconstn's }
  1980. ([nf_internal,nf_explicit]*n.flags=[]) and
  1981. { either a typeconversion node coming from u32bit }
  1982. (((n.nodetype=typeconvn) and
  1983. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  1984. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  1985. { or an ordconstnode which was/is a valid cardinal }
  1986. ((n.nodetype=ordconstn) and
  1987. (tordconstnode(n).value>=int64(low(longint))) and
  1988. (tordconstnode(n).value<=high(cardinal)))) then
  1989. begin
  1990. if ((n.nodetype=typeconvn) and
  1991. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  1992. ((n.nodetype=ordconstn) and
  1993. (tordconstnode(n).value<0)) then
  1994. gotsint:=true;
  1995. exit(true);
  1996. end;
  1997. result:=false;
  1998. end;
  1999. function docheckremove64bittypeconvs(n: tnode): boolean;
  2000. begin
  2001. result:=false;
  2002. if wasoriginallyint32(n) then
  2003. exit(true);
  2004. case n.nodetype of
  2005. subn,orn,xorn:
  2006. begin
  2007. { nf_internal is set by taddnode.typecheckpass in }
  2008. { case the arguments of this subn were u32bit, but }
  2009. { upcasted to s64bit for calculation correctness }
  2010. { (normally only needed when range checking, but }
  2011. { also done otherwise so there is no difference }
  2012. { in overload choosing etc between $r+ and $r-) }
  2013. if (nf_internal in n.flags) then
  2014. result:=true
  2015. else
  2016. result:=
  2017. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2018. docheckremove64bittypeconvs(tbinarynode(n).right);
  2019. end;
  2020. addn,muln,divn,modn,andn:
  2021. begin
  2022. if n.nodetype in [muln,divn,modn] then
  2023. gotmuldivmod:=true;
  2024. result:=
  2025. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2026. docheckremove64bittypeconvs(tbinarynode(n).right);
  2027. end;
  2028. end;
  2029. end;
  2030. begin { checkremove64bittypeconvs }
  2031. gotmuldivmod:=false;
  2032. gotsint:=false;
  2033. result:=
  2034. docheckremove64bittypeconvs(n) and
  2035. not(gotmuldivmod and gotsint);
  2036. end;
  2037. procedure doremove64bittypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean);
  2038. begin
  2039. case n.nodetype of
  2040. subn,addn,muln,divn,modn,xorn,andn,orn:
  2041. begin
  2042. exclude(n.flags,nf_internal);
  2043. if not forceunsigned and
  2044. is_signed(n.resultdef) then
  2045. begin
  2046. doremove64bittypeconvs(tbinarynode(n).left,s32inttype,false);
  2047. doremove64bittypeconvs(tbinarynode(n).right,s32inttype,false);
  2048. n.resultdef:=s32inttype
  2049. end
  2050. else
  2051. begin
  2052. doremove64bittypeconvs(tbinarynode(n).left,u32inttype,forceunsigned);
  2053. doremove64bittypeconvs(tbinarynode(n).right,u32inttype,forceunsigned);
  2054. n.resultdef:=u32inttype
  2055. end;
  2056. end;
  2057. ordconstn:
  2058. inserttypeconv_internal(n,todef);
  2059. typeconvn:
  2060. begin
  2061. n.resultdef:=todef;
  2062. ttypeconvnode(n).totypedef:=todef;
  2063. end;
  2064. end;
  2065. end;
  2066. {$endif not cpu64bitalu}
  2067. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2068. var
  2069. hp: tnode;
  2070. {$ifndef cpu64bitalu}
  2071. foundsint: boolean;
  2072. {$endif not cpu64bitalu}
  2073. begin
  2074. result := nil;
  2075. { Constant folding and other node transitions to
  2076. remove the typeconv node }
  2077. case left.nodetype of
  2078. stringconstn :
  2079. if (convtype=tc_string_2_string) and
  2080. (
  2081. ((not is_widechararray(left.resultdef) and
  2082. not is_wide_or_unicode_string(left.resultdef)) or
  2083. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) or
  2084. (current_settings.sourcecodepage<>CP_UTF8)
  2085. )
  2086. { non-ascii chars would be replaced with '?' -> loses info }
  2087. {not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)))}
  2088. ) then
  2089. begin
  2090. tstringconstnode(left).changestringtype(resultdef);
  2091. result:=left;
  2092. left:=nil;
  2093. exit;
  2094. end;
  2095. realconstn :
  2096. begin
  2097. if (convtype = tc_real_2_currency) then
  2098. result := typecheck_real_to_currency
  2099. else if (convtype = tc_real_2_real) then
  2100. result := typecheck_real_to_real
  2101. else
  2102. exit;
  2103. if not(assigned(result)) then
  2104. begin
  2105. result := left;
  2106. left := nil;
  2107. end;
  2108. if (result.nodetype = realconstn) then
  2109. begin
  2110. hp:=result;
  2111. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2112. if ([nf_explicit,nf_internal] * flags <> []) then
  2113. include(result.flags, nf_explicit);
  2114. hp.free;
  2115. end;
  2116. end;
  2117. niln :
  2118. begin
  2119. { nil to ordinal node }
  2120. if (resultdef.typ=orddef) then
  2121. begin
  2122. hp:=cordconstnode.create(0,resultdef,true);
  2123. if ([nf_explicit,nf_internal] * flags <> []) then
  2124. include(hp.flags, nf_explicit);
  2125. result:=hp;
  2126. exit;
  2127. end
  2128. else
  2129. { fold nil to any pointer type }
  2130. if (resultdef.typ=pointerdef) then
  2131. begin
  2132. hp:=cnilnode.create;
  2133. hp.resultdef:=resultdef;
  2134. if ([nf_explicit,nf_internal] * flags <> []) then
  2135. include(hp.flags, nf_explicit);
  2136. result:=hp;
  2137. exit;
  2138. end
  2139. else
  2140. { remove typeconv after niln, but not when the result is a
  2141. methodpointer. The typeconv of the methodpointer will then
  2142. take care of updateing size of niln to OS_64 }
  2143. if not((resultdef.typ=procvardef) and
  2144. not(tprocvardef(resultdef).is_addressonly)) then
  2145. begin
  2146. left.resultdef:=resultdef;
  2147. if ([nf_explicit,nf_internal] * flags <> []) then
  2148. include(left.flags, nf_explicit);
  2149. result:=left;
  2150. left:=nil;
  2151. exit;
  2152. end;
  2153. end;
  2154. ordconstn :
  2155. begin
  2156. { ordinal contants can be directly converted }
  2157. { but not char to char because it is a widechar to char or via versa }
  2158. { which needs extra code to do the code page transistion }
  2159. { constant ordinal to pointer }
  2160. if (resultdef.typ=pointerdef) and
  2161. (convtype<>tc_cchar_2_pchar) then
  2162. begin
  2163. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2164. if ([nf_explicit,nf_internal] * flags <> []) then
  2165. include(hp.flags, nf_explicit);
  2166. result:=hp;
  2167. exit;
  2168. end
  2169. else if is_ordinal(resultdef) and
  2170. not(convtype=tc_char_2_char) then
  2171. begin
  2172. { replace the resultdef and recheck the range }
  2173. if ([nf_explicit,nf_internal] * flags <> []) then
  2174. include(left.flags, nf_explicit)
  2175. else
  2176. { no longer an ordconst with an explicit typecast }
  2177. exclude(left.flags, nf_explicit);
  2178. { when converting from one boolean type to another, force }
  2179. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2180. { (Delphi-compatibile) }
  2181. if is_boolean(left.resultdef) and
  2182. is_boolean(resultdef) and
  2183. (is_cbool(left.resultdef) or
  2184. is_cbool(resultdef)) then
  2185. begin
  2186. if is_pasbool(resultdef) then
  2187. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2188. else
  2189. {$ifdef VER2_2}
  2190. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  2191. tordconstnode(left).value:=-tordconstnode(left).value;
  2192. {$else}
  2193. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2194. {$endif VER2_2}
  2195. end
  2196. else
  2197. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags),false);
  2198. left.resultdef:=resultdef;
  2199. tordconstnode(left).typedef:=resultdef;
  2200. result:=left;
  2201. left:=nil;
  2202. exit;
  2203. end;
  2204. end;
  2205. pointerconstn :
  2206. begin
  2207. { pointerconstn to any pointer is folded too }
  2208. if (resultdef.typ=pointerdef) then
  2209. begin
  2210. left.resultdef:=resultdef;
  2211. if ([nf_explicit,nf_internal] * flags <> []) then
  2212. include(left.flags, nf_explicit)
  2213. else
  2214. { no longer an ordconst with an explicit typecast }
  2215. exclude(left.flags, nf_explicit);
  2216. result:=left;
  2217. left:=nil;
  2218. exit;
  2219. end
  2220. { constant pointer to ordinal }
  2221. else if is_ordinal(resultdef) then
  2222. begin
  2223. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2224. resultdef,not(nf_explicit in flags));
  2225. if ([nf_explicit,nf_internal] * flags <> []) then
  2226. include(hp.flags, nf_explicit);
  2227. result:=hp;
  2228. exit;
  2229. end;
  2230. end;
  2231. end;
  2232. {$ifndef cpu64bitalu}
  2233. { must be done before code below, because we need the
  2234. typeconversions for ordconstn's as well }
  2235. case convtype of
  2236. tc_int_2_int:
  2237. begin
  2238. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2239. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2240. (resultdef.size <= 4) and
  2241. is_64bitint(left.resultdef) and
  2242. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2243. checkremove64bittypeconvs(left,foundsint) then
  2244. begin
  2245. { avoid unnecessary widening of intermediary calculations }
  2246. { to 64 bit }
  2247. doremove64bittypeconvs(left,generrordef,not foundsint);
  2248. end;
  2249. end;
  2250. end;
  2251. {$endif not cpu64bitalu}
  2252. end;
  2253. procedure Ttypeconvnode.mark_write;
  2254. begin
  2255. left.mark_write;
  2256. end;
  2257. function ttypeconvnode.first_cord_to_pointer : tnode;
  2258. begin
  2259. result:=nil;
  2260. internalerror(200104043);
  2261. end;
  2262. function ttypeconvnode.first_int_to_int : tnode;
  2263. begin
  2264. first_int_to_int:=nil;
  2265. expectloc:=left.expectloc;
  2266. if not is_void(left.resultdef) then
  2267. begin
  2268. if (left.expectloc<>LOC_REGISTER) and
  2269. ((resultdef.size>left.resultdef.size) or
  2270. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2271. expectloc:=LOC_REGISTER
  2272. else
  2273. if (left.expectloc=LOC_CREGISTER) and
  2274. (resultdef.size<left.resultdef.size) then
  2275. expectloc:=LOC_REGISTER;
  2276. end;
  2277. end;
  2278. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2279. begin
  2280. result:=nil;
  2281. expectloc:=LOC_REGISTER;
  2282. end;
  2283. function ttypeconvnode.first_cstring_to_int : tnode;
  2284. begin
  2285. result:=nil;
  2286. internalerror(200510014);
  2287. end;
  2288. function ttypeconvnode.first_string_to_chararray : tnode;
  2289. begin
  2290. first_string_to_chararray:=nil;
  2291. expectloc:=left.expectloc;
  2292. end;
  2293. function ttypeconvnode.first_char_to_string : tnode;
  2294. begin
  2295. first_char_to_string:=nil;
  2296. expectloc:=LOC_REFERENCE;
  2297. end;
  2298. function ttypeconvnode.first_nothing : tnode;
  2299. begin
  2300. first_nothing:=nil;
  2301. end;
  2302. function ttypeconvnode.first_array_to_pointer : tnode;
  2303. begin
  2304. first_array_to_pointer:=nil;
  2305. expectloc:=LOC_REGISTER;
  2306. end;
  2307. function ttypeconvnode.first_int_to_real: tnode;
  2308. var
  2309. fname: string[32];
  2310. begin
  2311. if target_info.system in systems_wince then
  2312. begin
  2313. { converting a 64bit integer to a float requires a helper }
  2314. if is_64bitint(left.resultdef) or
  2315. is_currency(left.resultdef) then
  2316. begin
  2317. { hack to avoid double division by 10000, as it's
  2318. already done by typecheckpass.resultdef_int_to_real }
  2319. if is_currency(left.resultdef) then
  2320. left.resultdef := s64inttype;
  2321. if is_signed(left.resultdef) then
  2322. fname:='I64TO'
  2323. else
  2324. fname:='UI64TO';
  2325. end
  2326. else
  2327. { other integers are supposed to be 32 bit }
  2328. begin
  2329. if is_signed(left.resultdef) then
  2330. fname:='ITO'
  2331. else
  2332. fname:='UTO';
  2333. firstpass(left);
  2334. end;
  2335. if tfloatdef(resultdef).floattype=s64real then
  2336. fname:=fname+'D'
  2337. else
  2338. fname:=fname+'S';
  2339. result:=ccallnode.createintern(fname,ccallparanode.create(
  2340. left,nil));
  2341. left:=nil;
  2342. firstpass(result);
  2343. exit;
  2344. end
  2345. else
  2346. begin
  2347. { converting a 64bit integer to a float requires a helper }
  2348. if is_64bitint(left.resultdef) or
  2349. is_currency(left.resultdef) then
  2350. begin
  2351. { hack to avoid double division by 10000, as it's
  2352. already done by typecheckpass.resultdef_int_to_real }
  2353. if is_currency(left.resultdef) then
  2354. left.resultdef := s64inttype;
  2355. if is_signed(left.resultdef) then
  2356. fname:='int64_to_'
  2357. else
  2358. { we can't do better currently }
  2359. fname:='qword_to_';
  2360. end
  2361. else
  2362. { other integers are supposed to be 32 bit }
  2363. begin
  2364. if is_signed(left.resultdef) then
  2365. fname:='int32_to_'
  2366. else
  2367. fname:='int64_to_';
  2368. firstpass(left);
  2369. end;
  2370. if tfloatdef(resultdef).floattype=s64real then
  2371. fname:=fname+'float64'
  2372. else
  2373. fname:=fname+'float32';
  2374. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2375. left,nil)),resultdef);
  2376. left:=nil;
  2377. firstpass(result);
  2378. exit;
  2379. end;
  2380. end;
  2381. function ttypeconvnode.first_real_to_real : tnode;
  2382. begin
  2383. {$ifdef cpufpemu}
  2384. if cs_fp_emulation in current_settings.moduleswitches then
  2385. begin
  2386. if target_info.system in systems_wince then
  2387. begin
  2388. case tfloatdef(left.resultdef).floattype of
  2389. s32real:
  2390. case tfloatdef(resultdef).floattype of
  2391. s64real:
  2392. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2393. s32real:
  2394. begin
  2395. result:=left;
  2396. left:=nil;
  2397. end;
  2398. else
  2399. internalerror(2005082704);
  2400. end;
  2401. s64real:
  2402. case tfloatdef(resultdef).floattype of
  2403. s32real:
  2404. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2405. s64real:
  2406. begin
  2407. result:=left;
  2408. left:=nil;
  2409. end;
  2410. else
  2411. internalerror(2005082703);
  2412. end;
  2413. else
  2414. internalerror(2005082702);
  2415. end;
  2416. left:=nil;
  2417. firstpass(result);
  2418. exit;
  2419. end
  2420. else
  2421. begin
  2422. case tfloatdef(left.resultdef).floattype of
  2423. s32real:
  2424. case tfloatdef(resultdef).floattype of
  2425. s64real:
  2426. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2427. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2428. s32real:
  2429. begin
  2430. result:=left;
  2431. left:=nil;
  2432. end;
  2433. else
  2434. internalerror(200610151);
  2435. end;
  2436. s64real:
  2437. case tfloatdef(resultdef).floattype of
  2438. s32real:
  2439. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2440. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2441. s64real:
  2442. begin
  2443. result:=left;
  2444. left:=nil;
  2445. end;
  2446. else
  2447. internalerror(200610152);
  2448. end;
  2449. else
  2450. internalerror(200610153);
  2451. end;
  2452. left:=nil;
  2453. firstpass(result);
  2454. exit;
  2455. end;
  2456. end
  2457. else
  2458. {$endif cpufpemu}
  2459. begin
  2460. first_real_to_real:=nil;
  2461. if not use_vectorfpu(resultdef) then
  2462. expectloc:=LOC_FPUREGISTER
  2463. else
  2464. expectloc:=LOC_MMREGISTER;
  2465. end;
  2466. end;
  2467. function ttypeconvnode.first_pointer_to_array : tnode;
  2468. begin
  2469. first_pointer_to_array:=nil;
  2470. expectloc:=LOC_REFERENCE;
  2471. end;
  2472. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2473. begin
  2474. first_cchar_to_pchar:=nil;
  2475. internalerror(200104021);
  2476. end;
  2477. function ttypeconvnode.first_bool_to_int : tnode;
  2478. begin
  2479. first_bool_to_int:=nil;
  2480. { byte(boolean) or word(wordbool) or longint(longbool) must
  2481. be accepted for var parameters }
  2482. if (nf_explicit in flags) and
  2483. (left.resultdef.size=resultdef.size) and
  2484. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2485. exit;
  2486. { when converting to 64bit, first convert to a 32bit int and then }
  2487. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2488. if resultdef.size > sizeof(aint) then
  2489. begin
  2490. result := ctypeconvnode.create_internal(left,s32inttype);
  2491. result := ctypeconvnode.create(result,resultdef);
  2492. left := nil;
  2493. firstpass(result);
  2494. exit;
  2495. end;
  2496. expectloc:=LOC_REGISTER;
  2497. end;
  2498. function ttypeconvnode.first_int_to_bool : tnode;
  2499. begin
  2500. first_int_to_bool:=nil;
  2501. { byte(boolean) or word(wordbool) or longint(longbool) must
  2502. be accepted for var parameters }
  2503. if (nf_explicit in flags) and
  2504. (left.resultdef.size=resultdef.size) and
  2505. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2506. exit;
  2507. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2508. { convert to a boolean (only necessary for 32bit processors) }
  2509. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2510. and is_cbool(resultdef) then
  2511. begin
  2512. left:=ctypeconvnode.create_internal(left,s32inttype);
  2513. firstpass(left);
  2514. exit;
  2515. end;
  2516. expectloc:=LOC_REGISTER;
  2517. end;
  2518. function ttypeconvnode.first_bool_to_bool : tnode;
  2519. begin
  2520. first_bool_to_bool:=nil;
  2521. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2522. expectloc := left.expectloc
  2523. else
  2524. expectloc:=LOC_REGISTER;
  2525. end;
  2526. function ttypeconvnode.first_char_to_char : tnode;
  2527. var
  2528. fname: string[18];
  2529. begin
  2530. if (torddef(resultdef).ordtype=uchar) and
  2531. (torddef(left.resultdef).ordtype=uwidechar) then
  2532. fname := 'fpc_uchar_to_char'
  2533. else if (torddef(resultdef).ordtype=uwidechar) and
  2534. (torddef(left.resultdef).ordtype=uchar) then
  2535. fname := 'fpc_char_to_uchar'
  2536. else
  2537. internalerror(2007081201);
  2538. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2539. left:=nil;
  2540. firstpass(result);
  2541. end;
  2542. function ttypeconvnode.first_proc_to_procvar : tnode;
  2543. begin
  2544. first_proc_to_procvar:=nil;
  2545. { if we take the address of a nested function, the current function/
  2546. procedure needs a stack frame since it's required to construct
  2547. the nested procvar }
  2548. if is_nested_pd(tprocvardef(resultdef)) then
  2549. include(current_procinfo.flags,pi_needs_stackframe);
  2550. if tabstractprocdef(resultdef).is_addressonly then
  2551. expectloc:=LOC_REGISTER
  2552. else
  2553. begin
  2554. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2555. CGMessage(parser_e_illegal_expression);
  2556. expectloc:=left.expectloc;
  2557. end;
  2558. end;
  2559. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2560. begin
  2561. first_nil_to_methodprocvar:=nil;
  2562. expectloc:=LOC_REFERENCE;
  2563. end;
  2564. function ttypeconvnode.first_set_to_set : tnode;
  2565. var
  2566. newstatement : tstatementnode;
  2567. temp : ttempcreatenode;
  2568. begin
  2569. { in theory, we should do range checking here,
  2570. but Delphi doesn't do it either (FK) }
  2571. if left.nodetype=setconstn then
  2572. begin
  2573. left.resultdef:=resultdef;
  2574. result:=left;
  2575. left:=nil;
  2576. end
  2577. { equal sets for the code generator? }
  2578. else if (left.resultdef.size=resultdef.size) and
  2579. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2580. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2581. { e.g., this prints true because of this:
  2582. var
  2583. sa: set of 1..2;
  2584. sb: set of 5..6;
  2585. b: byte;
  2586. begin
  2587. b:=1;
  2588. sa:=[1..2];
  2589. sb:=sa;
  2590. writeln(b in sb);
  2591. end.
  2592. }
  2593. begin
  2594. result:=left;
  2595. left:=nil;
  2596. end
  2597. else
  2598. begin
  2599. result:=internalstatements(newstatement);
  2600. { in case left is a smallset expression, it can be an addn or so. }
  2601. { fpc_varset_load expects a formal const parameter, which doesn't }
  2602. { accept set addn's -> assign to a temp first and pass the temp }
  2603. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2604. begin
  2605. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2606. addstatement(newstatement,temp);
  2607. { temp := left }
  2608. addstatement(newstatement,cassignmentnode.create(
  2609. ctemprefnode.create(temp),left));
  2610. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2611. addstatement(newstatement,ctemprefnode.create(temp));
  2612. left:=result;
  2613. firstpass(left);
  2614. { recreate the result's internalstatements list }
  2615. result:=internalstatements(newstatement);
  2616. end;
  2617. { create temp for result }
  2618. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2619. addstatement(newstatement,temp);
  2620. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2621. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2622. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2623. ccallparanode.create(ctemprefnode.create(temp),
  2624. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2625. ccallparanode.create(left,nil))))))
  2626. );
  2627. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2628. addstatement(newstatement,ctemprefnode.create(temp));
  2629. left:=nil;
  2630. end;
  2631. end;
  2632. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2633. begin
  2634. first_ansistring_to_pchar:=nil;
  2635. expectloc:=LOC_REGISTER;
  2636. end;
  2637. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2638. begin
  2639. first_arrayconstructor_to_set:=nil;
  2640. internalerror(200104022);
  2641. end;
  2642. function ttypeconvnode.first_class_to_intf : tnode;
  2643. var
  2644. hd : tobjectdef;
  2645. ImplIntf : TImplementedInterface;
  2646. begin
  2647. result:=nil;
  2648. expectloc:=LOC_REGISTER;
  2649. hd:=tobjectdef(left.resultdef);
  2650. while assigned(hd) do
  2651. begin
  2652. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2653. if assigned(ImplIntf) then
  2654. begin
  2655. case ImplIntf.IType of
  2656. etStandard:
  2657. { handle in pass 2 }
  2658. ;
  2659. etFieldValue, etFieldValueClass:
  2660. if is_interface(tobjectdef(resultdef)) then
  2661. begin
  2662. result:=left;
  2663. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2664. { this ensures proper refcounting when field is of class type }
  2665. if not is_interface(result.resultdef) then
  2666. inserttypeconv(result, resultdef);
  2667. left:=nil;
  2668. end
  2669. else
  2670. begin
  2671. internalerror(200802213);
  2672. end;
  2673. etStaticMethodResult, etStaticMethodClass,
  2674. etVirtualMethodResult, etVirtualMethodClass:
  2675. if is_interface(tobjectdef(resultdef)) then
  2676. begin
  2677. { TODO: generating a call to TObject.GetInterface instead could yield
  2678. smaller code size. OTOH, refcounting gotchas are possible that way. }
  2679. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2680. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2681. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2682. left,[]);
  2683. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2684. { if it is a class, process it further in a similar way }
  2685. if not is_interface(result.resultdef) then
  2686. inserttypeconv(result, resultdef);
  2687. left:=nil;
  2688. end
  2689. else if is_class(tobjectdef(resultdef)) then
  2690. begin
  2691. internalerror(200802211);
  2692. end
  2693. else
  2694. internalerror(200802231);
  2695. else
  2696. internalerror(200802165);
  2697. end;
  2698. break;
  2699. end;
  2700. hd:=hd.childof;
  2701. end;
  2702. if hd=nil then
  2703. internalerror(200802164);
  2704. end;
  2705. function ttypeconvnode.first_string_to_string : tnode;
  2706. var
  2707. procname: string[31];
  2708. newblock : tblocknode;
  2709. newstat : tstatementnode;
  2710. restemp : ttempcreatenode;
  2711. begin
  2712. { get the correct procedure name }
  2713. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  2714. '_to_'+tstringdef(resultdef).stringtypname;
  2715. if tstringdef(resultdef).stringtype=st_shortstring then
  2716. begin
  2717. newblock:=internalstatements(newstat);
  2718. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  2719. addstatement(newstat,restemp);
  2720. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  2721. ctemprefnode.create(restemp),nil))));
  2722. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  2723. addstatement(newstat,ctemprefnode.create(restemp));
  2724. result:=newblock;
  2725. end
  2726. { encoding parameter required? }
  2727. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  2728. ((tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring]) or
  2729. { ansistring to ansistring and no RawByteString envolved? }
  2730. (//(tstringdef(resultdef).encoding<>65535) and
  2731. (tstringdef(left.resultdef).stringtype=st_ansistring)
  2732. //(tstringdef(left.resultdef).encoding<>65535)
  2733. )
  2734. ) then
  2735. result:=ccallnode.createinternres(procname,
  2736. ccallparanode.create(cordconstnode.create(tstringdef(resultdef).encoding,u16inttype,true),
  2737. ccallparanode.create(left,nil)),resultdef)
  2738. else
  2739. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  2740. left:=nil;
  2741. end;
  2742. function ttypeconvnode._first_int_to_int : tnode;
  2743. begin
  2744. result:=first_int_to_int;
  2745. end;
  2746. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2747. begin
  2748. result:=first_cstring_to_pchar;
  2749. end;
  2750. function ttypeconvnode._first_cstring_to_int : tnode;
  2751. begin
  2752. result:=first_cstring_to_int;
  2753. end;
  2754. function ttypeconvnode._first_string_to_chararray : tnode;
  2755. begin
  2756. result:=first_string_to_chararray;
  2757. end;
  2758. function ttypeconvnode._first_char_to_string : tnode;
  2759. begin
  2760. result:=first_char_to_string;
  2761. end;
  2762. function ttypeconvnode._first_nothing : tnode;
  2763. begin
  2764. result:=first_nothing;
  2765. end;
  2766. function ttypeconvnode._first_array_to_pointer : tnode;
  2767. begin
  2768. result:=first_array_to_pointer;
  2769. end;
  2770. function ttypeconvnode._first_int_to_real : tnode;
  2771. begin
  2772. result:=first_int_to_real;
  2773. end;
  2774. function ttypeconvnode._first_real_to_real : tnode;
  2775. begin
  2776. result:=first_real_to_real;
  2777. end;
  2778. function ttypeconvnode._first_pointer_to_array : tnode;
  2779. begin
  2780. result:=first_pointer_to_array;
  2781. end;
  2782. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2783. begin
  2784. result:=first_cchar_to_pchar;
  2785. end;
  2786. function ttypeconvnode._first_bool_to_int : tnode;
  2787. begin
  2788. result:=first_bool_to_int;
  2789. end;
  2790. function ttypeconvnode._first_int_to_bool : tnode;
  2791. begin
  2792. result:=first_int_to_bool;
  2793. end;
  2794. function ttypeconvnode._first_bool_to_bool : tnode;
  2795. begin
  2796. result:=first_bool_to_bool;
  2797. end;
  2798. function ttypeconvnode._first_proc_to_procvar : tnode;
  2799. begin
  2800. result:=first_proc_to_procvar;
  2801. end;
  2802. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2803. begin
  2804. result:=first_nil_to_methodprocvar;
  2805. end;
  2806. function ttypeconvnode._first_set_to_set : tnode;
  2807. begin
  2808. result:=first_set_to_set;
  2809. end;
  2810. function ttypeconvnode._first_cord_to_pointer : tnode;
  2811. begin
  2812. result:=first_cord_to_pointer;
  2813. end;
  2814. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2815. begin
  2816. result:=first_ansistring_to_pchar;
  2817. end;
  2818. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2819. begin
  2820. result:=first_arrayconstructor_to_set;
  2821. end;
  2822. function ttypeconvnode._first_class_to_intf : tnode;
  2823. begin
  2824. result:=first_class_to_intf;
  2825. end;
  2826. function ttypeconvnode._first_char_to_char : tnode;
  2827. begin
  2828. result:=first_char_to_char;
  2829. end;
  2830. function ttypeconvnode._first_string_to_string : tnode;
  2831. begin
  2832. result:=first_string_to_string;
  2833. end;
  2834. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2835. const
  2836. firstconvert : array[tconverttype] of pointer = (
  2837. nil, { none }
  2838. @ttypeconvnode._first_nothing, {equal}
  2839. @ttypeconvnode._first_nothing, {not_possible}
  2840. @ttypeconvnode._first_string_to_string,
  2841. @ttypeconvnode._first_char_to_string,
  2842. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2843. nil, { removed in typecheck_chararray_to_string }
  2844. @ttypeconvnode._first_cchar_to_pchar,
  2845. @ttypeconvnode._first_cstring_to_pchar,
  2846. @ttypeconvnode._first_cstring_to_int,
  2847. @ttypeconvnode._first_ansistring_to_pchar,
  2848. @ttypeconvnode._first_string_to_chararray,
  2849. nil, { removed in typecheck_chararray_to_string }
  2850. @ttypeconvnode._first_array_to_pointer,
  2851. @ttypeconvnode._first_pointer_to_array,
  2852. @ttypeconvnode._first_int_to_int,
  2853. @ttypeconvnode._first_int_to_bool,
  2854. @ttypeconvnode._first_bool_to_bool,
  2855. @ttypeconvnode._first_bool_to_int,
  2856. @ttypeconvnode._first_real_to_real,
  2857. @ttypeconvnode._first_int_to_real,
  2858. nil, { removed in typecheck_real_to_currency }
  2859. @ttypeconvnode._first_proc_to_procvar,
  2860. @ttypeconvnode._first_nil_to_methodprocvar,
  2861. @ttypeconvnode._first_arrayconstructor_to_set,
  2862. @ttypeconvnode._first_set_to_set,
  2863. @ttypeconvnode._first_cord_to_pointer,
  2864. @ttypeconvnode._first_nothing,
  2865. @ttypeconvnode._first_nothing,
  2866. @ttypeconvnode._first_class_to_intf,
  2867. @ttypeconvnode._first_char_to_char,
  2868. @ttypeconvnode._first_nothing,
  2869. @ttypeconvnode._first_nothing,
  2870. nil,
  2871. nil,
  2872. nil,
  2873. nil,
  2874. nil,
  2875. nil,
  2876. nil
  2877. );
  2878. type
  2879. tprocedureofobject = function : tnode of object;
  2880. var
  2881. r : packed record
  2882. proc : pointer;
  2883. obj : pointer;
  2884. end;
  2885. begin
  2886. { this is a little bit dirty but it works }
  2887. { and should be quite portable too }
  2888. r.proc:=firstconvert[c];
  2889. r.obj:=self;
  2890. if not assigned(r.proc) then
  2891. internalerror(200312081);
  2892. first_call_helper:=tprocedureofobject(r)()
  2893. end;
  2894. function ttypeconvnode.pass_1 : tnode;
  2895. begin
  2896. if warn_pointer_to_signed then
  2897. cgmessage(type_w_pointer_to_signed);
  2898. result:=nil;
  2899. firstpass(left);
  2900. if codegenerror then
  2901. exit;
  2902. expectloc:=left.expectloc;
  2903. result:=first_call_helper(convtype);
  2904. end;
  2905. function ttypeconvnode.retains_value_location:boolean;
  2906. begin
  2907. result:=(convtype=tc_equal) or
  2908. { typecasting from void is always allowed }
  2909. is_void(left.resultdef) or
  2910. (left.resultdef.typ=formaldef) or
  2911. { int 2 int with same size reuses same location, or for
  2912. tp7 mode also allow size < orignal size }
  2913. (
  2914. (convtype=tc_int_2_int) and
  2915. (
  2916. not is_bitpacked_access(left) and
  2917. (resultdef.size=left.resultdef.size) or
  2918. ((m_tp7 in current_settings.modeswitches) and
  2919. (resultdef.size<left.resultdef.size))
  2920. )
  2921. ) or
  2922. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2923. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2924. (nf_explicit in flags) and
  2925. (resultdef.size=left.resultdef.size));
  2926. end;
  2927. function ttypeconvnode.assign_allowed:boolean;
  2928. begin
  2929. result:=retains_value_location;
  2930. { When using only a part of the value it can't be in a register since
  2931. that will load the value in a new register first }
  2932. { the same goes for changing the sign of equal-sized values which
  2933. are smaller than an entire register }
  2934. if result and
  2935. (resultdef.size<left.resultdef.size) or
  2936. ((resultdef.size=left.resultdef.size) and
  2937. (left.resultdef.size<sizeof(aint)) and
  2938. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  2939. make_not_regable(left,[ra_addr_regable]);
  2940. end;
  2941. function ttypeconvnode.docompare(p: tnode) : boolean;
  2942. begin
  2943. docompare :=
  2944. inherited docompare(p) and
  2945. (convtype = ttypeconvnode(p).convtype) and
  2946. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  2947. end;
  2948. procedure ttypeconvnode._second_int_to_int;
  2949. begin
  2950. second_int_to_int;
  2951. end;
  2952. procedure ttypeconvnode._second_string_to_string;
  2953. begin
  2954. second_string_to_string;
  2955. end;
  2956. procedure ttypeconvnode._second_cstring_to_pchar;
  2957. begin
  2958. second_cstring_to_pchar;
  2959. end;
  2960. procedure ttypeconvnode._second_cstring_to_int;
  2961. begin
  2962. second_cstring_to_int;
  2963. end;
  2964. procedure ttypeconvnode._second_string_to_chararray;
  2965. begin
  2966. second_string_to_chararray;
  2967. end;
  2968. procedure ttypeconvnode._second_array_to_pointer;
  2969. begin
  2970. second_array_to_pointer;
  2971. end;
  2972. procedure ttypeconvnode._second_pointer_to_array;
  2973. begin
  2974. second_pointer_to_array;
  2975. end;
  2976. procedure ttypeconvnode._second_chararray_to_string;
  2977. begin
  2978. second_chararray_to_string;
  2979. end;
  2980. procedure ttypeconvnode._second_char_to_string;
  2981. begin
  2982. second_char_to_string;
  2983. end;
  2984. procedure ttypeconvnode._second_int_to_real;
  2985. begin
  2986. second_int_to_real;
  2987. end;
  2988. procedure ttypeconvnode._second_real_to_real;
  2989. begin
  2990. second_real_to_real;
  2991. end;
  2992. procedure ttypeconvnode._second_cord_to_pointer;
  2993. begin
  2994. second_cord_to_pointer;
  2995. end;
  2996. procedure ttypeconvnode._second_proc_to_procvar;
  2997. begin
  2998. second_proc_to_procvar;
  2999. end;
  3000. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3001. begin
  3002. second_nil_to_methodprocvar;
  3003. end;
  3004. procedure ttypeconvnode._second_bool_to_int;
  3005. begin
  3006. second_bool_to_int;
  3007. end;
  3008. procedure ttypeconvnode._second_int_to_bool;
  3009. begin
  3010. second_int_to_bool;
  3011. end;
  3012. procedure ttypeconvnode._second_bool_to_bool;
  3013. begin
  3014. second_bool_to_bool;
  3015. end;
  3016. procedure ttypeconvnode._second_set_to_set;
  3017. begin
  3018. second_set_to_set;
  3019. end;
  3020. procedure ttypeconvnode._second_ansistring_to_pchar;
  3021. begin
  3022. second_ansistring_to_pchar;
  3023. end;
  3024. procedure ttypeconvnode._second_class_to_intf;
  3025. begin
  3026. second_class_to_intf;
  3027. end;
  3028. procedure ttypeconvnode._second_char_to_char;
  3029. begin
  3030. second_char_to_char;
  3031. end;
  3032. procedure ttypeconvnode._second_nothing;
  3033. begin
  3034. second_nothing;
  3035. end;
  3036. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3037. const
  3038. secondconvert : array[tconverttype] of pointer = (
  3039. @ttypeconvnode._second_nothing, {none}
  3040. @ttypeconvnode._second_nothing, {equal}
  3041. @ttypeconvnode._second_nothing, {not_possible}
  3042. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3043. @ttypeconvnode._second_char_to_string,
  3044. @ttypeconvnode._second_nothing, {char_to_charray}
  3045. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3046. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3047. @ttypeconvnode._second_cstring_to_pchar,
  3048. @ttypeconvnode._second_cstring_to_int,
  3049. @ttypeconvnode._second_ansistring_to_pchar,
  3050. @ttypeconvnode._second_string_to_chararray,
  3051. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3052. @ttypeconvnode._second_array_to_pointer,
  3053. @ttypeconvnode._second_pointer_to_array,
  3054. @ttypeconvnode._second_int_to_int,
  3055. @ttypeconvnode._second_int_to_bool,
  3056. @ttypeconvnode._second_bool_to_bool,
  3057. @ttypeconvnode._second_bool_to_int,
  3058. @ttypeconvnode._second_real_to_real,
  3059. @ttypeconvnode._second_int_to_real,
  3060. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3061. @ttypeconvnode._second_proc_to_procvar,
  3062. @ttypeconvnode._second_nil_to_methodprocvar,
  3063. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3064. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3065. @ttypeconvnode._second_cord_to_pointer,
  3066. @ttypeconvnode._second_nothing, { interface 2 string }
  3067. @ttypeconvnode._second_nothing, { interface 2 guid }
  3068. @ttypeconvnode._second_class_to_intf,
  3069. @ttypeconvnode._second_char_to_char,
  3070. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3071. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3072. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3073. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3074. @ttypeconvnode._second_nothing, { variant_2_enum }
  3075. @ttypeconvnode._second_nothing, { enum_2_variant }
  3076. @ttypeconvnode._second_nothing, { variant_2_interface }
  3077. @ttypeconvnode._second_nothing, { interface_2_variant }
  3078. @ttypeconvnode._second_nothing { array_2_dynarray }
  3079. );
  3080. type
  3081. tprocedureofobject = procedure of object;
  3082. var
  3083. r : packed record
  3084. proc : pointer;
  3085. obj : pointer;
  3086. end;
  3087. begin
  3088. { this is a little bit dirty but it works }
  3089. { and should be quite portable too }
  3090. r.proc:=secondconvert[c];
  3091. r.obj:=self;
  3092. tprocedureofobject(r)();
  3093. end;
  3094. {*****************************************************************************
  3095. TASNODE
  3096. *****************************************************************************}
  3097. function tasisnode.pass_typecheck: tnode;
  3098. var
  3099. hp : tnode;
  3100. begin
  3101. result:=nil;
  3102. typecheckpass(right);
  3103. typecheckpass(left);
  3104. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3105. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3106. if codegenerror then
  3107. exit;
  3108. if (right.resultdef.typ=classrefdef) then
  3109. begin
  3110. { left maybe an interface reference }
  3111. if is_interfacecom(left.resultdef) then
  3112. begin
  3113. { relation checks are not possible }
  3114. end
  3115. { or left must be a class }
  3116. else if is_class(left.resultdef) then
  3117. begin
  3118. { the operands must be related }
  3119. if (not(tobjectdef(left.resultdef).is_related(
  3120. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3121. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  3122. tobjectdef(left.resultdef)))) then
  3123. CGMessage2(type_e_classes_not_related,
  3124. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3125. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3126. end
  3127. else
  3128. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3129. case nodetype of
  3130. isn:
  3131. resultdef:=pasbool8type;
  3132. asn:
  3133. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3134. end;
  3135. end
  3136. else if is_interface(right.resultdef) or is_dispinterface(right.resultdef) then
  3137. begin
  3138. { left is a class }
  3139. if not(is_class(left.resultdef) or
  3140. is_interfacecom(left.resultdef)) then
  3141. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3142. case nodetype of
  3143. isn:
  3144. resultdef:=pasbool8type;
  3145. asn:
  3146. resultdef:=right.resultdef;
  3147. end;
  3148. { load the GUID of the interface }
  3149. if (right.nodetype=typen) then
  3150. begin
  3151. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3152. begin
  3153. if assigned(tobjectdef(right.resultdef).iidstr) then
  3154. begin
  3155. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3156. tstringconstnode(hp).changestringtype(cshortstringtype);
  3157. right.free;
  3158. right:=hp;
  3159. end
  3160. else
  3161. internalerror(201006131);
  3162. end
  3163. else
  3164. begin
  3165. if assigned(tobjectdef(right.resultdef).iidguid) then
  3166. begin
  3167. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3168. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3169. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3170. right.free;
  3171. right:=hp;
  3172. end
  3173. else
  3174. internalerror(201006132);
  3175. end;
  3176. typecheckpass(right);
  3177. end;
  3178. end
  3179. else
  3180. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3181. end;
  3182. {*****************************************************************************
  3183. TISNODE
  3184. *****************************************************************************}
  3185. constructor tisnode.create(l,r : tnode);
  3186. begin
  3187. inherited create(isn,l,r);
  3188. end;
  3189. function tisnode.pass_1 : tnode;
  3190. var
  3191. procname: string;
  3192. begin
  3193. result:=nil;
  3194. { Passing a class type to an "is" expression cannot result in a class
  3195. of that type to be constructed.
  3196. }
  3197. include(right.flags,nf_ignore_for_wpo);
  3198. if is_class(left.resultdef) and
  3199. (right.resultdef.typ=classrefdef) then
  3200. result := ccallnode.createinternres('fpc_do_is',
  3201. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3202. resultdef)
  3203. else
  3204. begin
  3205. if is_class(left.resultdef) then
  3206. if is_shortstring(right.resultdef) then
  3207. procname := 'fpc_class_is_corbaintf'
  3208. else
  3209. procname := 'fpc_class_is_intf'
  3210. else
  3211. if right.resultdef.typ=classrefdef then
  3212. procname := 'fpc_intf_is_class'
  3213. else
  3214. procname := 'fpc_intf_is';
  3215. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3216. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3217. end;
  3218. left := nil;
  3219. right := nil;
  3220. //firstpass(call);
  3221. if codegenerror then
  3222. exit;
  3223. end;
  3224. { dummy pass_2, it will never be called, but we need one since }
  3225. { you can't instantiate an abstract class }
  3226. procedure tisnode.pass_generate_code;
  3227. begin
  3228. end;
  3229. {*****************************************************************************
  3230. TASNODE
  3231. *****************************************************************************}
  3232. constructor tasnode.create(l,r : tnode);
  3233. begin
  3234. inherited create(asn,l,r);
  3235. call := nil;
  3236. end;
  3237. destructor tasnode.destroy;
  3238. begin
  3239. call.free;
  3240. inherited destroy;
  3241. end;
  3242. function tasnode.dogetcopy: tnode;
  3243. begin
  3244. result := inherited dogetcopy;
  3245. if assigned(call) then
  3246. tasnode(result).call := call.getcopy
  3247. else
  3248. tasnode(result).call := nil;
  3249. end;
  3250. function tasnode.docompare(p: tnode): boolean;
  3251. begin
  3252. result:=
  3253. inherited docompare(p) and
  3254. tasnode(p).call.isequal(call);
  3255. end;
  3256. function tasnode.pass_1 : tnode;
  3257. var
  3258. procname: string;
  3259. begin
  3260. result:=nil;
  3261. { Passing a class type to an "as" expression cannot result in a class
  3262. of that type to be constructed.
  3263. }
  3264. include(right.flags,nf_ignore_for_wpo);
  3265. if not assigned(call) then
  3266. begin
  3267. if is_class(left.resultdef) and
  3268. (right.resultdef.typ=classrefdef) then
  3269. call := ccallnode.createinternres('fpc_do_as',
  3270. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3271. resultdef)
  3272. else
  3273. begin
  3274. if is_class(left.resultdef) then
  3275. if is_shortstring(right.resultdef) then
  3276. procname := 'fpc_class_as_corbaintf'
  3277. else
  3278. procname := 'fpc_class_as_intf'
  3279. else
  3280. if right.resultdef.typ=classrefdef then
  3281. procname := 'fpc_intf_as_class'
  3282. else
  3283. procname := 'fpc_intf_as';
  3284. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3285. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3286. end;
  3287. left := nil;
  3288. right := nil;
  3289. firstpass(call);
  3290. if codegenerror then
  3291. exit;
  3292. expectloc:=call.expectloc;
  3293. end;
  3294. end;
  3295. end.