ncnv.pas 133 KB

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