ncnv.pas 135 KB

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