ncnv.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,symppu,defbase,
  24. nld;
  25. type
  26. ttypeconvnode = class(tunarynode)
  27. totype : ttype;
  28. convtype : tconverttype;
  29. constructor create(node : tnode;const t : ttype);virtual;
  30. constructor create_explicit(node : tnode;const t : ttype);
  31. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  32. procedure ppuwrite(ppufile:tcompilerppufile);override;
  33. procedure derefimpl;override;
  34. function getcopy : tnode;override;
  35. function pass_1 : tnode;override;
  36. function det_resulttype:tnode;override;
  37. {$ifdef var_notification}
  38. procedure mark_write;override;
  39. {$endif}
  40. function docompare(p: tnode) : boolean; override;
  41. private
  42. function resulttype_cord_to_pointer : tnode;
  43. function resulttype_chararray_to_string : tnode;
  44. function resulttype_string_to_chararray : tnode;
  45. function resulttype_string_to_string : tnode;
  46. function resulttype_char_to_string : tnode;
  47. function resulttype_char_to_chararray : tnode;
  48. function resulttype_int_to_real : tnode;
  49. function resulttype_real_to_real : tnode;
  50. function resulttype_cchar_to_pchar : tnode;
  51. function resulttype_cstring_to_pchar : tnode;
  52. function resulttype_char_to_char : tnode;
  53. function resulttype_arrayconstructor_to_set : tnode;
  54. function resulttype_pchar_to_string : tnode;
  55. function resulttype_interface_to_guid : tnode;
  56. function resulttype_dynarray_to_openarray : tnode;
  57. function resulttype_call_helper(c : tconverttype) : tnode;
  58. protected
  59. function first_int_to_int : tnode;virtual;
  60. function first_cstring_to_pchar : tnode;virtual;
  61. function first_string_to_chararray : tnode;virtual;
  62. function first_char_to_string : tnode;virtual;
  63. function first_nothing : tnode;virtual;
  64. function first_array_to_pointer : tnode;virtual;
  65. function first_int_to_real : tnode;virtual;
  66. function first_real_to_real : tnode;virtual;
  67. function first_pointer_to_array : tnode;virtual;
  68. function first_cchar_to_pchar : tnode;virtual;
  69. function first_bool_to_int : tnode;virtual;
  70. function first_int_to_bool : tnode;virtual;
  71. function first_bool_to_bool : tnode;virtual;
  72. function first_proc_to_procvar : tnode;virtual;
  73. function first_load_smallset : tnode;virtual;
  74. function first_cord_to_pointer : tnode;virtual;
  75. function first_ansistring_to_pchar : tnode;virtual;
  76. function first_arrayconstructor_to_set : tnode;virtual;
  77. function first_class_to_intf : tnode;virtual;
  78. function first_char_to_char : tnode;virtual;
  79. function first_call_helper(c : tconverttype) : tnode;
  80. { these wrapper are necessary, because the first_* stuff is called }
  81. { through a table. Without the wrappers override wouldn't have }
  82. { any effect }
  83. function _first_int_to_int : tnode;
  84. function _first_cstring_to_pchar : tnode;
  85. function _first_string_to_chararray : tnode;
  86. function _first_char_to_string : tnode;
  87. function _first_nothing : tnode;
  88. function _first_array_to_pointer : tnode;
  89. function _first_int_to_real : tnode;
  90. function _first_real_to_real : tnode;
  91. function _first_pointer_to_array : tnode;
  92. function _first_cchar_to_pchar : tnode;
  93. function _first_bool_to_int : tnode;
  94. function _first_int_to_bool : tnode;
  95. function _first_bool_to_bool : tnode;
  96. function _first_proc_to_procvar : tnode;
  97. function _first_load_smallset : tnode;
  98. function _first_cord_to_pointer : tnode;
  99. function _first_ansistring_to_pchar : tnode;
  100. function _first_arrayconstructor_to_set : tnode;
  101. function _first_class_to_intf : tnode;
  102. function _first_char_to_char : tnode;
  103. procedure second_int_to_int;virtual;abstract;
  104. procedure second_string_to_string;virtual;abstract;
  105. procedure second_cstring_to_pchar;virtual;abstract;
  106. procedure second_string_to_chararray;virtual;abstract;
  107. procedure second_array_to_pointer;virtual;abstract;
  108. procedure second_pointer_to_array;virtual;abstract;
  109. procedure second_chararray_to_string;virtual;abstract;
  110. procedure second_char_to_string;virtual;abstract;
  111. procedure second_int_to_real;virtual;abstract;
  112. procedure second_real_to_real;virtual;abstract;
  113. procedure second_cord_to_pointer;virtual;abstract;
  114. procedure second_proc_to_procvar;virtual;abstract;
  115. procedure second_bool_to_int;virtual;abstract;
  116. procedure second_int_to_bool;virtual;abstract;
  117. procedure second_bool_to_bool;virtual;abstract;
  118. procedure second_load_smallset;virtual;abstract;
  119. procedure second_ansistring_to_pchar;virtual;abstract;
  120. procedure second_pchar_to_string;virtual;abstract;
  121. procedure second_class_to_intf;virtual;abstract;
  122. procedure second_char_to_char;virtual;abstract;
  123. procedure second_nothing; virtual;abstract;
  124. end;
  125. ttypeconvnodeclass = class of ttypeconvnode;
  126. tasnode = class(tbinarynode)
  127. constructor create(l,r : tnode);virtual;
  128. function pass_1 : tnode;override;
  129. function det_resulttype:tnode;override;
  130. function getcopy: tnode;override;
  131. destructor destroy; override;
  132. protected
  133. call: tnode;
  134. end;
  135. tasnodeclass = class of tasnode;
  136. tisnode = class(tbinarynode)
  137. constructor create(l,r : tnode);virtual;
  138. function pass_1 : tnode;override;
  139. function det_resulttype:tnode;override;
  140. procedure pass_2;override;
  141. end;
  142. tisnodeclass = class of tisnode;
  143. var
  144. ctypeconvnode : ttypeconvnodeclass;
  145. casnode : tasnodeclass;
  146. cisnode : tisnodeclass;
  147. procedure inserttypeconv(var p:tnode;const t:ttype);
  148. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  149. procedure arrayconstructor_to_set(var p : tnode);
  150. implementation
  151. uses
  152. globtype,systems,tokens,
  153. cutils,verbose,globals,widestr,
  154. symconst,symdef,symsym,symtable,
  155. ncon,ncal,nset,nadd,ninl,nmem,nmat,
  156. cgbase,
  157. htypechk,pass_1,cpubase,cpuinfo;
  158. {*****************************************************************************
  159. Helpers
  160. *****************************************************************************}
  161. procedure inserttypeconv(var p:tnode;const t:ttype);
  162. begin
  163. if not assigned(p.resulttype.def) then
  164. begin
  165. resulttypepass(p);
  166. if codegenerror then
  167. exit;
  168. end;
  169. { don't insert obsolete type conversions }
  170. if is_equal(p.resulttype.def,t.def) and
  171. not ((p.resulttype.def.deftype=setdef) and
  172. (tsetdef(p.resulttype.def).settype <>
  173. tsetdef(t.def).settype)) then
  174. begin
  175. p.resulttype:=t;
  176. end
  177. else
  178. begin
  179. p:=ctypeconvnode.create(p,t);
  180. resulttypepass(p);
  181. end;
  182. end;
  183. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  184. begin
  185. if not assigned(p.resulttype.def) then
  186. begin
  187. resulttypepass(p);
  188. if codegenerror then
  189. exit;
  190. end;
  191. { don't insert obsolete type conversions }
  192. if is_equal(p.resulttype.def,t.def) and
  193. not ((p.resulttype.def.deftype=setdef) and
  194. (tsetdef(p.resulttype.def).settype <>
  195. tsetdef(t.def).settype)) then
  196. begin
  197. p.resulttype:=t;
  198. end
  199. else
  200. begin
  201. p:=ctypeconvnode.create_explicit(p,t);
  202. resulttypepass(p);
  203. end;
  204. end;
  205. {*****************************************************************************
  206. Array constructor to Set Conversion
  207. *****************************************************************************}
  208. procedure arrayconstructor_to_set(var p : tnode);
  209. var
  210. constp : tsetconstnode;
  211. buildp,
  212. p2,p3,p4 : tnode;
  213. htype : ttype;
  214. constset : Pconstset;
  215. constsetlo,
  216. constsethi : longint;
  217. procedure update_constsethi(t:ttype);
  218. begin
  219. if ((t.def.deftype=orddef) and
  220. (torddef(t.def).high>=constsethi)) then
  221. begin
  222. constsethi:=torddef(t.def).high;
  223. if htype.def=nil then
  224. begin
  225. if (constsethi>255) or
  226. (torddef(t.def).low<0) then
  227. htype:=u8bittype
  228. else
  229. htype:=t;
  230. end;
  231. if constsethi>255 then
  232. constsethi:=255;
  233. end
  234. else if ((t.def.deftype=enumdef) and
  235. (tenumdef(t.def).max>=constsethi)) then
  236. begin
  237. if htype.def=nil then
  238. htype:=t;
  239. constsethi:=tenumdef(t.def).max;
  240. end;
  241. end;
  242. procedure do_set(pos : longint);
  243. {$ifdef oldset}
  244. var
  245. mask,l : longint;
  246. {$endif}
  247. begin
  248. if (pos and not $ff)<>0 then
  249. Message(parser_e_illegal_set_expr);
  250. if pos>constsethi then
  251. constsethi:=pos;
  252. if pos<constsetlo then
  253. constsetlo:=pos;
  254. {$ifdef oldset}
  255. { to do this correctly we use the 32bit array }
  256. l:=pos shr 5;
  257. mask:=1 shl (pos mod 32);
  258. { do we allow the same twice }
  259. if (pconst32bitset(constset)^[l] and mask)<>0 then
  260. Message(parser_e_illegal_set_expr);
  261. pconst32bitset(constset)^[l]:=pconst32bitset(constset)^[l] or mask;
  262. {$else}
  263. include(constset^,pos);
  264. {$endif}
  265. end;
  266. var
  267. l : Longint;
  268. lr,hr : TConstExprInt;
  269. hp : tarrayconstructornode;
  270. begin
  271. if p.nodetype<>arrayconstructorn then
  272. internalerror(200205105);
  273. new(constset);
  274. {$ifdef oldset}
  275. FillChar(constset^,sizeof(constset^),0);
  276. {$else}
  277. constset^:=[];
  278. {$endif}
  279. htype.reset;
  280. constsetlo:=0;
  281. constsethi:=0;
  282. constp:=csetconstnode.create(nil,htype);
  283. constp.value_set:=constset;
  284. buildp:=constp;
  285. hp:=tarrayconstructornode(p);
  286. if assigned(hp.left) then
  287. begin
  288. while assigned(hp) do
  289. begin
  290. p4:=nil; { will contain the tree to create the set }
  291. {split a range into p2 and p3 }
  292. if hp.left.nodetype=arrayconstructorrangen then
  293. begin
  294. p2:=tarrayconstructorrangenode(hp.left).left;
  295. p3:=tarrayconstructorrangenode(hp.left).right;
  296. tarrayconstructorrangenode(hp.left).left:=nil;
  297. tarrayconstructorrangenode(hp.left).right:=nil;
  298. end
  299. else
  300. begin
  301. p2:=hp.left;
  302. hp.left:=nil;
  303. p3:=nil;
  304. end;
  305. resulttypepass(p2);
  306. if assigned(p3) then
  307. resulttypepass(p3);
  308. if codegenerror then
  309. break;
  310. case p2.resulttype.def.deftype of
  311. enumdef,
  312. orddef:
  313. begin
  314. getrange(p2.resulttype.def,lr,hr);
  315. if assigned(p3) then
  316. begin
  317. { this isn't good, you'll get problems with
  318. type t010 = 0..10;
  319. ts = set of t010;
  320. var s : ts;b : t010
  321. begin s:=[1,2,b]; end.
  322. if is_integer(p3^.resulttype.def) then
  323. begin
  324. inserttypeconv(p3,u8bitdef);
  325. end;
  326. }
  327. if assigned(htype.def) and not(is_equal(htype.def,p3.resulttype.def)) then
  328. begin
  329. aktfilepos:=p3.fileinfo;
  330. CGMessage(type_e_typeconflict_in_set);
  331. end
  332. else
  333. begin
  334. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  335. begin
  336. if not(is_integer(p3.resulttype.def)) then
  337. htype:=p3.resulttype
  338. else
  339. begin
  340. inserttypeconv(p3,u8bittype);
  341. inserttypeconv(p2,u8bittype);
  342. end;
  343. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  344. do_set(l);
  345. p2.free;
  346. p3.free;
  347. end
  348. else
  349. begin
  350. update_constsethi(p2.resulttype);
  351. inserttypeconv(p2,htype);
  352. update_constsethi(p3.resulttype);
  353. inserttypeconv(p3,htype);
  354. if assigned(htype.def) then
  355. inserttypeconv(p3,htype)
  356. else
  357. inserttypeconv(p3,u8bittype);
  358. p4:=csetelementnode.create(p2,p3);
  359. end;
  360. end;
  361. end
  362. else
  363. begin
  364. { Single value }
  365. if p2.nodetype=ordconstn then
  366. begin
  367. if not(is_integer(p2.resulttype.def)) then
  368. update_constsethi(p2.resulttype)
  369. else
  370. inserttypeconv(p2,u8bittype);
  371. do_set(tordconstnode(p2).value);
  372. p2.free;
  373. end
  374. else
  375. begin
  376. update_constsethi(p2.resulttype);
  377. if assigned(htype.def) then
  378. inserttypeconv(p2,htype)
  379. else
  380. inserttypeconv(p2,u8bittype);
  381. p4:=csetelementnode.create(p2,nil);
  382. end;
  383. end;
  384. end;
  385. stringdef :
  386. begin
  387. { if we've already set elements which are constants }
  388. { throw an error }
  389. if ((htype.def=nil) and assigned(buildp)) or
  390. not(is_char(htype.def)) then
  391. CGMessage(type_e_typeconflict_in_set)
  392. else
  393. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  394. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  395. if htype.def=nil then
  396. htype:=cchartype;
  397. p2.free;
  398. end;
  399. else
  400. CGMessage(type_e_ordinal_expr_expected);
  401. end;
  402. { insert the set creation tree }
  403. if assigned(p4) then
  404. buildp:=caddnode.create(addn,buildp,p4);
  405. { load next and dispose current node }
  406. p2:=hp;
  407. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  408. tarrayconstructornode(p2).right:=nil;
  409. p2.free;
  410. end;
  411. if (htype.def=nil) then
  412. htype:=u8bittype;
  413. end
  414. else
  415. begin
  416. { empty set [], only remove node }
  417. p.free;
  418. end;
  419. { set the initial set type }
  420. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  421. { determine the resulttype for the tree }
  422. resulttypepass(buildp);
  423. { set the new tree }
  424. p:=buildp;
  425. end;
  426. {*****************************************************************************
  427. TTYPECONVNODE
  428. *****************************************************************************}
  429. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  430. begin
  431. inherited create(typeconvn,node);
  432. convtype:=tc_not_possible;
  433. totype:=t;
  434. if t.def=nil then
  435. internalerror(200103281);
  436. set_file_line(node);
  437. end;
  438. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  439. begin
  440. self.create(node,t);
  441. toggleflag(nf_explizit);
  442. end;
  443. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  444. begin
  445. inherited ppuload(t,ppufile);
  446. ppufile.gettype(totype);
  447. convtype:=tconverttype(ppufile.getbyte);
  448. end;
  449. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  450. begin
  451. inherited ppuwrite(ppufile);
  452. ppufile.puttype(totype);
  453. ppufile.putbyte(byte(convtype));
  454. end;
  455. procedure ttypeconvnode.derefimpl;
  456. begin
  457. inherited derefimpl;
  458. totype.resolve;
  459. end;
  460. function ttypeconvnode.getcopy : tnode;
  461. var
  462. n : ttypeconvnode;
  463. begin
  464. n:=ttypeconvnode(inherited getcopy);
  465. n.convtype:=convtype;
  466. getcopy:=n;
  467. end;
  468. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  469. var
  470. t : tnode;
  471. begin
  472. result:=nil;
  473. if left.nodetype=ordconstn then
  474. begin
  475. { check if we have a valid pointer constant (JM) }
  476. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  477. if (sizeof(TConstPtrUInt) = 4) then
  478. begin
  479. if (tordconstnode(left).value < low(longint)) or
  480. (tordconstnode(left).value > high(cardinal)) then
  481. CGMessage(parser_e_range_check_error);
  482. end
  483. else if (sizeof(TConstPtrUInt) = 8) then
  484. begin
  485. if (tordconstnode(left).value < low(int64)) or
  486. (tordconstnode(left).value > high(qword)) then
  487. CGMessage(parser_e_range_check_error);
  488. end
  489. else
  490. internalerror(2001020801);
  491. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  492. result:=t;
  493. end
  494. else
  495. internalerror(200104023);
  496. end;
  497. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  498. begin
  499. result := ccallnode.createinternres(
  500. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  501. ccallparanode.create(left,nil),resulttype);
  502. left := nil;
  503. end;
  504. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  505. var
  506. arrsize: longint;
  507. begin
  508. with tarraydef(resulttype.def) do
  509. begin
  510. if highrange<lowrange then
  511. internalerror(75432653);
  512. arrsize := highrange-lowrange+1;
  513. end;
  514. if (left.nodetype = stringconstn) and
  515. { left.length+1 since there's always a terminating #0 character (JM) }
  516. (tstringconstnode(left).len+1 >= arrsize) and
  517. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  518. begin
  519. { handled separately }
  520. result := nil;
  521. exit;
  522. end;
  523. result := ccallnode.createinternres(
  524. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  525. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  526. cordconstnode.create(arrsize,s32bittype,true),nil)),resulttype);
  527. left := nil;
  528. end;
  529. function ttypeconvnode.resulttype_string_to_string : tnode;
  530. var
  531. procname: string[31];
  532. stringpara : tcallparanode;
  533. pw : pcompilerwidestring;
  534. pc : pchar;
  535. begin
  536. result:=nil;
  537. if left.nodetype=stringconstn then
  538. begin
  539. { convert ascii 2 unicode }
  540. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  541. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  542. begin
  543. initwidestring(pw);
  544. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  545. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  546. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  547. end
  548. else
  549. { convert unicode 2 ascii }
  550. if (tstringconstnode(left).st_type=st_widestring) and
  551. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  552. begin
  553. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  554. getmem(pc,getlengthwidestring(pw)+1);
  555. unicode2ascii(pw,pc);
  556. donewidestring(pw);
  557. tstringconstnode(left).value_str:=pc;
  558. end;
  559. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  560. tstringconstnode(left).resulttype:=resulttype;
  561. result:=left;
  562. left:=nil;
  563. end
  564. else
  565. begin
  566. { get the correct procedure name }
  567. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  568. '_to_'+tstringdef(resulttype.def).stringtypname;
  569. { create parameter (and remove left node from typeconvnode }
  570. { since it's reused as parameter) }
  571. stringpara := ccallparanode.create(left,nil);
  572. left := nil;
  573. { when converting to shortstrings, we have to pass high(destination) too }
  574. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  575. stringpara.right := ccallparanode.create(cinlinenode.create(
  576. in_high_x,false,self.getcopy),nil);
  577. { and create the callnode }
  578. result := ccallnode.createinternres(procname,stringpara,resulttype);
  579. end;
  580. end;
  581. function ttypeconvnode.resulttype_char_to_string : tnode;
  582. var
  583. procname: string[31];
  584. para : tcallparanode;
  585. hp : tstringconstnode;
  586. ws : pcompilerwidestring;
  587. begin
  588. result:=nil;
  589. if left.nodetype=ordconstn then
  590. begin
  591. if tstringdef(resulttype.def).string_typ=st_widestring then
  592. begin
  593. initwidestring(ws);
  594. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  595. hp:=cstringconstnode.createwstr(ws);
  596. donewidestring(ws);
  597. end
  598. else
  599. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  600. result:=hp;
  601. end
  602. else
  603. { shortstrings are handled 'inline' }
  604. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  605. begin
  606. { create the parameter }
  607. para := ccallparanode.create(left,nil);
  608. left := nil;
  609. { and the procname }
  610. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  611. { and finally the call }
  612. result := ccallnode.createinternres(procname,para,resulttype);
  613. end
  614. else
  615. begin
  616. { create word(byte(char) shl 8 or 1) for litte endian machines }
  617. { and word(byte(char) or 256) for big endian machines }
  618. left := ctypeconvnode.create(left,u8bittype);
  619. left.toggleflag(nf_explizit);
  620. if (target_info.endian = endian_little) then
  621. left := caddnode.create(orn,
  622. cshlshrnode.create(shln,left,cordconstnode.create(8,s32bittype,false)),
  623. cordconstnode.create(1,s32bittype,false))
  624. else
  625. left := caddnode.create(orn,left,
  626. cordconstnode.create(1 shl 8,s32bittype,false));
  627. left := ctypeconvnode.create(left,u16bittype);
  628. left.toggleflag(nf_explizit);
  629. resulttypepass(left);
  630. end;
  631. end;
  632. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  633. begin
  634. if resulttype.def.size <> 1 then
  635. begin
  636. { convert first to string, then to chararray }
  637. inserttypeconv(left,cshortstringtype);
  638. inserttypeconv(left,resulttype);
  639. result:=left;
  640. left := nil;
  641. exit;
  642. end;
  643. result := nil;
  644. end;
  645. function ttypeconvnode.resulttype_char_to_char : tnode;
  646. var
  647. hp : tordconstnode;
  648. begin
  649. result:=nil;
  650. if left.nodetype=ordconstn then
  651. begin
  652. if (torddef(resulttype.def).typ=uchar) and
  653. (torddef(left.resulttype.def).typ=uwidechar) then
  654. begin
  655. hp:=cordconstnode.create(
  656. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  657. cchartype,true);
  658. result:=hp;
  659. end
  660. else if (torddef(resulttype.def).typ=uwidechar) and
  661. (torddef(left.resulttype.def).typ=uchar) then
  662. begin
  663. hp:=cordconstnode.create(
  664. asciichar2unicode(chr(tordconstnode(left).value)),
  665. cwidechartype,true);
  666. result:=hp;
  667. end
  668. else
  669. internalerror(200105131);
  670. exit;
  671. end;
  672. end;
  673. function ttypeconvnode.resulttype_int_to_real : tnode;
  674. var
  675. t : trealconstnode;
  676. begin
  677. result:=nil;
  678. if left.nodetype=ordconstn then
  679. begin
  680. t:=crealconstnode.create(tordconstnode(left).value,resulttype);
  681. result:=t;
  682. end;
  683. end;
  684. function ttypeconvnode.resulttype_real_to_real : tnode;
  685. var
  686. t : tnode;
  687. begin
  688. result:=nil;
  689. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  690. begin
  691. end
  692. else
  693. if is_currency(resulttype.def) then
  694. begin
  695. end;
  696. if left.nodetype=realconstn then
  697. begin
  698. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  699. result:=t;
  700. end;
  701. end;
  702. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  703. begin
  704. result:=nil;
  705. if is_pwidechar(resulttype.def) then
  706. inserttypeconv(left,cwidestringtype)
  707. else
  708. inserttypeconv(left,cshortstringtype);
  709. { evaluate again, reset resulttype so the convert_typ
  710. will be calculated again and cstring_to_pchar will
  711. be used for futher conversion }
  712. result:=det_resulttype;
  713. end;
  714. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  715. begin
  716. result:=nil;
  717. if is_pwidechar(resulttype.def) then
  718. inserttypeconv(left,cwidestringtype);
  719. end;
  720. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  721. var
  722. hp : tnode;
  723. begin
  724. result:=nil;
  725. if left.nodetype<>arrayconstructorn then
  726. internalerror(5546);
  727. { remove typeconv node }
  728. hp:=left;
  729. left:=nil;
  730. { create a set constructor tree }
  731. arrayconstructor_to_set(hp);
  732. result:=hp;
  733. end;
  734. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  735. begin
  736. result := ccallnode.createinternres(
  737. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  738. ccallparanode.create(left,nil),resulttype);
  739. left := nil;
  740. end;
  741. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  742. begin
  743. if tobjectdef(left.resulttype.def).isiidguidvalid then
  744. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid);
  745. end;
  746. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  747. begin
  748. { a dynamic array is a pointer to an array, so to convert it to }
  749. { an open array, we have to dereference it (JM) }
  750. result := ctypeconvnode.create(left,voidpointertype);
  751. { left is reused }
  752. left := nil;
  753. result.toggleflag(nf_explizit);
  754. result := cderefnode.create(result);
  755. result.resulttype := resulttype;
  756. end;
  757. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  758. const
  759. resulttypeconvert : array[tconverttype] of pointer = (
  760. {equal} nil,
  761. {not_possible} nil,
  762. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  763. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  764. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  765. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  766. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  767. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  768. { ansistring_2_pchar } nil,
  769. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  770. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  771. { array_2_pointer } nil,
  772. { pointer_2_array } nil,
  773. { int_2_int } nil,
  774. { int_2_bool } nil,
  775. { bool_2_bool } nil,
  776. { bool_2_int } nil,
  777. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  778. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  779. { proc_2_procvar } nil,
  780. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  781. { load_smallset } nil,
  782. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  783. { intf_2_string } nil,
  784. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  785. { class_2_intf } nil,
  786. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  787. { normal_2_smallset} nil,
  788. { dynarray_2_openarray} @resulttype_dynarray_to_openarray
  789. );
  790. type
  791. tprocedureofobject = function : tnode of object;
  792. var
  793. r : packed record
  794. proc : pointer;
  795. obj : pointer;
  796. end;
  797. begin
  798. result:=nil;
  799. { this is a little bit dirty but it works }
  800. { and should be quite portable too }
  801. r.proc:=resulttypeconvert[c];
  802. r.obj:=self;
  803. if assigned(r.proc) then
  804. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  805. end;
  806. function ttypeconvnode.det_resulttype:tnode;
  807. var
  808. hp : tnode;
  809. currprocdef,
  810. aprocdef : tprocdef;
  811. begin
  812. result:=nil;
  813. resulttype:=totype;
  814. resulttypepass(left);
  815. if codegenerror then
  816. exit;
  817. { remove obsolete type conversions }
  818. if is_equal(left.resulttype.def,resulttype.def) then
  819. begin
  820. { becuase is_equal only checks the basetype for sets we need to
  821. check here if we are loading a smallset into a normalset }
  822. if (resulttype.def.deftype=setdef) and
  823. (left.resulttype.def.deftype=setdef) and
  824. ((tsetdef(resulttype.def).settype = smallset) xor
  825. (tsetdef(left.resulttype.def).settype = smallset)) then
  826. begin
  827. { constant sets can be converted by changing the type only }
  828. if (left.nodetype=setconstn) then
  829. begin
  830. tsetdef(left.resulttype.def).changesettype(tsetdef(resulttype.def).settype);
  831. result:=left;
  832. left:=nil;
  833. exit;
  834. end;
  835. if (tsetdef(resulttype.def).settype <> smallset) then
  836. convtype:=tc_load_smallset
  837. else
  838. convtype := tc_normal_2_smallset;
  839. exit;
  840. end
  841. else
  842. begin
  843. left.resulttype:=resulttype;
  844. result:=left;
  845. left:=nil;
  846. exit;
  847. end;
  848. end;
  849. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  850. if assigned(aprocdef) then
  851. begin
  852. procinfo.flags:=procinfo.flags or pi_do_call;
  853. hp:=ccallnode.create(ccallparanode.create(left,nil),
  854. overloaded_operators[_assignment],nil,nil);
  855. { tell explicitly which def we must use !! (PM) }
  856. tcallnode(hp).procdefinition:=aprocdef;
  857. left:=nil;
  858. result:=hp;
  859. exit;
  860. end;
  861. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  862. begin
  863. {Procedures have a resulttype.def of voiddef and functions of their
  864. own resulttype.def. They will therefore always be incompatible with
  865. a procvar. Because isconvertable cannot check for procedures we
  866. use an extra check for them.}
  867. if (m_tp_procvar in aktmodeswitches) then
  868. begin
  869. if (resulttype.def.deftype=procvardef) and
  870. (is_procsym_load(left) or is_procsym_call(left)) then
  871. begin
  872. if is_procsym_call(left) then
  873. begin
  874. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  875. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  876. currprocdef,tcallnode(left).symtableproc);
  877. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  878. assigned(tcallnode(left).methodpointer) then
  879. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  880. resulttypepass(hp);
  881. left.free;
  882. left:=hp;
  883. aprocdef:=tprocdef(left.resulttype.def);
  884. end
  885. else
  886. begin
  887. if (left.nodetype<>addrn) then
  888. aprocdef:=tprocsym(tloadnode(left).symtableentry).first_procdef;
  889. end;
  890. convtype:=tc_proc_2_procvar;
  891. { Now check if the procedure we are going to assign to
  892. the procvar, is compatible with the procvar's type }
  893. if assigned(aprocdef) then
  894. begin
  895. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def),false) then
  896. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  897. end
  898. else
  899. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  900. exit;
  901. end;
  902. end;
  903. if nf_explizit in flags then
  904. begin
  905. { check if the result could be in a register }
  906. if not(tstoreddef(resulttype.def).is_intregable) and
  907. not(tstoreddef(resulttype.def).is_fpuregable) then
  908. make_not_regable(left);
  909. { boolean to byte are special because the
  910. location can be different }
  911. if is_integer(resulttype.def) and
  912. is_boolean(left.resulttype.def) then
  913. begin
  914. convtype:=tc_bool_2_int;
  915. exit;
  916. end;
  917. if is_char(resulttype.def) and
  918. is_boolean(left.resulttype.def) then
  919. begin
  920. convtype:=tc_bool_2_int;
  921. exit;
  922. end;
  923. { ansistring to pchar }
  924. if is_pchar(resulttype.def) and
  925. is_ansistring(left.resulttype.def) then
  926. begin
  927. convtype:=tc_ansistring_2_pchar;
  928. exit;
  929. end;
  930. { do common tc_equal cast }
  931. convtype:=tc_equal;
  932. { enum to ordinal will always be s32bit }
  933. if (left.resulttype.def.deftype=enumdef) and
  934. is_ordinal(resulttype.def) then
  935. begin
  936. if left.nodetype=ordconstn then
  937. begin
  938. hp:=cordconstnode.create(tordconstnode(left).value,
  939. resulttype,true);
  940. result:=hp;
  941. exit;
  942. end
  943. else
  944. begin
  945. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  946. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  947. end;
  948. end
  949. { ordinal to enumeration }
  950. else
  951. if (resulttype.def.deftype=enumdef) and
  952. is_ordinal(left.resulttype.def) then
  953. begin
  954. if left.nodetype=ordconstn then
  955. begin
  956. hp:=cordconstnode.create(tordconstnode(left).value,
  957. resulttype,true);
  958. result:=hp;
  959. exit;
  960. end
  961. else
  962. begin
  963. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  964. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  965. end;
  966. end
  967. { nil to ordinal node }
  968. else if (left.nodetype=niln) and is_ordinal(resulttype.def) then
  969. begin
  970. hp:=cordconstnode.create(0,resulttype,true);
  971. result:=hp;
  972. exit;
  973. end
  974. { constant pointer to ordinal }
  975. else if is_ordinal(resulttype.def) and
  976. (left.nodetype=pointerconstn) then
  977. begin
  978. hp:=cordconstnode.create(tpointerconstnode(left).value,
  979. resulttype,true);
  980. result:=hp;
  981. exit;
  982. end
  983. { class to class or object to object, with checkobject support }
  984. else if (resulttype.def.deftype=objectdef) and
  985. (left.resulttype.def.deftype=objectdef) then
  986. begin
  987. if (cs_check_object in aktlocalswitches) then
  988. begin
  989. if is_class_or_interface(resulttype.def) then
  990. begin
  991. { we can translate the typeconvnode to 'as' when
  992. typecasting to a class or interface }
  993. hp:=casnode.create(left,cloadvmtnode.create(ctypenode.create(resulttype)));
  994. left:=nil;
  995. result:=hp;
  996. exit;
  997. end;
  998. end
  999. else
  1000. begin
  1001. { check if the types are related }
  1002. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1003. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1004. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  1005. end;
  1006. end
  1007. {Are we typecasting an ordconst to a char?}
  1008. else
  1009. if is_char(resulttype.def) and
  1010. is_ordinal(left.resulttype.def) then
  1011. begin
  1012. if left.nodetype=ordconstn then
  1013. begin
  1014. hp:=cordconstnode.create(tordconstnode(left).value,
  1015. resulttype,true);
  1016. result:=hp;
  1017. exit;
  1018. end
  1019. else
  1020. begin
  1021. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  1022. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1023. end;
  1024. end
  1025. {Are we typecasting an ordconst to a wchar?}
  1026. else
  1027. if is_widechar(resulttype.def) and
  1028. is_ordinal(left.resulttype.def) then
  1029. begin
  1030. if left.nodetype=ordconstn then
  1031. begin
  1032. hp:=cordconstnode.create(tordconstnode(left).value,
  1033. resulttype,true);
  1034. result:=hp;
  1035. exit;
  1036. end
  1037. else
  1038. begin
  1039. if IsConvertable(left.resulttype.def,u16bittype.def,convtype,ordconstn,false)=0 then
  1040. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1041. end;
  1042. end
  1043. { char to ordinal }
  1044. else
  1045. if is_char(left.resulttype.def) and
  1046. is_ordinal(resulttype.def) then
  1047. begin
  1048. if left.nodetype=ordconstn then
  1049. begin
  1050. hp:=cordconstnode.create(tordconstnode(left).value,
  1051. resulttype,true);
  1052. result:=hp;
  1053. exit;
  1054. end
  1055. else
  1056. begin
  1057. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  1058. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1059. end;
  1060. end
  1061. { widechar to ordinal }
  1062. else
  1063. if is_widechar(left.resulttype.def) and
  1064. is_ordinal(resulttype.def) then
  1065. begin
  1066. if left.nodetype=ordconstn then
  1067. begin
  1068. hp:=cordconstnode.create(tordconstnode(left).value,
  1069. resulttype,true);
  1070. result:=hp;
  1071. exit;
  1072. end
  1073. else
  1074. begin
  1075. if IsConvertable(u16bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  1076. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1077. end;
  1078. end
  1079. { ordinal to pointer }
  1080. else
  1081. if (m_delphi in aktmodeswitches) and
  1082. is_ordinal(left.resulttype.def) and
  1083. (resulttype.def.deftype=pointerdef) then
  1084. begin
  1085. if left.nodetype=pointerconstn then
  1086. begin
  1087. hp:=cordconstnode.create(tpointerconstnode(left).value,
  1088. resulttype,true);
  1089. result:=hp;
  1090. exit;
  1091. end
  1092. else
  1093. begin
  1094. if IsConvertable(left.resulttype.def,ordpointertype.def,convtype,ordconstn,false)=0 then
  1095. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1096. end;
  1097. end
  1098. { only if the same size or formal def }
  1099. { why do we allow typecasting of voiddef ?? (PM) }
  1100. else
  1101. begin
  1102. if not(
  1103. (left.resulttype.def.deftype=formaldef) or
  1104. (not(is_open_array(left.resulttype.def)) and
  1105. (left.resulttype.def.size=resulttype.def.size)) or
  1106. (is_void(left.resulttype.def) and
  1107. (left.nodetype=derefn))
  1108. ) then
  1109. CGMessage(cg_e_illegal_type_conversion);
  1110. if ((left.resulttype.def.deftype=orddef) and
  1111. (resulttype.def.deftype=pointerdef)) or
  1112. ((resulttype.def.deftype=orddef) and
  1113. (left.resulttype.def.deftype=pointerdef)) then
  1114. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  1115. end;
  1116. { the conversion into a strutured type is only }
  1117. { possible, if the source is not a register }
  1118. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  1119. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  1120. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  1121. it also works if the assignment is overloaded
  1122. YES but this code is not executed if assignment is overloaded (PM)
  1123. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  1124. CGMessage(cg_e_illegal_type_conversion);
  1125. end
  1126. else
  1127. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1128. end;
  1129. { tp7 procvar support, when right is not a procvardef and we got a
  1130. loadn of a procvar then convert to a calln, the check for the
  1131. result is already done in is_convertible, also no conflict with
  1132. @procvar is here because that has an extra addrn }
  1133. if (m_tp_procvar in aktmodeswitches) and
  1134. (resulttype.def.deftype<>procvardef) and
  1135. (left.resulttype.def.deftype=procvardef) and
  1136. (left.nodetype=loadn) then
  1137. begin
  1138. hp:=ccallnode.create(nil,nil,nil,nil);
  1139. tcallnode(hp).set_procvar(left);
  1140. resulttypepass(hp);
  1141. left:=hp;
  1142. end;
  1143. { remove typeconv after niln, but not when the result is a
  1144. methodpointer. The typeconv of the methodpointer will then
  1145. take care of updateing size of niln to OS_64 }
  1146. if (left.nodetype=niln) and
  1147. not((resulttype.def.deftype=procvardef) and
  1148. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1149. begin
  1150. left.resulttype:=resulttype;
  1151. result:=left;
  1152. left:=nil;
  1153. exit;
  1154. end;
  1155. { ordinal contants can be directly converted }
  1156. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) and
  1157. { but not char to char because it is a widechar to char or via versa }
  1158. { which needs extra code to do the code page transistion }
  1159. not(convtype=tc_char_2_char) then
  1160. begin
  1161. { replace the resulttype and recheck the range }
  1162. left.resulttype:=resulttype;
  1163. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  1164. result:=left;
  1165. left:=nil;
  1166. exit;
  1167. end;
  1168. { fold nil to any pointer type }
  1169. if (left.nodetype=niln) and (resulttype.def.deftype=pointerdef) then
  1170. begin
  1171. hp:=cnilnode.create;
  1172. hp.resulttype:=resulttype;
  1173. result:=hp;
  1174. exit;
  1175. end;
  1176. { further, pointerconstn to any pointer is folded too }
  1177. if (left.nodetype=pointerconstn) and (resulttype.def.deftype=pointerdef) then
  1178. begin
  1179. left.resulttype:=resulttype;
  1180. result:=left;
  1181. left:=nil;
  1182. exit;
  1183. end;
  1184. { now call the resulttype helper to do constant folding }
  1185. result:=resulttype_call_helper(convtype);
  1186. end;
  1187. {$ifdef var_notification}
  1188. procedure Ttypeconvnode.mark_write;
  1189. begin
  1190. left.mark_write;
  1191. end;
  1192. {$endif}
  1193. function ttypeconvnode.first_cord_to_pointer : tnode;
  1194. begin
  1195. result:=nil;
  1196. internalerror(200104043);
  1197. end;
  1198. function ttypeconvnode.first_int_to_int : tnode;
  1199. begin
  1200. first_int_to_int:=nil;
  1201. if (left.location.loc<>LOC_REGISTER) and
  1202. (resulttype.def.size>left.resulttype.def.size) then
  1203. location.loc:=LOC_REGISTER;
  1204. if is_64bitint(resulttype.def) then
  1205. registers32:=max(registers32,2)
  1206. else
  1207. registers32:=max(registers32,1);
  1208. end;
  1209. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1210. begin
  1211. first_cstring_to_pchar:=nil;
  1212. registers32:=1;
  1213. location.loc:=LOC_REGISTER;
  1214. end;
  1215. function ttypeconvnode.first_string_to_chararray : tnode;
  1216. begin
  1217. first_string_to_chararray:=nil;
  1218. registers32:=1;
  1219. location.loc:=LOC_REGISTER;
  1220. end;
  1221. function ttypeconvnode.first_char_to_string : tnode;
  1222. begin
  1223. first_char_to_string:=nil;
  1224. location.loc:=LOC_CREFERENCE;
  1225. end;
  1226. function ttypeconvnode.first_nothing : tnode;
  1227. begin
  1228. first_nothing:=nil;
  1229. end;
  1230. function ttypeconvnode.first_array_to_pointer : tnode;
  1231. begin
  1232. first_array_to_pointer:=nil;
  1233. if registers32<1 then
  1234. registers32:=1;
  1235. location.loc:=LOC_REGISTER;
  1236. end;
  1237. function ttypeconvnode.first_int_to_real: tnode;
  1238. var
  1239. fname: string[19];
  1240. typname : string[12];
  1241. begin
  1242. { Get the type name }
  1243. { Normally the typename should be one of the following:
  1244. single, double - carl
  1245. }
  1246. typname := lower(pbestrealtype^.def.gettypename);
  1247. { converting a 64bit integer to a float requires a helper }
  1248. if is_64bitint(left.resulttype.def) then
  1249. begin
  1250. if is_signed(left.resulttype.def) then
  1251. fname := 'fpc_int64_to_'+typname
  1252. else
  1253. fname := 'fpc_qword_to_'+typname;
  1254. result := ccallnode.createintern(fname,ccallparanode.create(
  1255. left,nil));
  1256. left:=nil;
  1257. firstpass(result);
  1258. exit;
  1259. end
  1260. else
  1261. { other integers are supposed to be 32 bit }
  1262. begin
  1263. if is_signed(left.resulttype.def) then
  1264. fname := 'fpc_longint_to_'+typname
  1265. else
  1266. fname := 'fpc_longword_to_'+typname;
  1267. result := ccallnode.createintern(fname,ccallparanode.create(
  1268. left,nil));
  1269. left:=nil;
  1270. firstpass(result);
  1271. exit;
  1272. end;
  1273. end;
  1274. function ttypeconvnode.first_real_to_real : tnode;
  1275. begin
  1276. first_real_to_real:=nil;
  1277. { comp isn't a floating type }
  1278. {$ifdef i386}
  1279. if (tfloatdef(resulttype.def).typ=s64comp) and
  1280. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1281. not (nf_explizit in flags) then
  1282. CGMessage(type_w_convert_real_2_comp);
  1283. {$endif}
  1284. if registersfpu<1 then
  1285. registersfpu:=1;
  1286. location.loc:=LOC_FPUREGISTER;
  1287. end;
  1288. function ttypeconvnode.first_pointer_to_array : tnode;
  1289. begin
  1290. first_pointer_to_array:=nil;
  1291. if registers32<1 then
  1292. registers32:=1;
  1293. location.loc:=LOC_REFERENCE;
  1294. end;
  1295. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1296. begin
  1297. first_cchar_to_pchar:=nil;
  1298. internalerror(200104021);
  1299. end;
  1300. function ttypeconvnode.first_bool_to_int : tnode;
  1301. begin
  1302. first_bool_to_int:=nil;
  1303. { byte(boolean) or word(wordbool) or longint(longbool) must
  1304. be accepted for var parameters }
  1305. if (nf_explizit in flags) and
  1306. (left.resulttype.def.size=resulttype.def.size) and
  1307. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1308. exit;
  1309. { when converting to 64bit, first convert to a 32bit int and then }
  1310. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1311. if resulttype.def.size > sizeof(aword) then
  1312. begin
  1313. result := ctypeconvnode.create(left,u32bittype);
  1314. result.toggleflag(nf_explizit);
  1315. result := ctypeconvnode.create(result,resulttype);
  1316. left := nil;
  1317. firstpass(result);
  1318. exit;
  1319. end;
  1320. location.loc:=LOC_REGISTER;
  1321. if registers32<1 then
  1322. registers32:=1;
  1323. end;
  1324. function ttypeconvnode.first_int_to_bool : tnode;
  1325. begin
  1326. first_int_to_bool:=nil;
  1327. { byte(boolean) or word(wordbool) or longint(longbool) must
  1328. be accepted for var parameters }
  1329. if (nf_explizit in flags) and
  1330. (left.resulttype.def.size=resulttype.def.size) and
  1331. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1332. exit;
  1333. location.loc:=LOC_REGISTER;
  1334. { need if bool to bool !!
  1335. not very nice !!
  1336. insertypeconv(left,s32bittype);
  1337. left.explizit:=true;
  1338. firstpass(left); }
  1339. if registers32<1 then
  1340. registers32:=1;
  1341. end;
  1342. function ttypeconvnode.first_bool_to_bool : tnode;
  1343. begin
  1344. first_bool_to_bool:=nil;
  1345. location.loc:=LOC_REGISTER;
  1346. if registers32<1 then
  1347. registers32:=1;
  1348. end;
  1349. function ttypeconvnode.first_char_to_char : tnode;
  1350. begin
  1351. first_char_to_char:=nil;
  1352. location.loc:=LOC_REGISTER;
  1353. if registers32<1 then
  1354. registers32:=1;
  1355. end;
  1356. function ttypeconvnode.first_proc_to_procvar : tnode;
  1357. begin
  1358. first_proc_to_procvar:=nil;
  1359. if (left.location.loc<>LOC_REFERENCE) then
  1360. CGMessage(cg_e_illegal_expression);
  1361. registers32:=left.registers32;
  1362. if registers32<1 then
  1363. registers32:=1;
  1364. location.loc:=LOC_REGISTER;
  1365. end;
  1366. function ttypeconvnode.first_load_smallset : tnode;
  1367. var
  1368. srsym: ttypesym;
  1369. p: tcallparanode;
  1370. begin
  1371. if not searchsystype('FPC_SMALL_SET',srsym) then
  1372. internalerror(200108313);
  1373. p := ccallparanode.create(left,nil);
  1374. { reused }
  1375. left := nil;
  1376. { convert parameter explicitely to fpc_small_set }
  1377. p.left := ctypeconvnode.create(p.left,srsym.restype);
  1378. p.left.toggleflag(nf_explizit);
  1379. { create call, adjust resulttype }
  1380. result :=
  1381. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1382. firstpass(result);
  1383. end;
  1384. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1385. begin
  1386. first_ansistring_to_pchar:=nil;
  1387. location.loc:=LOC_REGISTER;
  1388. if registers32<1 then
  1389. registers32:=1;
  1390. end;
  1391. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1392. begin
  1393. first_arrayconstructor_to_set:=nil;
  1394. internalerror(200104022);
  1395. end;
  1396. function ttypeconvnode.first_class_to_intf : tnode;
  1397. begin
  1398. first_class_to_intf:=nil;
  1399. location.loc:=LOC_REFERENCE;
  1400. if registers32<1 then
  1401. registers32:=1;
  1402. end;
  1403. function ttypeconvnode._first_int_to_int : tnode;
  1404. begin
  1405. result:=first_int_to_int;
  1406. end;
  1407. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1408. begin
  1409. result:=first_cstring_to_pchar;
  1410. end;
  1411. function ttypeconvnode._first_string_to_chararray : tnode;
  1412. begin
  1413. result:=first_string_to_chararray;
  1414. end;
  1415. function ttypeconvnode._first_char_to_string : tnode;
  1416. begin
  1417. result:=first_char_to_string;
  1418. end;
  1419. function ttypeconvnode._first_nothing : tnode;
  1420. begin
  1421. result:=first_nothing;
  1422. end;
  1423. function ttypeconvnode._first_array_to_pointer : tnode;
  1424. begin
  1425. result:=first_array_to_pointer;
  1426. end;
  1427. function ttypeconvnode._first_int_to_real : tnode;
  1428. begin
  1429. result:=first_int_to_real;
  1430. end;
  1431. function ttypeconvnode._first_real_to_real : tnode;
  1432. begin
  1433. result:=first_real_to_real;
  1434. end;
  1435. function ttypeconvnode._first_pointer_to_array : tnode;
  1436. begin
  1437. result:=first_pointer_to_array;
  1438. end;
  1439. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1440. begin
  1441. result:=first_cchar_to_pchar;
  1442. end;
  1443. function ttypeconvnode._first_bool_to_int : tnode;
  1444. begin
  1445. result:=first_bool_to_int;
  1446. end;
  1447. function ttypeconvnode._first_int_to_bool : tnode;
  1448. begin
  1449. result:=first_int_to_bool;
  1450. end;
  1451. function ttypeconvnode._first_bool_to_bool : tnode;
  1452. begin
  1453. result:=first_bool_to_bool;
  1454. end;
  1455. function ttypeconvnode._first_proc_to_procvar : tnode;
  1456. begin
  1457. result:=first_proc_to_procvar;
  1458. end;
  1459. function ttypeconvnode._first_load_smallset : tnode;
  1460. begin
  1461. result:=first_load_smallset;
  1462. end;
  1463. function ttypeconvnode._first_cord_to_pointer : tnode;
  1464. begin
  1465. result:=first_cord_to_pointer;
  1466. end;
  1467. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1468. begin
  1469. result:=first_ansistring_to_pchar;
  1470. end;
  1471. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1472. begin
  1473. result:=first_arrayconstructor_to_set;
  1474. end;
  1475. function ttypeconvnode._first_class_to_intf : tnode;
  1476. begin
  1477. result:=first_class_to_intf;
  1478. end;
  1479. function ttypeconvnode._first_char_to_char : tnode;
  1480. begin
  1481. result:=first_char_to_char;
  1482. end;
  1483. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1484. const
  1485. firstconvert : array[tconverttype] of pointer = (
  1486. @ttypeconvnode._first_nothing, {equal}
  1487. @ttypeconvnode._first_nothing, {not_possible}
  1488. nil, { removed in resulttype_string_to_string }
  1489. @ttypeconvnode._first_char_to_string,
  1490. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1491. nil, { removed in resulttype_chararray_to_string }
  1492. @ttypeconvnode._first_cchar_to_pchar,
  1493. @ttypeconvnode._first_cstring_to_pchar,
  1494. @ttypeconvnode._first_ansistring_to_pchar,
  1495. @ttypeconvnode._first_string_to_chararray,
  1496. nil, { removed in resulttype_chararray_to_string }
  1497. @ttypeconvnode._first_array_to_pointer,
  1498. @ttypeconvnode._first_pointer_to_array,
  1499. @ttypeconvnode._first_int_to_int,
  1500. @ttypeconvnode._first_int_to_bool,
  1501. @ttypeconvnode._first_bool_to_bool,
  1502. @ttypeconvnode._first_bool_to_int,
  1503. @ttypeconvnode._first_real_to_real,
  1504. @ttypeconvnode._first_int_to_real,
  1505. @ttypeconvnode._first_proc_to_procvar,
  1506. @ttypeconvnode._first_arrayconstructor_to_set,
  1507. @ttypeconvnode._first_load_smallset,
  1508. @ttypeconvnode._first_cord_to_pointer,
  1509. @ttypeconvnode._first_nothing,
  1510. @ttypeconvnode._first_nothing,
  1511. @ttypeconvnode._first_class_to_intf,
  1512. @ttypeconvnode._first_char_to_char,
  1513. @ttypeconvnode._first_nothing,
  1514. @ttypeconvnode._first_nothing
  1515. );
  1516. type
  1517. tprocedureofobject = function : tnode of object;
  1518. var
  1519. r : packed record
  1520. proc : pointer;
  1521. obj : pointer;
  1522. end;
  1523. begin
  1524. { this is a little bit dirty but it works }
  1525. { and should be quite portable too }
  1526. r.proc:=firstconvert[c];
  1527. r.obj:=self;
  1528. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1529. end;
  1530. function ttypeconvnode.pass_1 : tnode;
  1531. begin
  1532. result:=nil;
  1533. firstpass(left);
  1534. if codegenerror then
  1535. exit;
  1536. { load the value_str from the left part }
  1537. registers32:=left.registers32;
  1538. registersfpu:=left.registersfpu;
  1539. {$ifdef SUPPORT_MMX}
  1540. registersmmx:=left.registersmmx;
  1541. {$endif}
  1542. location.loc:=left.location.loc;
  1543. if nf_explizit in flags then
  1544. begin
  1545. { check if the result could be in a register }
  1546. if not(tstoreddef(resulttype.def).is_intregable) and
  1547. not(tstoreddef(resulttype.def).is_fpuregable) then
  1548. make_not_regable(left);
  1549. end;
  1550. result:=first_call_helper(convtype);
  1551. end;
  1552. function ttypeconvnode.docompare(p: tnode) : boolean;
  1553. begin
  1554. docompare :=
  1555. inherited docompare(p) and
  1556. (convtype = ttypeconvnode(p).convtype);
  1557. end;
  1558. {*****************************************************************************
  1559. TISNODE
  1560. *****************************************************************************}
  1561. constructor tisnode.create(l,r : tnode);
  1562. begin
  1563. inherited create(isn,l,r);
  1564. end;
  1565. function tisnode.det_resulttype:tnode;
  1566. var
  1567. paras: tcallparanode;
  1568. begin
  1569. result:=nil;
  1570. resulttypepass(left);
  1571. resulttypepass(right);
  1572. set_varstate(left,true);
  1573. set_varstate(right,true);
  1574. if codegenerror then
  1575. exit;
  1576. if (right.resulttype.def.deftype=classrefdef) then
  1577. begin
  1578. { left must be a class }
  1579. if is_class(left.resulttype.def) then
  1580. begin
  1581. { the operands must be related }
  1582. if (not(tobjectdef(left.resulttype.def).is_related(
  1583. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1584. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1585. tobjectdef(left.resulttype.def)))) then
  1586. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1587. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1588. end
  1589. else
  1590. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1591. { call fpc_do_is helper }
  1592. paras := ccallparanode.create(
  1593. left,
  1594. ccallparanode.create(
  1595. right,nil));
  1596. result := ccallnode.createintern('fpc_do_is',paras);
  1597. left := nil;
  1598. right := nil;
  1599. end
  1600. else if is_interface(right.resulttype.def) then
  1601. begin
  1602. { left is a class }
  1603. if is_class(left.resulttype.def) then
  1604. begin
  1605. { the operands must be related }
  1606. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1607. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1608. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1609. end
  1610. { left is an interface }
  1611. else if is_interface(left.resulttype.def) then
  1612. begin
  1613. { the operands must be related }
  1614. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1615. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1616. CGMessage(type_e_mismatch);
  1617. end
  1618. else
  1619. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1620. { call fpc_do_is helper }
  1621. paras := ccallparanode.create(
  1622. left,
  1623. ccallparanode.create(
  1624. right,nil));
  1625. result := ccallnode.createintern('fpc_do_is',paras);
  1626. left := nil;
  1627. right := nil;
  1628. end
  1629. else
  1630. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1631. resulttype:=booltype;
  1632. end;
  1633. function tisnode.pass_1 : tnode;
  1634. begin
  1635. internalerror(200204254);
  1636. result:=nil;
  1637. end;
  1638. { dummy pass_2, it will never be called, but we need one since }
  1639. { you can't instantiate an abstract class }
  1640. procedure tisnode.pass_2;
  1641. begin
  1642. end;
  1643. {*****************************************************************************
  1644. TASNODE
  1645. *****************************************************************************}
  1646. constructor tasnode.create(l,r : tnode);
  1647. begin
  1648. inherited create(asn,l,r);
  1649. call := nil;
  1650. end;
  1651. destructor tasnode.destroy;
  1652. begin
  1653. call.free;
  1654. inherited destroy;
  1655. end;
  1656. function tasnode.det_resulttype:tnode;
  1657. var
  1658. hp : tnode;
  1659. begin
  1660. result:=nil;
  1661. resulttypepass(right);
  1662. resulttypepass(left);
  1663. set_varstate(right,true);
  1664. set_varstate(left,true);
  1665. if codegenerror then
  1666. exit;
  1667. if (right.resulttype.def.deftype=classrefdef) then
  1668. begin
  1669. { left must be a class }
  1670. if is_class(left.resulttype.def) then
  1671. begin
  1672. { the operands must be related }
  1673. if (not(tobjectdef(left.resulttype.def).is_related(
  1674. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1675. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1676. tobjectdef(left.resulttype.def)))) then
  1677. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1678. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1679. end
  1680. else
  1681. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1682. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1683. end
  1684. else if is_interface(right.resulttype.def) then
  1685. begin
  1686. { left is a class }
  1687. if is_class(left.resulttype.def) then
  1688. begin
  1689. { the operands must be related }
  1690. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1691. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1692. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1693. end
  1694. { left is an interface }
  1695. else if is_interface(left.resulttype.def) then
  1696. begin
  1697. { the operands must be related }
  1698. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1699. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1700. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1701. end
  1702. else
  1703. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1704. resulttype:=right.resulttype;
  1705. { load the GUID of the interface }
  1706. if (right.nodetype=typen) then
  1707. begin
  1708. if tobjectdef(right.resulttype.def).isiidguidvalid then
  1709. begin
  1710. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid);
  1711. right.free;
  1712. right:=hp;
  1713. end
  1714. else
  1715. internalerror(200206282);
  1716. resulttypepass(right);
  1717. end;
  1718. end
  1719. else
  1720. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1721. end;
  1722. function tasnode.getcopy: tnode;
  1723. begin
  1724. result := inherited getcopy;
  1725. if assigned(call) then
  1726. tasnode(result).call := call.getcopy
  1727. else
  1728. tasnode(result).call := nil;
  1729. end;
  1730. function tasnode.pass_1 : tnode;
  1731. var
  1732. procname: string;
  1733. begin
  1734. result:=nil;
  1735. if not assigned(call) then
  1736. begin
  1737. if is_class(left.resulttype.def) and
  1738. (right.resulttype.def.deftype=classrefdef) then
  1739. call := ccallnode.createinternres('fpc_do_as',
  1740. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1741. resulttype)
  1742. else
  1743. begin
  1744. if is_class(left.resulttype.def) then
  1745. procname := 'fpc_class_as_intf'
  1746. else
  1747. procname := 'fpc_intf_as';
  1748. call := ccallnode.createinternres(procname,
  1749. ccallparanode.create(right,ccallparanode.create(left,nil)),
  1750. resulttype);
  1751. end;
  1752. left := nil;
  1753. right := nil;
  1754. firstpass(call);
  1755. if codegenerror then
  1756. exit;
  1757. location.loc:=call.location.loc;
  1758. registers32:=call.registers32;
  1759. registersfpu:=call.registersfpu;
  1760. {$ifdef SUPPORT_MMX}
  1761. registersmmx:=call.registersmmx;
  1762. {$endif SUPPORT_MMX}
  1763. end;
  1764. end;
  1765. begin
  1766. ctypeconvnode:=ttypeconvnode;
  1767. casnode:=tasnode;
  1768. cisnode:=tisnode;
  1769. end.
  1770. {
  1771. $Log$
  1772. Revision 1.81 2002-09-16 14:11:13 peter
  1773. * add argument to equal_paras() to support default values or not
  1774. Revision 1.80 2002/09/07 20:40:23 carl
  1775. * cardinal -> longword
  1776. Revision 1.79 2002/09/07 15:25:03 peter
  1777. * old logs removed and tabs fixed
  1778. Revision 1.78 2002/09/07 12:16:04 carl
  1779. * second part bug report 1996 fix, testrange in cordconstnode
  1780. only called if option is set (also make parsing a tiny faster)
  1781. Revision 1.77 2002/09/05 05:56:07 jonas
  1782. - reverted my last commit, it was completely bogus :(
  1783. Revision 1.75 2002/09/02 19:24:42 peter
  1784. * array of char support for Str()
  1785. Revision 1.74 2002/09/01 08:01:16 daniel
  1786. * Removed sets from Tcallnode.det_resulttype
  1787. + Added read/write notifications of variables. These will be usefull
  1788. for providing information for several optimizations. For example
  1789. the value of the loop variable of a for loop does matter is the
  1790. variable is read after the for loop, but if it's no longer used
  1791. or written, it doesn't matter and this can be used to optimize
  1792. the loop code generation.
  1793. Revision 1.73 2002/08/23 16:14:49 peter
  1794. * tempgen cleanup
  1795. * tt_noreuse temp type added that will be used in genentrycode
  1796. Revision 1.72 2002/08/20 18:23:33 jonas
  1797. * the as node again uses a compilerproc
  1798. + (untested) support for interface "as" statements
  1799. Revision 1.71 2002/08/19 19:36:43 peter
  1800. * More fixes for cross unit inlining, all tnodes are now implemented
  1801. * Moved pocall_internconst to po_internconst because it is not a
  1802. calling type at all and it conflicted when inlining of these small
  1803. functions was requested
  1804. Revision 1.70 2002/08/17 09:23:36 florian
  1805. * first part of procinfo rewrite
  1806. Revision 1.69 2002/08/14 19:26:55 carl
  1807. + generic int_to_real type conversion
  1808. + generic unaryminus node
  1809. Revision 1.68 2002/08/11 16:08:55 florian
  1810. + support of explicit type case boolean->char
  1811. Revision 1.67 2002/08/11 15:28:00 florian
  1812. + support of explicit type case <any ordinal type>->pointer
  1813. (delphi mode only)
  1814. Revision 1.66 2002/08/09 07:33:01 florian
  1815. * a couple of interface related fixes
  1816. Revision 1.65 2002/07/29 21:23:42 florian
  1817. * more fixes for the ppc
  1818. + wrappers for the tcnvnode.first_* stuff introduced
  1819. Revision 1.64 2002/07/23 12:34:30 daniel
  1820. * Readded old set code. To use it define 'oldset'. Activated by default
  1821. for ppc.
  1822. Revision 1.63 2002/07/23 09:51:22 daniel
  1823. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1824. are worth comitting.
  1825. Revision 1.62 2002/07/22 11:48:04 daniel
  1826. * Sets are now internally sets.
  1827. Revision 1.61 2002/07/20 17:16:02 florian
  1828. + source code page support
  1829. Revision 1.60 2002/07/20 11:57:54 florian
  1830. * types.pas renamed to defbase.pas because D6 contains a types
  1831. unit so this would conflicts if D6 programms are compiled
  1832. + Willamette/SSE2 instructions to assembler added
  1833. Revision 1.59 2002/07/01 16:23:53 peter
  1834. * cg64 patch
  1835. * basics for currency
  1836. * asnode updates for class and interface (not finished)
  1837. Revision 1.58 2002/05/18 13:34:09 peter
  1838. * readded missing revisions
  1839. }