nld.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. inc(tabstractvarsym(symtableentry).refs);
  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. inc(tabstractvarsym(symtableentry).refs);
  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. inc(tabstractvarsym(symtableentry).refs,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_operator_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. {
  573. hp:=
  574. ccallparanode.create(
  575. ctypeconvnode.create_internal(right,voidpointertype),
  576. ccallparanode.create(
  577. ctypeconvnode.create_internal(left,voidpointertype),
  578. nil));
  579. result:=ccallnode.createintern('fpc_intf_assign',hp);
  580. }
  581. hp:=
  582. ccallparanode.create(
  583. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  584. ccallparanode.create(
  585. ctypeconvnode.create_internal(right,voidpointertype),
  586. ccallparanode.create(
  587. ctypeconvnode.create_internal(left,voidpointertype),
  588. nil)));
  589. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  590. left:=nil;
  591. right:=nil;
  592. exit;
  593. end;
  594. { call helpers for variant, they can contain non ref. counted types like
  595. vararrays which must be really copied }
  596. if left.resultdef.typ=variantdef then
  597. begin
  598. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  599. caddrnode.create_internal(right),voidpointertype),
  600. ccallparanode.create(ctypeconvnode.create_internal(
  601. caddrnode.create_internal(left),voidpointertype),
  602. nil));
  603. result:=ccallnode.createintern('fpc_variant_copy',hp);
  604. left:=nil;
  605. right:=nil;
  606. exit;
  607. end;
  608. { call helpers for windows widestrings, they aren't ref. counted }
  609. if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  610. begin
  611. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  612. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  613. nil));
  614. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  615. left:=nil;
  616. right:=nil;
  617. exit;
  618. end;
  619. { check if local proc/func is assigned to procvar }
  620. if right.resultdef.typ=procvardef then
  621. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  622. end;
  623. function tassignmentnode.pass_1 : tnode;
  624. var
  625. hp: tnode;
  626. oldassignmentnode : tassignmentnode;
  627. begin
  628. result:=nil;
  629. expectloc:=LOC_VOID;
  630. firstpass(left);
  631. { Optimize the reuse of the destination of the assingment in left.
  632. Allow the use of the left inside the tree generated on the right.
  633. This is especially usefull for string routines where the destination
  634. is pushed as a parameter. Using the final destination of left directly
  635. save a temp allocation and copy of data (PFV) }
  636. oldassignmentnode:=aktassignmentnode;
  637. if right.nodetype=addn then
  638. aktassignmentnode:=self
  639. else
  640. aktassignmentnode:=nil;
  641. firstpass(right);
  642. aktassignmentnode:=oldassignmentnode;
  643. if nf_assign_done_in_right in flags then
  644. begin
  645. result:=right;
  646. right:=nil;
  647. exit;
  648. end;
  649. if codegenerror then
  650. exit;
  651. if (cs_opt_level1 in current_settings.optimizerswitches) and
  652. (right.nodetype = calln) and
  653. (right.resultdef=left.resultdef) and
  654. { left must be a temp, since otherwise as soon as you modify the }
  655. { result, the current left node is modified and that one may }
  656. { still be an argument to the function or even accessed in the }
  657. { function }
  658. (
  659. (
  660. (left.nodetype = temprefn) and
  661. paramanager.ret_in_param(right.resultdef,tcallnode(right).procdefinition.proccalloption)
  662. ) or
  663. { there's special support for ansi/widestrings in the callnode }
  664. is_ansistring(right.resultdef) or
  665. is_widestring(right.resultdef)
  666. ) then
  667. begin
  668. make_not_regable(left,vr_addr);
  669. tcallnode(right).funcretnode := left;
  670. result := right;
  671. left := nil;
  672. right := nil;
  673. exit;
  674. end;
  675. { assignment to refcounted variable -> inc/decref }
  676. if (not is_class(left.resultdef) and
  677. left.resultdef.needs_inittable) then
  678. include(current_procinfo.flags,pi_do_call);
  679. if (is_shortstring(left.resultdef)) then
  680. begin
  681. if right.resultdef.typ=stringdef then
  682. begin
  683. if (right.nodetype<>stringconstn) or
  684. (tstringconstnode(right).len<>0) then
  685. begin
  686. {$ifdef old_append_str}
  687. if (cs_opt_level1 in current_settings.optimizerswitches) and
  688. (right.nodetype in [calln,blockn]) and
  689. (left.nodetype = temprefn) and
  690. is_shortstring(right.resultdef) and
  691. not is_open_string(left.resultdef) and
  692. (tstringdef(left.resultdef).len = 255) then
  693. begin
  694. { the blocknode case is handled in pass_generate_code at the temp }
  695. { reference level (mainly for callparatemp) (JM) }
  696. if (right.nodetype = calln) then
  697. begin
  698. tcallnode(right).funcretnode := left;
  699. result := right;
  700. end
  701. else
  702. exit;
  703. end
  704. else
  705. {$endif old_append_str}
  706. begin
  707. hp:=ccallparanode.create
  708. (right,
  709. ccallparanode.create(cinlinenode.create
  710. (in_high_x,false,left.getcopy),nil));
  711. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp,left);
  712. firstpass(result);
  713. end;
  714. left:=nil;
  715. right:=nil;
  716. exit;
  717. end;
  718. end;
  719. end;
  720. registersint:=left.registersint+right.registersint;
  721. registersfpu:=max(left.registersfpu,right.registersfpu);
  722. {$ifdef SUPPORT_MMX}
  723. registersmmx:=max(left.registersmmx,right.registersmmx);
  724. {$endif SUPPORT_MMX}
  725. end;
  726. function tassignmentnode.docompare(p: tnode): boolean;
  727. begin
  728. docompare :=
  729. inherited docompare(p) and
  730. (assigntype = tassignmentnode(p).assigntype);
  731. end;
  732. {$ifdef state_tracking}
  733. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  734. var se:Tstate_entry;
  735. begin
  736. track_state_pass:=false;
  737. if exec_known then
  738. begin
  739. track_state_pass:=right.track_state_pass(exec_known);
  740. {Force a new resultdef pass.}
  741. right.resultdef:=nil;
  742. do_typecheckpass(right);
  743. typecheckpass(right);
  744. aktstate.store_fact(left.getcopy,right.getcopy);
  745. end
  746. else
  747. aktstate.delete_fact(left);
  748. end;
  749. {$endif}
  750. {*****************************************************************************
  751. TARRAYCONSTRUCTORRANGENODE
  752. *****************************************************************************}
  753. constructor tarrayconstructorrangenode.create(l,r : tnode);
  754. begin
  755. inherited create(arrayconstructorrangen,l,r);
  756. end;
  757. function tarrayconstructorrangenode.pass_typecheck:tnode;
  758. begin
  759. result:=nil;
  760. typecheckpass(left);
  761. typecheckpass(right);
  762. set_varstate(left,vs_read,[vsf_must_be_valid]);
  763. set_varstate(right,vs_read,[vsf_must_be_valid]);
  764. if codegenerror then
  765. exit;
  766. resultdef:=left.resultdef;
  767. end;
  768. function tarrayconstructorrangenode.pass_1 : tnode;
  769. begin
  770. firstpass(left);
  771. firstpass(right);
  772. expectloc:=LOC_CREFERENCE;
  773. calcregisters(self,0,0,0);
  774. result:=nil;
  775. end;
  776. {****************************************************************************
  777. TARRAYCONSTRUCTORNODE
  778. *****************************************************************************}
  779. constructor tarrayconstructornode.create(l,r : tnode);
  780. begin
  781. inherited create(arrayconstructorn,l,r);
  782. end;
  783. function tarrayconstructornode.dogetcopy : tnode;
  784. var
  785. n : tarrayconstructornode;
  786. begin
  787. n:=tarrayconstructornode(inherited dogetcopy);
  788. result:=n;
  789. end;
  790. function tarrayconstructornode.pass_typecheck:tnode;
  791. var
  792. hdef : tdef;
  793. hp : tarrayconstructornode;
  794. len : longint;
  795. varia : boolean;
  796. begin
  797. result:=nil;
  798. { are we allowing array constructor? Then convert it to a set.
  799. Do this only if we didn't convert the arrayconstructor yet. This
  800. is needed for the cases where the resultdef is forced for a second
  801. run }
  802. if (not allow_array_constructor) then
  803. begin
  804. hp:=tarrayconstructornode(getcopy);
  805. arrayconstructor_to_set(tnode(hp));
  806. result:=hp;
  807. exit;
  808. end;
  809. { only pass left tree, right tree contains next construct if any }
  810. hdef:=nil;
  811. len:=0;
  812. varia:=false;
  813. if assigned(left) then
  814. begin
  815. hp:=self;
  816. while assigned(hp) do
  817. begin
  818. typecheckpass(hp.left);
  819. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  820. if (hdef=nil) then
  821. hdef:=hp.left.resultdef
  822. else
  823. begin
  824. if (not varia) and (not equal_defs(hdef,hp.left.resultdef)) then
  825. begin
  826. { If both are integers we need to take the type that can hold both
  827. defs }
  828. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  829. begin
  830. if is_in_limit(hdef,hp.left.resultdef) then
  831. hdef:=hp.left.resultdef;
  832. end
  833. else
  834. if (nf_novariaallowed in flags) then
  835. varia:=true;
  836. end;
  837. end;
  838. inc(len);
  839. hp:=tarrayconstructornode(hp.right);
  840. end;
  841. end;
  842. { Set the type of empty or varia arrays to void. Also
  843. do this if the type is array of const/open array
  844. because those can't be used with setelementdef }
  845. if not assigned(hdef) or
  846. varia or
  847. is_array_of_const(hdef) or
  848. is_open_array(hdef) then
  849. hdef:=voidtype;
  850. resultdef:=tarraydef.create(0,len-1,s32inttype);
  851. tarraydef(resultdef).elementdef:=hdef;
  852. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  853. if varia then
  854. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  855. end;
  856. procedure tarrayconstructornode.force_type(def:tdef);
  857. var
  858. hp : tarrayconstructornode;
  859. begin
  860. tarraydef(resultdef).elementdef:=def;
  861. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  862. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  863. if assigned(left) then
  864. begin
  865. hp:=self;
  866. while assigned(hp) do
  867. begin
  868. inserttypeconv(hp.left,def);
  869. hp:=tarrayconstructornode(hp.right);
  870. end;
  871. end;
  872. end;
  873. procedure tarrayconstructornode.insert_typeconvs;
  874. var
  875. hp : tarrayconstructornode;
  876. dovariant : boolean;
  877. begin
  878. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  879. { only pass left tree, right tree contains next construct if any }
  880. if assigned(left) then
  881. begin
  882. hp:=self;
  883. while assigned(hp) do
  884. begin
  885. typecheckpass(hp.left);
  886. { Insert typeconvs for array of const }
  887. if dovariant then
  888. { at this time C varargs are no longer an arrayconstructornode }
  889. insert_varargstypeconv(hp.left,false);
  890. hp:=tarrayconstructornode(hp.right);
  891. end;
  892. end;
  893. end;
  894. function tarrayconstructornode.pass_1 : tnode;
  895. var
  896. hp : tarrayconstructornode;
  897. do_variant:boolean;
  898. begin
  899. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  900. result:=nil;
  901. { Insert required type convs, this must be
  902. done in pass 1, because the call must be
  903. typecheckpassed already }
  904. if assigned(left) then
  905. begin
  906. insert_typeconvs;
  907. { call firstpass for all nodes }
  908. hp:=self;
  909. while assigned(hp) do
  910. begin
  911. if hp.left<>nil then
  912. begin
  913. {This check is pessimistic; a call will happen depending
  914. on the location in which the elements will be found in
  915. pass 2.}
  916. if not do_variant then
  917. include(current_procinfo.flags,pi_do_call);
  918. firstpass(hp.left);
  919. end;
  920. hp:=tarrayconstructornode(hp.right);
  921. end;
  922. end;
  923. expectloc:=LOC_CREFERENCE;
  924. calcregisters(self,0,0,0);
  925. end;
  926. function tarrayconstructornode.docompare(p: tnode): boolean;
  927. begin
  928. docompare:=inherited docompare(p);
  929. end;
  930. {*****************************************************************************
  931. TTYPENODE
  932. *****************************************************************************}
  933. constructor ttypenode.create(def:tdef);
  934. begin
  935. inherited create(typen);
  936. typedef:=def;
  937. allowed:=false;
  938. end;
  939. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  940. begin
  941. inherited ppuload(t,ppufile);
  942. ppufile.getderef(typedefderef);
  943. allowed:=boolean(ppufile.getbyte);
  944. end;
  945. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  946. begin
  947. inherited ppuwrite(ppufile);
  948. ppufile.putderef(typedefderef);
  949. ppufile.putbyte(byte(allowed));
  950. end;
  951. procedure ttypenode.buildderefimpl;
  952. begin
  953. inherited buildderefimpl;
  954. typedefderef.build(typedef);
  955. end;
  956. procedure ttypenode.derefimpl;
  957. begin
  958. inherited derefimpl;
  959. typedef:=tdef(typedefderef.resolve);
  960. end;
  961. function ttypenode.pass_typecheck:tnode;
  962. begin
  963. result:=nil;
  964. resultdef:=typedef;
  965. { check if it's valid }
  966. if typedef.typ = errordef then
  967. CGMessage(parser_e_illegal_expression);
  968. end;
  969. function ttypenode.pass_1 : tnode;
  970. begin
  971. result:=nil;
  972. expectloc:=LOC_VOID;
  973. { a typenode can't generate code, so we give here
  974. an error. Else it'll be an abstract error in pass_generate_code.
  975. Only when the allowed flag is set we don't generate
  976. an error }
  977. if not allowed then
  978. Message(parser_e_no_type_not_allowed_here);
  979. end;
  980. function ttypenode.dogetcopy : tnode;
  981. var
  982. n : ttypenode;
  983. begin
  984. n:=ttypenode(inherited dogetcopy);
  985. n.allowed:=allowed;
  986. n.typedef:=typedef;
  987. result:=n;
  988. end;
  989. function ttypenode.docompare(p: tnode): boolean;
  990. begin
  991. docompare :=
  992. inherited docompare(p);
  993. end;
  994. {*****************************************************************************
  995. TRTTINODE
  996. *****************************************************************************}
  997. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  998. begin
  999. inherited create(rttin);
  1000. rttidef:=def;
  1001. rttitype:=rt;
  1002. end;
  1003. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1004. begin
  1005. inherited ppuload(t,ppufile);
  1006. ppufile.getderef(rttidefderef);
  1007. rttitype:=trttitype(ppufile.getbyte);
  1008. end;
  1009. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1010. begin
  1011. inherited ppuwrite(ppufile);
  1012. ppufile.putderef(rttidefderef);
  1013. ppufile.putbyte(byte(rttitype));
  1014. end;
  1015. procedure trttinode.buildderefimpl;
  1016. begin
  1017. inherited buildderefimpl;
  1018. rttidefderef.build(rttidef);
  1019. end;
  1020. procedure trttinode.derefimpl;
  1021. begin
  1022. inherited derefimpl;
  1023. rttidef:=tstoreddef(rttidefderef.resolve);
  1024. end;
  1025. function trttinode.dogetcopy : tnode;
  1026. var
  1027. n : trttinode;
  1028. begin
  1029. n:=trttinode(inherited dogetcopy);
  1030. n.rttidef:=rttidef;
  1031. n.rttitype:=rttitype;
  1032. result:=n;
  1033. end;
  1034. function trttinode.pass_typecheck:tnode;
  1035. begin
  1036. { rtti information will be returned as a void pointer }
  1037. result:=nil;
  1038. resultdef:=voidpointertype;
  1039. end;
  1040. function trttinode.pass_1 : tnode;
  1041. begin
  1042. result:=nil;
  1043. expectloc:=LOC_CREFERENCE;
  1044. end;
  1045. function trttinode.docompare(p: tnode): boolean;
  1046. begin
  1047. docompare :=
  1048. inherited docompare(p) and
  1049. (rttidef = trttinode(p).rttidef) and
  1050. (rttitype = trttinode(p).rttitype);
  1051. end;
  1052. begin
  1053. cloadnode:=tloadnode;
  1054. cassignmentnode:=tassignmentnode;
  1055. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1056. carrayconstructornode:=tarrayconstructornode;
  1057. ctypenode:=ttypenode;
  1058. crttinode:=trttinode;
  1059. end.