ncnv.pas 127 KB

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