ncnv.pas 136 KB

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