ncnv.pas 128 KB

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