nld.pas 46 KB

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