ncnv.pas 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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),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)),
  623. cordconstnode.create(1,s32bittype))
  624. else
  625. left := caddnode.create(orn,left,
  626. cordconstnode.create(1 shl 8,s32bittype));
  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))),cchartype);
  657. result:=hp;
  658. end
  659. else if (torddef(resulttype.def).typ=uwidechar) and
  660. (torddef(left.resulttype.def).typ=uchar) then
  661. begin
  662. hp:=cordconstnode.create(
  663. asciichar2unicode(chr(tordconstnode(left).value)),cwidechartype);
  664. result:=hp;
  665. end
  666. else
  667. internalerror(200105131);
  668. exit;
  669. end;
  670. end;
  671. function ttypeconvnode.resulttype_int_to_real : tnode;
  672. var
  673. t : trealconstnode;
  674. begin
  675. result:=nil;
  676. if left.nodetype=ordconstn then
  677. begin
  678. t:=crealconstnode.create(tordconstnode(left).value,resulttype);
  679. result:=t;
  680. end;
  681. end;
  682. function ttypeconvnode.resulttype_real_to_real : tnode;
  683. var
  684. t : tnode;
  685. begin
  686. result:=nil;
  687. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  688. begin
  689. end
  690. else
  691. if is_currency(resulttype.def) then
  692. begin
  693. end;
  694. if left.nodetype=realconstn then
  695. begin
  696. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  697. result:=t;
  698. end;
  699. end;
  700. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  701. begin
  702. result:=nil;
  703. if is_pwidechar(resulttype.def) then
  704. inserttypeconv(left,cwidestringtype)
  705. else
  706. inserttypeconv(left,cshortstringtype);
  707. { evaluate again, reset resulttype so the convert_typ
  708. will be calculated again and cstring_to_pchar will
  709. be used for futher conversion }
  710. result:=det_resulttype;
  711. end;
  712. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  713. begin
  714. result:=nil;
  715. if is_pwidechar(resulttype.def) then
  716. inserttypeconv(left,cwidestringtype);
  717. end;
  718. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  719. var
  720. hp : tnode;
  721. begin
  722. result:=nil;
  723. if left.nodetype<>arrayconstructorn then
  724. internalerror(5546);
  725. { remove typeconv node }
  726. hp:=left;
  727. left:=nil;
  728. { create a set constructor tree }
  729. arrayconstructor_to_set(hp);
  730. result:=hp;
  731. end;
  732. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  733. begin
  734. result := ccallnode.createinternres(
  735. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  736. ccallparanode.create(left,nil),resulttype);
  737. left := nil;
  738. end;
  739. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  740. begin
  741. if tobjectdef(left.resulttype.def).isiidguidvalid then
  742. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid);
  743. end;
  744. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  745. begin
  746. { a dynamic array is a pointer to an array, so to convert it to }
  747. { an open array, we have to dereference it (JM) }
  748. result := ctypeconvnode.create(left,voidpointertype);
  749. { left is reused }
  750. left := nil;
  751. result.toggleflag(nf_explizit);
  752. result := cderefnode.create(result);
  753. result.resulttype := resulttype;
  754. end;
  755. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  756. const
  757. resulttypeconvert : array[tconverttype] of pointer = (
  758. {equal} nil,
  759. {not_possible} nil,
  760. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  761. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  762. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  763. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  764. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  765. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  766. { ansistring_2_pchar } nil,
  767. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  768. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  769. { array_2_pointer } nil,
  770. { pointer_2_array } nil,
  771. { int_2_int } nil,
  772. { int_2_bool } nil,
  773. { bool_2_bool } nil,
  774. { bool_2_int } nil,
  775. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  776. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  777. { proc_2_procvar } nil,
  778. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  779. { load_smallset } nil,
  780. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  781. { intf_2_string } nil,
  782. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  783. { class_2_intf } nil,
  784. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  785. { normal_2_smallset} nil,
  786. { dynarray_2_openarray} @resulttype_dynarray_to_openarray
  787. );
  788. type
  789. tprocedureofobject = function : tnode of object;
  790. var
  791. r : packed record
  792. proc : pointer;
  793. obj : pointer;
  794. end;
  795. begin
  796. result:=nil;
  797. { this is a little bit dirty but it works }
  798. { and should be quite portable too }
  799. r.proc:=resulttypeconvert[c];
  800. r.obj:=self;
  801. if assigned(r.proc) then
  802. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  803. end;
  804. function ttypeconvnode.det_resulttype:tnode;
  805. var
  806. hp : tnode;
  807. currprocdef,
  808. aprocdef : tprocdef;
  809. begin
  810. result:=nil;
  811. resulttype:=totype;
  812. resulttypepass(left);
  813. if codegenerror then
  814. exit;
  815. { remove obsolete type conversions }
  816. if is_equal(left.resulttype.def,resulttype.def) then
  817. begin
  818. { becuase is_equal only checks the basetype for sets we need to
  819. check here if we are loading a smallset into a normalset }
  820. if (resulttype.def.deftype=setdef) and
  821. (left.resulttype.def.deftype=setdef) and
  822. ((tsetdef(resulttype.def).settype = smallset) xor
  823. (tsetdef(left.resulttype.def).settype = smallset)) then
  824. begin
  825. { constant sets can be converted by changing the type only }
  826. if (left.nodetype=setconstn) then
  827. begin
  828. tsetdef(left.resulttype.def).changesettype(tsetdef(resulttype.def).settype);
  829. result:=left;
  830. left:=nil;
  831. exit;
  832. end;
  833. if (tsetdef(resulttype.def).settype <> smallset) then
  834. convtype:=tc_load_smallset
  835. else
  836. convtype := tc_normal_2_smallset;
  837. exit;
  838. end
  839. else
  840. begin
  841. left.resulttype:=resulttype;
  842. result:=left;
  843. left:=nil;
  844. exit;
  845. end;
  846. end;
  847. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  848. if assigned(aprocdef) then
  849. begin
  850. procinfo.flags:=procinfo.flags or pi_do_call;
  851. hp:=ccallnode.create(ccallparanode.create(left,nil),
  852. overloaded_operators[_assignment],nil,nil);
  853. { tell explicitly which def we must use !! (PM) }
  854. tcallnode(hp).procdefinition:=aprocdef;
  855. left:=nil;
  856. result:=hp;
  857. exit;
  858. end;
  859. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  860. begin
  861. {Procedures have a resulttype.def of voiddef and functions of their
  862. own resulttype.def. They will therefore always be incompatible with
  863. a procvar. Because isconvertable cannot check for procedures we
  864. use an extra check for them.}
  865. if (m_tp_procvar in aktmodeswitches) then
  866. begin
  867. if (resulttype.def.deftype=procvardef) and
  868. (is_procsym_load(left) or is_procsym_call(left)) then
  869. begin
  870. if is_procsym_call(left) then
  871. begin
  872. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  873. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  874. currprocdef,tcallnode(left).symtableproc);
  875. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  876. assigned(tcallnode(left).methodpointer) then
  877. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  878. resulttypepass(hp);
  879. left.free;
  880. left:=hp;
  881. aprocdef:=tprocdef(left.resulttype.def);
  882. end
  883. else
  884. begin
  885. if (left.nodetype<>addrn) then
  886. aprocdef:=tprocsym(tloadnode(left).symtableentry).first_procdef;
  887. end;
  888. convtype:=tc_proc_2_procvar;
  889. { Now check if the procedure we are going to assign to
  890. the procvar, is compatible with the procvar's type }
  891. if assigned(aprocdef) then
  892. begin
  893. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def),false) then
  894. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  895. end
  896. else
  897. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  898. exit;
  899. end;
  900. end;
  901. if nf_explizit in flags then
  902. begin
  903. { check if the result could be in a register }
  904. if not(tstoreddef(resulttype.def).is_intregable) and
  905. not(tstoreddef(resulttype.def).is_fpuregable) then
  906. make_not_regable(left);
  907. { boolean to byte are special because the
  908. location can be different }
  909. if is_integer(resulttype.def) and
  910. is_boolean(left.resulttype.def) then
  911. begin
  912. convtype:=tc_bool_2_int;
  913. exit;
  914. end;
  915. if is_char(resulttype.def) and
  916. is_boolean(left.resulttype.def) then
  917. begin
  918. convtype:=tc_bool_2_int;
  919. exit;
  920. end;
  921. { ansistring to pchar }
  922. if is_pchar(resulttype.def) and
  923. is_ansistring(left.resulttype.def) then
  924. begin
  925. convtype:=tc_ansistring_2_pchar;
  926. exit;
  927. end;
  928. { do common tc_equal cast }
  929. convtype:=tc_equal;
  930. { enum to ordinal will always be s32bit }
  931. if (left.resulttype.def.deftype=enumdef) and
  932. is_ordinal(resulttype.def) then
  933. begin
  934. if left.nodetype=ordconstn then
  935. begin
  936. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  937. result:=hp;
  938. exit;
  939. end
  940. else
  941. begin
  942. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  943. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  944. end;
  945. end
  946. { ordinal to enumeration }
  947. else
  948. if (resulttype.def.deftype=enumdef) and
  949. is_ordinal(left.resulttype.def) then
  950. begin
  951. if left.nodetype=ordconstn then
  952. begin
  953. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  954. result:=hp;
  955. exit;
  956. end
  957. else
  958. begin
  959. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  960. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  961. end;
  962. end
  963. { nil to ordinal node }
  964. else if (left.nodetype=niln) and is_ordinal(resulttype.def) then
  965. begin
  966. hp:=cordconstnode.create(0,resulttype);
  967. result:=hp;
  968. exit;
  969. end
  970. { constant pointer to ordinal }
  971. else if is_ordinal(resulttype.def) and
  972. (left.nodetype=pointerconstn) then
  973. begin
  974. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  975. result:=hp;
  976. exit;
  977. end
  978. { class to class or object to object, with checkobject support }
  979. else if (resulttype.def.deftype=objectdef) and
  980. (left.resulttype.def.deftype=objectdef) then
  981. begin
  982. if (cs_check_object in aktlocalswitches) then
  983. begin
  984. if is_class_or_interface(resulttype.def) then
  985. begin
  986. { we can translate the typeconvnode to 'as' when
  987. typecasting to a class or interface }
  988. hp:=casnode.create(left,cloadvmtnode.create(ctypenode.create(resulttype)));
  989. left:=nil;
  990. result:=hp;
  991. exit;
  992. end;
  993. end
  994. else
  995. begin
  996. { check if the types are related }
  997. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  998. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  999. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  1000. end;
  1001. end
  1002. {Are we typecasting an ordconst to a char?}
  1003. else
  1004. if is_char(resulttype.def) and
  1005. is_ordinal(left.resulttype.def) then
  1006. begin
  1007. if left.nodetype=ordconstn then
  1008. begin
  1009. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  1010. result:=hp;
  1011. exit;
  1012. end
  1013. else
  1014. begin
  1015. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  1016. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1017. end;
  1018. end
  1019. {Are we typecasting an ordconst to a wchar?}
  1020. else
  1021. if is_widechar(resulttype.def) and
  1022. is_ordinal(left.resulttype.def) then
  1023. begin
  1024. if left.nodetype=ordconstn then
  1025. begin
  1026. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  1027. result:=hp;
  1028. exit;
  1029. end
  1030. else
  1031. begin
  1032. if IsConvertable(left.resulttype.def,u16bittype.def,convtype,ordconstn,false)=0 then
  1033. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1034. end;
  1035. end
  1036. { char to ordinal }
  1037. else
  1038. if is_char(left.resulttype.def) and
  1039. is_ordinal(resulttype.def) then
  1040. begin
  1041. if left.nodetype=ordconstn then
  1042. begin
  1043. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  1044. result:=hp;
  1045. exit;
  1046. end
  1047. else
  1048. begin
  1049. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  1050. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1051. end;
  1052. end
  1053. { widechar to ordinal }
  1054. else
  1055. if is_widechar(left.resulttype.def) and
  1056. is_ordinal(resulttype.def) then
  1057. begin
  1058. if left.nodetype=ordconstn then
  1059. begin
  1060. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  1061. result:=hp;
  1062. exit;
  1063. end
  1064. else
  1065. begin
  1066. if IsConvertable(u16bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  1067. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1068. end;
  1069. end
  1070. { ordinal to pointer }
  1071. else
  1072. if (m_delphi in aktmodeswitches) and
  1073. is_ordinal(left.resulttype.def) and
  1074. (resulttype.def.deftype=pointerdef) then
  1075. begin
  1076. if left.nodetype=pointerconstn then
  1077. begin
  1078. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  1079. result:=hp;
  1080. exit;
  1081. end
  1082. else
  1083. begin
  1084. if IsConvertable(left.resulttype.def,ordpointertype.def,convtype,ordconstn,false)=0 then
  1085. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1086. end;
  1087. end
  1088. { only if the same size or formal def }
  1089. { why do we allow typecasting of voiddef ?? (PM) }
  1090. else
  1091. begin
  1092. if not(
  1093. (left.resulttype.def.deftype=formaldef) or
  1094. (not(is_open_array(left.resulttype.def)) and
  1095. (left.resulttype.def.size=resulttype.def.size)) or
  1096. (is_void(left.resulttype.def) and
  1097. (left.nodetype=derefn))
  1098. ) then
  1099. CGMessage(cg_e_illegal_type_conversion);
  1100. if ((left.resulttype.def.deftype=orddef) and
  1101. (resulttype.def.deftype=pointerdef)) or
  1102. ((resulttype.def.deftype=orddef) and
  1103. (left.resulttype.def.deftype=pointerdef)) then
  1104. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  1105. end;
  1106. { the conversion into a strutured type is only }
  1107. { possible, if the source is not a register }
  1108. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  1109. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  1110. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  1111. it also works if the assignment is overloaded
  1112. YES but this code is not executed if assignment is overloaded (PM)
  1113. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  1114. CGMessage(cg_e_illegal_type_conversion);
  1115. end
  1116. else
  1117. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1118. end;
  1119. { tp7 procvar support, when right is not a procvardef and we got a
  1120. loadn of a procvar then convert to a calln, the check for the
  1121. result is already done in is_convertible, also no conflict with
  1122. @procvar is here because that has an extra addrn }
  1123. if (m_tp_procvar in aktmodeswitches) and
  1124. (resulttype.def.deftype<>procvardef) and
  1125. (left.resulttype.def.deftype=procvardef) and
  1126. (left.nodetype=loadn) then
  1127. begin
  1128. hp:=ccallnode.create(nil,nil,nil,nil);
  1129. tcallnode(hp).set_procvar(left);
  1130. resulttypepass(hp);
  1131. left:=hp;
  1132. end;
  1133. { remove typeconv after niln, but not when the result is a
  1134. methodpointer. The typeconv of the methodpointer will then
  1135. take care of updateing size of niln to OS_64 }
  1136. if (left.nodetype=niln) and
  1137. not((resulttype.def.deftype=procvardef) and
  1138. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1139. begin
  1140. left.resulttype:=resulttype;
  1141. result:=left;
  1142. left:=nil;
  1143. exit;
  1144. end;
  1145. { ordinal contants can be directly converted }
  1146. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) and
  1147. { but not char to char because it is a widechar to char or via versa }
  1148. { which needs extra code to do the code page transistion }
  1149. not(convtype=tc_char_2_char) then
  1150. begin
  1151. { replace the resulttype and recheck the range }
  1152. left.resulttype:=resulttype;
  1153. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  1154. result:=left;
  1155. left:=nil;
  1156. exit;
  1157. end;
  1158. { fold nil to any pointer type }
  1159. if (left.nodetype=niln) and (resulttype.def.deftype=pointerdef) then
  1160. begin
  1161. hp:=cnilnode.create;
  1162. hp.resulttype:=resulttype;
  1163. result:=hp;
  1164. exit;
  1165. end;
  1166. { further, pointerconstn to any pointer is folded too }
  1167. if (left.nodetype=pointerconstn) and (resulttype.def.deftype=pointerdef) then
  1168. begin
  1169. left.resulttype:=resulttype;
  1170. result:=left;
  1171. left:=nil;
  1172. exit;
  1173. end;
  1174. { now call the resulttype helper to do constant folding }
  1175. result:=resulttype_call_helper(convtype);
  1176. end;
  1177. {$ifdef var_notification}
  1178. procedure Ttypeconvnode.mark_write;
  1179. begin
  1180. left.mark_write;
  1181. end;
  1182. {$endif}
  1183. function ttypeconvnode.first_cord_to_pointer : tnode;
  1184. begin
  1185. result:=nil;
  1186. internalerror(200104043);
  1187. end;
  1188. function ttypeconvnode.first_int_to_int : tnode;
  1189. begin
  1190. first_int_to_int:=nil;
  1191. if (left.location.loc<>LOC_REGISTER) and
  1192. (resulttype.def.size>left.resulttype.def.size) then
  1193. location.loc:=LOC_REGISTER;
  1194. if is_64bitint(resulttype.def) then
  1195. registers32:=max(registers32,2)
  1196. else
  1197. registers32:=max(registers32,1);
  1198. end;
  1199. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1200. begin
  1201. first_cstring_to_pchar:=nil;
  1202. registers32:=1;
  1203. location.loc:=LOC_REGISTER;
  1204. end;
  1205. function ttypeconvnode.first_string_to_chararray : tnode;
  1206. begin
  1207. first_string_to_chararray:=nil;
  1208. registers32:=1;
  1209. location.loc:=LOC_REGISTER;
  1210. end;
  1211. function ttypeconvnode.first_char_to_string : tnode;
  1212. begin
  1213. first_char_to_string:=nil;
  1214. location.loc:=LOC_CREFERENCE;
  1215. end;
  1216. function ttypeconvnode.first_nothing : tnode;
  1217. begin
  1218. first_nothing:=nil;
  1219. end;
  1220. function ttypeconvnode.first_array_to_pointer : tnode;
  1221. begin
  1222. first_array_to_pointer:=nil;
  1223. if registers32<1 then
  1224. registers32:=1;
  1225. location.loc:=LOC_REGISTER;
  1226. end;
  1227. function ttypeconvnode.first_int_to_real: tnode;
  1228. var
  1229. fname: string[19];
  1230. typname : string[12];
  1231. begin
  1232. { Get the type name }
  1233. { Normally the typename should be one of the following:
  1234. single, double - carl
  1235. }
  1236. typname := lower(pbestrealtype^.def.gettypename);
  1237. { converting a 64bit integer to a float requires a helper }
  1238. if is_64bitint(left.resulttype.def) then
  1239. begin
  1240. if is_signed(left.resulttype.def) then
  1241. fname := 'fpc_int64_to_'+typname
  1242. else
  1243. fname := 'fpc_qword_to_'+typname;
  1244. result := ccallnode.createintern(fname,ccallparanode.create(
  1245. left,nil));
  1246. left:=nil;
  1247. firstpass(result);
  1248. exit;
  1249. end
  1250. else
  1251. { other integers are supposed to be 32 bit }
  1252. begin
  1253. if is_signed(left.resulttype.def) then
  1254. fname := 'fpc_longint_to_'+typname
  1255. else
  1256. fname := 'fpc_cardinal_to_'+typname;
  1257. result := ccallnode.createintern(fname,ccallparanode.create(
  1258. left,nil));
  1259. left:=nil;
  1260. firstpass(result);
  1261. exit;
  1262. end;
  1263. end;
  1264. function ttypeconvnode.first_real_to_real : tnode;
  1265. begin
  1266. first_real_to_real:=nil;
  1267. { comp isn't a floating type }
  1268. {$ifdef i386}
  1269. if (tfloatdef(resulttype.def).typ=s64comp) and
  1270. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1271. not (nf_explizit in flags) then
  1272. CGMessage(type_w_convert_real_2_comp);
  1273. {$endif}
  1274. if registersfpu<1 then
  1275. registersfpu:=1;
  1276. location.loc:=LOC_FPUREGISTER;
  1277. end;
  1278. function ttypeconvnode.first_pointer_to_array : tnode;
  1279. begin
  1280. first_pointer_to_array:=nil;
  1281. if registers32<1 then
  1282. registers32:=1;
  1283. location.loc:=LOC_REFERENCE;
  1284. end;
  1285. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1286. begin
  1287. first_cchar_to_pchar:=nil;
  1288. internalerror(200104021);
  1289. end;
  1290. function ttypeconvnode.first_bool_to_int : tnode;
  1291. begin
  1292. first_bool_to_int:=nil;
  1293. { byte(boolean) or word(wordbool) or longint(longbool) must
  1294. be accepted for var parameters }
  1295. if (nf_explizit in flags) and
  1296. (left.resulttype.def.size=resulttype.def.size) and
  1297. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1298. exit;
  1299. { when converting to 64bit, first convert to a 32bit int and then }
  1300. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1301. if resulttype.def.size > sizeof(aword) then
  1302. begin
  1303. result := ctypeconvnode.create(left,u32bittype);
  1304. result.toggleflag(nf_explizit);
  1305. result := ctypeconvnode.create(result,resulttype);
  1306. left := nil;
  1307. firstpass(result);
  1308. exit;
  1309. end;
  1310. location.loc:=LOC_REGISTER;
  1311. if registers32<1 then
  1312. registers32:=1;
  1313. end;
  1314. function ttypeconvnode.first_int_to_bool : tnode;
  1315. begin
  1316. first_int_to_bool:=nil;
  1317. { byte(boolean) or word(wordbool) or longint(longbool) must
  1318. be accepted for var parameters }
  1319. if (nf_explizit in flags) and
  1320. (left.resulttype.def.size=resulttype.def.size) and
  1321. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1322. exit;
  1323. location.loc:=LOC_REGISTER;
  1324. { need if bool to bool !!
  1325. not very nice !!
  1326. insertypeconv(left,s32bittype);
  1327. left.explizit:=true;
  1328. firstpass(left); }
  1329. if registers32<1 then
  1330. registers32:=1;
  1331. end;
  1332. function ttypeconvnode.first_bool_to_bool : tnode;
  1333. begin
  1334. first_bool_to_bool:=nil;
  1335. location.loc:=LOC_REGISTER;
  1336. if registers32<1 then
  1337. registers32:=1;
  1338. end;
  1339. function ttypeconvnode.first_char_to_char : tnode;
  1340. begin
  1341. first_char_to_char:=nil;
  1342. location.loc:=LOC_REGISTER;
  1343. if registers32<1 then
  1344. registers32:=1;
  1345. end;
  1346. function ttypeconvnode.first_proc_to_procvar : tnode;
  1347. begin
  1348. first_proc_to_procvar:=nil;
  1349. if (left.location.loc<>LOC_REFERENCE) then
  1350. CGMessage(cg_e_illegal_expression);
  1351. registers32:=left.registers32;
  1352. if registers32<1 then
  1353. registers32:=1;
  1354. location.loc:=LOC_REGISTER;
  1355. end;
  1356. function ttypeconvnode.first_load_smallset : tnode;
  1357. var
  1358. srsym: ttypesym;
  1359. p: tcallparanode;
  1360. begin
  1361. if not searchsystype('FPC_SMALL_SET',srsym) then
  1362. internalerror(200108313);
  1363. p := ccallparanode.create(left,nil);
  1364. { reused }
  1365. left := nil;
  1366. { convert parameter explicitely to fpc_small_set }
  1367. p.left := ctypeconvnode.create(p.left,srsym.restype);
  1368. p.left.toggleflag(nf_explizit);
  1369. { create call, adjust resulttype }
  1370. result :=
  1371. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1372. firstpass(result);
  1373. end;
  1374. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1375. begin
  1376. first_ansistring_to_pchar:=nil;
  1377. location.loc:=LOC_REGISTER;
  1378. if registers32<1 then
  1379. registers32:=1;
  1380. end;
  1381. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1382. begin
  1383. first_arrayconstructor_to_set:=nil;
  1384. internalerror(200104022);
  1385. end;
  1386. function ttypeconvnode.first_class_to_intf : tnode;
  1387. begin
  1388. first_class_to_intf:=nil;
  1389. location.loc:=LOC_REFERENCE;
  1390. if registers32<1 then
  1391. registers32:=1;
  1392. end;
  1393. function ttypeconvnode._first_int_to_int : tnode;
  1394. begin
  1395. result:=first_int_to_int;
  1396. end;
  1397. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1398. begin
  1399. result:=first_cstring_to_pchar;
  1400. end;
  1401. function ttypeconvnode._first_string_to_chararray : tnode;
  1402. begin
  1403. result:=first_string_to_chararray;
  1404. end;
  1405. function ttypeconvnode._first_char_to_string : tnode;
  1406. begin
  1407. result:=first_char_to_string;
  1408. end;
  1409. function ttypeconvnode._first_nothing : tnode;
  1410. begin
  1411. result:=first_nothing;
  1412. end;
  1413. function ttypeconvnode._first_array_to_pointer : tnode;
  1414. begin
  1415. result:=first_array_to_pointer;
  1416. end;
  1417. function ttypeconvnode._first_int_to_real : tnode;
  1418. begin
  1419. result:=first_int_to_real;
  1420. end;
  1421. function ttypeconvnode._first_real_to_real : tnode;
  1422. begin
  1423. result:=first_real_to_real;
  1424. end;
  1425. function ttypeconvnode._first_pointer_to_array : tnode;
  1426. begin
  1427. result:=first_pointer_to_array;
  1428. end;
  1429. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1430. begin
  1431. result:=first_cchar_to_pchar;
  1432. end;
  1433. function ttypeconvnode._first_bool_to_int : tnode;
  1434. begin
  1435. result:=first_bool_to_int;
  1436. end;
  1437. function ttypeconvnode._first_int_to_bool : tnode;
  1438. begin
  1439. result:=first_int_to_bool;
  1440. end;
  1441. function ttypeconvnode._first_bool_to_bool : tnode;
  1442. begin
  1443. result:=first_bool_to_bool;
  1444. end;
  1445. function ttypeconvnode._first_proc_to_procvar : tnode;
  1446. begin
  1447. result:=first_proc_to_procvar;
  1448. end;
  1449. function ttypeconvnode._first_load_smallset : tnode;
  1450. begin
  1451. result:=first_load_smallset;
  1452. end;
  1453. function ttypeconvnode._first_cord_to_pointer : tnode;
  1454. begin
  1455. result:=first_cord_to_pointer;
  1456. end;
  1457. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1458. begin
  1459. result:=first_ansistring_to_pchar;
  1460. end;
  1461. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1462. begin
  1463. result:=first_arrayconstructor_to_set;
  1464. end;
  1465. function ttypeconvnode._first_class_to_intf : tnode;
  1466. begin
  1467. result:=first_class_to_intf;
  1468. end;
  1469. function ttypeconvnode._first_char_to_char : tnode;
  1470. begin
  1471. result:=first_char_to_char;
  1472. end;
  1473. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1474. const
  1475. firstconvert : array[tconverttype] of pointer = (
  1476. @ttypeconvnode._first_nothing, {equal}
  1477. @ttypeconvnode._first_nothing, {not_possible}
  1478. nil, { removed in resulttype_string_to_string }
  1479. @ttypeconvnode._first_char_to_string,
  1480. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1481. nil, { removed in resulttype_chararray_to_string }
  1482. @ttypeconvnode._first_cchar_to_pchar,
  1483. @ttypeconvnode._first_cstring_to_pchar,
  1484. @ttypeconvnode._first_ansistring_to_pchar,
  1485. @ttypeconvnode._first_string_to_chararray,
  1486. nil, { removed in resulttype_chararray_to_string }
  1487. @ttypeconvnode._first_array_to_pointer,
  1488. @ttypeconvnode._first_pointer_to_array,
  1489. @ttypeconvnode._first_int_to_int,
  1490. @ttypeconvnode._first_int_to_bool,
  1491. @ttypeconvnode._first_bool_to_bool,
  1492. @ttypeconvnode._first_bool_to_int,
  1493. @ttypeconvnode._first_real_to_real,
  1494. @ttypeconvnode._first_int_to_real,
  1495. @ttypeconvnode._first_proc_to_procvar,
  1496. @ttypeconvnode._first_arrayconstructor_to_set,
  1497. @ttypeconvnode._first_load_smallset,
  1498. @ttypeconvnode._first_cord_to_pointer,
  1499. @ttypeconvnode._first_nothing,
  1500. @ttypeconvnode._first_nothing,
  1501. @ttypeconvnode._first_class_to_intf,
  1502. @ttypeconvnode._first_char_to_char,
  1503. @ttypeconvnode._first_nothing,
  1504. @ttypeconvnode._first_nothing
  1505. );
  1506. type
  1507. tprocedureofobject = function : tnode of object;
  1508. var
  1509. r : packed record
  1510. proc : pointer;
  1511. obj : pointer;
  1512. end;
  1513. begin
  1514. { this is a little bit dirty but it works }
  1515. { and should be quite portable too }
  1516. r.proc:=firstconvert[c];
  1517. r.obj:=self;
  1518. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1519. end;
  1520. function ttypeconvnode.pass_1 : tnode;
  1521. begin
  1522. result:=nil;
  1523. firstpass(left);
  1524. if codegenerror then
  1525. exit;
  1526. { load the value_str from the left part }
  1527. registers32:=left.registers32;
  1528. registersfpu:=left.registersfpu;
  1529. {$ifdef SUPPORT_MMX}
  1530. registersmmx:=left.registersmmx;
  1531. {$endif}
  1532. location.loc:=left.location.loc;
  1533. if nf_explizit in flags then
  1534. begin
  1535. { check if the result could be in a register }
  1536. if not(tstoreddef(resulttype.def).is_intregable) and
  1537. not(tstoreddef(resulttype.def).is_fpuregable) then
  1538. make_not_regable(left);
  1539. end;
  1540. result:=first_call_helper(convtype);
  1541. end;
  1542. function ttypeconvnode.docompare(p: tnode) : boolean;
  1543. begin
  1544. docompare :=
  1545. inherited docompare(p) and
  1546. (convtype = ttypeconvnode(p).convtype);
  1547. end;
  1548. {*****************************************************************************
  1549. TISNODE
  1550. *****************************************************************************}
  1551. constructor tisnode.create(l,r : tnode);
  1552. begin
  1553. inherited create(isn,l,r);
  1554. end;
  1555. function tisnode.det_resulttype:tnode;
  1556. var
  1557. paras: tcallparanode;
  1558. begin
  1559. result:=nil;
  1560. resulttypepass(left);
  1561. resulttypepass(right);
  1562. set_varstate(left,true);
  1563. set_varstate(right,true);
  1564. if codegenerror then
  1565. exit;
  1566. if (right.resulttype.def.deftype=classrefdef) then
  1567. begin
  1568. { left must be a class }
  1569. if is_class(left.resulttype.def) then
  1570. begin
  1571. { the operands must be related }
  1572. if (not(tobjectdef(left.resulttype.def).is_related(
  1573. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1574. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1575. tobjectdef(left.resulttype.def)))) then
  1576. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1577. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1578. end
  1579. else
  1580. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1581. { call fpc_do_is helper }
  1582. paras := ccallparanode.create(
  1583. left,
  1584. ccallparanode.create(
  1585. right,nil));
  1586. result := ccallnode.createintern('fpc_do_is',paras);
  1587. left := nil;
  1588. right := nil;
  1589. end
  1590. else if is_interface(right.resulttype.def) then
  1591. begin
  1592. { left is a class }
  1593. if is_class(left.resulttype.def) then
  1594. begin
  1595. { the operands must be related }
  1596. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1597. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1598. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1599. end
  1600. { left is an interface }
  1601. else if is_interface(left.resulttype.def) then
  1602. begin
  1603. { the operands must be related }
  1604. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1605. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1606. CGMessage(type_e_mismatch);
  1607. end
  1608. else
  1609. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1610. { call fpc_do_is helper }
  1611. paras := ccallparanode.create(
  1612. left,
  1613. ccallparanode.create(
  1614. right,nil));
  1615. result := ccallnode.createintern('fpc_do_is',paras);
  1616. left := nil;
  1617. right := nil;
  1618. end
  1619. else
  1620. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1621. resulttype:=booltype;
  1622. end;
  1623. function tisnode.pass_1 : tnode;
  1624. begin
  1625. internalerror(200204254);
  1626. result:=nil;
  1627. end;
  1628. { dummy pass_2, it will never be called, but we need one since }
  1629. { you can't instantiate an abstract class }
  1630. procedure tisnode.pass_2;
  1631. begin
  1632. end;
  1633. {*****************************************************************************
  1634. TASNODE
  1635. *****************************************************************************}
  1636. constructor tasnode.create(l,r : tnode);
  1637. begin
  1638. inherited create(asn,l,r);
  1639. call := nil;
  1640. end;
  1641. destructor tasnode.destroy;
  1642. begin
  1643. call.free;
  1644. inherited destroy;
  1645. end;
  1646. function tasnode.det_resulttype:tnode;
  1647. begin
  1648. result:=nil;
  1649. resulttypepass(right);
  1650. resulttypepass(left);
  1651. set_varstate(right,true);
  1652. set_varstate(left,true);
  1653. if codegenerror then
  1654. exit;
  1655. if (right.resulttype.def.deftype=classrefdef) then
  1656. begin
  1657. { left must be a class }
  1658. if is_class(left.resulttype.def) then
  1659. begin
  1660. { the operands must be related }
  1661. if (not(tobjectdef(left.resulttype.def).is_related(
  1662. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1663. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1664. tobjectdef(left.resulttype.def)))) then
  1665. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1666. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1667. end
  1668. else
  1669. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1670. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1671. end
  1672. else if is_interface(right.resulttype.def) then
  1673. begin
  1674. { left is a class }
  1675. if is_class(left.resulttype.def) then
  1676. begin
  1677. { the operands must be related }
  1678. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1679. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1680. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1681. end
  1682. { left is an interface }
  1683. else if is_interface(left.resulttype.def) then
  1684. begin
  1685. { the operands must be related }
  1686. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1687. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1688. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1689. end
  1690. else
  1691. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1692. resulttype:=right.resulttype;
  1693. { load the GUID of the interface }
  1694. if (right.nodetype=typen) then
  1695. begin
  1696. if tobjectdef(left.resulttype.def).isiidguidvalid then
  1697. right:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid)
  1698. else
  1699. internalerror(200206282);
  1700. resulttypepass(right);
  1701. end;
  1702. end
  1703. else
  1704. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1705. end;
  1706. function tasnode.getcopy: tnode;
  1707. begin
  1708. result := inherited getcopy;
  1709. if assigned(call) then
  1710. tasnode(result).call := call.getcopy
  1711. else
  1712. tasnode(result).call := nil;
  1713. end;
  1714. function tasnode.pass_1 : tnode;
  1715. var
  1716. procname: string;
  1717. begin
  1718. result:=nil;
  1719. if not assigned(call) then
  1720. begin
  1721. if is_class(left.resulttype.def) and
  1722. (right.resulttype.def.deftype=classrefdef) then
  1723. call := ccallnode.createinternres('fpc_do_as',
  1724. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1725. resulttype)
  1726. else
  1727. begin
  1728. if is_class(left.resulttype.def) then
  1729. procname := 'fpc_class_as_intf'
  1730. else
  1731. procname := 'fpc_intf_as';
  1732. call := ccallnode.createinternres(procname,
  1733. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1734. resulttype);
  1735. end;
  1736. left := nil;
  1737. right := nil;
  1738. firstpass(call);
  1739. if codegenerror then
  1740. exit;
  1741. location.loc:=call.location.loc;
  1742. registers32:=call.registers32;
  1743. registersfpu:=call.registersfpu;
  1744. {$ifdef SUPPORT_MMX}
  1745. registersmmx:=call.registersmmx;
  1746. {$endif SUPPORT_MMX}
  1747. end;
  1748. end;
  1749. begin
  1750. ctypeconvnode:=ttypeconvnode;
  1751. casnode:=tasnode;
  1752. cisnode:=tisnode;
  1753. end.
  1754. {
  1755. $Log$
  1756. Revision 1.75 2002-09-02 19:24:42 peter
  1757. * array of char support for Str()
  1758. Revision 1.74 2002/09/01 08:01:16 daniel
  1759. * Removed sets from Tcallnode.det_resulttype
  1760. + Added read/write notifications of variables. These will be usefull
  1761. for providing information for several optimizations. For example
  1762. the value of the loop variable of a for loop does matter is the
  1763. variable is read after the for loop, but if it's no longer used
  1764. or written, it doesn't matter and this can be used to optimize
  1765. the loop code generation.
  1766. Revision 1.73 2002/08/23 16:14:49 peter
  1767. * tempgen cleanup
  1768. * tt_noreuse temp type added that will be used in genentrycode
  1769. Revision 1.72 2002/08/20 18:23:33 jonas
  1770. * the as node again uses a compilerproc
  1771. + (untested) support for interface "as" statements
  1772. Revision 1.71 2002/08/19 19:36:43 peter
  1773. * More fixes for cross unit inlining, all tnodes are now implemented
  1774. * Moved pocall_internconst to po_internconst because it is not a
  1775. calling type at all and it conflicted when inlining of these small
  1776. functions was requested
  1777. Revision 1.70 2002/08/17 09:23:36 florian
  1778. * first part of procinfo rewrite
  1779. Revision 1.69 2002/08/14 19:26:55 carl
  1780. + generic int_to_real type conversion
  1781. + generic unaryminus node
  1782. Revision 1.68 2002/08/11 16:08:55 florian
  1783. + support of explicit type case boolean->char
  1784. Revision 1.67 2002/08/11 15:28:00 florian
  1785. + support of explicit type case <any ordinal type>->pointer
  1786. (delphi mode only)
  1787. Revision 1.66 2002/08/09 07:33:01 florian
  1788. * a couple of interface related fixes
  1789. Revision 1.65 2002/07/29 21:23:42 florian
  1790. * more fixes for the ppc
  1791. + wrappers for the tcnvnode.first_* stuff introduced
  1792. Revision 1.64 2002/07/23 12:34:30 daniel
  1793. * Readded old set code. To use it define 'oldset'. Activated by default
  1794. for ppc.
  1795. Revision 1.63 2002/07/23 09:51:22 daniel
  1796. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1797. are worth comitting.
  1798. Revision 1.62 2002/07/22 11:48:04 daniel
  1799. * Sets are now internally sets.
  1800. Revision 1.61 2002/07/20 17:16:02 florian
  1801. + source code page support
  1802. Revision 1.60 2002/07/20 11:57:54 florian
  1803. * types.pas renamed to defbase.pas because D6 contains a types
  1804. unit so this would conflicts if D6 programms are compiled
  1805. + Willamette/SSE2 instructions to assembler added
  1806. Revision 1.59 2002/07/01 16:23:53 peter
  1807. * cg64 patch
  1808. * basics for currency
  1809. * asnode updates for class and interface (not finished)
  1810. Revision 1.58 2002/05/18 13:34:09 peter
  1811. * readded missing revisions
  1812. Revision 1.57 2002/05/16 19:46:37 carl
  1813. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1814. + try to fix temp allocation (still in ifdef)
  1815. + generic constructor calls
  1816. + start of tassembler / tmodulebase class cleanup
  1817. Revision 1.55 2002/05/12 16:53:07 peter
  1818. * moved entry and exitcode to ncgutil and cgobj
  1819. * foreach gets extra argument for passing local data to the
  1820. iterator function
  1821. * -CR checks also class typecasts at runtime by changing them
  1822. into as
  1823. * fixed compiler to cycle with the -CR option
  1824. * fixed stabs with elf writer, finally the global variables can
  1825. be watched
  1826. * removed a lot of routines from cga unit and replaced them by
  1827. calls to cgobj
  1828. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1829. u32bit then the other is typecasted also to u32bit without giving
  1830. a rangecheck warning/error.
  1831. * fixed pascal calling method with reversing also the high tree in
  1832. the parast, detected by tcalcst3 test
  1833. Revision 1.54 2002/04/25 20:16:38 peter
  1834. * moved more routines from cga/n386util
  1835. Revision 1.53 2002/04/23 19:16:34 peter
  1836. * add pinline unit that inserts compiler supported functions using
  1837. one or more statements
  1838. * moved finalize and setlength from ninl to pinline
  1839. Revision 1.52 2002/04/21 19:02:03 peter
  1840. * removed newn and disposen nodes, the code is now directly
  1841. inlined from pexpr
  1842. * -an option that will write the secondpass nodes to the .s file, this
  1843. requires EXTDEBUG define to actually write the info
  1844. * fixed various internal errors and crashes due recent code changes
  1845. Revision 1.51 2002/04/06 18:10:42 jonas
  1846. * several powerpc-related additions and fixes
  1847. Revision 1.50 2002/04/04 19:05:58 peter
  1848. * removed unused units
  1849. * use tlocation.size in cg.a_*loc*() routines
  1850. Revision 1.49 2002/04/02 17:11:28 peter
  1851. * tlocation,treference update
  1852. * LOC_CONSTANT added for better constant handling
  1853. * secondadd splitted in multiple routines
  1854. * location_force_reg added for loading a location to a register
  1855. of a specified size
  1856. * secondassignment parses now first the right and then the left node
  1857. (this is compatible with Kylix). This saves a lot of push/pop especially
  1858. with string operations
  1859. * adapted some routines to use the new cg methods
  1860. Revision 1.48 2002/02/03 09:30:03 peter
  1861. * more fixes for protected handling
  1862. }