ncnv.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  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. totype : ttype;
  29. convtype : tconverttype;
  30. constructor create(node : tnode;const t : ttype);virtual;
  31. constructor create_explicit(node : tnode;const t : ttype);
  32. constructor create_internal(node : tnode;const t : ttype);
  33. constructor create_proc_to_procvar(node : tnode);
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure buildderefimpl;override;
  37. procedure derefimpl;override;
  38. function getcopy : tnode;override;
  39. procedure printnodeinfo(var t : text);override;
  40. function pass_1 : tnode;override;
  41. function det_resulttype:tnode;override;
  42. procedure mark_write;override;
  43. function docompare(p: tnode) : boolean; override;
  44. function assign_allowed:boolean;
  45. procedure second_call_helper(c : tconverttype);
  46. private
  47. function resulttype_int_to_int : tnode;
  48. function resulttype_cord_to_pointer : tnode;
  49. function resulttype_chararray_to_string : tnode;
  50. function resulttype_string_to_chararray : tnode;
  51. function resulttype_string_to_string : tnode;
  52. function resulttype_char_to_string : tnode;
  53. function resulttype_char_to_chararray : tnode;
  54. function resulttype_int_to_real : tnode;
  55. function resulttype_real_to_real : tnode;
  56. function resulttype_real_to_currency : tnode;
  57. function resulttype_cchar_to_pchar : tnode;
  58. function resulttype_cstring_to_pchar : tnode;
  59. function resulttype_cstring_to_int : tnode;
  60. function resulttype_char_to_char : tnode;
  61. function resulttype_arrayconstructor_to_set : tnode;
  62. function resulttype_pchar_to_string : tnode;
  63. function resulttype_interface_to_guid : tnode;
  64. function resulttype_dynarray_to_openarray : tnode;
  65. function resulttype_pwchar_to_string : tnode;
  66. function resulttype_variant_to_dynarray : tnode;
  67. function resulttype_dynarray_to_variant : tnode;
  68. function resulttype_call_helper(c : tconverttype) : tnode;
  69. function resulttype_variant_to_enum : tnode;
  70. function resulttype_enum_to_variant : tnode;
  71. function resulttype_proc_to_procvar : tnode;
  72. function resulttype_variant_to_interface : tnode;
  73. function resulttype_interface_to_variant : tnode;
  74. function resulttype_array_2_dynarray : tnode;
  75. protected
  76. function first_int_to_int : tnode;virtual;
  77. function first_cstring_to_pchar : tnode;virtual;
  78. function first_cstring_to_int : tnode;virtual;
  79. function first_string_to_chararray : tnode;virtual;
  80. function first_char_to_string : tnode;virtual;
  81. function first_nothing : tnode;virtual;
  82. function first_array_to_pointer : tnode;virtual;
  83. function first_int_to_real : tnode;virtual;
  84. function first_real_to_real : tnode;virtual;
  85. function first_pointer_to_array : tnode;virtual;
  86. function first_cchar_to_pchar : tnode;virtual;
  87. function first_bool_to_int : tnode;virtual;
  88. function first_int_to_bool : tnode;virtual;
  89. function first_bool_to_bool : tnode;virtual;
  90. function first_proc_to_procvar : tnode;virtual;
  91. function first_load_smallset : tnode;virtual;
  92. function first_cord_to_pointer : tnode;virtual;
  93. function first_ansistring_to_pchar : tnode;virtual;
  94. function first_arrayconstructor_to_set : tnode;virtual;
  95. function first_class_to_intf : tnode;virtual;
  96. function first_char_to_char : tnode;virtual;
  97. function first_call_helper(c : tconverttype) : tnode;
  98. { these wrapper are necessary, because the first_* stuff is called }
  99. { through a table. Without the wrappers override wouldn't have }
  100. { any effect }
  101. function _first_int_to_int : tnode;
  102. function _first_cstring_to_pchar : tnode;
  103. function _first_cstring_to_int : tnode;
  104. function _first_string_to_chararray : tnode;
  105. function _first_char_to_string : tnode;
  106. function _first_nothing : tnode;
  107. function _first_array_to_pointer : tnode;
  108. function _first_int_to_real : tnode;
  109. function _first_real_to_real: tnode;
  110. function _first_pointer_to_array : tnode;
  111. function _first_cchar_to_pchar : tnode;
  112. function _first_bool_to_int : tnode;
  113. function _first_int_to_bool : tnode;
  114. function _first_bool_to_bool : tnode;
  115. function _first_proc_to_procvar : tnode;
  116. function _first_load_smallset : tnode;
  117. function _first_cord_to_pointer : tnode;
  118. function _first_ansistring_to_pchar : tnode;
  119. function _first_arrayconstructor_to_set : tnode;
  120. function _first_class_to_intf : tnode;
  121. function _first_char_to_char : tnode;
  122. procedure _second_int_to_int;virtual;
  123. procedure _second_string_to_string;virtual;
  124. procedure _second_cstring_to_pchar;virtual;
  125. procedure _second_cstring_to_int;virtual;
  126. procedure _second_string_to_chararray;virtual;
  127. procedure _second_array_to_pointer;virtual;
  128. procedure _second_pointer_to_array;virtual;
  129. procedure _second_chararray_to_string;virtual;
  130. procedure _second_char_to_string;virtual;
  131. procedure _second_int_to_real;virtual;
  132. procedure _second_real_to_real;virtual;
  133. procedure _second_cord_to_pointer;virtual;
  134. procedure _second_proc_to_procvar;virtual;
  135. procedure _second_bool_to_int;virtual;
  136. procedure _second_int_to_bool;virtual;
  137. procedure _second_bool_to_bool;virtual;
  138. procedure _second_load_smallset;virtual;
  139. procedure _second_ansistring_to_pchar;virtual;
  140. procedure _second_class_to_intf;virtual;
  141. procedure _second_char_to_char;virtual;
  142. procedure _second_nothing; virtual;
  143. procedure second_int_to_int;virtual;abstract;
  144. procedure second_string_to_string;virtual;abstract;
  145. procedure second_cstring_to_pchar;virtual;abstract;
  146. procedure second_cstring_to_int;virtual;abstract;
  147. procedure second_string_to_chararray;virtual;abstract;
  148. procedure second_array_to_pointer;virtual;abstract;
  149. procedure second_pointer_to_array;virtual;abstract;
  150. procedure second_chararray_to_string;virtual;abstract;
  151. procedure second_char_to_string;virtual;abstract;
  152. procedure second_int_to_real;virtual;abstract;
  153. procedure second_real_to_real;virtual;abstract;
  154. procedure second_cord_to_pointer;virtual;abstract;
  155. procedure second_proc_to_procvar;virtual;abstract;
  156. procedure second_bool_to_int;virtual;abstract;
  157. procedure second_int_to_bool;virtual;abstract;
  158. procedure second_bool_to_bool;virtual;abstract;
  159. procedure second_load_smallset;virtual;abstract;
  160. procedure second_ansistring_to_pchar;virtual;abstract;
  161. procedure second_class_to_intf;virtual;abstract;
  162. procedure second_char_to_char;virtual;abstract;
  163. procedure second_nothing; virtual;abstract;
  164. end;
  165. ttypeconvnodeclass = class of ttypeconvnode;
  166. tasnode = class(tbinarynode)
  167. constructor create(l,r : tnode);virtual;
  168. function pass_1 : tnode;override;
  169. function det_resulttype:tnode;override;
  170. function getcopy: tnode;override;
  171. destructor destroy; override;
  172. protected
  173. call: tnode;
  174. end;
  175. tasnodeclass = class of tasnode;
  176. tisnode = class(tbinarynode)
  177. constructor create(l,r : tnode);virtual;
  178. function pass_1 : tnode;override;
  179. function det_resulttype:tnode;override;
  180. procedure pass_2;override;
  181. end;
  182. tisnodeclass = class of tisnode;
  183. var
  184. ctypeconvnode : ttypeconvnodeclass;
  185. casnode : tasnodeclass;
  186. cisnode : tisnodeclass;
  187. procedure inserttypeconv(var p:tnode;const t:ttype);
  188. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  189. procedure arrayconstructor_to_set(var p : tnode);
  190. implementation
  191. uses
  192. cclasses,globtype,systems,
  193. cutils,verbose,globals,widestr,
  194. symconst,symdef,symsym,symbase,symtable,
  195. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  196. cgbase,procinfo,
  197. htypechk,pass_1,cpuinfo;
  198. {*****************************************************************************
  199. Helpers
  200. *****************************************************************************}
  201. procedure inserttypeconv(var p:tnode;const t:ttype);
  202. begin
  203. if not assigned(p.resulttype.def) then
  204. begin
  205. resulttypepass(p);
  206. if codegenerror then
  207. exit;
  208. end;
  209. { don't insert obsolete type conversions }
  210. if equal_defs(p.resulttype.def,t.def) and
  211. not ((p.resulttype.def.deftype=setdef) and
  212. (tsetdef(p.resulttype.def).settype <>
  213. tsetdef(t.def).settype)) then
  214. begin
  215. p.resulttype:=t;
  216. end
  217. else
  218. begin
  219. p:=ctypeconvnode.create(p,t);
  220. resulttypepass(p);
  221. end;
  222. end;
  223. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  224. begin
  225. if not assigned(p.resulttype.def) then
  226. begin
  227. resulttypepass(p);
  228. if codegenerror then
  229. exit;
  230. end;
  231. { don't insert obsolete type conversions }
  232. if equal_defs(p.resulttype.def,t.def) and
  233. not ((p.resulttype.def.deftype=setdef) and
  234. (tsetdef(p.resulttype.def).settype <>
  235. tsetdef(t.def).settype)) then
  236. begin
  237. p.resulttype:=t;
  238. end
  239. else
  240. begin
  241. p:=ctypeconvnode.create_internal(p,t);
  242. resulttypepass(p);
  243. end;
  244. end;
  245. {*****************************************************************************
  246. Array constructor to Set Conversion
  247. *****************************************************************************}
  248. procedure arrayconstructor_to_set(var p : tnode);
  249. var
  250. constp : tsetconstnode;
  251. buildp,
  252. p2,p3,p4 : tnode;
  253. htype : ttype;
  254. constset : Pconstset;
  255. constsetlo,
  256. constsethi : TConstExprInt;
  257. procedure update_constsethi(t:ttype);
  258. begin
  259. if ((t.def.deftype=orddef) and
  260. (torddef(t.def).high>=constsethi)) then
  261. begin
  262. if torddef(t.def).typ=uwidechar then
  263. begin
  264. constsethi:=255;
  265. if htype.def=nil then
  266. htype:=t;
  267. end
  268. else
  269. begin
  270. constsethi:=torddef(t.def).high;
  271. if htype.def=nil then
  272. begin
  273. if (constsethi>255) or
  274. (torddef(t.def).low<0) then
  275. htype:=u8inttype
  276. else
  277. htype:=t;
  278. end;
  279. if constsethi>255 then
  280. constsethi:=255;
  281. end;
  282. end
  283. else if ((t.def.deftype=enumdef) and
  284. (tenumdef(t.def).max>=constsethi)) then
  285. begin
  286. if htype.def=nil then
  287. htype:=t;
  288. constsethi:=tenumdef(t.def).max;
  289. end;
  290. end;
  291. procedure do_set(pos : longint);
  292. begin
  293. if (pos and not $ff)<>0 then
  294. Message(parser_e_illegal_set_expr);
  295. if pos>constsethi then
  296. constsethi:=pos;
  297. if pos<constsetlo then
  298. constsetlo:=pos;
  299. if pos in constset^ then
  300. Message(parser_e_illegal_set_expr);
  301. include(constset^,pos);
  302. end;
  303. var
  304. l : Longint;
  305. lr,hr : TConstExprInt;
  306. hp : tarrayconstructornode;
  307. begin
  308. if p.nodetype<>arrayconstructorn then
  309. internalerror(200205105);
  310. new(constset);
  311. constset^:=[];
  312. htype.reset;
  313. constsetlo:=0;
  314. constsethi:=0;
  315. constp:=csetconstnode.create(nil,htype);
  316. constp.value_set:=constset;
  317. buildp:=constp;
  318. hp:=tarrayconstructornode(p);
  319. if assigned(hp.left) then
  320. begin
  321. while assigned(hp) do
  322. begin
  323. p4:=nil; { will contain the tree to create the set }
  324. {split a range into p2 and p3 }
  325. if hp.left.nodetype=arrayconstructorrangen then
  326. begin
  327. p2:=tarrayconstructorrangenode(hp.left).left;
  328. p3:=tarrayconstructorrangenode(hp.left).right;
  329. tarrayconstructorrangenode(hp.left).left:=nil;
  330. tarrayconstructorrangenode(hp.left).right:=nil;
  331. end
  332. else
  333. begin
  334. p2:=hp.left;
  335. hp.left:=nil;
  336. p3:=nil;
  337. end;
  338. resulttypepass(p2);
  339. set_varstate(p2,vs_used,[vsf_must_be_valid]);
  340. if assigned(p3) then
  341. begin
  342. resulttypepass(p3);
  343. set_varstate(p3,vs_used,[vsf_must_be_valid]);
  344. end;
  345. if codegenerror then
  346. break;
  347. case p2.resulttype.def.deftype of
  348. enumdef,
  349. orddef:
  350. begin
  351. getrange(p2.resulttype.def,lr,hr);
  352. if assigned(p3) then
  353. begin
  354. { this isn't good, you'll get problems with
  355. type t010 = 0..10;
  356. ts = set of t010;
  357. var s : ts;b : t010
  358. begin s:=[1,2,b]; end.
  359. if is_integer(p3^.resulttype.def) then
  360. begin
  361. inserttypeconv(p3,u8bitdef);
  362. end;
  363. }
  364. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  365. begin
  366. aktfilepos:=p3.fileinfo;
  367. CGMessage(type_e_typeconflict_in_set);
  368. end
  369. else
  370. begin
  371. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  372. begin
  373. if not(is_integer(p3.resulttype.def)) then
  374. htype:=p3.resulttype
  375. else
  376. begin
  377. inserttypeconv(p3,u8inttype);
  378. inserttypeconv(p2,u8inttype);
  379. end;
  380. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  381. do_set(l);
  382. p2.free;
  383. p3.free;
  384. end
  385. else
  386. begin
  387. update_constsethi(p2.resulttype);
  388. inserttypeconv(p2,htype);
  389. update_constsethi(p3.resulttype);
  390. inserttypeconv(p3,htype);
  391. if assigned(htype.def) then
  392. inserttypeconv(p3,htype)
  393. else
  394. inserttypeconv(p3,u8inttype);
  395. p4:=csetelementnode.create(p2,p3);
  396. end;
  397. end;
  398. end
  399. else
  400. begin
  401. { Single value }
  402. if p2.nodetype=ordconstn then
  403. begin
  404. if not(is_integer(p2.resulttype.def)) then
  405. begin
  406. { for constant set elements, delphi allows the usage of elements of enumerations which
  407. have value>255 if there is no element with a value > 255 used }
  408. if (m_delphi in aktmodeswitches) and (p2.resulttype.def.deftype=enumdef) then
  409. begin
  410. if tordconstnode(p2).value>constsethi then
  411. constsethi:=tordconstnode(p2).value;
  412. if htype.def=nil then
  413. htype:=p2.resulttype;
  414. end
  415. else
  416. update_constsethi(p2.resulttype);
  417. end;
  418. if assigned(htype.def) then
  419. inserttypeconv(p2,htype)
  420. else
  421. inserttypeconv(p2,u8inttype);
  422. do_set(tordconstnode(p2).value);
  423. p2.free;
  424. end
  425. else
  426. begin
  427. update_constsethi(p2.resulttype);
  428. if assigned(htype.def) then
  429. inserttypeconv(p2,htype)
  430. else
  431. inserttypeconv(p2,u8inttype);
  432. p4:=csetelementnode.create(p2,nil);
  433. end;
  434. end;
  435. end;
  436. stringdef :
  437. begin
  438. { if we've already set elements which are constants }
  439. { throw an error }
  440. if ((htype.def=nil) and assigned(buildp)) or
  441. not(is_char(htype.def)) then
  442. CGMessage(type_e_typeconflict_in_set)
  443. else
  444. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  445. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  446. if htype.def=nil then
  447. htype:=cchartype;
  448. p2.free;
  449. end;
  450. else
  451. CGMessage(type_e_ordinal_expr_expected);
  452. end;
  453. { insert the set creation tree }
  454. if assigned(p4) then
  455. buildp:=caddnode.create(addn,buildp,p4);
  456. { load next and dispose current node }
  457. p2:=hp;
  458. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  459. tarrayconstructornode(p2).right:=nil;
  460. p2.free;
  461. end;
  462. if (htype.def=nil) then
  463. htype:=u8inttype;
  464. end
  465. else
  466. begin
  467. { empty set [], only remove node }
  468. p.free;
  469. end;
  470. { set the initial set type }
  471. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  472. { determine the resulttype for the tree }
  473. resulttypepass(buildp);
  474. { set the new tree }
  475. p:=buildp;
  476. end;
  477. {*****************************************************************************
  478. TTYPECONVNODE
  479. *****************************************************************************}
  480. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  481. begin
  482. inherited create(typeconvn,node);
  483. convtype:=tc_none;
  484. totype:=t;
  485. if t.def=nil then
  486. internalerror(200103281);
  487. fileinfo:=node.fileinfo;
  488. end;
  489. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  490. begin
  491. self.create(node,t);
  492. include(flags,nf_explicit);
  493. end;
  494. constructor ttypeconvnode.create_internal(node : tnode;const t:ttype);
  495. begin
  496. self.create(node,t);
  497. { handle like explicit conversions }
  498. include(flags,nf_explicit);
  499. include(flags,nf_internal);
  500. end;
  501. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  502. begin
  503. self.create(node,voidtype);
  504. convtype:=tc_proc_2_procvar;
  505. end;
  506. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  507. begin
  508. inherited ppuload(t,ppufile);
  509. ppufile.gettype(totype);
  510. convtype:=tconverttype(ppufile.getbyte);
  511. end;
  512. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  513. begin
  514. inherited ppuwrite(ppufile);
  515. ppufile.puttype(totype);
  516. ppufile.putbyte(byte(convtype));
  517. end;
  518. procedure ttypeconvnode.buildderefimpl;
  519. begin
  520. inherited buildderefimpl;
  521. totype.buildderef;
  522. end;
  523. procedure ttypeconvnode.derefimpl;
  524. begin
  525. inherited derefimpl;
  526. totype.resolve;
  527. end;
  528. function ttypeconvnode.getcopy : tnode;
  529. var
  530. n : ttypeconvnode;
  531. begin
  532. n:=ttypeconvnode(inherited getcopy);
  533. n.convtype:=convtype;
  534. n.totype:=totype;
  535. getcopy:=n;
  536. end;
  537. procedure ttypeconvnode.printnodeinfo(var t : text);
  538. const
  539. convtyp2str : array[tconverttype] of pchar = (
  540. 'tc_none',
  541. 'tc_equal',
  542. 'tc_not_possible',
  543. 'tc_string_2_string',
  544. 'tc_char_2_string',
  545. 'tc_char_2_chararray',
  546. 'tc_pchar_2_string',
  547. 'tc_cchar_2_pchar',
  548. 'tc_cstring_2_pchar',
  549. 'tc_cstring_2_int',
  550. 'tc_ansistring_2_pchar',
  551. 'tc_string_2_chararray',
  552. 'tc_chararray_2_string',
  553. 'tc_array_2_pointer',
  554. 'tc_pointer_2_array',
  555. 'tc_int_2_int',
  556. 'tc_int_2_bool',
  557. 'tc_bool_2_bool',
  558. 'tc_bool_2_int',
  559. 'tc_real_2_real',
  560. 'tc_int_2_real',
  561. 'tc_real_2_currency',
  562. 'tc_proc_2_procvar',
  563. 'tc_arrayconstructor_2_set',
  564. 'tc_load_smallset',
  565. 'tc_cord_2_pointer',
  566. 'tc_intf_2_string',
  567. 'tc_intf_2_guid',
  568. 'tc_class_2_intf',
  569. 'tc_char_2_char',
  570. 'tc_normal_2_smallset',
  571. 'tc_dynarray_2_openarray',
  572. 'tc_pwchar_2_string',
  573. 'tc_variant_2_dynarray',
  574. 'tc_dynarray_2_variant',
  575. 'tc_variant_2_enum',
  576. 'tc_enum_2_variant',
  577. 'tc_interface_2_variant',
  578. 'tc_variant_2_interface',
  579. 'tc_array_2_dynarray'
  580. );
  581. begin
  582. inherited printnodeinfo(t);
  583. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  584. end;
  585. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  586. var
  587. t : tnode;
  588. begin
  589. result:=nil;
  590. if left.nodetype=ordconstn then
  591. begin
  592. { check if we have a valid pointer constant (JM) }
  593. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  594. if (sizeof(TConstPtrUInt) = 4) then
  595. begin
  596. if (tordconstnode(left).value < low(longint)) or
  597. (tordconstnode(left).value > high(cardinal)) then
  598. CGMessage(parser_e_range_check_error);
  599. end
  600. else if (sizeof(TConstPtrUInt) = 8) then
  601. begin
  602. if (tordconstnode(left).value < low(int64)) or
  603. (tordconstnode(left).value > high(qword)) then
  604. CGMessage(parser_e_range_check_error);
  605. end
  606. else
  607. internalerror(2001020801);
  608. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  609. result:=t;
  610. end
  611. else
  612. internalerror(200104023);
  613. end;
  614. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  615. var
  616. chartype : string[8];
  617. begin
  618. if is_widechar(tarraydef(left.resulttype.def).elementtype.def) then
  619. chartype:='widechar'
  620. else
  621. chartype:='char';
  622. result := ccallnode.createinternres(
  623. 'fpc_'+chartype+'array_to_'+tstringdef(resulttype.def).stringtypname,
  624. ccallparanode.create(left,nil),resulttype);
  625. left := nil;
  626. end;
  627. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  628. var
  629. arrsize : aint;
  630. chartype : string[8];
  631. begin
  632. result := nil;
  633. with tarraydef(resulttype.def) do
  634. begin
  635. if highrange<lowrange then
  636. internalerror(200501051);
  637. arrsize := highrange-lowrange+1;
  638. end;
  639. if (left.nodetype = stringconstn) and
  640. { left.length+1 since there's always a terminating #0 character (JM) }
  641. (tstringconstnode(left).len+1 >= arrsize) and
  642. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  643. begin
  644. { handled separately }
  645. result := nil;
  646. exit;
  647. end;
  648. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  649. chartype:='widechar'
  650. else
  651. chartype:='char';
  652. result := ccallnode.createinternres(
  653. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  654. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  655. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  656. left := nil;
  657. end;
  658. function ttypeconvnode.resulttype_string_to_string : tnode;
  659. var
  660. procname: string[31];
  661. stringpara : tcallparanode;
  662. pw : pcompilerwidestring;
  663. pc : pchar;
  664. begin
  665. result:=nil;
  666. if left.nodetype=stringconstn then
  667. begin
  668. { convert ascii 2 unicode }
  669. {$ifdef ansistring_bits}
  670. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  671. (tstringconstnode(left).st_type in [st_ansistring16,st_ansistring32,
  672. st_ansistring64,st_shortstring,st_longstring]) then
  673. {$else}
  674. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  675. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  676. {$endif}
  677. begin
  678. initwidestring(pw);
  679. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  680. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  681. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  682. end
  683. else
  684. { convert unicode 2 ascii }
  685. {$ifdef ansistring_bits}
  686. if (tstringconstnode(left).st_type=st_widestring) and
  687. (tstringdef(resulttype.def).string_typ in [st_ansistring16,st_ansistring32,
  688. st_ansistring64,st_shortstring,st_longstring]) then
  689. {$else}
  690. if (tstringconstnode(left).st_type=st_widestring) and
  691. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  692. {$endif}
  693. begin
  694. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  695. getmem(pc,getlengthwidestring(pw)+1);
  696. unicode2ascii(pw,pc);
  697. donewidestring(pw);
  698. tstringconstnode(left).value_str:=pc;
  699. end;
  700. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  701. tstringconstnode(left).resulttype:=resulttype;
  702. result:=left;
  703. left:=nil;
  704. end
  705. else
  706. begin
  707. { get the correct procedure name }
  708. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  709. '_to_'+tstringdef(resulttype.def).stringtypname;
  710. { create parameter (and remove left node from typeconvnode }
  711. { since it's reused as parameter) }
  712. stringpara := ccallparanode.create(left,nil);
  713. left := nil;
  714. { when converting to shortstrings, we have to pass high(destination) too }
  715. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  716. stringpara.right := ccallparanode.create(cinlinenode.create(
  717. in_high_x,false,self.getcopy),nil);
  718. { and create the callnode }
  719. result := ccallnode.createinternres(procname,stringpara,resulttype);
  720. end;
  721. end;
  722. function ttypeconvnode.resulttype_char_to_string : tnode;
  723. var
  724. procname: string[31];
  725. para : tcallparanode;
  726. hp : tstringconstnode;
  727. ws : pcompilerwidestring;
  728. begin
  729. result:=nil;
  730. if left.nodetype=ordconstn then
  731. begin
  732. if tstringdef(resulttype.def).string_typ=st_widestring then
  733. begin
  734. initwidestring(ws);
  735. if torddef(left.resulttype.def).typ=uwidechar then
  736. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  737. else
  738. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  739. hp:=cstringconstnode.createwstr(ws);
  740. donewidestring(ws);
  741. end
  742. else
  743. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  744. result:=hp;
  745. end
  746. else
  747. { shortstrings are handled 'inline' }
  748. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  749. begin
  750. { create the parameter }
  751. para := ccallparanode.create(left,nil);
  752. left := nil;
  753. { and the procname }
  754. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  755. { and finally the call }
  756. result := ccallnode.createinternres(procname,para,resulttype);
  757. end
  758. else
  759. begin
  760. { create word(byte(char) shl 8 or 1) for litte endian machines }
  761. { and word(byte(char) or 256) for big endian machines }
  762. left := ctypeconvnode.create_internal(left,u8inttype);
  763. if (target_info.endian = endian_little) then
  764. left := caddnode.create(orn,
  765. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  766. cordconstnode.create(1,s32inttype,false))
  767. else
  768. left := caddnode.create(orn,left,
  769. cordconstnode.create(1 shl 8,s32inttype,false));
  770. left := ctypeconvnode.create_internal(left,u16inttype);
  771. resulttypepass(left);
  772. end;
  773. end;
  774. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  775. begin
  776. if resulttype.def.size <> 1 then
  777. begin
  778. { convert first to string, then to chararray }
  779. inserttypeconv(left,cshortstringtype);
  780. inserttypeconv(left,resulttype);
  781. result:=left;
  782. left := nil;
  783. exit;
  784. end;
  785. result := nil;
  786. end;
  787. function ttypeconvnode.resulttype_char_to_char : tnode;
  788. var
  789. hp : tordconstnode;
  790. begin
  791. result:=nil;
  792. if left.nodetype=ordconstn then
  793. begin
  794. if (torddef(resulttype.def).typ=uchar) and
  795. (torddef(left.resulttype.def).typ=uwidechar) then
  796. begin
  797. hp:=cordconstnode.create(
  798. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  799. cchartype,true);
  800. result:=hp;
  801. end
  802. else if (torddef(resulttype.def).typ=uwidechar) and
  803. (torddef(left.resulttype.def).typ=uchar) then
  804. begin
  805. hp:=cordconstnode.create(
  806. asciichar2unicode(chr(tordconstnode(left).value)),
  807. cwidechartype,true);
  808. result:=hp;
  809. end
  810. else
  811. internalerror(200105131);
  812. exit;
  813. end;
  814. end;
  815. function ttypeconvnode.resulttype_int_to_int : tnode;
  816. var
  817. v : TConstExprInt;
  818. begin
  819. result:=nil;
  820. if left.nodetype=ordconstn then
  821. begin
  822. v:=tordconstnode(left).value;
  823. if is_currency(resulttype.def) then
  824. v:=v*10000;
  825. if (resulttype.def.deftype=pointerdef) then
  826. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  827. else
  828. begin
  829. if is_currency(left.resulttype.def) then
  830. v:=v div 10000;
  831. result:=cordconstnode.create(v,resulttype,false);
  832. end;
  833. end
  834. else if left.nodetype=pointerconstn then
  835. begin
  836. v:=tpointerconstnode(left).value;
  837. if (resulttype.def.deftype=pointerdef) then
  838. result:=cpointerconstnode.create(v,resulttype)
  839. else
  840. begin
  841. if is_currency(resulttype.def) then
  842. v:=v*10000;
  843. result:=cordconstnode.create(v,resulttype,false);
  844. end;
  845. end
  846. else
  847. begin
  848. { multiply by 10000 for currency. We need to use getcopy to pass
  849. the argument because the current node is always disposed. Only
  850. inserting the multiply in the left node is not possible because
  851. it'll get in an infinite loop to convert int->currency }
  852. if is_currency(resulttype.def) then
  853. begin
  854. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  855. include(result.flags,nf_is_currency);
  856. end
  857. else if is_currency(left.resulttype.def) then
  858. begin
  859. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  860. include(result.flags,nf_is_currency);
  861. end;
  862. end;
  863. end;
  864. function ttypeconvnode.resulttype_int_to_real : tnode;
  865. var
  866. rv : bestreal;
  867. begin
  868. result:=nil;
  869. if left.nodetype=ordconstn then
  870. begin
  871. rv:=tordconstnode(left).value;
  872. if is_currency(resulttype.def) then
  873. rv:=rv*10000.0
  874. else if is_currency(left.resulttype.def) then
  875. rv:=rv/10000.0;
  876. result:=crealconstnode.create(rv,resulttype);
  877. end
  878. else
  879. begin
  880. { multiply by 10000 for currency. We need to use getcopy to pass
  881. the argument because the current node is always disposed. Only
  882. inserting the multiply in the left node is not possible because
  883. it'll get in an infinite loop to convert int->currency }
  884. if is_currency(resulttype.def) then
  885. begin
  886. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  887. include(result.flags,nf_is_currency);
  888. end
  889. else if is_currency(left.resulttype.def) then
  890. begin
  891. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  892. include(result.flags,nf_is_currency);
  893. end;
  894. end;
  895. end;
  896. function ttypeconvnode.resulttype_real_to_currency : tnode;
  897. begin
  898. if not is_currency(resulttype.def) then
  899. internalerror(200304221);
  900. result:=nil;
  901. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  902. include(left.flags,nf_is_currency);
  903. resulttypepass(left);
  904. { Convert constants directly, else call Round() }
  905. if left.nodetype=realconstn then
  906. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  907. else
  908. result:=ccallnode.createinternres('fpc_round_real',
  909. ccallparanode.create(left,nil),resulttype);
  910. left:=nil;
  911. end;
  912. function ttypeconvnode.resulttype_real_to_real : tnode;
  913. begin
  914. result:=nil;
  915. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  916. begin
  917. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  918. include(left.flags,nf_is_currency);
  919. resulttypepass(left);
  920. end
  921. else
  922. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  923. begin
  924. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  925. include(left.flags,nf_is_currency);
  926. resulttypepass(left);
  927. end;
  928. if left.nodetype=realconstn then
  929. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  930. end;
  931. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  932. begin
  933. result:=nil;
  934. if is_pwidechar(resulttype.def) then
  935. inserttypeconv(left,cwidestringtype)
  936. else
  937. inserttypeconv(left,cshortstringtype);
  938. { evaluate again, reset resulttype so the convert_typ
  939. will be calculated again and cstring_to_pchar will
  940. be used for futher conversion }
  941. convtype:=tc_none;
  942. result:=det_resulttype;
  943. end;
  944. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  945. begin
  946. result:=nil;
  947. if is_pwidechar(resulttype.def) then
  948. inserttypeconv(left,cwidestringtype);
  949. end;
  950. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  951. var
  952. fcc : cardinal;
  953. pb : pbyte;
  954. begin
  955. result:=nil;
  956. if left.nodetype<>stringconstn then
  957. internalerror(200510012);
  958. if tstringconstnode(left).len=4 then
  959. begin
  960. pb:=pbyte(tstringconstnode(left).value_str);
  961. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  962. result:=cordconstnode.create(fcc,u32inttype,false);
  963. end
  964. else
  965. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  966. end;
  967. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  968. var
  969. hp : tnode;
  970. begin
  971. result:=nil;
  972. if left.nodetype<>arrayconstructorn then
  973. internalerror(5546);
  974. { remove typeconv node }
  975. hp:=left;
  976. left:=nil;
  977. { create a set constructor tree }
  978. arrayconstructor_to_set(hp);
  979. result:=hp;
  980. end;
  981. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  982. begin
  983. result := ccallnode.createinternres(
  984. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  985. ccallparanode.create(left,nil),resulttype);
  986. left := nil;
  987. end;
  988. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  989. begin
  990. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  991. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  992. end;
  993. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  994. begin
  995. { a dynamic array is a pointer to an array, so to convert it to }
  996. { an open array, we have to dereference it (JM) }
  997. result := ctypeconvnode.create_internal(left,voidpointertype);
  998. resulttypepass(result);
  999. { left is reused }
  1000. left := nil;
  1001. result := cderefnode.create(result);
  1002. include(result.flags,nf_no_checkpointer);
  1003. result.resulttype := resulttype;
  1004. end;
  1005. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  1006. begin
  1007. result := ccallnode.createinternres(
  1008. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  1009. ccallparanode.create(left,nil),resulttype);
  1010. left := nil;
  1011. end;
  1012. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  1013. begin
  1014. result := ccallnode.createinternres(
  1015. 'fpc_variant_to_dynarray',
  1016. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1017. ccallparanode.create(left,nil)
  1018. ),resulttype);
  1019. resulttypepass(result);
  1020. left:=nil;
  1021. end;
  1022. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1023. begin
  1024. result := ccallnode.createinternres(
  1025. 'fpc_dynarray_to_variant',
  1026. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1027. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1028. ),resulttype);
  1029. resulttypepass(result);
  1030. left:=nil;
  1031. end;
  1032. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1033. begin
  1034. result := ccallnode.createinternres(
  1035. 'fpc_variant_to_interface',
  1036. ccallparanode.create(left,nil)
  1037. ,resulttype);
  1038. resulttypepass(result);
  1039. left:=nil;
  1040. end;
  1041. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1042. begin
  1043. result := ccallnode.createinternres(
  1044. 'fpc_interface_to_variant',
  1045. ccallparanode.create(left,nil)
  1046. ,resulttype);
  1047. resulttypepass(result);
  1048. left:=nil;
  1049. end;
  1050. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1051. begin
  1052. result := ctypeconvnode.create_internal(left,sinttype);
  1053. result := ctypeconvnode.create_internal(result,resulttype);
  1054. resulttypepass(result);
  1055. { left is reused }
  1056. left := nil;
  1057. end;
  1058. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1059. begin
  1060. result := ctypeconvnode.create_internal(left,sinttype);
  1061. result := ctypeconvnode.create_internal(result,cvarianttype);
  1062. resulttypepass(result);
  1063. { left is reused }
  1064. left := nil;
  1065. end;
  1066. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1067. var
  1068. newstatement : tstatementnode;
  1069. temp : ttempcreatenode;
  1070. temp2 : ttempcreatenode;
  1071. begin
  1072. { create statements with call to getmem+initialize }
  1073. result:=internalstatements(newstatement);
  1074. { create temp for result }
  1075. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1076. addstatement(newstatement,temp);
  1077. { get temp for array of lengths }
  1078. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1079. addstatement(newstatement,temp2);
  1080. { one dimensional }
  1081. addstatement(newstatement,cassignmentnode.create(
  1082. ctemprefnode.create_offset(temp2,0),
  1083. cordconstnode.create
  1084. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1085. { create call to fpc_dynarr_setlength }
  1086. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1087. ccallparanode.create(caddrnode.create_internal
  1088. (ctemprefnode.create(temp2)),
  1089. ccallparanode.create(cordconstnode.create
  1090. (1,s32inttype,true),
  1091. ccallparanode.create(caddrnode.create_internal
  1092. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1093. ccallparanode.create(
  1094. ctypeconvnode.create_internal(
  1095. ctemprefnode.create(temp),voidpointertype),
  1096. nil))))
  1097. ));
  1098. addstatement(newstatement,ctempdeletenode.create(temp2));
  1099. { copy ... }
  1100. addstatement(newstatement,cassignmentnode.create(
  1101. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1102. left
  1103. ));
  1104. { left is reused }
  1105. left:=nil;
  1106. { the last statement should return the value as
  1107. location and type, this is done be referencing the
  1108. temp and converting it first from a persistent temp to
  1109. normal temp }
  1110. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1111. addstatement(newstatement,ctemprefnode.create(temp));
  1112. end;
  1113. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1114. var
  1115. newparast : tsymtable absolute arg;
  1116. vs : tparavarsym;
  1117. begin
  1118. if tsym(p).typ<>paravarsym then
  1119. exit;
  1120. with tparavarsym(p) do
  1121. begin
  1122. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1123. vs.defaultconstsym:=defaultconstsym;
  1124. newparast.insert(vs);
  1125. end;
  1126. end;
  1127. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1128. var
  1129. pd : tabstractprocdef;
  1130. begin
  1131. result:=nil;
  1132. pd:=tabstractprocdef(left.resulttype.def);
  1133. { create procvardef }
  1134. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1135. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1136. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1137. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1138. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1139. { method ? then set the methodpointer flag }
  1140. if (pd.owner.symtabletype=objectsymtable) then
  1141. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1142. { was it a local procedure? }
  1143. if (pd.owner.symtabletype=localsymtable) then
  1144. include(tprocvardef(resulttype.def).procoptions,po_local);
  1145. { only need the address of the method? this is needed
  1146. for @tobject.create. In this case there will be a loadn without
  1147. a methodpointer. }
  1148. if (left.nodetype=loadn) and
  1149. not assigned(tloadnode(left).left) then
  1150. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1151. { Add parameters use only references, we don't need to keep the
  1152. parast. We use the parast from the original function to calculate
  1153. our parameter data and reset it afterwards }
  1154. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1155. tprocvardef(resulttype.def).calcparas;
  1156. end;
  1157. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1158. const
  1159. resulttypeconvert : array[tconverttype] of pointer = (
  1160. {none} nil,
  1161. {equal} nil,
  1162. {not_possible} nil,
  1163. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1164. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1165. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1166. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1167. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1168. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1169. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1170. { ansistring_2_pchar } nil,
  1171. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1172. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1173. { array_2_pointer } nil,
  1174. { pointer_2_array } nil,
  1175. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1176. { int_2_bool } nil,
  1177. { bool_2_bool } nil,
  1178. { bool_2_int } nil,
  1179. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1180. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1181. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1182. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1183. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1184. { load_smallset } nil,
  1185. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1186. { intf_2_string } nil,
  1187. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1188. { class_2_intf } nil,
  1189. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1190. { normal_2_smallset} nil,
  1191. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1192. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1193. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1194. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1195. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1196. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1197. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1198. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1199. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1200. );
  1201. type
  1202. tprocedureofobject = function : tnode of object;
  1203. var
  1204. r : packed record
  1205. proc : pointer;
  1206. obj : pointer;
  1207. end;
  1208. begin
  1209. result:=nil;
  1210. { this is a little bit dirty but it works }
  1211. { and should be quite portable too }
  1212. r.proc:=resulttypeconvert[c];
  1213. r.obj:=self;
  1214. if assigned(r.proc) then
  1215. result:=tprocedureofobject(r)();
  1216. end;
  1217. function ttypeconvnode.det_resulttype:tnode;
  1218. var
  1219. htype : ttype;
  1220. hp : tnode;
  1221. currprocdef : tabstractprocdef;
  1222. aprocdef : tprocdef;
  1223. eq : tequaltype;
  1224. cdoptions : tcompare_defs_options;
  1225. begin
  1226. result:=nil;
  1227. resulttype:=totype;
  1228. resulttypepass(left);
  1229. if codegenerror then
  1230. exit;
  1231. { When absolute force tc_equal }
  1232. if (nf_absolute in flags) then
  1233. begin
  1234. convtype:=tc_equal;
  1235. if not(tstoreddef(resulttype.def).is_intregable) and
  1236. not(tstoreddef(resulttype.def).is_fpuregable) then
  1237. make_not_regable(left);
  1238. exit;
  1239. end;
  1240. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1241. convert on the procvar value. This is used to access the
  1242. fields of a methodpointer }
  1243. if not(nf_load_procvar in flags) and
  1244. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1245. maybe_call_procvar(left,true);
  1246. { convert array constructors to sets, because there is no conversion
  1247. possible for array constructors }
  1248. if (resulttype.def.deftype<>arraydef) and
  1249. is_array_constructor(left.resulttype.def) then
  1250. begin
  1251. arrayconstructor_to_set(left);
  1252. resulttypepass(left);
  1253. end;
  1254. if convtype=tc_none then
  1255. begin
  1256. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1257. if nf_explicit in flags then
  1258. include(cdoptions,cdo_explicit);
  1259. if nf_internal in flags then
  1260. include(cdoptions,cdo_internal);
  1261. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1262. case eq of
  1263. te_exact,
  1264. te_equal :
  1265. begin
  1266. { because is_equal only checks the basetype for sets we need to
  1267. check here if we are loading a smallset into a normalset }
  1268. if (resulttype.def.deftype=setdef) and
  1269. (left.resulttype.def.deftype=setdef) and
  1270. ((tsetdef(resulttype.def).settype = smallset) xor
  1271. (tsetdef(left.resulttype.def).settype = smallset)) then
  1272. begin
  1273. { constant sets can be converted by changing the type only }
  1274. if (left.nodetype=setconstn) then
  1275. begin
  1276. left.resulttype:=resulttype;
  1277. result:=left;
  1278. left:=nil;
  1279. exit;
  1280. end;
  1281. if (tsetdef(resulttype.def).settype <> smallset) then
  1282. convtype:=tc_load_smallset
  1283. else
  1284. convtype := tc_normal_2_smallset;
  1285. exit;
  1286. end
  1287. else
  1288. begin
  1289. { Only leave when there is no conversion to do.
  1290. We can still need to call a conversion routine,
  1291. like the routine to convert a stringconstnode }
  1292. if convtype in [tc_equal,tc_not_possible] then
  1293. begin
  1294. left.resulttype:=resulttype;
  1295. result:=left;
  1296. left:=nil;
  1297. exit;
  1298. end;
  1299. end;
  1300. end;
  1301. te_convert_l1,
  1302. te_convert_l2,
  1303. te_convert_l3 :
  1304. begin
  1305. { nothing to do }
  1306. end;
  1307. te_convert_operator :
  1308. begin
  1309. include(current_procinfo.flags,pi_do_call);
  1310. inc(aprocdef.procsym.refs);
  1311. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1312. { tell explicitly which def we must use !! (PM) }
  1313. tcallnode(hp).procdefinition:=aprocdef;
  1314. left:=nil;
  1315. result:=hp;
  1316. exit;
  1317. end;
  1318. te_incompatible :
  1319. begin
  1320. { Procedures have a resulttype.def of voiddef and functions of their
  1321. own resulttype.def. They will therefore always be incompatible with
  1322. a procvar. Because isconvertable cannot check for procedures we
  1323. use an extra check for them.}
  1324. if (left.nodetype=calln) and
  1325. (tcallnode(left).para_count=0) and
  1326. (resulttype.def.deftype=procvardef) and
  1327. (
  1328. (m_tp_procvar in aktmodeswitches) or
  1329. (m_mac_procvar in aktmodeswitches)
  1330. ) then
  1331. begin
  1332. if assigned(tcallnode(left).right) then
  1333. begin
  1334. { this is already a procvar, if it is really equal
  1335. is checked below }
  1336. convtype:=tc_equal;
  1337. hp:=tcallnode(left).right.getcopy;
  1338. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1339. end
  1340. else
  1341. begin
  1342. convtype:=tc_proc_2_procvar;
  1343. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1344. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1345. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1346. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1347. begin
  1348. if assigned(tcallnode(left).methodpointer) then
  1349. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1350. else
  1351. tloadnode(hp).set_mp(load_self_node);
  1352. end;
  1353. resulttypepass(hp);
  1354. end;
  1355. left.free;
  1356. left:=hp;
  1357. { Now check if the procedure we are going to assign to
  1358. the procvar, is compatible with the procvar's type }
  1359. if not(nf_explicit in flags) and
  1360. (proc_to_procvar_equal(currprocdef,
  1361. tprocvardef(resulttype.def),true)=te_incompatible) then
  1362. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1363. exit;
  1364. end;
  1365. { Handle explicit type conversions }
  1366. if nf_explicit in flags then
  1367. begin
  1368. { do common tc_equal cast }
  1369. convtype:=tc_equal;
  1370. { ordinal constants can be resized to 1,2,4,8 bytes }
  1371. if (left.nodetype=ordconstn) then
  1372. begin
  1373. { Insert typeconv for ordinal to the correct size first on left, after
  1374. that the other conversion can be done }
  1375. htype.reset;
  1376. case longint(resulttype.def.size) of
  1377. 1 :
  1378. htype:=s8inttype;
  1379. 2 :
  1380. htype:=s16inttype;
  1381. 4 :
  1382. htype:=s32inttype;
  1383. 8 :
  1384. htype:=s64inttype;
  1385. end;
  1386. { we need explicit, because it can also be an enum }
  1387. if assigned(htype.def) then
  1388. inserttypeconv_internal(left,htype)
  1389. else
  1390. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1391. end;
  1392. { check if the result could be in a register }
  1393. if (not(tstoreddef(resulttype.def).is_intregable) and
  1394. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1395. ((left.resulttype.def.deftype = floatdef) and
  1396. (resulttype.def.deftype <> floatdef)) then
  1397. make_not_regable(left);
  1398. { class/interface to class/interface, with checkobject support }
  1399. if is_class_or_interface(resulttype.def) and
  1400. is_class_or_interface(left.resulttype.def) then
  1401. begin
  1402. { check if the types are related }
  1403. if not(nf_internal in flags) and
  1404. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1405. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1406. begin
  1407. { Give an error when typecasting class to interface, this is compatible
  1408. with delphi }
  1409. if is_interface(resulttype.def) and
  1410. not is_interface(left.resulttype.def) then
  1411. CGMessage2(type_e_classes_not_related,
  1412. FullTypeName(left.resulttype.def,resulttype.def),
  1413. FullTypeName(resulttype.def,left.resulttype.def))
  1414. else
  1415. CGMessage2(type_w_classes_not_related,
  1416. FullTypeName(left.resulttype.def,resulttype.def),
  1417. FullTypeName(resulttype.def,left.resulttype.def))
  1418. end;
  1419. { Add runtime check? }
  1420. if (cs_check_object in aktlocalswitches) then
  1421. begin
  1422. { we can translate the typeconvnode to 'as' when
  1423. typecasting to a class or interface }
  1424. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1425. left:=nil;
  1426. result:=hp;
  1427. exit;
  1428. end;
  1429. end
  1430. else
  1431. begin
  1432. { only if the same size or formal def }
  1433. if not(
  1434. (left.resulttype.def.deftype=formaldef) or
  1435. (
  1436. not(is_open_array(left.resulttype.def)) and
  1437. (left.resulttype.def.size=resulttype.def.size)
  1438. ) or
  1439. (
  1440. is_void(left.resulttype.def) and
  1441. (left.nodetype=derefn)
  1442. )
  1443. ) then
  1444. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1445. end;
  1446. end
  1447. else
  1448. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1449. end;
  1450. else
  1451. internalerror(200211231);
  1452. end;
  1453. end;
  1454. { Give hint or warning for unportable code, exceptions are
  1455. - typecasts from constants
  1456. - void }
  1457. if not(nf_internal in flags) and
  1458. (left.nodetype<>ordconstn) and
  1459. not(is_void(left.resulttype.def)) and
  1460. (((left.resulttype.def.deftype=orddef) and
  1461. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1462. ((resulttype.def.deftype=orddef) and
  1463. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1464. begin
  1465. { Give a warning when sizes don't match, because then info will be lost }
  1466. if left.resulttype.def.size=resulttype.def.size then
  1467. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1468. else
  1469. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1470. end;
  1471. { Constant folding and other node transitions to
  1472. remove the typeconv node }
  1473. case left.nodetype of
  1474. niln :
  1475. begin
  1476. { nil to ordinal node }
  1477. if (resulttype.def.deftype=orddef) then
  1478. begin
  1479. hp:=cordconstnode.create(0,resulttype,true);
  1480. result:=hp;
  1481. exit;
  1482. end
  1483. else
  1484. { fold nil to any pointer type }
  1485. if (resulttype.def.deftype=pointerdef) then
  1486. begin
  1487. hp:=cnilnode.create;
  1488. hp.resulttype:=resulttype;
  1489. result:=hp;
  1490. exit;
  1491. end
  1492. else
  1493. { remove typeconv after niln, but not when the result is a
  1494. methodpointer. The typeconv of the methodpointer will then
  1495. take care of updateing size of niln to OS_64 }
  1496. if not((resulttype.def.deftype=procvardef) and
  1497. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1498. begin
  1499. left.resulttype:=resulttype;
  1500. result:=left;
  1501. left:=nil;
  1502. exit;
  1503. end;
  1504. end;
  1505. ordconstn :
  1506. begin
  1507. { ordinal contants can be directly converted }
  1508. { but not char to char because it is a widechar to char or via versa }
  1509. { which needs extra code to do the code page transistion }
  1510. { constant ordinal to pointer }
  1511. if (resulttype.def.deftype=pointerdef) and
  1512. (convtype<>tc_cchar_2_pchar) then
  1513. begin
  1514. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1515. result:=hp;
  1516. exit;
  1517. end
  1518. else if is_ordinal(resulttype.def) and
  1519. not(convtype=tc_char_2_char) then
  1520. begin
  1521. { replace the resulttype and recheck the range }
  1522. left.resulttype:=resulttype;
  1523. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1524. result:=left;
  1525. left:=nil;
  1526. exit;
  1527. end;
  1528. end;
  1529. pointerconstn :
  1530. begin
  1531. { pointerconstn to any pointer is folded too }
  1532. if (resulttype.def.deftype=pointerdef) then
  1533. begin
  1534. left.resulttype:=resulttype;
  1535. result:=left;
  1536. left:=nil;
  1537. exit;
  1538. end
  1539. { constant pointer to ordinal }
  1540. else if is_ordinal(resulttype.def) then
  1541. begin
  1542. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1543. resulttype,not(nf_explicit in flags));
  1544. result:=hp;
  1545. exit;
  1546. end;
  1547. end;
  1548. end;
  1549. { now call the resulttype helper to do constant folding }
  1550. result:=resulttype_call_helper(convtype);
  1551. end;
  1552. procedure Ttypeconvnode.mark_write;
  1553. begin
  1554. left.mark_write;
  1555. end;
  1556. function ttypeconvnode.first_cord_to_pointer : tnode;
  1557. begin
  1558. result:=nil;
  1559. internalerror(200104043);
  1560. end;
  1561. function ttypeconvnode.first_int_to_int : tnode;
  1562. begin
  1563. first_int_to_int:=nil;
  1564. expectloc:=left.expectloc;
  1565. if not is_void(left.resulttype.def) then
  1566. begin
  1567. if (left.expectloc<>LOC_REGISTER) and
  1568. (resulttype.def.size>left.resulttype.def.size) then
  1569. expectloc:=LOC_REGISTER
  1570. else
  1571. if (left.expectloc=LOC_CREGISTER) and
  1572. (resulttype.def.size<left.resulttype.def.size) then
  1573. expectloc:=LOC_REGISTER;
  1574. end;
  1575. {$ifndef cpu64bit}
  1576. if is_64bit(resulttype.def) then
  1577. registersint:=max(registersint,2)
  1578. else
  1579. {$endif cpu64bit}
  1580. registersint:=max(registersint,1);
  1581. end;
  1582. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1583. begin
  1584. result:=nil;
  1585. registersint:=1;
  1586. expectloc:=LOC_REGISTER;
  1587. end;
  1588. function ttypeconvnode.first_cstring_to_int : tnode;
  1589. begin
  1590. result:=nil;
  1591. internalerror(200510014);
  1592. end;
  1593. function ttypeconvnode.first_string_to_chararray : tnode;
  1594. begin
  1595. first_string_to_chararray:=nil;
  1596. expectloc:=left.expectloc;
  1597. end;
  1598. function ttypeconvnode.first_char_to_string : tnode;
  1599. begin
  1600. first_char_to_string:=nil;
  1601. expectloc:=LOC_REFERENCE;
  1602. end;
  1603. function ttypeconvnode.first_nothing : tnode;
  1604. begin
  1605. first_nothing:=nil;
  1606. end;
  1607. function ttypeconvnode.first_array_to_pointer : tnode;
  1608. begin
  1609. first_array_to_pointer:=nil;
  1610. if registersint<1 then
  1611. registersint:=1;
  1612. expectloc:=LOC_REGISTER;
  1613. end;
  1614. function ttypeconvnode.first_int_to_real: tnode;
  1615. var
  1616. fname: string[32];
  1617. typname : string[12];
  1618. begin
  1619. { Get the type name }
  1620. { Normally the typename should be one of the following:
  1621. single, double - carl
  1622. }
  1623. typname := lower(pbestrealtype^.def.gettypename);
  1624. { converting a 64bit integer to a float requires a helper }
  1625. if is_64bit(left.resulttype.def) then
  1626. begin
  1627. if is_signed(left.resulttype.def) then
  1628. fname := 'fpc_int64_to_'+typname
  1629. else
  1630. {$warning generic conversion from int to float does not support unsigned integers}
  1631. fname := 'fpc_int64_to_'+typname;
  1632. result := ccallnode.createintern(fname,ccallparanode.create(
  1633. left,nil));
  1634. left:=nil;
  1635. firstpass(result);
  1636. exit;
  1637. end
  1638. else
  1639. { other integers are supposed to be 32 bit }
  1640. begin
  1641. {$warning generic conversion from int to float does not support unsigned integers}
  1642. if is_signed(left.resulttype.def) then
  1643. fname := 'fpc_longint_to_'+typname
  1644. else
  1645. fname := 'fpc_longint_to_'+typname;
  1646. result := ccallnode.createintern(fname,ccallparanode.create(
  1647. left,nil));
  1648. left:=nil;
  1649. firstpass(result);
  1650. exit;
  1651. end;
  1652. end;
  1653. function ttypeconvnode.first_real_to_real : tnode;
  1654. begin
  1655. {$ifdef cpufpemu}
  1656. if cs_fp_emulation in aktmoduleswitches then
  1657. begin
  1658. if target_info.system in system_wince then
  1659. begin
  1660. case tfloatdef(left.resulttype.def).typ of
  1661. s32real:
  1662. case tfloatdef(resulttype.def).typ of
  1663. s64real:
  1664. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1665. else
  1666. internalerror(2005082704);
  1667. end;
  1668. s64real:
  1669. case tfloatdef(resulttype.def).typ of
  1670. s32real:
  1671. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1672. else
  1673. internalerror(2005082703);
  1674. end;
  1675. else
  1676. internalerror(2005082702);
  1677. end;
  1678. left:=nil;
  1679. firstpass(result);
  1680. exit;
  1681. end
  1682. else
  1683. begin
  1684. {!! FIXME }
  1685. internalerror(2005082701);
  1686. end;
  1687. end
  1688. else
  1689. {$endif cpufpemu}
  1690. begin
  1691. first_real_to_real:=nil;
  1692. { comp isn't a floating type }
  1693. if registersfpu<1 then
  1694. registersfpu:=1;
  1695. expectloc:=LOC_FPUREGISTER;
  1696. end;
  1697. end;
  1698. function ttypeconvnode.first_pointer_to_array : tnode;
  1699. begin
  1700. first_pointer_to_array:=nil;
  1701. if registersint<1 then
  1702. registersint:=1;
  1703. expectloc:=LOC_REFERENCE;
  1704. end;
  1705. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1706. begin
  1707. first_cchar_to_pchar:=nil;
  1708. internalerror(200104021);
  1709. end;
  1710. function ttypeconvnode.first_bool_to_int : tnode;
  1711. begin
  1712. first_bool_to_int:=nil;
  1713. { byte(boolean) or word(wordbool) or longint(longbool) must
  1714. be accepted for var parameters }
  1715. if (nf_explicit in flags) and
  1716. (left.resulttype.def.size=resulttype.def.size) and
  1717. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1718. exit;
  1719. { when converting to 64bit, first convert to a 32bit int and then }
  1720. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1721. if resulttype.def.size > sizeof(aint) then
  1722. begin
  1723. result := ctypeconvnode.create_internal(left,u32inttype);
  1724. result := ctypeconvnode.create(result,resulttype);
  1725. left := nil;
  1726. firstpass(result);
  1727. exit;
  1728. end;
  1729. expectloc:=LOC_REGISTER;
  1730. if registersint<1 then
  1731. registersint:=1;
  1732. end;
  1733. function ttypeconvnode.first_int_to_bool : tnode;
  1734. begin
  1735. first_int_to_bool:=nil;
  1736. { byte(boolean) or word(wordbool) or longint(longbool) must
  1737. be accepted for var parameters }
  1738. if (nf_explicit in flags) and
  1739. (left.resulttype.def.size=resulttype.def.size) and
  1740. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1741. exit;
  1742. expectloc:=LOC_REGISTER;
  1743. { need if bool to bool !!
  1744. not very nice !!
  1745. insertypeconv(left,s32inttype);
  1746. left.explizit:=true;
  1747. firstpass(left); }
  1748. if registersint<1 then
  1749. registersint:=1;
  1750. end;
  1751. function ttypeconvnode.first_bool_to_bool : tnode;
  1752. begin
  1753. first_bool_to_bool:=nil;
  1754. expectloc:=LOC_REGISTER;
  1755. if registersint<1 then
  1756. registersint:=1;
  1757. end;
  1758. function ttypeconvnode.first_char_to_char : tnode;
  1759. begin
  1760. first_char_to_char:=first_int_to_int;
  1761. end;
  1762. function ttypeconvnode.first_proc_to_procvar : tnode;
  1763. begin
  1764. first_proc_to_procvar:=nil;
  1765. if tabstractprocdef(resulttype.def).is_addressonly then
  1766. begin
  1767. registersint:=left.registersint;
  1768. if registersint<1 then
  1769. registersint:=1;
  1770. expectloc:=LOC_REGISTER;
  1771. end
  1772. else
  1773. begin
  1774. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1775. CGMessage(parser_e_illegal_expression);
  1776. registersint:=left.registersint;
  1777. expectloc:=left.expectloc
  1778. end
  1779. end;
  1780. function ttypeconvnode.first_load_smallset : tnode;
  1781. var
  1782. srsym: ttypesym;
  1783. p: tcallparanode;
  1784. begin
  1785. if not searchsystype('FPC_SMALL_SET',srsym) then
  1786. internalerror(200108313);
  1787. p := ccallparanode.create(left,nil);
  1788. { reused }
  1789. left := nil;
  1790. { convert parameter explicitely to fpc_small_set }
  1791. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1792. { create call, adjust resulttype }
  1793. result :=
  1794. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1795. firstpass(result);
  1796. end;
  1797. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1798. begin
  1799. first_ansistring_to_pchar:=nil;
  1800. expectloc:=LOC_REGISTER;
  1801. if registersint<1 then
  1802. registersint:=1;
  1803. end;
  1804. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1805. begin
  1806. first_arrayconstructor_to_set:=nil;
  1807. internalerror(200104022);
  1808. end;
  1809. function ttypeconvnode.first_class_to_intf : tnode;
  1810. begin
  1811. first_class_to_intf:=nil;
  1812. expectloc:=LOC_REGISTER;
  1813. if registersint<1 then
  1814. registersint:=1;
  1815. end;
  1816. function ttypeconvnode._first_int_to_int : tnode;
  1817. begin
  1818. result:=first_int_to_int;
  1819. end;
  1820. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1821. begin
  1822. result:=first_cstring_to_pchar;
  1823. end;
  1824. function ttypeconvnode._first_cstring_to_int : tnode;
  1825. begin
  1826. result:=first_cstring_to_int;
  1827. end;
  1828. function ttypeconvnode._first_string_to_chararray : tnode;
  1829. begin
  1830. result:=first_string_to_chararray;
  1831. end;
  1832. function ttypeconvnode._first_char_to_string : tnode;
  1833. begin
  1834. result:=first_char_to_string;
  1835. end;
  1836. function ttypeconvnode._first_nothing : tnode;
  1837. begin
  1838. result:=first_nothing;
  1839. end;
  1840. function ttypeconvnode._first_array_to_pointer : tnode;
  1841. begin
  1842. result:=first_array_to_pointer;
  1843. end;
  1844. function ttypeconvnode._first_int_to_real : tnode;
  1845. begin
  1846. result:=first_int_to_real;
  1847. end;
  1848. function ttypeconvnode._first_real_to_real : tnode;
  1849. begin
  1850. result:=first_real_to_real;
  1851. end;
  1852. function ttypeconvnode._first_pointer_to_array : tnode;
  1853. begin
  1854. result:=first_pointer_to_array;
  1855. end;
  1856. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1857. begin
  1858. result:=first_cchar_to_pchar;
  1859. end;
  1860. function ttypeconvnode._first_bool_to_int : tnode;
  1861. begin
  1862. result:=first_bool_to_int;
  1863. end;
  1864. function ttypeconvnode._first_int_to_bool : tnode;
  1865. begin
  1866. result:=first_int_to_bool;
  1867. end;
  1868. function ttypeconvnode._first_bool_to_bool : tnode;
  1869. begin
  1870. result:=first_bool_to_bool;
  1871. end;
  1872. function ttypeconvnode._first_proc_to_procvar : tnode;
  1873. begin
  1874. result:=first_proc_to_procvar;
  1875. end;
  1876. function ttypeconvnode._first_load_smallset : tnode;
  1877. begin
  1878. result:=first_load_smallset;
  1879. end;
  1880. function ttypeconvnode._first_cord_to_pointer : tnode;
  1881. begin
  1882. result:=first_cord_to_pointer;
  1883. end;
  1884. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1885. begin
  1886. result:=first_ansistring_to_pchar;
  1887. end;
  1888. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1889. begin
  1890. result:=first_arrayconstructor_to_set;
  1891. end;
  1892. function ttypeconvnode._first_class_to_intf : tnode;
  1893. begin
  1894. result:=first_class_to_intf;
  1895. end;
  1896. function ttypeconvnode._first_char_to_char : tnode;
  1897. begin
  1898. result:=first_char_to_char;
  1899. end;
  1900. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1901. const
  1902. firstconvert : array[tconverttype] of pointer = (
  1903. nil, { none }
  1904. @ttypeconvnode._first_nothing, {equal}
  1905. @ttypeconvnode._first_nothing, {not_possible}
  1906. nil, { removed in resulttype_string_to_string }
  1907. @ttypeconvnode._first_char_to_string,
  1908. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1909. nil, { removed in resulttype_chararray_to_string }
  1910. @ttypeconvnode._first_cchar_to_pchar,
  1911. @ttypeconvnode._first_cstring_to_pchar,
  1912. @ttypeconvnode._first_cstring_to_int,
  1913. @ttypeconvnode._first_ansistring_to_pchar,
  1914. @ttypeconvnode._first_string_to_chararray,
  1915. nil, { removed in resulttype_chararray_to_string }
  1916. @ttypeconvnode._first_array_to_pointer,
  1917. @ttypeconvnode._first_pointer_to_array,
  1918. @ttypeconvnode._first_int_to_int,
  1919. @ttypeconvnode._first_int_to_bool,
  1920. @ttypeconvnode._first_bool_to_bool,
  1921. @ttypeconvnode._first_bool_to_int,
  1922. @ttypeconvnode._first_real_to_real,
  1923. @ttypeconvnode._first_int_to_real,
  1924. nil, { removed in resulttype_real_to_currency }
  1925. @ttypeconvnode._first_proc_to_procvar,
  1926. @ttypeconvnode._first_arrayconstructor_to_set,
  1927. @ttypeconvnode._first_load_smallset,
  1928. @ttypeconvnode._first_cord_to_pointer,
  1929. @ttypeconvnode._first_nothing,
  1930. @ttypeconvnode._first_nothing,
  1931. @ttypeconvnode._first_class_to_intf,
  1932. @ttypeconvnode._first_char_to_char,
  1933. @ttypeconvnode._first_nothing,
  1934. @ttypeconvnode._first_nothing,
  1935. nil,
  1936. nil,
  1937. nil,
  1938. nil,
  1939. nil,
  1940. nil,
  1941. nil,
  1942. nil
  1943. );
  1944. type
  1945. tprocedureofobject = function : tnode of object;
  1946. var
  1947. r : packed record
  1948. proc : pointer;
  1949. obj : pointer;
  1950. end;
  1951. begin
  1952. { this is a little bit dirty but it works }
  1953. { and should be quite portable too }
  1954. r.proc:=firstconvert[c];
  1955. r.obj:=self;
  1956. if not assigned(r.proc) then
  1957. internalerror(200312081);
  1958. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1959. end;
  1960. function ttypeconvnode.pass_1 : tnode;
  1961. begin
  1962. result:=nil;
  1963. firstpass(left);
  1964. if codegenerror then
  1965. exit;
  1966. { load the value_str from the left part }
  1967. registersint:=left.registersint;
  1968. registersfpu:=left.registersfpu;
  1969. {$ifdef SUPPORT_MMX}
  1970. registersmmx:=left.registersmmx;
  1971. {$endif}
  1972. expectloc:=left.expectloc;
  1973. result:=first_call_helper(convtype);
  1974. end;
  1975. function ttypeconvnode.assign_allowed:boolean;
  1976. begin
  1977. result:=(convtype=tc_equal) or
  1978. { typecasting from void is always allowed }
  1979. is_void(left.resulttype.def) or
  1980. (left.resulttype.def.deftype=formaldef) or
  1981. { int 2 int with same size reuses same location, or for
  1982. tp7 mode also allow size < orignal size }
  1983. (
  1984. (convtype=tc_int_2_int) and
  1985. (
  1986. (resulttype.def.size=left.resulttype.def.size) or
  1987. ((m_tp7 in aktmodeswitches) and
  1988. (resulttype.def.size<left.resulttype.def.size))
  1989. )
  1990. ) or
  1991. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1992. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1993. (nf_explicit in flags) and
  1994. (resulttype.def.size=left.resulttype.def.size));
  1995. { When using only a part of the value it can't be in a register since
  1996. that will load the value in a new register first }
  1997. if (resulttype.def.size<left.resulttype.def.size) then
  1998. make_not_regable(left);
  1999. end;
  2000. function ttypeconvnode.docompare(p: tnode) : boolean;
  2001. begin
  2002. docompare :=
  2003. inherited docompare(p) and
  2004. (convtype = ttypeconvnode(p).convtype);
  2005. end;
  2006. procedure ttypeconvnode._second_int_to_int;
  2007. begin
  2008. second_int_to_int;
  2009. end;
  2010. procedure ttypeconvnode._second_string_to_string;
  2011. begin
  2012. second_string_to_string;
  2013. end;
  2014. procedure ttypeconvnode._second_cstring_to_pchar;
  2015. begin
  2016. second_cstring_to_pchar;
  2017. end;
  2018. procedure ttypeconvnode._second_cstring_to_int;
  2019. begin
  2020. second_cstring_to_int;
  2021. end;
  2022. procedure ttypeconvnode._second_string_to_chararray;
  2023. begin
  2024. second_string_to_chararray;
  2025. end;
  2026. procedure ttypeconvnode._second_array_to_pointer;
  2027. begin
  2028. second_array_to_pointer;
  2029. end;
  2030. procedure ttypeconvnode._second_pointer_to_array;
  2031. begin
  2032. second_pointer_to_array;
  2033. end;
  2034. procedure ttypeconvnode._second_chararray_to_string;
  2035. begin
  2036. second_chararray_to_string;
  2037. end;
  2038. procedure ttypeconvnode._second_char_to_string;
  2039. begin
  2040. second_char_to_string;
  2041. end;
  2042. procedure ttypeconvnode._second_int_to_real;
  2043. begin
  2044. second_int_to_real;
  2045. end;
  2046. procedure ttypeconvnode._second_real_to_real;
  2047. begin
  2048. second_real_to_real;
  2049. end;
  2050. procedure ttypeconvnode._second_cord_to_pointer;
  2051. begin
  2052. second_cord_to_pointer;
  2053. end;
  2054. procedure ttypeconvnode._second_proc_to_procvar;
  2055. begin
  2056. second_proc_to_procvar;
  2057. end;
  2058. procedure ttypeconvnode._second_bool_to_int;
  2059. begin
  2060. second_bool_to_int;
  2061. end;
  2062. procedure ttypeconvnode._second_int_to_bool;
  2063. begin
  2064. second_int_to_bool;
  2065. end;
  2066. procedure ttypeconvnode._second_bool_to_bool;
  2067. begin
  2068. second_bool_to_bool;
  2069. end;
  2070. procedure ttypeconvnode._second_load_smallset;
  2071. begin
  2072. second_load_smallset;
  2073. end;
  2074. procedure ttypeconvnode._second_ansistring_to_pchar;
  2075. begin
  2076. second_ansistring_to_pchar;
  2077. end;
  2078. procedure ttypeconvnode._second_class_to_intf;
  2079. begin
  2080. second_class_to_intf;
  2081. end;
  2082. procedure ttypeconvnode._second_char_to_char;
  2083. begin
  2084. second_char_to_char;
  2085. end;
  2086. procedure ttypeconvnode._second_nothing;
  2087. begin
  2088. second_nothing;
  2089. end;
  2090. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2091. const
  2092. secondconvert : array[tconverttype] of pointer = (
  2093. @ttypeconvnode._second_nothing, {none}
  2094. @ttypeconvnode._second_nothing, {equal}
  2095. @ttypeconvnode._second_nothing, {not_possible}
  2096. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2097. @ttypeconvnode._second_char_to_string,
  2098. @ttypeconvnode._second_nothing, {char_to_charray}
  2099. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2100. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2101. @ttypeconvnode._second_cstring_to_pchar,
  2102. @ttypeconvnode._second_cstring_to_int,
  2103. @ttypeconvnode._second_ansistring_to_pchar,
  2104. @ttypeconvnode._second_string_to_chararray,
  2105. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2106. @ttypeconvnode._second_array_to_pointer,
  2107. @ttypeconvnode._second_pointer_to_array,
  2108. @ttypeconvnode._second_int_to_int,
  2109. @ttypeconvnode._second_int_to_bool,
  2110. @ttypeconvnode._second_bool_to_bool,
  2111. @ttypeconvnode._second_bool_to_int,
  2112. @ttypeconvnode._second_real_to_real,
  2113. @ttypeconvnode._second_int_to_real,
  2114. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2115. @ttypeconvnode._second_proc_to_procvar,
  2116. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2117. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2118. @ttypeconvnode._second_cord_to_pointer,
  2119. @ttypeconvnode._second_nothing, { interface 2 string }
  2120. @ttypeconvnode._second_nothing, { interface 2 guid }
  2121. @ttypeconvnode._second_class_to_intf,
  2122. @ttypeconvnode._second_char_to_char,
  2123. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2124. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2125. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2126. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2127. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2128. @ttypeconvnode._second_nothing, { variant_2_enum }
  2129. @ttypeconvnode._second_nothing, { enum_2_variant }
  2130. @ttypeconvnode._second_nothing, { variant_2_interface }
  2131. @ttypeconvnode._second_nothing, { interface_2_variant }
  2132. @ttypeconvnode._second_nothing { array_2_dynarray }
  2133. );
  2134. type
  2135. tprocedureofobject = procedure of object;
  2136. var
  2137. r : packed record
  2138. proc : pointer;
  2139. obj : pointer;
  2140. end;
  2141. begin
  2142. { this is a little bit dirty but it works }
  2143. { and should be quite portable too }
  2144. r.proc:=secondconvert[c];
  2145. r.obj:=self;
  2146. tprocedureofobject(r)();
  2147. end;
  2148. {*****************************************************************************
  2149. TISNODE
  2150. *****************************************************************************}
  2151. constructor tisnode.create(l,r : tnode);
  2152. begin
  2153. inherited create(isn,l,r);
  2154. end;
  2155. function tisnode.det_resulttype:tnode;
  2156. var
  2157. paras: tcallparanode;
  2158. begin
  2159. result:=nil;
  2160. resulttypepass(left);
  2161. resulttypepass(right);
  2162. set_varstate(left,vs_used,[vsf_must_be_valid]);
  2163. set_varstate(right,vs_used,[vsf_must_be_valid]);
  2164. if codegenerror then
  2165. exit;
  2166. if (right.resulttype.def.deftype=classrefdef) then
  2167. begin
  2168. { left must be a class }
  2169. if is_class(left.resulttype.def) then
  2170. begin
  2171. { the operands must be related }
  2172. if (not(tobjectdef(left.resulttype.def).is_related(
  2173. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2174. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2175. tobjectdef(left.resulttype.def)))) then
  2176. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2177. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2178. end
  2179. else
  2180. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2181. { call fpc_do_is helper }
  2182. paras := ccallparanode.create(
  2183. left,
  2184. ccallparanode.create(
  2185. right,nil));
  2186. result := ccallnode.createintern('fpc_do_is',paras);
  2187. left := nil;
  2188. right := nil;
  2189. end
  2190. else if is_interface(right.resulttype.def) then
  2191. begin
  2192. { left is a class }
  2193. if is_class(left.resulttype.def) then
  2194. begin
  2195. { the operands must be related }
  2196. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2197. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2198. CGMessage2(type_e_classes_not_related,
  2199. FullTypeName(left.resulttype.def,right.resulttype.def),
  2200. FullTypeName(right.resulttype.def,left.resulttype.def))
  2201. end
  2202. { left is an interface }
  2203. else if is_interface(left.resulttype.def) then
  2204. begin
  2205. { the operands must be related }
  2206. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2207. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2208. CGMessage2(type_e_classes_not_related,
  2209. FullTypeName(left.resulttype.def,right.resulttype.def),
  2210. FullTypeName(right.resulttype.def,left.resulttype.def));
  2211. end
  2212. else
  2213. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2214. { call fpc_do_is helper }
  2215. paras := ccallparanode.create(
  2216. left,
  2217. ccallparanode.create(
  2218. right,nil));
  2219. result := ccallnode.createintern('fpc_do_is',paras);
  2220. left := nil;
  2221. right := nil;
  2222. end
  2223. else
  2224. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2225. resulttype:=booltype;
  2226. end;
  2227. function tisnode.pass_1 : tnode;
  2228. begin
  2229. internalerror(200204254);
  2230. result:=nil;
  2231. end;
  2232. { dummy pass_2, it will never be called, but we need one since }
  2233. { you can't instantiate an abstract class }
  2234. procedure tisnode.pass_2;
  2235. begin
  2236. end;
  2237. {*****************************************************************************
  2238. TASNODE
  2239. *****************************************************************************}
  2240. constructor tasnode.create(l,r : tnode);
  2241. begin
  2242. inherited create(asn,l,r);
  2243. call := nil;
  2244. end;
  2245. destructor tasnode.destroy;
  2246. begin
  2247. call.free;
  2248. inherited destroy;
  2249. end;
  2250. function tasnode.det_resulttype:tnode;
  2251. var
  2252. hp : tnode;
  2253. begin
  2254. result:=nil;
  2255. resulttypepass(right);
  2256. resulttypepass(left);
  2257. set_varstate(right,vs_used,[vsf_must_be_valid]);
  2258. set_varstate(left,vs_used,[vsf_must_be_valid]);
  2259. if codegenerror then
  2260. exit;
  2261. if (right.resulttype.def.deftype=classrefdef) then
  2262. begin
  2263. { left must be a class }
  2264. if is_class(left.resulttype.def) then
  2265. begin
  2266. { the operands must be related }
  2267. if (not(tobjectdef(left.resulttype.def).is_related(
  2268. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2269. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2270. tobjectdef(left.resulttype.def)))) then
  2271. CGMessage2(type_e_classes_not_related,
  2272. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2273. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2274. end
  2275. else
  2276. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2277. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2278. end
  2279. else if is_interface(right.resulttype.def) then
  2280. begin
  2281. { left is a class }
  2282. if not(is_class(left.resulttype.def) or
  2283. is_interface(left.resulttype.def)) then
  2284. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2285. resulttype:=right.resulttype;
  2286. { load the GUID of the interface }
  2287. if (right.nodetype=typen) then
  2288. begin
  2289. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2290. begin
  2291. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2292. right.free;
  2293. right:=hp;
  2294. end
  2295. else
  2296. internalerror(200206282);
  2297. resulttypepass(right);
  2298. end;
  2299. end
  2300. else
  2301. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2302. end;
  2303. function tasnode.getcopy: tnode;
  2304. begin
  2305. result := inherited getcopy;
  2306. if assigned(call) then
  2307. tasnode(result).call := call.getcopy
  2308. else
  2309. tasnode(result).call := nil;
  2310. end;
  2311. function tasnode.pass_1 : tnode;
  2312. var
  2313. procname: string;
  2314. begin
  2315. result:=nil;
  2316. if not assigned(call) then
  2317. begin
  2318. if is_class(left.resulttype.def) and
  2319. (right.resulttype.def.deftype=classrefdef) then
  2320. call := ccallnode.createinternres('fpc_do_as',
  2321. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2322. resulttype)
  2323. else
  2324. begin
  2325. if is_class(left.resulttype.def) then
  2326. procname := 'fpc_class_as_intf'
  2327. else
  2328. procname := 'fpc_intf_as';
  2329. call := ccallnode.createinternres(procname,
  2330. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2331. resulttype);
  2332. end;
  2333. left := nil;
  2334. right := nil;
  2335. firstpass(call);
  2336. if codegenerror then
  2337. exit;
  2338. expectloc:=call.expectloc;
  2339. registersint:=call.registersint;
  2340. registersfpu:=call.registersfpu;
  2341. {$ifdef SUPPORT_MMX}
  2342. registersmmx:=call.registersmmx;
  2343. {$endif SUPPORT_MMX}
  2344. end;
  2345. end;
  2346. begin
  2347. ctypeconvnode:=ttypeconvnode;
  2348. casnode:=tasnode;
  2349. cisnode:=tisnode;
  2350. end.