ncnv.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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. else
  950. if is_pchar(resulttype.def) and
  951. is_widestring(left.resulttype.def) then
  952. inserttypeconv(left,cansistringtype);
  953. end;
  954. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  955. var
  956. fcc : cardinal;
  957. pb : pbyte;
  958. begin
  959. result:=nil;
  960. if left.nodetype<>stringconstn then
  961. internalerror(200510012);
  962. if tstringconstnode(left).len=4 then
  963. begin
  964. pb:=pbyte(tstringconstnode(left).value_str);
  965. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  966. result:=cordconstnode.create(fcc,u32inttype,false);
  967. end
  968. else
  969. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  970. end;
  971. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  972. var
  973. hp : tnode;
  974. begin
  975. result:=nil;
  976. if left.nodetype<>arrayconstructorn then
  977. internalerror(5546);
  978. { remove typeconv node }
  979. hp:=left;
  980. left:=nil;
  981. { create a set constructor tree }
  982. arrayconstructor_to_set(hp);
  983. result:=hp;
  984. end;
  985. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  986. begin
  987. result := ccallnode.createinternres(
  988. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  989. ccallparanode.create(left,nil),resulttype);
  990. left := nil;
  991. end;
  992. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  993. begin
  994. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  995. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  996. end;
  997. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  998. begin
  999. { a dynamic array is a pointer to an array, so to convert it to }
  1000. { an open array, we have to dereference it (JM) }
  1001. result := ctypeconvnode.create_internal(left,voidpointertype);
  1002. resulttypepass(result);
  1003. { left is reused }
  1004. left := nil;
  1005. result := cderefnode.create(result);
  1006. include(result.flags,nf_no_checkpointer);
  1007. result.resulttype := resulttype;
  1008. end;
  1009. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  1010. begin
  1011. result := ccallnode.createinternres(
  1012. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  1013. ccallparanode.create(left,nil),resulttype);
  1014. left := nil;
  1015. end;
  1016. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  1017. begin
  1018. result := ccallnode.createinternres(
  1019. 'fpc_variant_to_dynarray',
  1020. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1021. ccallparanode.create(left,nil)
  1022. ),resulttype);
  1023. resulttypepass(result);
  1024. left:=nil;
  1025. end;
  1026. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1027. begin
  1028. result := ccallnode.createinternres(
  1029. 'fpc_dynarray_to_variant',
  1030. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  1031. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1032. ),resulttype);
  1033. resulttypepass(result);
  1034. left:=nil;
  1035. end;
  1036. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1037. begin
  1038. result := ccallnode.createinternres(
  1039. 'fpc_variant_to_interface',
  1040. ccallparanode.create(left,nil)
  1041. ,resulttype);
  1042. resulttypepass(result);
  1043. left:=nil;
  1044. end;
  1045. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1046. begin
  1047. result := ccallnode.createinternres(
  1048. 'fpc_interface_to_variant',
  1049. ccallparanode.create(left,nil)
  1050. ,resulttype);
  1051. resulttypepass(result);
  1052. left:=nil;
  1053. end;
  1054. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1055. begin
  1056. result := ctypeconvnode.create_internal(left,sinttype);
  1057. result := ctypeconvnode.create_internal(result,resulttype);
  1058. resulttypepass(result);
  1059. { left is reused }
  1060. left := nil;
  1061. end;
  1062. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1063. begin
  1064. result := ctypeconvnode.create_internal(left,sinttype);
  1065. result := ctypeconvnode.create_internal(result,cvarianttype);
  1066. resulttypepass(result);
  1067. { left is reused }
  1068. left := nil;
  1069. end;
  1070. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1071. var
  1072. newstatement : tstatementnode;
  1073. temp : ttempcreatenode;
  1074. temp2 : ttempcreatenode;
  1075. begin
  1076. { create statements with call to getmem+initialize }
  1077. result:=internalstatements(newstatement);
  1078. { create temp for result }
  1079. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1080. addstatement(newstatement,temp);
  1081. { get temp for array of lengths }
  1082. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1083. addstatement(newstatement,temp2);
  1084. { one dimensional }
  1085. addstatement(newstatement,cassignmentnode.create(
  1086. ctemprefnode.create_offset(temp2,0),
  1087. cordconstnode.create
  1088. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1089. { create call to fpc_dynarr_setlength }
  1090. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1091. ccallparanode.create(caddrnode.create_internal
  1092. (ctemprefnode.create(temp2)),
  1093. ccallparanode.create(cordconstnode.create
  1094. (1,s32inttype,true),
  1095. ccallparanode.create(caddrnode.create_internal
  1096. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1097. ccallparanode.create(
  1098. ctypeconvnode.create_internal(
  1099. ctemprefnode.create(temp),voidpointertype),
  1100. nil))))
  1101. ));
  1102. addstatement(newstatement,ctempdeletenode.create(temp2));
  1103. { copy ... }
  1104. addstatement(newstatement,cassignmentnode.create(
  1105. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1106. left
  1107. ));
  1108. { left is reused }
  1109. left:=nil;
  1110. { the last statement should return the value as
  1111. location and type, this is done be referencing the
  1112. temp and converting it first from a persistent temp to
  1113. normal temp }
  1114. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1115. addstatement(newstatement,ctemprefnode.create(temp));
  1116. end;
  1117. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1118. var
  1119. newparast : tsymtable absolute arg;
  1120. vs : tparavarsym;
  1121. begin
  1122. if tsym(p).typ<>paravarsym then
  1123. exit;
  1124. with tparavarsym(p) do
  1125. begin
  1126. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1127. vs.defaultconstsym:=defaultconstsym;
  1128. newparast.insert(vs);
  1129. end;
  1130. end;
  1131. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1132. var
  1133. pd : tabstractprocdef;
  1134. begin
  1135. result:=nil;
  1136. pd:=tabstractprocdef(left.resulttype.def);
  1137. { create procvardef }
  1138. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1139. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1140. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1141. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1142. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1143. { method ? then set the methodpointer flag }
  1144. if (pd.owner.symtabletype=objectsymtable) then
  1145. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1146. { was it a local procedure? }
  1147. if (pd.owner.symtabletype=localsymtable) then
  1148. include(tprocvardef(resulttype.def).procoptions,po_local);
  1149. { only need the address of the method? this is needed
  1150. for @tobject.create. In this case there will be a loadn without
  1151. a methodpointer. }
  1152. if (left.nodetype=loadn) and
  1153. not assigned(tloadnode(left).left) then
  1154. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1155. { Add parameters use only references, we don't need to keep the
  1156. parast. We use the parast from the original function to calculate
  1157. our parameter data and reset it afterwards }
  1158. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1159. tprocvardef(resulttype.def).calcparas;
  1160. end;
  1161. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1162. const
  1163. resulttypeconvert : array[tconverttype] of pointer = (
  1164. {none} nil,
  1165. {equal} nil,
  1166. {not_possible} nil,
  1167. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1168. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1169. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1170. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1171. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1172. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1173. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1174. { ansistring_2_pchar } nil,
  1175. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1176. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1177. { array_2_pointer } nil,
  1178. { pointer_2_array } nil,
  1179. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1180. { int_2_bool } nil,
  1181. { bool_2_bool } nil,
  1182. { bool_2_int } nil,
  1183. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1184. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1185. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1186. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1187. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1188. { load_smallset } nil,
  1189. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1190. { intf_2_string } nil,
  1191. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1192. { class_2_intf } nil,
  1193. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1194. { normal_2_smallset} nil,
  1195. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1196. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1197. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1198. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1199. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1200. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1201. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1202. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1203. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1204. );
  1205. type
  1206. tprocedureofobject = function : tnode of object;
  1207. var
  1208. r : packed record
  1209. proc : pointer;
  1210. obj : pointer;
  1211. end;
  1212. begin
  1213. result:=nil;
  1214. { this is a little bit dirty but it works }
  1215. { and should be quite portable too }
  1216. r.proc:=resulttypeconvert[c];
  1217. r.obj:=self;
  1218. if assigned(r.proc) then
  1219. result:=tprocedureofobject(r)();
  1220. end;
  1221. function ttypeconvnode.det_resulttype:tnode;
  1222. var
  1223. htype : ttype;
  1224. hp : tnode;
  1225. currprocdef : tabstractprocdef;
  1226. aprocdef : tprocdef;
  1227. eq : tequaltype;
  1228. cdoptions : tcompare_defs_options;
  1229. begin
  1230. result:=nil;
  1231. resulttype:=totype;
  1232. resulttypepass(left);
  1233. if codegenerror then
  1234. exit;
  1235. { When absolute force tc_equal }
  1236. if (nf_absolute in flags) then
  1237. begin
  1238. convtype:=tc_equal;
  1239. if not(tstoreddef(resulttype.def).is_intregable) and
  1240. not(tstoreddef(resulttype.def).is_fpuregable) then
  1241. make_not_regable(left);
  1242. exit;
  1243. end;
  1244. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1245. convert on the procvar value. This is used to access the
  1246. fields of a methodpointer }
  1247. if not(nf_load_procvar in flags) and
  1248. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1249. maybe_call_procvar(left,true);
  1250. { convert array constructors to sets, because there is no conversion
  1251. possible for array constructors }
  1252. if (resulttype.def.deftype<>arraydef) and
  1253. is_array_constructor(left.resulttype.def) then
  1254. begin
  1255. arrayconstructor_to_set(left);
  1256. resulttypepass(left);
  1257. end;
  1258. if convtype=tc_none then
  1259. begin
  1260. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1261. if nf_explicit in flags then
  1262. include(cdoptions,cdo_explicit);
  1263. if nf_internal in flags then
  1264. include(cdoptions,cdo_internal);
  1265. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1266. case eq of
  1267. te_exact,
  1268. te_equal :
  1269. begin
  1270. { because is_equal only checks the basetype for sets we need to
  1271. check here if we are loading a smallset into a normalset }
  1272. if (resulttype.def.deftype=setdef) and
  1273. (left.resulttype.def.deftype=setdef) and
  1274. ((tsetdef(resulttype.def).settype = smallset) xor
  1275. (tsetdef(left.resulttype.def).settype = smallset)) then
  1276. begin
  1277. { constant sets can be converted by changing the type only }
  1278. if (left.nodetype=setconstn) then
  1279. begin
  1280. left.resulttype:=resulttype;
  1281. result:=left;
  1282. left:=nil;
  1283. exit;
  1284. end;
  1285. if (tsetdef(resulttype.def).settype <> smallset) then
  1286. convtype:=tc_load_smallset
  1287. else
  1288. convtype := tc_normal_2_smallset;
  1289. exit;
  1290. end
  1291. else
  1292. begin
  1293. { Only leave when there is no conversion to do.
  1294. We can still need to call a conversion routine,
  1295. like the routine to convert a stringconstnode }
  1296. if convtype in [tc_equal,tc_not_possible] then
  1297. begin
  1298. left.resulttype:=resulttype;
  1299. result:=left;
  1300. left:=nil;
  1301. exit;
  1302. end;
  1303. end;
  1304. end;
  1305. te_convert_l1,
  1306. te_convert_l2,
  1307. te_convert_l3 :
  1308. begin
  1309. { nothing to do }
  1310. end;
  1311. te_convert_operator :
  1312. begin
  1313. include(current_procinfo.flags,pi_do_call);
  1314. inc(aprocdef.procsym.refs);
  1315. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1316. { tell explicitly which def we must use !! (PM) }
  1317. tcallnode(hp).procdefinition:=aprocdef;
  1318. left:=nil;
  1319. result:=hp;
  1320. exit;
  1321. end;
  1322. te_incompatible :
  1323. begin
  1324. { Procedures have a resulttype.def of voiddef and functions of their
  1325. own resulttype.def. They will therefore always be incompatible with
  1326. a procvar. Because isconvertable cannot check for procedures we
  1327. use an extra check for them.}
  1328. if (left.nodetype=calln) and
  1329. (tcallnode(left).para_count=0) and
  1330. (resulttype.def.deftype=procvardef) and
  1331. (
  1332. (m_tp_procvar in aktmodeswitches) or
  1333. (m_mac_procvar in aktmodeswitches)
  1334. ) then
  1335. begin
  1336. if assigned(tcallnode(left).right) then
  1337. begin
  1338. { this is already a procvar, if it is really equal
  1339. is checked below }
  1340. convtype:=tc_equal;
  1341. hp:=tcallnode(left).right.getcopy;
  1342. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1343. end
  1344. else
  1345. begin
  1346. convtype:=tc_proc_2_procvar;
  1347. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1348. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1349. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1350. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1351. begin
  1352. if assigned(tcallnode(left).methodpointer) then
  1353. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1354. else
  1355. tloadnode(hp).set_mp(load_self_node);
  1356. end;
  1357. resulttypepass(hp);
  1358. end;
  1359. left.free;
  1360. left:=hp;
  1361. { Now check if the procedure we are going to assign to
  1362. the procvar, is compatible with the procvar's type }
  1363. if not(nf_explicit in flags) and
  1364. (proc_to_procvar_equal(currprocdef,tprocvardef(resulttype.def))=te_incompatible) then
  1365. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1366. exit;
  1367. end;
  1368. { Handle explicit type conversions }
  1369. if nf_explicit in flags then
  1370. begin
  1371. { do common tc_equal cast }
  1372. convtype:=tc_equal;
  1373. { ordinal constants can be resized to 1,2,4,8 bytes }
  1374. if (left.nodetype=ordconstn) then
  1375. begin
  1376. { Insert typeconv for ordinal to the correct size first on left, after
  1377. that the other conversion can be done }
  1378. htype.reset;
  1379. case longint(resulttype.def.size) of
  1380. 1 :
  1381. htype:=s8inttype;
  1382. 2 :
  1383. htype:=s16inttype;
  1384. 4 :
  1385. htype:=s32inttype;
  1386. 8 :
  1387. htype:=s64inttype;
  1388. end;
  1389. { we need explicit, because it can also be an enum }
  1390. if assigned(htype.def) then
  1391. inserttypeconv_internal(left,htype)
  1392. else
  1393. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1394. end;
  1395. { check if the result could be in a register }
  1396. if (not(tstoreddef(resulttype.def).is_intregable) and
  1397. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1398. ((left.resulttype.def.deftype = floatdef) and
  1399. (resulttype.def.deftype <> floatdef)) then
  1400. make_not_regable(left);
  1401. { class/interface to class/interface, with checkobject support }
  1402. if is_class_or_interface(resulttype.def) and
  1403. is_class_or_interface(left.resulttype.def) then
  1404. begin
  1405. { check if the types are related }
  1406. if not(nf_internal in flags) and
  1407. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1408. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1409. begin
  1410. { Give an error when typecasting class to interface, this is compatible
  1411. with delphi }
  1412. if is_interface(resulttype.def) and
  1413. not is_interface(left.resulttype.def) then
  1414. CGMessage2(type_e_classes_not_related,
  1415. FullTypeName(left.resulttype.def,resulttype.def),
  1416. FullTypeName(resulttype.def,left.resulttype.def))
  1417. else
  1418. CGMessage2(type_w_classes_not_related,
  1419. FullTypeName(left.resulttype.def,resulttype.def),
  1420. FullTypeName(resulttype.def,left.resulttype.def))
  1421. end;
  1422. { Add runtime check? }
  1423. if (cs_check_object in aktlocalswitches) then
  1424. begin
  1425. { we can translate the typeconvnode to 'as' when
  1426. typecasting to a class or interface }
  1427. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1428. left:=nil;
  1429. result:=hp;
  1430. exit;
  1431. end;
  1432. end
  1433. else
  1434. begin
  1435. { only if the same size or formal def }
  1436. if not(
  1437. (left.resulttype.def.deftype=formaldef) or
  1438. (
  1439. not(is_open_array(left.resulttype.def)) and
  1440. (left.resulttype.def.size=resulttype.def.size)
  1441. ) or
  1442. (
  1443. is_void(left.resulttype.def) and
  1444. (left.nodetype=derefn)
  1445. )
  1446. ) then
  1447. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1448. end;
  1449. end
  1450. else
  1451. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1452. end;
  1453. else
  1454. internalerror(200211231);
  1455. end;
  1456. end;
  1457. { Give hint or warning for unportable code, exceptions are
  1458. - typecasts from constants
  1459. - void }
  1460. if not(nf_internal in flags) and
  1461. (left.nodetype<>ordconstn) and
  1462. not(is_void(left.resulttype.def)) and
  1463. (((left.resulttype.def.deftype=orddef) and
  1464. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1465. ((resulttype.def.deftype=orddef) and
  1466. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1467. begin
  1468. { Give a warning when sizes don't match, because then info will be lost }
  1469. if left.resulttype.def.size=resulttype.def.size then
  1470. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1471. else
  1472. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1473. end;
  1474. { Constant folding and other node transitions to
  1475. remove the typeconv node }
  1476. case left.nodetype of
  1477. niln :
  1478. begin
  1479. { nil to ordinal node }
  1480. if (resulttype.def.deftype=orddef) then
  1481. begin
  1482. hp:=cordconstnode.create(0,resulttype,true);
  1483. result:=hp;
  1484. exit;
  1485. end
  1486. else
  1487. { fold nil to any pointer type }
  1488. if (resulttype.def.deftype=pointerdef) then
  1489. begin
  1490. hp:=cnilnode.create;
  1491. hp.resulttype:=resulttype;
  1492. result:=hp;
  1493. exit;
  1494. end
  1495. else
  1496. { remove typeconv after niln, but not when the result is a
  1497. methodpointer. The typeconv of the methodpointer will then
  1498. take care of updateing size of niln to OS_64 }
  1499. if not((resulttype.def.deftype=procvardef) and
  1500. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1501. begin
  1502. left.resulttype:=resulttype;
  1503. result:=left;
  1504. left:=nil;
  1505. exit;
  1506. end;
  1507. end;
  1508. ordconstn :
  1509. begin
  1510. { ordinal contants can be directly converted }
  1511. { but not char to char because it is a widechar to char or via versa }
  1512. { which needs extra code to do the code page transistion }
  1513. { constant ordinal to pointer }
  1514. if (resulttype.def.deftype=pointerdef) and
  1515. (convtype<>tc_cchar_2_pchar) then
  1516. begin
  1517. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1518. result:=hp;
  1519. exit;
  1520. end
  1521. else if is_ordinal(resulttype.def) and
  1522. not(convtype=tc_char_2_char) then
  1523. begin
  1524. { replace the resulttype and recheck the range }
  1525. left.resulttype:=resulttype;
  1526. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1527. result:=left;
  1528. left:=nil;
  1529. exit;
  1530. end;
  1531. end;
  1532. pointerconstn :
  1533. begin
  1534. { pointerconstn to any pointer is folded too }
  1535. if (resulttype.def.deftype=pointerdef) then
  1536. begin
  1537. left.resulttype:=resulttype;
  1538. result:=left;
  1539. left:=nil;
  1540. exit;
  1541. end
  1542. { constant pointer to ordinal }
  1543. else if is_ordinal(resulttype.def) then
  1544. begin
  1545. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1546. resulttype,not(nf_explicit in flags));
  1547. result:=hp;
  1548. exit;
  1549. end;
  1550. end;
  1551. end;
  1552. { now call the resulttype helper to do constant folding }
  1553. result:=resulttype_call_helper(convtype);
  1554. end;
  1555. procedure Ttypeconvnode.mark_write;
  1556. begin
  1557. left.mark_write;
  1558. end;
  1559. function ttypeconvnode.first_cord_to_pointer : tnode;
  1560. begin
  1561. result:=nil;
  1562. internalerror(200104043);
  1563. end;
  1564. function ttypeconvnode.first_int_to_int : tnode;
  1565. begin
  1566. first_int_to_int:=nil;
  1567. expectloc:=left.expectloc;
  1568. if not is_void(left.resulttype.def) then
  1569. begin
  1570. if (left.expectloc<>LOC_REGISTER) and
  1571. (resulttype.def.size>left.resulttype.def.size) then
  1572. expectloc:=LOC_REGISTER
  1573. else
  1574. if (left.expectloc=LOC_CREGISTER) and
  1575. (resulttype.def.size<left.resulttype.def.size) then
  1576. expectloc:=LOC_REGISTER;
  1577. end;
  1578. {$ifndef cpu64bit}
  1579. if is_64bit(resulttype.def) then
  1580. registersint:=max(registersint,2)
  1581. else
  1582. {$endif cpu64bit}
  1583. registersint:=max(registersint,1);
  1584. end;
  1585. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1586. begin
  1587. result:=nil;
  1588. registersint:=1;
  1589. expectloc:=LOC_REGISTER;
  1590. end;
  1591. function ttypeconvnode.first_cstring_to_int : tnode;
  1592. begin
  1593. result:=nil;
  1594. internalerror(200510014);
  1595. end;
  1596. function ttypeconvnode.first_string_to_chararray : tnode;
  1597. begin
  1598. first_string_to_chararray:=nil;
  1599. expectloc:=left.expectloc;
  1600. end;
  1601. function ttypeconvnode.first_char_to_string : tnode;
  1602. begin
  1603. first_char_to_string:=nil;
  1604. expectloc:=LOC_REFERENCE;
  1605. end;
  1606. function ttypeconvnode.first_nothing : tnode;
  1607. begin
  1608. first_nothing:=nil;
  1609. end;
  1610. function ttypeconvnode.first_array_to_pointer : tnode;
  1611. begin
  1612. first_array_to_pointer:=nil;
  1613. if registersint<1 then
  1614. registersint:=1;
  1615. expectloc:=LOC_REGISTER;
  1616. end;
  1617. function ttypeconvnode.first_int_to_real: tnode;
  1618. var
  1619. fname: string[32];
  1620. typname : string[12];
  1621. begin
  1622. { Get the type name }
  1623. { Normally the typename should be one of the following:
  1624. single, double - carl
  1625. }
  1626. typname := lower(pbestrealtype^.def.gettypename);
  1627. { converting a 64bit integer to a float requires a helper }
  1628. if is_64bit(left.resulttype.def) then
  1629. begin
  1630. if is_signed(left.resulttype.def) then
  1631. fname := 'fpc_int64_to_'+typname
  1632. else
  1633. {$warning generic conversion from int to float does not support unsigned integers}
  1634. fname := 'fpc_int64_to_'+typname;
  1635. result := ccallnode.createintern(fname,ccallparanode.create(
  1636. left,nil));
  1637. left:=nil;
  1638. firstpass(result);
  1639. exit;
  1640. end
  1641. else
  1642. { other integers are supposed to be 32 bit }
  1643. begin
  1644. {$warning generic conversion from int to float does not support unsigned integers}
  1645. if is_signed(left.resulttype.def) then
  1646. fname := 'fpc_longint_to_'+typname
  1647. else
  1648. fname := 'fpc_longint_to_'+typname;
  1649. result := ccallnode.createintern(fname,ccallparanode.create(
  1650. left,nil));
  1651. left:=nil;
  1652. firstpass(result);
  1653. exit;
  1654. end;
  1655. end;
  1656. function ttypeconvnode.first_real_to_real : tnode;
  1657. begin
  1658. {$ifdef cpufpemu}
  1659. if cs_fp_emulation in aktmoduleswitches then
  1660. begin
  1661. if target_info.system in system_wince then
  1662. begin
  1663. case tfloatdef(left.resulttype.def).typ of
  1664. s32real:
  1665. case tfloatdef(resulttype.def).typ of
  1666. s64real:
  1667. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1668. else
  1669. internalerror(2005082704);
  1670. end;
  1671. s64real:
  1672. case tfloatdef(resulttype.def).typ of
  1673. s32real:
  1674. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1675. else
  1676. internalerror(2005082703);
  1677. end;
  1678. else
  1679. internalerror(2005082702);
  1680. end;
  1681. left:=nil;
  1682. firstpass(result);
  1683. exit;
  1684. end
  1685. else
  1686. begin
  1687. {!! FIXME }
  1688. internalerror(2005082701);
  1689. end;
  1690. end
  1691. else
  1692. {$endif cpufpemu}
  1693. begin
  1694. first_real_to_real:=nil;
  1695. { comp isn't a floating type }
  1696. if registersfpu<1 then
  1697. registersfpu:=1;
  1698. expectloc:=LOC_FPUREGISTER;
  1699. end;
  1700. end;
  1701. function ttypeconvnode.first_pointer_to_array : tnode;
  1702. begin
  1703. first_pointer_to_array:=nil;
  1704. if registersint<1 then
  1705. registersint:=1;
  1706. expectloc:=LOC_REFERENCE;
  1707. end;
  1708. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1709. begin
  1710. first_cchar_to_pchar:=nil;
  1711. internalerror(200104021);
  1712. end;
  1713. function ttypeconvnode.first_bool_to_int : tnode;
  1714. begin
  1715. first_bool_to_int:=nil;
  1716. { byte(boolean) or word(wordbool) or longint(longbool) must
  1717. be accepted for var parameters }
  1718. if (nf_explicit in flags) and
  1719. (left.resulttype.def.size=resulttype.def.size) and
  1720. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1721. exit;
  1722. { when converting to 64bit, first convert to a 32bit int and then }
  1723. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1724. if resulttype.def.size > sizeof(aint) then
  1725. begin
  1726. result := ctypeconvnode.create_internal(left,u32inttype);
  1727. result := ctypeconvnode.create(result,resulttype);
  1728. left := nil;
  1729. firstpass(result);
  1730. exit;
  1731. end;
  1732. expectloc:=LOC_REGISTER;
  1733. if registersint<1 then
  1734. registersint:=1;
  1735. end;
  1736. function ttypeconvnode.first_int_to_bool : tnode;
  1737. begin
  1738. first_int_to_bool:=nil;
  1739. { byte(boolean) or word(wordbool) or longint(longbool) must
  1740. be accepted for var parameters }
  1741. if (nf_explicit in flags) and
  1742. (left.resulttype.def.size=resulttype.def.size) and
  1743. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1744. exit;
  1745. expectloc:=LOC_REGISTER;
  1746. { need if bool to bool !!
  1747. not very nice !!
  1748. insertypeconv(left,s32inttype);
  1749. left.explizit:=true;
  1750. firstpass(left); }
  1751. if registersint<1 then
  1752. registersint:=1;
  1753. end;
  1754. function ttypeconvnode.first_bool_to_bool : tnode;
  1755. begin
  1756. first_bool_to_bool:=nil;
  1757. expectloc:=LOC_REGISTER;
  1758. if registersint<1 then
  1759. registersint:=1;
  1760. end;
  1761. function ttypeconvnode.first_char_to_char : tnode;
  1762. begin
  1763. first_char_to_char:=first_int_to_int;
  1764. end;
  1765. function ttypeconvnode.first_proc_to_procvar : tnode;
  1766. begin
  1767. first_proc_to_procvar:=nil;
  1768. if tabstractprocdef(resulttype.def).is_addressonly then
  1769. begin
  1770. registersint:=left.registersint;
  1771. if registersint<1 then
  1772. registersint:=1;
  1773. expectloc:=LOC_REGISTER;
  1774. end
  1775. else
  1776. begin
  1777. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1778. CGMessage(parser_e_illegal_expression);
  1779. registersint:=left.registersint;
  1780. expectloc:=left.expectloc
  1781. end
  1782. end;
  1783. function ttypeconvnode.first_load_smallset : tnode;
  1784. var
  1785. srsym: ttypesym;
  1786. p: tcallparanode;
  1787. begin
  1788. if not searchsystype('FPC_SMALL_SET',srsym) then
  1789. internalerror(200108313);
  1790. p := ccallparanode.create(left,nil);
  1791. { reused }
  1792. left := nil;
  1793. { convert parameter explicitely to fpc_small_set }
  1794. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1795. { create call, adjust resulttype }
  1796. result :=
  1797. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1798. firstpass(result);
  1799. end;
  1800. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1801. begin
  1802. first_ansistring_to_pchar:=nil;
  1803. expectloc:=LOC_REGISTER;
  1804. if registersint<1 then
  1805. registersint:=1;
  1806. end;
  1807. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1808. begin
  1809. first_arrayconstructor_to_set:=nil;
  1810. internalerror(200104022);
  1811. end;
  1812. function ttypeconvnode.first_class_to_intf : tnode;
  1813. begin
  1814. first_class_to_intf:=nil;
  1815. expectloc:=LOC_REGISTER;
  1816. if registersint<1 then
  1817. registersint:=1;
  1818. end;
  1819. function ttypeconvnode._first_int_to_int : tnode;
  1820. begin
  1821. result:=first_int_to_int;
  1822. end;
  1823. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1824. begin
  1825. result:=first_cstring_to_pchar;
  1826. end;
  1827. function ttypeconvnode._first_cstring_to_int : tnode;
  1828. begin
  1829. result:=first_cstring_to_int;
  1830. end;
  1831. function ttypeconvnode._first_string_to_chararray : tnode;
  1832. begin
  1833. result:=first_string_to_chararray;
  1834. end;
  1835. function ttypeconvnode._first_char_to_string : tnode;
  1836. begin
  1837. result:=first_char_to_string;
  1838. end;
  1839. function ttypeconvnode._first_nothing : tnode;
  1840. begin
  1841. result:=first_nothing;
  1842. end;
  1843. function ttypeconvnode._first_array_to_pointer : tnode;
  1844. begin
  1845. result:=first_array_to_pointer;
  1846. end;
  1847. function ttypeconvnode._first_int_to_real : tnode;
  1848. begin
  1849. result:=first_int_to_real;
  1850. end;
  1851. function ttypeconvnode._first_real_to_real : tnode;
  1852. begin
  1853. result:=first_real_to_real;
  1854. end;
  1855. function ttypeconvnode._first_pointer_to_array : tnode;
  1856. begin
  1857. result:=first_pointer_to_array;
  1858. end;
  1859. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1860. begin
  1861. result:=first_cchar_to_pchar;
  1862. end;
  1863. function ttypeconvnode._first_bool_to_int : tnode;
  1864. begin
  1865. result:=first_bool_to_int;
  1866. end;
  1867. function ttypeconvnode._first_int_to_bool : tnode;
  1868. begin
  1869. result:=first_int_to_bool;
  1870. end;
  1871. function ttypeconvnode._first_bool_to_bool : tnode;
  1872. begin
  1873. result:=first_bool_to_bool;
  1874. end;
  1875. function ttypeconvnode._first_proc_to_procvar : tnode;
  1876. begin
  1877. result:=first_proc_to_procvar;
  1878. end;
  1879. function ttypeconvnode._first_load_smallset : tnode;
  1880. begin
  1881. result:=first_load_smallset;
  1882. end;
  1883. function ttypeconvnode._first_cord_to_pointer : tnode;
  1884. begin
  1885. result:=first_cord_to_pointer;
  1886. end;
  1887. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1888. begin
  1889. result:=first_ansistring_to_pchar;
  1890. end;
  1891. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1892. begin
  1893. result:=first_arrayconstructor_to_set;
  1894. end;
  1895. function ttypeconvnode._first_class_to_intf : tnode;
  1896. begin
  1897. result:=first_class_to_intf;
  1898. end;
  1899. function ttypeconvnode._first_char_to_char : tnode;
  1900. begin
  1901. result:=first_char_to_char;
  1902. end;
  1903. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1904. const
  1905. firstconvert : array[tconverttype] of pointer = (
  1906. nil, { none }
  1907. @ttypeconvnode._first_nothing, {equal}
  1908. @ttypeconvnode._first_nothing, {not_possible}
  1909. nil, { removed in resulttype_string_to_string }
  1910. @ttypeconvnode._first_char_to_string,
  1911. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1912. nil, { removed in resulttype_chararray_to_string }
  1913. @ttypeconvnode._first_cchar_to_pchar,
  1914. @ttypeconvnode._first_cstring_to_pchar,
  1915. @ttypeconvnode._first_cstring_to_int,
  1916. @ttypeconvnode._first_ansistring_to_pchar,
  1917. @ttypeconvnode._first_string_to_chararray,
  1918. nil, { removed in resulttype_chararray_to_string }
  1919. @ttypeconvnode._first_array_to_pointer,
  1920. @ttypeconvnode._first_pointer_to_array,
  1921. @ttypeconvnode._first_int_to_int,
  1922. @ttypeconvnode._first_int_to_bool,
  1923. @ttypeconvnode._first_bool_to_bool,
  1924. @ttypeconvnode._first_bool_to_int,
  1925. @ttypeconvnode._first_real_to_real,
  1926. @ttypeconvnode._first_int_to_real,
  1927. nil, { removed in resulttype_real_to_currency }
  1928. @ttypeconvnode._first_proc_to_procvar,
  1929. @ttypeconvnode._first_arrayconstructor_to_set,
  1930. @ttypeconvnode._first_load_smallset,
  1931. @ttypeconvnode._first_cord_to_pointer,
  1932. @ttypeconvnode._first_nothing,
  1933. @ttypeconvnode._first_nothing,
  1934. @ttypeconvnode._first_class_to_intf,
  1935. @ttypeconvnode._first_char_to_char,
  1936. @ttypeconvnode._first_nothing,
  1937. @ttypeconvnode._first_nothing,
  1938. nil,
  1939. nil,
  1940. nil,
  1941. nil,
  1942. nil,
  1943. nil,
  1944. nil,
  1945. nil
  1946. );
  1947. type
  1948. tprocedureofobject = function : tnode of object;
  1949. var
  1950. r : packed record
  1951. proc : pointer;
  1952. obj : pointer;
  1953. end;
  1954. begin
  1955. { this is a little bit dirty but it works }
  1956. { and should be quite portable too }
  1957. r.proc:=firstconvert[c];
  1958. r.obj:=self;
  1959. if not assigned(r.proc) then
  1960. internalerror(200312081);
  1961. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1962. end;
  1963. function ttypeconvnode.pass_1 : tnode;
  1964. begin
  1965. result:=nil;
  1966. firstpass(left);
  1967. if codegenerror then
  1968. exit;
  1969. { load the value_str from the left part }
  1970. registersint:=left.registersint;
  1971. registersfpu:=left.registersfpu;
  1972. {$ifdef SUPPORT_MMX}
  1973. registersmmx:=left.registersmmx;
  1974. {$endif}
  1975. expectloc:=left.expectloc;
  1976. result:=first_call_helper(convtype);
  1977. end;
  1978. function ttypeconvnode.assign_allowed:boolean;
  1979. begin
  1980. result:=(convtype=tc_equal) or
  1981. { typecasting from void is always allowed }
  1982. is_void(left.resulttype.def) or
  1983. (left.resulttype.def.deftype=formaldef) or
  1984. { int 2 int with same size reuses same location, or for
  1985. tp7 mode also allow size < orignal size }
  1986. (
  1987. (convtype=tc_int_2_int) and
  1988. (
  1989. (resulttype.def.size=left.resulttype.def.size) or
  1990. ((m_tp7 in aktmodeswitches) and
  1991. (resulttype.def.size<left.resulttype.def.size))
  1992. )
  1993. ) or
  1994. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1995. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1996. (nf_explicit in flags) and
  1997. (resulttype.def.size=left.resulttype.def.size));
  1998. { When using only a part of the value it can't be in a register since
  1999. that will load the value in a new register first }
  2000. if (resulttype.def.size<left.resulttype.def.size) then
  2001. make_not_regable(left);
  2002. end;
  2003. function ttypeconvnode.docompare(p: tnode) : boolean;
  2004. begin
  2005. docompare :=
  2006. inherited docompare(p) and
  2007. (convtype = ttypeconvnode(p).convtype);
  2008. end;
  2009. procedure ttypeconvnode._second_int_to_int;
  2010. begin
  2011. second_int_to_int;
  2012. end;
  2013. procedure ttypeconvnode._second_string_to_string;
  2014. begin
  2015. second_string_to_string;
  2016. end;
  2017. procedure ttypeconvnode._second_cstring_to_pchar;
  2018. begin
  2019. second_cstring_to_pchar;
  2020. end;
  2021. procedure ttypeconvnode._second_cstring_to_int;
  2022. begin
  2023. second_cstring_to_int;
  2024. end;
  2025. procedure ttypeconvnode._second_string_to_chararray;
  2026. begin
  2027. second_string_to_chararray;
  2028. end;
  2029. procedure ttypeconvnode._second_array_to_pointer;
  2030. begin
  2031. second_array_to_pointer;
  2032. end;
  2033. procedure ttypeconvnode._second_pointer_to_array;
  2034. begin
  2035. second_pointer_to_array;
  2036. end;
  2037. procedure ttypeconvnode._second_chararray_to_string;
  2038. begin
  2039. second_chararray_to_string;
  2040. end;
  2041. procedure ttypeconvnode._second_char_to_string;
  2042. begin
  2043. second_char_to_string;
  2044. end;
  2045. procedure ttypeconvnode._second_int_to_real;
  2046. begin
  2047. second_int_to_real;
  2048. end;
  2049. procedure ttypeconvnode._second_real_to_real;
  2050. begin
  2051. second_real_to_real;
  2052. end;
  2053. procedure ttypeconvnode._second_cord_to_pointer;
  2054. begin
  2055. second_cord_to_pointer;
  2056. end;
  2057. procedure ttypeconvnode._second_proc_to_procvar;
  2058. begin
  2059. second_proc_to_procvar;
  2060. end;
  2061. procedure ttypeconvnode._second_bool_to_int;
  2062. begin
  2063. second_bool_to_int;
  2064. end;
  2065. procedure ttypeconvnode._second_int_to_bool;
  2066. begin
  2067. second_int_to_bool;
  2068. end;
  2069. procedure ttypeconvnode._second_bool_to_bool;
  2070. begin
  2071. second_bool_to_bool;
  2072. end;
  2073. procedure ttypeconvnode._second_load_smallset;
  2074. begin
  2075. second_load_smallset;
  2076. end;
  2077. procedure ttypeconvnode._second_ansistring_to_pchar;
  2078. begin
  2079. second_ansistring_to_pchar;
  2080. end;
  2081. procedure ttypeconvnode._second_class_to_intf;
  2082. begin
  2083. second_class_to_intf;
  2084. end;
  2085. procedure ttypeconvnode._second_char_to_char;
  2086. begin
  2087. second_char_to_char;
  2088. end;
  2089. procedure ttypeconvnode._second_nothing;
  2090. begin
  2091. second_nothing;
  2092. end;
  2093. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2094. const
  2095. secondconvert : array[tconverttype] of pointer = (
  2096. @ttypeconvnode._second_nothing, {none}
  2097. @ttypeconvnode._second_nothing, {equal}
  2098. @ttypeconvnode._second_nothing, {not_possible}
  2099. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2100. @ttypeconvnode._second_char_to_string,
  2101. @ttypeconvnode._second_nothing, {char_to_charray}
  2102. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2103. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2104. @ttypeconvnode._second_cstring_to_pchar,
  2105. @ttypeconvnode._second_cstring_to_int,
  2106. @ttypeconvnode._second_ansistring_to_pchar,
  2107. @ttypeconvnode._second_string_to_chararray,
  2108. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2109. @ttypeconvnode._second_array_to_pointer,
  2110. @ttypeconvnode._second_pointer_to_array,
  2111. @ttypeconvnode._second_int_to_int,
  2112. @ttypeconvnode._second_int_to_bool,
  2113. @ttypeconvnode._second_bool_to_bool,
  2114. @ttypeconvnode._second_bool_to_int,
  2115. @ttypeconvnode._second_real_to_real,
  2116. @ttypeconvnode._second_int_to_real,
  2117. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2118. @ttypeconvnode._second_proc_to_procvar,
  2119. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2120. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2121. @ttypeconvnode._second_cord_to_pointer,
  2122. @ttypeconvnode._second_nothing, { interface 2 string }
  2123. @ttypeconvnode._second_nothing, { interface 2 guid }
  2124. @ttypeconvnode._second_class_to_intf,
  2125. @ttypeconvnode._second_char_to_char,
  2126. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2127. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2128. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2129. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2130. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2131. @ttypeconvnode._second_nothing, { variant_2_enum }
  2132. @ttypeconvnode._second_nothing, { enum_2_variant }
  2133. @ttypeconvnode._second_nothing, { variant_2_interface }
  2134. @ttypeconvnode._second_nothing, { interface_2_variant }
  2135. @ttypeconvnode._second_nothing { array_2_dynarray }
  2136. );
  2137. type
  2138. tprocedureofobject = procedure of object;
  2139. var
  2140. r : packed record
  2141. proc : pointer;
  2142. obj : pointer;
  2143. end;
  2144. begin
  2145. { this is a little bit dirty but it works }
  2146. { and should be quite portable too }
  2147. r.proc:=secondconvert[c];
  2148. r.obj:=self;
  2149. tprocedureofobject(r)();
  2150. end;
  2151. {*****************************************************************************
  2152. TISNODE
  2153. *****************************************************************************}
  2154. constructor tisnode.create(l,r : tnode);
  2155. begin
  2156. inherited create(isn,l,r);
  2157. end;
  2158. function tisnode.det_resulttype:tnode;
  2159. var
  2160. paras: tcallparanode;
  2161. begin
  2162. result:=nil;
  2163. resulttypepass(left);
  2164. resulttypepass(right);
  2165. set_varstate(left,vs_used,[vsf_must_be_valid]);
  2166. set_varstate(right,vs_used,[vsf_must_be_valid]);
  2167. if codegenerror then
  2168. exit;
  2169. if (right.resulttype.def.deftype=classrefdef) then
  2170. begin
  2171. { left must be a class }
  2172. if is_class(left.resulttype.def) then
  2173. begin
  2174. { the operands must be related }
  2175. if (not(tobjectdef(left.resulttype.def).is_related(
  2176. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2177. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2178. tobjectdef(left.resulttype.def)))) then
  2179. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2180. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2181. end
  2182. else
  2183. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2184. { call fpc_do_is helper }
  2185. paras := ccallparanode.create(
  2186. left,
  2187. ccallparanode.create(
  2188. right,nil));
  2189. result := ccallnode.createintern('fpc_do_is',paras);
  2190. left := nil;
  2191. right := nil;
  2192. end
  2193. else if is_interface(right.resulttype.def) then
  2194. begin
  2195. { left is a class }
  2196. if is_class(left.resulttype.def) then
  2197. begin
  2198. { the operands must be related }
  2199. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2200. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2201. CGMessage2(type_e_classes_not_related,
  2202. FullTypeName(left.resulttype.def,right.resulttype.def),
  2203. FullTypeName(right.resulttype.def,left.resulttype.def))
  2204. end
  2205. { left is an interface }
  2206. else if is_interface(left.resulttype.def) then
  2207. begin
  2208. { the operands must be related }
  2209. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2210. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2211. CGMessage2(type_e_classes_not_related,
  2212. FullTypeName(left.resulttype.def,right.resulttype.def),
  2213. FullTypeName(right.resulttype.def,left.resulttype.def));
  2214. end
  2215. else
  2216. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2217. { call fpc_do_is helper }
  2218. paras := ccallparanode.create(
  2219. left,
  2220. ccallparanode.create(
  2221. right,nil));
  2222. result := ccallnode.createintern('fpc_do_is',paras);
  2223. left := nil;
  2224. right := nil;
  2225. end
  2226. else
  2227. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2228. resulttype:=booltype;
  2229. end;
  2230. function tisnode.pass_1 : tnode;
  2231. begin
  2232. internalerror(200204254);
  2233. result:=nil;
  2234. end;
  2235. { dummy pass_2, it will never be called, but we need one since }
  2236. { you can't instantiate an abstract class }
  2237. procedure tisnode.pass_2;
  2238. begin
  2239. end;
  2240. {*****************************************************************************
  2241. TASNODE
  2242. *****************************************************************************}
  2243. constructor tasnode.create(l,r : tnode);
  2244. begin
  2245. inherited create(asn,l,r);
  2246. call := nil;
  2247. end;
  2248. destructor tasnode.destroy;
  2249. begin
  2250. call.free;
  2251. inherited destroy;
  2252. end;
  2253. function tasnode.det_resulttype:tnode;
  2254. var
  2255. hp : tnode;
  2256. begin
  2257. result:=nil;
  2258. resulttypepass(right);
  2259. resulttypepass(left);
  2260. set_varstate(right,vs_used,[vsf_must_be_valid]);
  2261. set_varstate(left,vs_used,[vsf_must_be_valid]);
  2262. if codegenerror then
  2263. exit;
  2264. if (right.resulttype.def.deftype=classrefdef) then
  2265. begin
  2266. { left must be a class }
  2267. if is_class(left.resulttype.def) then
  2268. begin
  2269. { the operands must be related }
  2270. if (not(tobjectdef(left.resulttype.def).is_related(
  2271. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2272. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2273. tobjectdef(left.resulttype.def)))) then
  2274. CGMessage2(type_e_classes_not_related,
  2275. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2276. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2277. end
  2278. else
  2279. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2280. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2281. end
  2282. else if is_interface(right.resulttype.def) then
  2283. begin
  2284. { left is a class }
  2285. if not(is_class(left.resulttype.def) or
  2286. is_interface(left.resulttype.def)) then
  2287. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2288. resulttype:=right.resulttype;
  2289. { load the GUID of the interface }
  2290. if (right.nodetype=typen) then
  2291. begin
  2292. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2293. begin
  2294. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2295. right.free;
  2296. right:=hp;
  2297. end
  2298. else
  2299. internalerror(200206282);
  2300. resulttypepass(right);
  2301. end;
  2302. end
  2303. else
  2304. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2305. end;
  2306. function tasnode.getcopy: tnode;
  2307. begin
  2308. result := inherited getcopy;
  2309. if assigned(call) then
  2310. tasnode(result).call := call.getcopy
  2311. else
  2312. tasnode(result).call := nil;
  2313. end;
  2314. function tasnode.pass_1 : tnode;
  2315. var
  2316. procname: string;
  2317. begin
  2318. result:=nil;
  2319. if not assigned(call) then
  2320. begin
  2321. if is_class(left.resulttype.def) and
  2322. (right.resulttype.def.deftype=classrefdef) then
  2323. call := ccallnode.createinternres('fpc_do_as',
  2324. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2325. resulttype)
  2326. else
  2327. begin
  2328. if is_class(left.resulttype.def) then
  2329. procname := 'fpc_class_as_intf'
  2330. else
  2331. procname := 'fpc_intf_as';
  2332. call := ccallnode.createinternres(procname,
  2333. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2334. resulttype);
  2335. end;
  2336. left := nil;
  2337. right := nil;
  2338. firstpass(call);
  2339. if codegenerror then
  2340. exit;
  2341. expectloc:=call.expectloc;
  2342. registersint:=call.registersint;
  2343. registersfpu:=call.registersfpu;
  2344. {$ifdef SUPPORT_MMX}
  2345. registersmmx:=call.registersmmx;
  2346. {$endif SUPPORT_MMX}
  2347. end;
  2348. end;
  2349. begin
  2350. ctypeconvnode:=ttypeconvnode;
  2351. casnode:=tasnode;
  2352. cisnode:=tisnode;
  2353. end.