nld.pas 42 KB

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