nld.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for load/assignment nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nld;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. {$ifdef state_tracking}
  23. nstate,
  24. {$endif}
  25. symconst,symbase,symtype,symsym,symdef;
  26. type
  27. tloadnode = class(tunarynode)
  28. symtableentry : tsym;
  29. symtableentryderef : tderef;
  30. symtable : tsymtable;
  31. procdef : tprocdef;
  32. procdefderef : tderef;
  33. constructor create(v : tsym;st : tsymtable);virtual;
  34. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure buildderefimpl;override;
  38. procedure derefimpl;override;
  39. procedure set_mp(p:tnode);
  40. function is_addr_param_load:boolean;
  41. function _getcopy : tnode;override;
  42. function pass_1 : tnode;override;
  43. function det_resulttype:tnode;override;
  44. procedure mark_write;override;
  45. function docompare(p: tnode): boolean; override;
  46. procedure printnodedata(var t:text);override;
  47. end;
  48. tloadnodeclass = class of tloadnode;
  49. { different assignment types }
  50. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  51. tassignmentnode = class(tbinarynode)
  52. assigntype : tassigntype;
  53. constructor create(l,r : tnode);virtual;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. function _getcopy : tnode;override;
  57. function pass_1 : tnode;override;
  58. function det_resulttype:tnode;override;
  59. {$ifdef state_tracking}
  60. function track_state_pass(exec_known:boolean):boolean;override;
  61. {$endif state_tracking}
  62. function docompare(p: tnode): boolean; override;
  63. end;
  64. tassignmentnodeclass = class of tassignmentnode;
  65. tarrayconstructorrangenode = class(tbinarynode)
  66. constructor create(l,r : tnode);virtual;
  67. function pass_1 : tnode;override;
  68. function det_resulttype:tnode;override;
  69. end;
  70. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  71. tarrayconstructornode = class(tbinarynode)
  72. constructor create(l,r : tnode);virtual;
  73. function _getcopy : tnode;override;
  74. function pass_1 : tnode;override;
  75. function det_resulttype:tnode;override;
  76. function docompare(p: tnode): boolean; override;
  77. procedure force_type(tt:ttype);
  78. procedure insert_typeconvs(iscvarargs: boolean);
  79. end;
  80. tarrayconstructornodeclass = class of tarrayconstructornode;
  81. ttypenode = class(tnode)
  82. allowed : boolean;
  83. restype : ttype;
  84. constructor create(t : ttype);virtual;
  85. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  86. procedure ppuwrite(ppufile:tcompilerppufile);override;
  87. procedure buildderefimpl;override;
  88. procedure derefimpl;override;
  89. function pass_1 : tnode;override;
  90. function det_resulttype:tnode;override;
  91. function docompare(p: tnode): boolean; override;
  92. end;
  93. ttypenodeclass = class of ttypenode;
  94. trttinode = class(tnode)
  95. l1,l2 : longint;
  96. rttitype : trttitype;
  97. rttidef : tstoreddef;
  98. rttidefderef : tderef;
  99. constructor create(def:tstoreddef;rt:trttitype);virtual;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure buildderefimpl;override;
  103. procedure derefimpl;override;
  104. function _getcopy : tnode;override;
  105. function pass_1 : tnode;override;
  106. function det_resulttype:tnode;override;
  107. function docompare(p: tnode): boolean; override;
  108. end;
  109. trttinodeclass = class of trttinode;
  110. var
  111. cloadnode : tloadnodeclass;
  112. cassignmentnode : tassignmentnodeclass;
  113. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  114. carrayconstructornode : tarrayconstructornodeclass;
  115. ctypenode : ttypenodeclass;
  116. crttinode : trttinodeclass;
  117. implementation
  118. uses
  119. cutils,verbose,globtype,globals,systems,
  120. symnot,
  121. defutil,defcmp,
  122. htypechk,pass_1,procinfo,paramgr,
  123. ncon,ninl,ncnv,nmem,ncal,nutils,
  124. cgobj,cgbase
  125. ;
  126. {*****************************************************************************
  127. TLOADNODE
  128. *****************************************************************************}
  129. constructor tloadnode.create(v : tsym;st : tsymtable);
  130. begin
  131. inherited create(loadn,nil);
  132. if not assigned(v) then
  133. internalerror(200108121);
  134. symtableentry:=v;
  135. symtable:=st;
  136. procdef:=nil;
  137. end;
  138. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  139. begin
  140. inherited create(loadn,nil);
  141. if not assigned(v) then
  142. internalerror(200108121);
  143. symtableentry:=v;
  144. symtable:=st;
  145. procdef:=d;
  146. end;
  147. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  148. begin
  149. inherited ppuload(t,ppufile);
  150. ppufile.getderef(symtableentryderef);
  151. symtable:=nil;
  152. ppufile.getderef(procdefderef);
  153. end;
  154. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  155. begin
  156. inherited ppuwrite(ppufile);
  157. ppufile.putderef(symtableentryderef);
  158. ppufile.putderef(procdefderef);
  159. end;
  160. procedure tloadnode.buildderefimpl;
  161. begin
  162. inherited buildderefimpl;
  163. symtableentryderef.build(symtableentry);
  164. procdefderef.build(procdef);
  165. end;
  166. procedure tloadnode.derefimpl;
  167. begin
  168. inherited derefimpl;
  169. symtableentry:=tsym(symtableentryderef.resolve);
  170. symtable:=symtableentry.owner;
  171. procdef:=tprocdef(procdefderef.resolve);
  172. end;
  173. procedure tloadnode.set_mp(p:tnode);
  174. begin
  175. { typen nodes should not be set }
  176. if p.nodetype=typen then
  177. internalerror(200301042);
  178. left:=p;
  179. end;
  180. function tloadnode._getcopy : tnode;
  181. var
  182. n : tloadnode;
  183. begin
  184. n:=tloadnode(inherited _getcopy);
  185. n.symtable:=symtable;
  186. n.symtableentry:=symtableentry;
  187. n.procdef:=procdef;
  188. result:=n;
  189. end;
  190. function tloadnode.is_addr_param_load:boolean;
  191. begin
  192. result:=(symtable.symtabletype=parasymtable) and
  193. (symtableentry.typ=paravarsym) and
  194. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  195. not(nf_load_self_pointer in flags) and
  196. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
  197. end;
  198. function tloadnode.det_resulttype:tnode;
  199. begin
  200. result:=nil;
  201. case symtableentry.typ of
  202. absolutevarsym :
  203. resulttype:=tabsolutevarsym(symtableentry).vartype;
  204. constsym:
  205. begin
  206. if tconstsym(symtableentry).consttyp=constresourcestring then
  207. resulttype:=cansistringtype
  208. else
  209. internalerror(22799);
  210. end;
  211. globalvarsym,
  212. paravarsym,
  213. localvarsym :
  214. begin
  215. inc(tabstractvarsym(symtableentry).refs);
  216. { Nested variable? The we need to load the framepointer of
  217. the parent procedure }
  218. if assigned(current_procinfo) then
  219. begin
  220. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  221. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  222. begin
  223. if assigned(left) then
  224. internalerror(200309289);
  225. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  226. { we can't inline the referenced parent procedure }
  227. exclude(tprocdef(symtable.defowner).procoptions,po_inline);
  228. { reference in nested procedures, variable needs to be in memory }
  229. make_not_regable(self,vr_none);
  230. end;
  231. { static variables referenced in procedures or from finalization,
  232. variable needs to be in memory.
  233. It is too hard and the benefit is too small to detect whether a
  234. variable is only used in the finalization to add support for it (PFV) }
  235. if (symtable.symtabletype=staticsymtable) and
  236. (
  237. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  238. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  239. ) then
  240. make_not_regable(self,vr_none);
  241. end;
  242. { fix self type which is declared as voidpointer in the
  243. definition }
  244. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  245. begin
  246. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  247. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  248. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  249. resulttype.setdef(tclassrefdef.create(resulttype))
  250. else if is_object(resulttype.def) and
  251. (nf_load_self_pointer in flags) then
  252. resulttype.setdef(tpointerdef.create(resulttype));
  253. end
  254. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  255. begin
  256. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  257. resulttype.setdef(tclassrefdef.create(resulttype));
  258. end
  259. else
  260. resulttype:=tabstractvarsym(symtableentry).vartype;
  261. end;
  262. typedconstsym :
  263. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  264. procsym :
  265. begin
  266. { Return the first procdef. In case of overlaoded
  267. procdefs the matching procdef will be choosen
  268. when the expected procvardef is known, see get_information
  269. in htypechk.pas (PFV) }
  270. if not assigned(procdef) then
  271. procdef:=tprocsym(symtableentry).first_procdef;
  272. { the result is a procdef, addrn and proc_to_procvar
  273. typeconvn need this as resulttype so they know
  274. that the address needs to be returned }
  275. resulttype.setdef(procdef);
  276. { process methodpointer }
  277. if assigned(left) then
  278. resulttypepass(left);
  279. end;
  280. labelsym:
  281. resulttype:=voidtype;
  282. else
  283. internalerror(200104141);
  284. end;
  285. end;
  286. procedure Tloadnode.mark_write;
  287. begin
  288. include(flags,nf_write);
  289. end;
  290. function tloadnode.pass_1 : tnode;
  291. begin
  292. result:=nil;
  293. expectloc:=LOC_REFERENCE;
  294. registersint:=0;
  295. registersfpu:=0;
  296. {$ifdef SUPPORT_MMX}
  297. registersmmx:=0;
  298. {$endif SUPPORT_MMX}
  299. if (cs_create_pic in aktmoduleswitches) and
  300. not(symtableentry.typ in [paravarsym,localvarsym]) then
  301. include(current_procinfo.flags,pi_needs_got);
  302. case symtableentry.typ of
  303. absolutevarsym :
  304. ;
  305. constsym:
  306. begin
  307. if tconstsym(symtableentry).consttyp=constresourcestring then
  308. expectloc:=LOC_CREFERENCE;
  309. end;
  310. globalvarsym,
  311. localvarsym,
  312. paravarsym :
  313. begin
  314. if assigned(left) then
  315. firstpass(left);
  316. if not is_addr_param_load and
  317. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  318. begin
  319. case tabstractvarsym(symtableentry).varregable of
  320. vr_intreg,
  321. vr_addr :
  322. expectloc:=LOC_CREGISTER;
  323. vr_fpureg :
  324. expectloc:=LOC_CFPUREGISTER;
  325. vr_mmreg :
  326. expectloc:=LOC_CMMREGISTER;
  327. end
  328. end
  329. else
  330. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  331. expectloc:=LOC_CREFERENCE;
  332. { we need a register for call by reference parameters }
  333. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  334. registersint:=1;
  335. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  336. registersint:=1;
  337. if (target_info.system=system_powerpc_darwin) and
  338. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  339. include(current_procinfo.flags,pi_needs_got);
  340. { call to get address of threadvar }
  341. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  342. include(current_procinfo.flags,pi_do_call);
  343. if nf_write in flags then
  344. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  345. else
  346. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  347. { count variable references }
  348. if cg.t_times>1 then
  349. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  350. end;
  351. typedconstsym :
  352. ;
  353. procsym :
  354. begin
  355. { method pointer ? }
  356. if assigned(left) then
  357. begin
  358. expectloc:=LOC_CREFERENCE;
  359. firstpass(left);
  360. registersint:=max(registersint,left.registersint);
  361. registersfpu:=max(registersfpu,left.registersfpu);
  362. {$ifdef SUPPORT_MMX}
  363. registersmmx:=max(registersmmx,left.registersmmx);
  364. {$endif SUPPORT_MMX}
  365. end;
  366. end;
  367. labelsym :
  368. ;
  369. else
  370. internalerror(200104143);
  371. end;
  372. end;
  373. function tloadnode.docompare(p: tnode): boolean;
  374. begin
  375. docompare :=
  376. inherited docompare(p) and
  377. (symtableentry = tloadnode(p).symtableentry) and
  378. (procdef = tloadnode(p).procdef) and
  379. (symtable = tloadnode(p).symtable);
  380. end;
  381. procedure Tloadnode.printnodedata(var t:text);
  382. begin
  383. inherited printnodedata(t);
  384. write(t,printnodeindention,'symbol = ',symtableentry.name);
  385. if symtableentry.typ=procsym then
  386. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  387. writeln(t,'');
  388. end;
  389. {*****************************************************************************
  390. TASSIGNMENTNODE
  391. *****************************************************************************}
  392. constructor tassignmentnode.create(l,r : tnode);
  393. begin
  394. inherited create(assignn,l,r);
  395. l.mark_write;
  396. assigntype:=at_normal;
  397. end;
  398. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  399. begin
  400. inherited ppuload(t,ppufile);
  401. assigntype:=tassigntype(ppufile.getbyte);
  402. end;
  403. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  404. begin
  405. inherited ppuwrite(ppufile);
  406. ppufile.putbyte(byte(assigntype));
  407. end;
  408. function tassignmentnode._getcopy : tnode;
  409. var
  410. n : tassignmentnode;
  411. begin
  412. n:=tassignmentnode(inherited _getcopy);
  413. n.assigntype:=assigntype;
  414. result:=n;
  415. end;
  416. function tassignmentnode.det_resulttype:tnode;
  417. var
  418. hp : tnode;
  419. useshelper : boolean;
  420. begin
  421. result:=nil;
  422. resulttype:=voidtype;
  423. { must be made unique }
  424. set_unique(left);
  425. resulttypepass(left);
  426. if is_ansistring(left.resulttype.def) then
  427. begin
  428. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  429. if (right.nodetype=addn) and
  430. left.isequal(tbinarynode(right).left) and
  431. { don't fold multiple concatenations else we could get trouble
  432. with multiple uses of s
  433. }
  434. (tbinarynode(right).left.nodetype<>addn) and
  435. (tbinarynode(right).right.nodetype<>addn) then
  436. begin
  437. { don't do a resulttypepass(right), since then the addnode }
  438. { may insert typeconversions that make this optimization }
  439. { opportunity quite difficult to detect (JM) }
  440. resulttypepass(tbinarynode(right).left);
  441. resulttypepass(tbinarynode(right).right);
  442. if (tbinarynode(right).right.nodetype=stringconstn) or
  443. is_char(tbinarynode(right).right.resulttype.def) or
  444. is_shortstring(tbinarynode(right).right.resulttype.def) or
  445. is_ansistring(tbinarynode(right).right.resulttype.def) then
  446. begin
  447. { remove property flag so it'll not trigger an error }
  448. exclude(left.flags,nf_isproperty);
  449. { generate call to helper }
  450. hp:=ccallparanode.create(tbinarynode(right).right,
  451. ccallparanode.create(left,nil));
  452. if is_char(tbinarynode(right).right.resulttype.def) then
  453. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  454. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  455. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  456. else
  457. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  458. tbinarynode(right).right:=nil;
  459. left:=nil;
  460. exit;
  461. end;
  462. end;
  463. end
  464. else
  465. if is_shortstring(left.resulttype.def) then
  466. begin
  467. { fold <shortstring>:=<shortstring>+<shortstring>,
  468. <shortstring>+<char> is handled by an optimized node }
  469. if (right.nodetype=addn) and
  470. left.isequal(tbinarynode(right).left) and
  471. { don't fold multiple concatenations else we could get trouble
  472. with multiple uses of s }
  473. (tbinarynode(right).left.nodetype<>addn) and
  474. (tbinarynode(right).right.nodetype<>addn) then
  475. begin
  476. { don't do a resulttypepass(right), since then the addnode }
  477. { may insert typeconversions that make this optimization }
  478. { opportunity quite difficult to detect (JM) }
  479. resulttypepass(tbinarynode(right).left);
  480. resulttypepass(tbinarynode(right).right);
  481. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  482. begin
  483. { remove property flag so it'll not trigger an error }
  484. exclude(left.flags,nf_isproperty);
  485. { generate call to helper }
  486. hp:=ccallparanode.create(tbinarynode(right).right,
  487. ccallparanode.create(left,nil));
  488. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  489. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  490. tbinarynode(right).right:=nil;
  491. left:=nil;
  492. exit;
  493. end;
  494. end;
  495. end;
  496. resulttypepass(right);
  497. set_varstate(right,vs_read,[vsf_must_be_valid]);
  498. set_varstate(left,vs_written,[]);
  499. if codegenerror then
  500. exit;
  501. { tp procvar support, when we don't expect a procvar
  502. then we need to call the procvar }
  503. if (left.resulttype.def.deftype<>procvardef) then
  504. maybe_call_procvar(right,true);
  505. { assignments to formaldefs and open arrays aren't allowed }
  506. if (left.resulttype.def.deftype=formaldef) or
  507. is_open_array(left.resulttype.def) then
  508. CGMessage(type_e_operator_not_allowed);
  509. { test if node can be assigned, properties are allowed }
  510. valid_for_assignment(left,true);
  511. { assigning nil to a dynamic array clears the array }
  512. if is_dynamic_array(left.resulttype.def) and
  513. (right.nodetype=niln) then
  514. begin
  515. hp:=ccallparanode.create(caddrnode.create_internal
  516. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  517. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  518. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  519. left:=nil;
  520. exit;
  521. end;
  522. { shortstring helpers can do the conversion directly,
  523. so treat them separatly }
  524. if (is_shortstring(left.resulttype.def)) then
  525. begin
  526. { insert typeconv, except for chars that are handled in
  527. secondpass and except for ansi/wide string that can
  528. be converted immediatly }
  529. if not(is_char(right.resulttype.def) or
  530. (right.resulttype.def.deftype=stringdef)) then
  531. inserttypeconv(right,left.resulttype);
  532. if right.resulttype.def.deftype=stringdef then
  533. begin
  534. useshelper:=true;
  535. { convert constant strings to shortstrings. But
  536. skip empty constant strings, that will be handled
  537. in secondpass }
  538. if (right.nodetype=stringconstn) then
  539. begin
  540. { verify if range fits within shortstring }
  541. { just emit a warning, delphi gives an }
  542. { error, only if the type definition of }
  543. { of the string is less < 255 characters }
  544. if not is_open_string(left.resulttype.def) and
  545. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  546. cgmessage(type_w_string_too_long);
  547. inserttypeconv(right,left.resulttype);
  548. if (tstringconstnode(right).len=0) then
  549. useshelper:=false;
  550. end;
  551. { rest is done in pass 1 (JM) }
  552. if useshelper then
  553. exit;
  554. end
  555. end
  556. else
  557. begin
  558. { check if the assignment may cause a range check error }
  559. check_ranges(fileinfo,right,left.resulttype.def);
  560. inserttypeconv(right,left.resulttype);
  561. end;
  562. { call helpers for interface }
  563. if is_interfacecom(left.resulttype.def) then
  564. begin
  565. {
  566. hp:=
  567. ccallparanode.create(
  568. ctypeconvnode.create_internal(right,voidpointertype),
  569. ccallparanode.create(
  570. ctypeconvnode.create_internal(left,voidpointertype),
  571. nil));
  572. result:=ccallnode.createintern('fpc_intf_assign',hp);
  573. }
  574. hp:=
  575. ccallparanode.create(
  576. cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^),
  577. ccallparanode.create(
  578. ctypeconvnode.create_internal(right,voidpointertype),
  579. ccallparanode.create(
  580. ctypeconvnode.create_internal(left,voidpointertype),
  581. nil)));
  582. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  583. left:=nil;
  584. right:=nil;
  585. exit;
  586. end;
  587. { call helpers for variant, they can contain non ref. counted types like
  588. vararrays which must be really copied }
  589. if left.resulttype.def.deftype=variantdef then
  590. begin
  591. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  592. caddrnode.create_internal(right),voidpointertype),
  593. ccallparanode.create(ctypeconvnode.create_internal(
  594. caddrnode.create_internal(left),voidpointertype),
  595. nil));
  596. result:=ccallnode.createintern('fpc_variant_copy',hp);
  597. left:=nil;
  598. right:=nil;
  599. exit;
  600. end;
  601. { call helpers for windows widestrings, they aren't ref. counted }
  602. if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resulttype.def) then
  603. begin
  604. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  605. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  606. nil));
  607. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  608. left:=nil;
  609. right:=nil;
  610. exit;
  611. end;
  612. { check if local proc/func is assigned to procvar }
  613. if right.resulttype.def.deftype=procvardef then
  614. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  615. end;
  616. function tassignmentnode.pass_1 : tnode;
  617. var
  618. hp: tnode;
  619. begin
  620. result:=nil;
  621. expectloc:=LOC_VOID;
  622. firstpass(left);
  623. firstpass(right);
  624. { assignment to refcounted variable -> inc/decref }
  625. if (not is_class(left.resulttype.def) and
  626. left.resulttype.def.needs_inittable) then
  627. include(current_procinfo.flags,pi_do_call);
  628. if codegenerror then
  629. exit;
  630. if (is_shortstring(left.resulttype.def)) then
  631. begin
  632. if right.resulttype.def.deftype=stringdef then
  633. begin
  634. if (right.nodetype<>stringconstn) or
  635. (tstringconstnode(right).len<>0) then
  636. begin
  637. if (cs_opt_level1 in aktoptimizerswitches) and
  638. (right.nodetype in [calln,blockn]) and
  639. (left.nodetype = temprefn) and
  640. is_shortstring(right.resulttype.def) and
  641. not is_open_string(left.resulttype.def) and
  642. (tstringdef(left.resulttype.def).len = 255) then
  643. begin
  644. { the blocknode case is handled in pass_2 at the temp }
  645. { reference level (mainly for callparatemp) (JM) }
  646. if (right.nodetype = calln) then
  647. begin
  648. tcallnode(right).funcretnode := left;
  649. result := right;
  650. end
  651. else
  652. exit;
  653. end
  654. else
  655. begin
  656. hp:=ccallparanode.create
  657. (right,
  658. ccallparanode.create(cinlinenode.create
  659. (in_high_x,false,left.getcopy),nil));
  660. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  661. firstpass(result);
  662. end;
  663. left:=nil;
  664. right:=nil;
  665. exit;
  666. end;
  667. end;
  668. end;
  669. if (cs_opt_level1 in aktoptimizerswitches) and
  670. (right.nodetype = calln) and
  671. { left must be a temp, since otherwise as soon as you modify the }
  672. { result, the current left node is modified and that one may }
  673. { still be an argument to the function or even accessed in the }
  674. { function }
  675. (((left.nodetype = temprefn) and
  676. paramanager.ret_in_param(right.resulttype.def,
  677. tcallnode(right).procdefinition.proccalloption)) or
  678. { there's special support for ansi/widestrings in the callnode }
  679. is_ansistring(right.resulttype.def) or
  680. is_widestring(right.resulttype.def)) then
  681. begin
  682. tcallnode(right).funcretnode := left;
  683. result := right;
  684. left := nil;
  685. right := nil;
  686. exit;
  687. end;
  688. registersint:=left.registersint+right.registersint;
  689. registersfpu:=max(left.registersfpu,right.registersfpu);
  690. {$ifdef SUPPORT_MMX}
  691. registersmmx:=max(left.registersmmx,right.registersmmx);
  692. {$endif SUPPORT_MMX}
  693. end;
  694. function tassignmentnode.docompare(p: tnode): boolean;
  695. begin
  696. docompare :=
  697. inherited docompare(p) and
  698. (assigntype = tassignmentnode(p).assigntype);
  699. end;
  700. {$ifdef state_tracking}
  701. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  702. var se:Tstate_entry;
  703. begin
  704. track_state_pass:=false;
  705. if exec_known then
  706. begin
  707. track_state_pass:=right.track_state_pass(exec_known);
  708. {Force a new resulttype pass.}
  709. right.resulttype.def:=nil;
  710. do_resulttypepass(right);
  711. resulttypepass(right);
  712. aktstate.store_fact(left.getcopy,right.getcopy);
  713. end
  714. else
  715. aktstate.delete_fact(left);
  716. end;
  717. {$endif}
  718. {*****************************************************************************
  719. TARRAYCONSTRUCTORRANGENODE
  720. *****************************************************************************}
  721. constructor tarrayconstructorrangenode.create(l,r : tnode);
  722. begin
  723. inherited create(arrayconstructorrangen,l,r);
  724. end;
  725. function tarrayconstructorrangenode.det_resulttype:tnode;
  726. begin
  727. result:=nil;
  728. resulttypepass(left);
  729. resulttypepass(right);
  730. set_varstate(left,vs_read,[vsf_must_be_valid]);
  731. set_varstate(right,vs_read,[vsf_must_be_valid]);
  732. if codegenerror then
  733. exit;
  734. resulttype:=left.resulttype;
  735. end;
  736. function tarrayconstructorrangenode.pass_1 : tnode;
  737. begin
  738. firstpass(left);
  739. firstpass(right);
  740. expectloc:=LOC_CREFERENCE;
  741. calcregisters(self,0,0,0);
  742. result:=nil;
  743. end;
  744. {****************************************************************************
  745. TARRAYCONSTRUCTORNODE
  746. *****************************************************************************}
  747. constructor tarrayconstructornode.create(l,r : tnode);
  748. begin
  749. inherited create(arrayconstructorn,l,r);
  750. end;
  751. function tarrayconstructornode._getcopy : tnode;
  752. var
  753. n : tarrayconstructornode;
  754. begin
  755. n:=tarrayconstructornode(inherited _getcopy);
  756. result:=n;
  757. end;
  758. function tarrayconstructornode.det_resulttype:tnode;
  759. var
  760. htype : ttype;
  761. hp : tarrayconstructornode;
  762. len : longint;
  763. varia : boolean;
  764. begin
  765. result:=nil;
  766. { are we allowing array constructor? Then convert it to a set }
  767. if not allow_array_constructor then
  768. begin
  769. hp:=tarrayconstructornode(getcopy);
  770. arrayconstructor_to_set(tnode(hp));
  771. result:=hp;
  772. exit;
  773. end;
  774. { only pass left tree, right tree contains next construct if any }
  775. htype.reset;
  776. len:=0;
  777. varia:=false;
  778. if assigned(left) then
  779. begin
  780. hp:=self;
  781. while assigned(hp) do
  782. begin
  783. resulttypepass(hp.left);
  784. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  785. if (htype.def=nil) then
  786. htype:=hp.left.resulttype
  787. else
  788. begin
  789. if ((nf_novariaallowed in flags) or (not varia)) and
  790. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  791. begin
  792. varia:=true;
  793. end;
  794. end;
  795. inc(len);
  796. hp:=tarrayconstructornode(hp.right);
  797. end;
  798. end;
  799. { Set the type of empty or varia arrays to void. Also
  800. do this if the type is array of const/open array
  801. because those can't be used with setelementtype }
  802. if not assigned(htype.def) or
  803. varia or
  804. is_array_of_const(htype.def) or
  805. is_open_array(htype.def) then
  806. htype:=voidtype;
  807. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  808. tarraydef(resulttype.def).setelementtype(htype);
  809. include(tarraydef(resulttype.def).arrayoptions,ado_IsConstructor);
  810. if varia then
  811. include(tarraydef(resulttype.def).arrayoptions,ado_IsVariant);
  812. end;
  813. procedure tarrayconstructornode.force_type(tt:ttype);
  814. var
  815. hp : tarrayconstructornode;
  816. begin
  817. tarraydef(resulttype.def).setelementtype(tt);
  818. include(tarraydef(resulttype.def).arrayoptions,ado_IsConstructor);
  819. exclude(tarraydef(resulttype.def).arrayoptions,ado_IsVariant);
  820. if assigned(left) then
  821. begin
  822. hp:=self;
  823. while assigned(hp) do
  824. begin
  825. inserttypeconv(hp.left,tt);
  826. hp:=tarrayconstructornode(hp.right);
  827. end;
  828. end;
  829. end;
  830. procedure tarrayconstructornode.insert_typeconvs(iscvarargs: boolean);
  831. var
  832. hp : tarrayconstructornode;
  833. dovariant : boolean;
  834. begin
  835. if (iscvarargs) then
  836. include(flags,nf_cvarargs);
  837. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resulttype.def).arrayoptions) or iscvarargs;
  838. { only pass left tree, right tree contains next construct if any }
  839. if assigned(left) then
  840. begin
  841. hp:=self;
  842. while assigned(hp) do
  843. begin
  844. resulttypepass(hp.left);
  845. { Insert typeconvs for array of const }
  846. if dovariant then
  847. begin
  848. if not(iscvarargs) and
  849. (hp.left.nodetype=stringconstn) then
  850. hp.left:=ctypeconvnode.create_internal(hp.left,cansistringtype)
  851. else
  852. case hp.left.resulttype.def.deftype of
  853. enumdef :
  854. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  855. arraydef :
  856. begin
  857. if is_chararray(hp.left.resulttype.def) then
  858. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  859. else
  860. if is_widechararray(hp.left.resulttype.def) then
  861. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  862. else
  863. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  864. end;
  865. orddef :
  866. begin
  867. if is_integer(hp.left.resulttype.def) and
  868. not(is_64bitint(hp.left.resulttype.def)) then
  869. hp.left:=ctypeconvnode.create(hp.left,s32inttype)
  870. else if is_void(hp.left.resulttype.def) then
  871. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename)
  872. else if iscvarargs and
  873. is_currency(hp.left.resulttype.def) then
  874. hp.left:=ctypeconvnode.create(hp.left,s64floattype);
  875. end;
  876. floatdef :
  877. if not(iscvarargs) then
  878. begin
  879. if not(is_currency(hp.left.resulttype.def)) then
  880. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  881. end
  882. else
  883. begin
  884. if is_constrealnode(hp.left) and
  885. not(nf_explicit in hp.left.flags) then
  886. MessagePos(hp.left.fileinfo,type_w_double_c_varargs);
  887. if (tfloatdef(hp.left.resulttype.def).typ in [{$ifndef x86_64}s32real,{$endif}s64currency]) or
  888. (is_constrealnode(hp.left) and
  889. not(nf_explicit in hp.left.flags)) then
  890. hp.left:=ctypeconvnode.create(hp.left,s64floattype);
  891. end;
  892. procvardef :
  893. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  894. stringdef:
  895. if iscvarargs then
  896. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  897. variantdef:
  898. if iscvarargs then
  899. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  900. pointerdef:
  901. ;
  902. classrefdef:
  903. if iscvarargs then
  904. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  905. objectdef :
  906. if iscvarargs or
  907. is_object(hp.left.resulttype.def) then
  908. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  909. else
  910. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  911. end;
  912. end;
  913. resulttypepass(hp.left);
  914. hp:=tarrayconstructornode(hp.right);
  915. end;
  916. end;
  917. end;
  918. function tarrayconstructornode.pass_1 : tnode;
  919. var
  920. hp : tarrayconstructornode;
  921. do_variant:boolean;
  922. begin
  923. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resulttype.def).arrayoptions);
  924. result:=nil;
  925. { Insert required type convs, this must be
  926. done in pass 1, because the call must be
  927. resulttypepassed already }
  928. if assigned(left) then
  929. begin
  930. { in case of C varargs, insert_typeconvs has already been called }
  931. if not(nf_cvarargs in flags) then
  932. insert_typeconvs(false);
  933. { call firstpass for all nodes }
  934. hp:=self;
  935. while assigned(hp) do
  936. begin
  937. if hp.left<>nil then
  938. begin
  939. {This check is pessimistic; a call will happen depending
  940. on the location in which the elements will be found in
  941. pass 2.}
  942. if not do_variant then
  943. include(current_procinfo.flags,pi_do_call);
  944. firstpass(hp.left);
  945. end;
  946. hp:=tarrayconstructornode(hp.right);
  947. end;
  948. end;
  949. expectloc:=LOC_CREFERENCE;
  950. calcregisters(self,0,0,0);
  951. end;
  952. function tarrayconstructornode.docompare(p: tnode): boolean;
  953. begin
  954. docompare:=inherited docompare(p);
  955. end;
  956. {*****************************************************************************
  957. TTYPENODE
  958. *****************************************************************************}
  959. constructor ttypenode.create(t : ttype);
  960. begin
  961. inherited create(typen);
  962. restype:=t;
  963. allowed:=false;
  964. end;
  965. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  966. begin
  967. inherited ppuload(t,ppufile);
  968. ppufile.gettype(restype);
  969. allowed:=boolean(ppufile.getbyte);
  970. end;
  971. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  972. begin
  973. inherited ppuwrite(ppufile);
  974. ppufile.puttype(restype);
  975. ppufile.putbyte(byte(allowed));
  976. end;
  977. procedure ttypenode.buildderefimpl;
  978. begin
  979. inherited buildderefimpl;
  980. restype.buildderef;
  981. end;
  982. procedure ttypenode.derefimpl;
  983. begin
  984. inherited derefimpl;
  985. restype.resolve;
  986. end;
  987. function ttypenode.det_resulttype:tnode;
  988. begin
  989. result:=nil;
  990. resulttype:=restype;
  991. { check if it's valid }
  992. if restype.def.deftype = errordef then
  993. CGMessage(parser_e_illegal_expression);
  994. end;
  995. function ttypenode.pass_1 : tnode;
  996. begin
  997. result:=nil;
  998. expectloc:=LOC_VOID;
  999. { a typenode can't generate code, so we give here
  1000. an error. Else it'll be an abstract error in pass_2.
  1001. Only when the allowed flag is set we don't generate
  1002. an error }
  1003. if not allowed then
  1004. Message(parser_e_no_type_not_allowed_here);
  1005. end;
  1006. function ttypenode.docompare(p: tnode): boolean;
  1007. begin
  1008. docompare :=
  1009. inherited docompare(p);
  1010. end;
  1011. {*****************************************************************************
  1012. TRTTINODE
  1013. *****************************************************************************}
  1014. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  1015. begin
  1016. inherited create(rttin);
  1017. rttidef:=def;
  1018. rttitype:=rt;
  1019. end;
  1020. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1021. begin
  1022. inherited ppuload(t,ppufile);
  1023. ppufile.getderef(rttidefderef);
  1024. rttitype:=trttitype(ppufile.getbyte);
  1025. end;
  1026. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1027. begin
  1028. inherited ppuwrite(ppufile);
  1029. ppufile.putderef(rttidefderef);
  1030. ppufile.putbyte(byte(rttitype));
  1031. end;
  1032. procedure trttinode.buildderefimpl;
  1033. begin
  1034. inherited buildderefimpl;
  1035. rttidefderef.build(rttidef);
  1036. end;
  1037. procedure trttinode.derefimpl;
  1038. begin
  1039. inherited derefimpl;
  1040. rttidef:=tstoreddef(rttidefderef.resolve);
  1041. end;
  1042. function trttinode._getcopy : tnode;
  1043. var
  1044. n : trttinode;
  1045. begin
  1046. n:=trttinode(inherited _getcopy);
  1047. n.rttidef:=rttidef;
  1048. n.rttitype:=rttitype;
  1049. result:=n;
  1050. end;
  1051. function trttinode.det_resulttype:tnode;
  1052. begin
  1053. { rtti information will be returned as a void pointer }
  1054. result:=nil;
  1055. resulttype:=voidpointertype;
  1056. end;
  1057. function trttinode.pass_1 : tnode;
  1058. begin
  1059. result:=nil;
  1060. expectloc:=LOC_CREFERENCE;
  1061. end;
  1062. function trttinode.docompare(p: tnode): boolean;
  1063. begin
  1064. docompare :=
  1065. inherited docompare(p) and
  1066. (rttidef = trttinode(p).rttidef) and
  1067. (rttitype = trttinode(p).rttitype);
  1068. end;
  1069. begin
  1070. cloadnode:=tloadnode;
  1071. cassignmentnode:=tassignmentnode;
  1072. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1073. carrayconstructornode:=tarrayconstructornode;
  1074. ctypenode:=ttypenode;
  1075. crttinode:=trttinode;
  1076. end.