nld.pas 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. tloadnodeflags = (
  29. loadnf_is_self,
  30. { tell the load node the address of the symbol into the location, i.e. location^ must
  31. be used to access the symbol
  32. this is for example needed to load self for objects }
  33. loadnf_load_addr,
  34. loadnf_inherited,
  35. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  36. this requires that the parameter is actually passed by value
  37. Be really carefull when using this flag! }
  38. loadnf_isinternal_ignoreconst,
  39. loadnf_only_uninitialized_hint
  40. );
  41. tloadnode = class(tunarynode)
  42. protected
  43. fprocdef : tprocdef;
  44. fprocdefderef : tderef;
  45. function handle_threadvar_access: tnode; virtual;
  46. public
  47. loadnodeflags : set of tloadnodeflags;
  48. symtableentry : tsym;
  49. symtableentryderef : tderef;
  50. symtable : TSymtable;
  51. constructor create(v : tsym;st : TSymtable);virtual;
  52. constructor create_procvar(v : tsym;d:tprocdef;st : TSymtable);virtual;
  53. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  54. procedure ppuwrite(ppufile:tcompilerppufile);override;
  55. procedure buildderefimpl;override;
  56. procedure derefimpl;override;
  57. procedure set_mp(p:tnode);
  58. function is_addr_param_load:boolean;virtual;
  59. function dogetcopy : tnode;override;
  60. function pass_1 : tnode;override;
  61. function pass_typecheck:tnode;override;
  62. procedure mark_write;override;
  63. function docompare(p: tnode): boolean; override;
  64. procedure printnodedata(var t:text);override;
  65. {$ifdef DEBUG_NODE_XML}
  66. procedure XMLPrintNodeData(var T: Text); override;
  67. {$endif DEBUG_NODE_XML}
  68. procedure setprocdef(p : tprocdef);
  69. property procdef: tprocdef read fprocdef write setprocdef;
  70. end;
  71. tloadnodeclass = class of tloadnode;
  72. { different assignment types }
  73. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  74. tassignmentnode = class(tbinarynode)
  75. protected
  76. function direct_shortstring_assignment: boolean; virtual;
  77. public
  78. assigntype : tassigntype;
  79. constructor create(l,r : tnode);virtual;
  80. { no checks for validity of assignment }
  81. constructor create_internal(l,r : tnode);virtual;
  82. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. function dogetcopy : tnode;override;
  85. function pass_1 : tnode;override;
  86. function pass_typecheck:tnode;override;
  87. function simplify(forinline : boolean) : tnode;override;
  88. {$ifdef state_tracking}
  89. function track_state_pass(exec_known:boolean):boolean;override;
  90. {$endif state_tracking}
  91. function docompare(p: tnode): boolean; override;
  92. {$ifdef DEBUG_NODE_XML}
  93. procedure XMLPrintNodeData(var T: Text); override;
  94. {$endif DEBUG_NODE_XML}
  95. end;
  96. tassignmentnodeclass = class of tassignmentnode;
  97. tarrayconstructorrangenode = class(tbinarynode)
  98. constructor create(l,r : tnode);virtual;
  99. function pass_1 : tnode;override;
  100. function pass_typecheck:tnode;override;
  101. end;
  102. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  103. tarrayconstructornode = class(tbinarynode)
  104. allow_array_constructor : boolean;
  105. private
  106. function has_range_node:boolean;
  107. protected
  108. procedure wrapmanagedvarrec(var n: tnode);virtual;abstract;
  109. public
  110. constructor create(l,r : tnode);virtual;
  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. procedure force_type(def:tdef);
  116. procedure insert_typeconvs;
  117. function isempty : boolean;
  118. end;
  119. tarrayconstructornodeclass = class of tarrayconstructornode;
  120. ttypenode = class(tnode)
  121. allowed : boolean;
  122. helperallowed : boolean;
  123. typedef : tdef;
  124. typedefderef : tderef;
  125. typesym : tsym;
  126. typesymderef : tderef;
  127. constructor create(def:tdef);virtual;
  128. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  129. procedure ppuwrite(ppufile:tcompilerppufile);override;
  130. procedure buildderefimpl;override;
  131. procedure derefimpl;override;
  132. function pass_1 : tnode;override;
  133. function pass_typecheck:tnode;override;
  134. function dogetcopy : tnode;override;
  135. function docompare(p: tnode): boolean; override;
  136. end;
  137. ttypenodeclass = class of ttypenode;
  138. trttinode = class(tnode)
  139. l1,l2 : longint;
  140. rttitype : trttitype;
  141. rttidef : tstoreddef;
  142. rttidefderef : tderef;
  143. rttidatatype : Trttidatatype;
  144. constructor create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);virtual;
  145. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  146. procedure ppuwrite(ppufile:tcompilerppufile);override;
  147. procedure buildderefimpl;override;
  148. procedure derefimpl;override;
  149. function dogetcopy : tnode;override;
  150. function pass_1 : tnode;override;
  151. function pass_typecheck:tnode;override;
  152. function docompare(p: tnode): boolean; override;
  153. end;
  154. trttinodeclass = class of trttinode;
  155. var
  156. cloadnode : tloadnodeclass = tloadnode;
  157. cassignmentnode : tassignmentnodeclass = tassignmentnode;
  158. carrayconstructorrangenode : tarrayconstructorrangenodeclass = tarrayconstructorrangenode;
  159. carrayconstructornode : tarrayconstructornodeclass = tarrayconstructornode;
  160. ctypenode : ttypenodeclass = ttypenode;
  161. crttinode : trttinodeclass = trttinode;
  162. { Current assignment node }
  163. aktassignmentnode : tassignmentnode;
  164. implementation
  165. uses
  166. verbose,globtype,globals,systems,constexp,compinnr,
  167. ppu,
  168. symtable,
  169. defutil,defcmp,
  170. cpuinfo,
  171. htypechk,pass_1,procinfo,paramgr,
  172. ncon,nflw,ninl,ncnv,nmem,ncal,nutils,
  173. cgbase
  174. ;
  175. {*****************************************************************************
  176. TLOADNODE
  177. *****************************************************************************}
  178. function tloadnode.handle_threadvar_access: tnode;
  179. begin
  180. { nothing special by default }
  181. result:=nil;
  182. end;
  183. constructor tloadnode.create(v : tsym;st : TSymtable);
  184. begin
  185. inherited create(loadn,nil);
  186. if not assigned(v) then
  187. internalerror(200108121);
  188. symtableentry:=v;
  189. symtable:=st;
  190. fprocdef:=nil;
  191. end;
  192. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : TSymtable);
  193. begin
  194. inherited create(loadn,nil);
  195. if not assigned(v) then
  196. internalerror(200108122);
  197. symtableentry:=v;
  198. symtable:=st;
  199. fprocdef:=d;
  200. end;
  201. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  202. begin
  203. inherited ppuload(t,ppufile);
  204. ppufile.getderef(symtableentryderef);
  205. symtable:=nil;
  206. ppufile.getderef(fprocdefderef);
  207. ppufile.getset(tppuset1(loadnodeflags));
  208. end;
  209. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  210. begin
  211. inherited ppuwrite(ppufile);
  212. ppufile.putderef(symtableentryderef);
  213. ppufile.putderef(fprocdefderef);
  214. ppufile.putset(tppuset1(loadnodeflags));
  215. end;
  216. procedure tloadnode.buildderefimpl;
  217. begin
  218. inherited buildderefimpl;
  219. symtableentryderef.build(symtableentry);
  220. fprocdefderef.build(fprocdef);
  221. end;
  222. procedure tloadnode.derefimpl;
  223. begin
  224. inherited derefimpl;
  225. symtableentry:=tsym(symtableentryderef.resolve);
  226. symtable:=symtableentry.owner;
  227. fprocdef:=tprocdef(fprocdefderef.resolve);
  228. end;
  229. procedure tloadnode.set_mp(p:tnode);
  230. begin
  231. { typen nodes should not be set }
  232. if p.nodetype=typen then
  233. internalerror(200301042);
  234. left:=p;
  235. end;
  236. function tloadnode.dogetcopy : tnode;
  237. var
  238. n : tloadnode;
  239. orglabel,
  240. labelcopy : tlabelnode;
  241. begin
  242. n:=tloadnode(inherited dogetcopy);
  243. n.symtable:=symtable;
  244. n.symtableentry:=symtableentry;
  245. n.fprocdef:=fprocdef;
  246. n.loadnodeflags:=loadnodeflags;
  247. if symtableentry.typ=labelsym then
  248. begin
  249. { see the comments for the tgotonode.labelsym field }
  250. orglabel:=tlabelnode(tlabelsym(symtableentry).code);
  251. labelcopy:=tlabelnode(orglabel.dogetcopy);
  252. if not assigned(labelcopy.labsym) then
  253. begin
  254. if not assigned(orglabel.labsym) then
  255. internalerror(2019091301);
  256. labelcopy.labsym:=clabelsym.create('$copiedlabelfrom$'+orglabel.labsym.RealName);
  257. labelcopy.labsym.code:=labelcopy;
  258. end;
  259. n.symtableentry:=labelcopy.labsym;
  260. end;
  261. result:=n;
  262. end;
  263. function tloadnode.is_addr_param_load:boolean;
  264. begin
  265. result:=(symtable.symtabletype=parasymtable) and
  266. (symtableentry.typ=paravarsym) and
  267. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  268. not(loadnf_load_addr in loadnodeflags) and
  269. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vardef,tprocdef(symtable.defowner).proccalloption);
  270. end;
  271. function tloadnode.pass_typecheck:tnode;
  272. begin
  273. result:=nil;
  274. case symtableentry.typ of
  275. absolutevarsym :
  276. resultdef:=tabsolutevarsym(symtableentry).vardef;
  277. constsym:
  278. begin
  279. if tconstsym(symtableentry).consttyp=constresourcestring then
  280. resultdef:=getansistringdef
  281. else
  282. internalerror(22799);
  283. end;
  284. staticvarsym :
  285. begin
  286. tabstractvarsym(symtableentry).IncRefCountBy(1);
  287. { static variables referenced in procedures or from finalization,
  288. variable needs to be in memory.
  289. It is too hard and the benefit is too small to detect whether a
  290. variable is only used in the finalization to add support for it (PFV) }
  291. if assigned(current_procinfo) and
  292. (symtable.symtabletype=staticsymtable) and
  293. (
  294. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  295. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  296. ) then
  297. make_not_regable(self,[ra_different_scope]);
  298. resultdef:=tabstractvarsym(symtableentry).vardef;
  299. if vo_is_thread_var in tstaticvarsym(symtableentry).varoptions then
  300. result:=handle_threadvar_access;
  301. end;
  302. paravarsym,
  303. localvarsym :
  304. begin
  305. tabstractvarsym(symtableentry).IncRefCountBy(1);
  306. { Nested variable? The we need to load the framepointer of
  307. the parent procedure }
  308. if assigned(current_procinfo) and
  309. (symtable.symtabletype in [localsymtable,parasymtable]) and
  310. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  311. begin
  312. if assigned(left) then
  313. internalerror(200309289);
  314. left:=cloadparentfpnode.create(tprocdef(symtable.defowner),lpf_forload);
  315. { we can't inline the referenced parent procedure }
  316. include(tprocdef(symtable.defowner).implprocoptions,pio_nested_access);
  317. { reference in nested procedures, variable needs to be in memory }
  318. { and behaves as if its address escapes its parent block }
  319. make_not_regable(self,[ra_different_scope]);
  320. end;
  321. resultdef:=tabstractvarsym(symtableentry).vardef;
  322. { e.g. self for objects is passed as var-parameter on the caller
  323. side, but on the callee-side we use it as a pointer ->
  324. adjust }
  325. if (loadnf_load_addr in loadnodeflags) then
  326. resultdef:=cpointerdef.getreusable(resultdef);
  327. if (vo_is_self in tabstractvarsym(symtableentry).varoptions) and (resultdef=objc_idtype) and
  328. (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  329. resultdef:=cclassrefdef.create(tprocdef(symtableentry.owner.defowner).struct)
  330. end;
  331. procsym :
  332. begin
  333. { Return the first procdef. In case of overloaded
  334. procdefs the matching procdef will be choosen
  335. when the expected procvardef is known, see get_information
  336. in htypechk.pas (PFV) }
  337. if not assigned(fprocdef) then
  338. fprocdef:=tprocdef(tprocsym(symtableentry).ProcdefList[0])
  339. else if po_kylixlocal in fprocdef.procoptions then
  340. CGMessage(type_e_cant_take_address_of_local_subroutine);
  341. { the result is a fprocdef, addrn and proc_to_procvar
  342. typeconvn need this as resultdef so they know
  343. that the address needs to be returned }
  344. resultdef:=fprocdef;
  345. { process methodpointer/framepointer }
  346. if assigned(left) then
  347. begin
  348. typecheckpass(left);
  349. if (po_classmethod in fprocdef.procoptions) and
  350. is_class(left.resultdef) and
  351. (left.nodetype<>niln) then
  352. begin
  353. left:=cloadvmtaddrnode.create(left);
  354. typecheckpass(left);
  355. end
  356. end;
  357. end;
  358. labelsym:
  359. begin
  360. tlabelsym(symtableentry).used:=true;
  361. resultdef:=voidtype;
  362. end;
  363. else
  364. internalerror(200104141);
  365. end;
  366. end;
  367. procedure Tloadnode.mark_write;
  368. begin
  369. include(flags,nf_write);
  370. end;
  371. function tloadnode.pass_1 : tnode;
  372. begin
  373. result:=nil;
  374. expectloc:=LOC_REFERENCE;
  375. case symtableentry.typ of
  376. absolutevarsym :
  377. ;
  378. constsym:
  379. begin
  380. if tconstsym(symtableentry).consttyp=constresourcestring then
  381. expectloc:=LOC_CREFERENCE;
  382. end;
  383. staticvarsym,
  384. localvarsym,
  385. paravarsym :
  386. begin
  387. if assigned(left) then
  388. firstpass(left);
  389. if not is_addr_param_load and
  390. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  391. expectloc:=tvarregable2tcgloc[tabstractvarsym(symtableentry).varregable]
  392. else
  393. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  394. expectloc:=LOC_CREFERENCE;
  395. { call to get address of threadvar }
  396. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  397. begin
  398. include(current_procinfo.flags,pi_do_call);
  399. include(current_procinfo.flags,pi_uses_threadvar);
  400. end;
  401. end;
  402. procsym :
  403. begin
  404. { initialise left for nested procs if necessary }
  405. if (m_nested_procvars in current_settings.modeswitches) then
  406. setprocdef(fprocdef);
  407. { method pointer or nested proc ? }
  408. if assigned(left) then
  409. begin
  410. expectloc:=LOC_CREGISTER;
  411. firstpass(left);
  412. end;
  413. end;
  414. labelsym :
  415. begin
  416. if not assigned(tlabelsym(symtableentry).asmblocklabel) and
  417. not assigned(tlabelsym(symtableentry).code) then
  418. Message(parser_e_label_outside_proc);
  419. end
  420. else
  421. internalerror(200104143);
  422. end;
  423. end;
  424. function tloadnode.docompare(p: tnode): boolean;
  425. begin
  426. docompare :=
  427. inherited docompare(p) and
  428. (symtableentry = tloadnode(p).symtableentry) and
  429. (fprocdef = tloadnode(p).fprocdef) and
  430. (symtable = tloadnode(p).symtable);
  431. end;
  432. procedure tloadnode.printnodedata(var t:text);
  433. begin
  434. inherited printnodedata(t);
  435. write(t,printnodeindention,'symbol = ',symtableentry.name);
  436. if symtableentry.typ=procsym then
  437. write(t,printnodeindention,'procdef = ',fprocdef.mangledname);
  438. writeln(t,'');
  439. end;
  440. {$ifdef DEBUG_NODE_XML}
  441. procedure TLoadNode.XMLPrintNodeData(var T: Text);
  442. begin
  443. inherited XMLPrintNodeData(T);
  444. WriteLn(T, printnodeindention, '<symbol>', symtableentry.name, '</symbol>');
  445. if symtableentry.typ = procsym then
  446. WriteLn(T, printnodeindention, '<procdef>', fprocdef.mangledname, '</procdef>');
  447. end;
  448. {$endif DEBUG_NODE_XML}
  449. procedure tloadnode.setprocdef(p : tprocdef);
  450. begin
  451. fprocdef:=p;
  452. resultdef:=p;
  453. { nested procedure? }
  454. if assigned(p) and
  455. is_nested_pd(p) then
  456. begin
  457. if not(m_nested_procvars in current_settings.modeswitches) then
  458. CGMessage(type_e_cant_take_address_of_local_subroutine)
  459. else
  460. begin
  461. { parent frame pointer pointer as "self" }
  462. left.free;
  463. left:=cloadparentfpnode.create(tprocdef(p.owner.defowner),lpf_forpara);
  464. end;
  465. end
  466. { we should never go from nested to non-nested }
  467. else if assigned(left) and
  468. (left.nodetype=loadparentfpn) then
  469. internalerror(2010072201);
  470. end;
  471. {*****************************************************************************
  472. TASSIGNMENTNODE
  473. *****************************************************************************}
  474. function tassignmentnode.direct_shortstring_assignment: boolean;
  475. begin
  476. result:=
  477. is_char(right.resultdef) or
  478. (right.resultdef.typ=stringdef);
  479. end;
  480. constructor tassignmentnode.create(l,r : tnode);
  481. begin
  482. inherited create(assignn,l,r);
  483. assigntype:=at_normal;
  484. if r.nodetype = typeconvn then
  485. ttypeconvnode(r).warn_pointer_to_signed:=false;
  486. end;
  487. constructor tassignmentnode.create_internal(l, r: tnode);
  488. begin
  489. create(l,r);
  490. include(flags,nf_internal);
  491. end;
  492. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  493. begin
  494. inherited ppuload(t,ppufile);
  495. assigntype:=tassigntype(ppufile.getbyte);
  496. end;
  497. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  498. begin
  499. inherited ppuwrite(ppufile);
  500. ppufile.putbyte(byte(assigntype));
  501. end;
  502. function tassignmentnode.dogetcopy : tnode;
  503. var
  504. n : tassignmentnode;
  505. begin
  506. n:=tassignmentnode(inherited dogetcopy);
  507. n.assigntype:=assigntype;
  508. result:=n;
  509. end;
  510. function tassignmentnode.simplify(forinline : boolean) : tnode;
  511. begin
  512. result:=nil;
  513. { assignment nodes can perform several floating point }
  514. { type conversions directly, so no typeconversions }
  515. { are inserted in those cases. When inlining, a }
  516. { variable may be replaced by a constant which can be }
  517. { converted at compile time, so check for this case }
  518. if is_real(left.resultdef) and
  519. is_real(right.resultdef) and
  520. is_constrealnode(right) and
  521. not equal_defs(right.resultdef,left.resultdef) then
  522. inserttypeconv(right,left.resultdef);
  523. end;
  524. function tassignmentnode.pass_typecheck:tnode;
  525. var
  526. hp : tnode;
  527. useshelper : boolean;
  528. oldassignmentnode : tassignmentnode;
  529. begin
  530. result:=nil;
  531. resultdef:=voidtype;
  532. { must be made unique }
  533. set_unique(left);
  534. typecheckpass(left);
  535. left.mark_write;
  536. { PI. This is needed to return correct resultdef of add nodes for ansistrings
  537. rawbytestring return needs to be replaced by left.resultdef }
  538. oldassignmentnode:=aktassignmentnode;
  539. aktassignmentnode:=self;
  540. typecheckpass(right);
  541. aktassignmentnode:=oldassignmentnode;
  542. set_varstate(right,vs_read,[vsf_must_be_valid]);
  543. set_varstate(left,vs_written,[]);
  544. if codegenerror then
  545. exit;
  546. { just in case the typecheckpass of right optimized something here }
  547. if nf_assign_done_in_right in flags then
  548. begin
  549. result:=right;
  550. right:=nil;
  551. exit;
  552. end;
  553. { tp procvar support, when we don't expect a procvar
  554. then we need to call the procvar }
  555. if (left.resultdef.typ<>procvardef) then
  556. maybe_call_procvar(right,true);
  557. { assignments to formaldefs and open arrays aren't allowed }
  558. if is_open_array(left.resultdef) then
  559. CGMessage(type_e_assignment_not_allowed)
  560. else if (left.resultdef.typ=formaldef) then
  561. if not(target_info.system in systems_managed_vm) then
  562. CGMessage(type_e_assignment_not_allowed)
  563. else
  564. begin
  565. { on managed platforms, assigning to formaldefs is allowed (but
  566. typecasting them on the left hand side isn't), but primitive
  567. values need to be boxed first }
  568. if (right.resultdef.typ in [orddef,floatdef]) then
  569. begin
  570. right:=cinlinenode.create(in_box_x,false,ccallparanode.create(right,nil));
  571. typecheckpass(right);
  572. end;
  573. end;
  574. { test if node can be assigned, properties are allowed }
  575. if not(nf_internal in flags) then
  576. if not valid_for_assignment(left,true) then
  577. { errors can in situations that cause the compiler to run out of
  578. memory, such as assigning to an implicit pointer-to-array
  579. converted node (that array is 2^31 or 2^63 bytes large) }
  580. exit;
  581. { assigning nil or [] to a dynamic array clears the array }
  582. if is_dynamic_array(left.resultdef) and
  583. (
  584. (right.nodetype=niln) or
  585. (
  586. (right.nodetype=arrayconstructorn) and
  587. (right.resultdef.typ=arraydef) and
  588. (tarraydef(right.resultdef).elementdef=voidtype) and
  589. tarrayconstructornode(right).isempty
  590. )
  591. ) then
  592. begin
  593. { remove property flag to avoid errors, see comments for }
  594. { tf_winlikewidestring assignments below }
  595. exclude(left.flags,nf_isproperty);
  596. { generate a setlength node so it can be intercepted by
  597. target-specific code }
  598. result:=cinlinenode.create(in_setlength_x,false,
  599. ccallparanode.create(genintconstnode(0),
  600. ccallparanode.create(left,nil)));
  601. left:=nil;
  602. exit;
  603. end;
  604. { shortstring helpers can do the conversion directly,
  605. so treat them separatly }
  606. if (is_shortstring(left.resultdef)) then
  607. begin
  608. { insert typeconv, except for chars that are handled in
  609. secondpass and except for ansi/wide string that can
  610. be converted immediatly }
  611. if not direct_shortstring_assignment then
  612. inserttypeconv(right,left.resultdef);
  613. if right.resultdef.typ=stringdef then
  614. begin
  615. useshelper:=true;
  616. { convert constant strings to shortstrings. But
  617. skip empty constant strings, that will be handled
  618. in secondpass }
  619. if (right.nodetype=stringconstn) then
  620. begin
  621. { verify if range fits within shortstring }
  622. { just emit a warning, delphi gives an }
  623. { error, only if the type definition of }
  624. { of the string is less < 255 characters }
  625. if not is_open_string(left.resultdef) and
  626. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  627. cgmessage(type_w_string_too_long);
  628. inserttypeconv(right,left.resultdef);
  629. if (right.nodetype=stringconstn) and
  630. (tstringconstnode(right).len=0) then
  631. useshelper:=false;
  632. end
  633. else if (tstringdef(right.resultdef).stringtype in [st_unicodestring,st_widestring]) then
  634. Message2(type_w_implicit_string_cast_loss,right.resultdef.typename,left.resultdef.typename);
  635. { rest is done in pass 1 (JM) }
  636. if useshelper then
  637. exit;
  638. end
  639. end
  640. { floating point assignments can also perform the conversion directly }
  641. else if is_real(left.resultdef) and is_real(right.resultdef) and
  642. not is_constrealnode(right)
  643. {$ifdef cpufpemu}
  644. { the emulator can't do this obviously }
  645. and not(current_settings.fputype in [fpu_libgcc,fpu_soft])
  646. {$endif cpufpemu}
  647. {$ifdef x86}
  648. { the assignment node code can't convert a double in an }
  649. { sse register to an extended value in memory more }
  650. { efficiently than a type conversion node, so don't }
  651. { bother implementing support for that }
  652. and (use_vectorfpu(left.resultdef) or not(use_vectorfpu(right.resultdef)))
  653. {$endif}
  654. {$ifdef arm}
  655. { the assignment node code can't convert a single in
  656. an interger register to a double in an mmregister or
  657. vice versa }
  658. and (use_vectorfpu(left.resultdef) and
  659. use_vectorfpu(right.resultdef) and
  660. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype))
  661. {$endif}
  662. then
  663. begin
  664. if not(nf_internal in flags) then
  665. check_ranges(fileinfo,right,left.resultdef);
  666. end
  667. else
  668. begin
  669. { check if the assignment may cause a range check error }
  670. if not(nf_internal in flags) then
  671. check_ranges(fileinfo,right,left.resultdef);
  672. { beginners might be confused about an error message like
  673. Incompatible types: got "untyped" expected "LongInt"
  674. when trying to assign the result of a procedure, so give
  675. a better error message, see also #19122 }
  676. if (left.resultdef.typ<>procvardef) and
  677. (right.nodetype=calln) and is_void(right.resultdef) then
  678. CGMessage(type_e_procedures_return_no_value)
  679. else if nf_internal in flags then
  680. inserttypeconv_internal(right,left.resultdef)
  681. else
  682. inserttypeconv(right,left.resultdef);
  683. end;
  684. { call helpers for interface }
  685. if is_interfacecom_or_dispinterface(left.resultdef) then
  686. begin
  687. { Normal interface assignments are handled by the generic refcount incr/decr }
  688. if not def_is_related(right.resultdef,left.resultdef) then
  689. begin
  690. { remove property flag to avoid errors, see comments for }
  691. { tf_winlikewidestring assignments below }
  692. exclude(left.flags,nf_isproperty);
  693. hp:=
  694. ccallparanode.create(
  695. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  696. ccallparanode.create(
  697. ctypeconvnode.create_internal(right,voidpointertype),
  698. ccallparanode.create(
  699. ctypeconvnode.create_internal(left,voidpointertype),
  700. nil)));
  701. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  702. left:=nil;
  703. right:=nil;
  704. exit;
  705. end;
  706. end;
  707. { check if local proc/func is assigned to procvar }
  708. if right.resultdef.typ=procvardef then
  709. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  710. end;
  711. function tassignmentnode.pass_1 : tnode;
  712. var
  713. hp: tnode;
  714. oldassignmentnode : tassignmentnode;
  715. hdef: tdef;
  716. hs: string;
  717. needrtti: boolean;
  718. begin
  719. result:=nil;
  720. expectloc:=LOC_VOID;
  721. firstpass(left);
  722. { Optimize the reuse of the destination of the assingment in left.
  723. Allow the use of the left inside the tree generated on the right.
  724. This is especially useful for string routines where the destination
  725. is pushed as a parameter. Using the final destination of left directly
  726. save a temp allocation and copy of data (PFV) }
  727. oldassignmentnode:=aktassignmentnode;
  728. aktassignmentnode:=self;
  729. firstpass(right);
  730. aktassignmentnode:=oldassignmentnode;
  731. if nf_assign_done_in_right in flags then
  732. begin
  733. result:=right;
  734. right:=nil;
  735. exit;
  736. end;
  737. if codegenerror then
  738. exit;
  739. { assignment to refcounted variable -> inc/decref }
  740. if is_managed_type(left.resultdef) then
  741. include(current_procinfo.flags,pi_do_call);
  742. needrtti:=false;
  743. if (is_shortstring(left.resultdef)) then
  744. begin
  745. if right.resultdef.typ=stringdef then
  746. begin
  747. if (right.nodetype<>stringconstn) or
  748. (tstringconstnode(right).len<>0) then
  749. begin
  750. { remove property flag to avoid errors, see comments for }
  751. { tf_winlikewidestring assignments below }
  752. exclude(left.flags, nf_isproperty);
  753. hp:=ccallparanode.create
  754. (right,
  755. ccallparanode.create(left,nil));
  756. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  757. firstpass(result);
  758. left:=nil;
  759. right:=nil;
  760. end;
  761. end;
  762. exit;
  763. end
  764. { call helpers for composite types containing automated types }
  765. else if is_managed_type(left.resultdef) and
  766. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  767. not is_interfacecom_or_dispinterface(left.resultdef) and
  768. not is_dynamic_array(left.resultdef) and
  769. not is_const(left) and
  770. not(target_info.system in systems_garbage_collected_managed_types) then
  771. begin
  772. hp:=ccallparanode.create(caddrnode.create_internal(
  773. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  774. ccallparanode.create(ctypeconvnode.create_internal(
  775. caddrnode.create_internal(left),voidpointertype),
  776. ccallparanode.create(ctypeconvnode.create_internal(
  777. caddrnode.create_internal(right),voidpointertype),
  778. nil)));
  779. result:=ccallnode.createintern('fpc_copy_proc',hp);
  780. firstpass(result);
  781. left:=nil;
  782. right:=nil;
  783. exit;
  784. end
  785. { call helpers for variant, they can contain non ref. counted types like
  786. vararrays which must be really copied }
  787. else if (left.resultdef.typ=variantdef) and
  788. not(is_const(left)) and
  789. not(target_info.system in systems_garbage_collected_managed_types) then
  790. begin
  791. { remove property flag to avoid errors, see comments for }
  792. { tf_winlikewidestring assignments below }
  793. exclude(left.flags,nf_isproperty);
  794. hdef:=search_system_type('TVARDATA').typedef;
  795. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  796. right,hdef),
  797. ccallparanode.create(ctypeconvnode.create_internal(
  798. left,hdef),
  799. nil));
  800. result:=ccallnode.createintern('fpc_variant_copy',hp);
  801. firstpass(result);
  802. left:=nil;
  803. right:=nil;
  804. exit;
  805. end
  806. else if not(target_info.system in systems_garbage_collected_managed_types) and
  807. not(is_const(left)) then
  808. begin
  809. { call helpers for pointer-sized managed types }
  810. if is_widestring(left.resultdef) then
  811. hs:='fpc_widestr_assign'
  812. else if is_ansistring(left.resultdef) then
  813. hs:='fpc_ansistr_assign'
  814. else if is_unicodestring(left.resultdef) then
  815. hs:='fpc_unicodestr_assign'
  816. else if is_interfacecom_or_dispinterface(left.resultdef) then
  817. hs:='fpc_intf_assign'
  818. else if is_dynamic_array(left.resultdef) then
  819. begin
  820. hs:='fpc_dynarray_assign';
  821. needrtti:=true;
  822. end
  823. else
  824. exit;
  825. end
  826. else
  827. exit;
  828. { The first argument of these procedures is a var parameter. Properties cannot }
  829. { be passed to var or out parameters, because in that case setters/getters are not }
  830. { used. Further, if we would allow it in case there are no getters or setters, you }
  831. { would need source changes in case these are introduced later on, thus defeating }
  832. { part of the transparency advantages of properties. In this particular case, }
  833. { however: }
  834. { a) if there is a setter, this code will not be used since then the assignment }
  835. { will be converted to a procedure call }
  836. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  837. { the refcount of the field to which we are writing }
  838. { c) source code changes are not required if a setter is added/removed, because }
  839. { this transformation is handled at compile time }
  840. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  841. { is a property which refers to a field without a setter call, we will not get }
  842. { an error about trying to pass a property as a var parameter }
  843. exclude(left.flags,nf_isproperty);
  844. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  845. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  846. nil));
  847. if needrtti then
  848. hp:=ccallparanode.create(
  849. caddrnode.create_internal(
  850. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  851. hp);
  852. result:=ccallnode.createintern(hs,hp);
  853. firstpass(result);
  854. left:=nil;
  855. right:=nil;
  856. end;
  857. function tassignmentnode.docompare(p: tnode): boolean;
  858. begin
  859. docompare :=
  860. inherited docompare(p) and
  861. (assigntype = tassignmentnode(p).assigntype);
  862. end;
  863. {$ifdef state_tracking}
  864. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  865. var se:Tstate_entry;
  866. begin
  867. track_state_pass:=false;
  868. if exec_known then
  869. begin
  870. track_state_pass:=right.track_state_pass(exec_known);
  871. {Force a new resultdef pass.}
  872. right.resultdef:=nil;
  873. do_typecheckpass(right);
  874. typecheckpass(right);
  875. aktstate.store_fact(left.getcopy,right.getcopy);
  876. end
  877. else
  878. aktstate.delete_fact(left);
  879. end;
  880. {$endif}
  881. {$ifdef DEBUG_NODE_XML}
  882. procedure TAssignmentNode.XMLPrintNodeData(var T: Text);
  883. begin
  884. { For assignments, put the left and right branches on the same level for clarity }
  885. XMLPrintNode(T, Left);
  886. XMLPrintNode(T, Right);
  887. PrintNodeUnindent;
  888. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  889. end;
  890. {$endif DEBUG_NODE_XML}
  891. {*****************************************************************************
  892. TARRAYCONSTRUCTORRANGENODE
  893. *****************************************************************************}
  894. constructor tarrayconstructorrangenode.create(l,r : tnode);
  895. begin
  896. inherited create(arrayconstructorrangen,l,r);
  897. end;
  898. function tarrayconstructorrangenode.pass_typecheck:tnode;
  899. begin
  900. result:=nil;
  901. typecheckpass(left);
  902. typecheckpass(right);
  903. set_varstate(left,vs_read,[vsf_must_be_valid]);
  904. set_varstate(right,vs_read,[vsf_must_be_valid]);
  905. if codegenerror then
  906. exit;
  907. resultdef:=left.resultdef;
  908. end;
  909. function tarrayconstructorrangenode.pass_1 : tnode;
  910. begin
  911. result:=nil;
  912. CGMessage(parser_e_illegal_expression);
  913. end;
  914. {****************************************************************************
  915. TARRAYCONSTRUCTORNODE
  916. *****************************************************************************}
  917. constructor tarrayconstructornode.create(l,r : tnode);
  918. begin
  919. inherited create(arrayconstructorn,l,r);
  920. allow_array_constructor:=false;
  921. end;
  922. function tarrayconstructornode.dogetcopy : tnode;
  923. var
  924. n : tarrayconstructornode;
  925. begin
  926. n:=tarrayconstructornode(inherited dogetcopy);
  927. result:=n;
  928. end;
  929. function tarrayconstructornode.has_range_node:boolean;
  930. var
  931. n : tarrayconstructornode;
  932. begin
  933. result:=false;
  934. n:=self;
  935. while assigned(n) do
  936. begin
  937. if assigned(n.left) and (n.left.nodetype=arrayconstructorrangen) then
  938. begin
  939. result:=true;
  940. break;
  941. end;
  942. n:=tarrayconstructornode(n.right);
  943. end;
  944. end;
  945. function tarrayconstructornode.isempty:boolean;
  946. begin
  947. result:=not(assigned(left)) and not(assigned(right));
  948. end;
  949. function tarrayconstructornode.pass_typecheck:tnode;
  950. var
  951. hdef : tdef;
  952. hp : tarrayconstructornode;
  953. len : longint;
  954. varia : boolean;
  955. eq : tequaltype;
  956. hnodetype : tnodetype;
  957. begin
  958. result:=nil;
  959. { are we allowing array constructor? Then convert it to a set.
  960. Do this only if we didn't convert the arrayconstructor yet. This
  961. is needed for the cases where the resultdef is forced for a second
  962. run }
  963. if not allow_array_constructor or has_range_node then
  964. begin
  965. hp:=tarrayconstructornode(getcopy);
  966. arrayconstructor_to_set(tnode(hp));
  967. result:=hp;
  968. exit;
  969. end;
  970. { only pass left tree, right tree contains next construct if any }
  971. hdef:=nil;
  972. hnodetype:=errorn;
  973. len:=0;
  974. varia:=false;
  975. if assigned(left) then
  976. begin
  977. hp:=self;
  978. while assigned(hp) do
  979. begin
  980. typecheckpass(hp.left);
  981. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  982. if (hdef=nil) then
  983. begin
  984. hdef:=hp.left.resultdef;
  985. hnodetype:=hp.left.nodetype;
  986. end
  987. else
  988. begin
  989. { If we got a niln we don't know the type yet and need to take the
  990. type of the next array element.
  991. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  992. if hnodetype=niln then
  993. begin
  994. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  995. if eq>te_incompatible then
  996. begin
  997. hdef:=hp.left.resultdef;
  998. hnodetype:=hp.left.nodetype;
  999. end;
  1000. end
  1001. else
  1002. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  1003. if (not varia) and (eq<te_equal) then
  1004. begin
  1005. { If both are integers we need to take the type that can hold both
  1006. defs }
  1007. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  1008. begin
  1009. if is_in_limit(hdef,hp.left.resultdef) then
  1010. hdef:=hp.left.resultdef;
  1011. end
  1012. else
  1013. if (nf_novariaallowed in flags) then
  1014. varia:=true;
  1015. end;
  1016. end;
  1017. inc(len);
  1018. hp:=tarrayconstructornode(hp.right);
  1019. end;
  1020. end;
  1021. { Set the type of empty or varia arrays to void. Also
  1022. do this if the type is array of const/open array
  1023. because those can't be used with setelementdef }
  1024. if not assigned(hdef) or
  1025. varia or
  1026. is_array_of_const(hdef) or
  1027. is_open_array(hdef) then
  1028. hdef:=voidtype;
  1029. resultdef:=carraydef.create(0,len-1,s32inttype);
  1030. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  1031. if varia then
  1032. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  1033. tarraydef(resultdef).elementdef:=hdef;
  1034. end;
  1035. procedure tarrayconstructornode.force_type(def:tdef);
  1036. var
  1037. hp : tarrayconstructornode;
  1038. begin
  1039. tarraydef(resultdef).elementdef:=def;
  1040. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  1041. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  1042. if assigned(left) then
  1043. begin
  1044. hp:=self;
  1045. while assigned(hp) do
  1046. begin
  1047. inserttypeconv(hp.left,def);
  1048. hp:=tarrayconstructornode(hp.right);
  1049. end;
  1050. end;
  1051. end;
  1052. procedure tarrayconstructornode.insert_typeconvs;
  1053. var
  1054. hp : tarrayconstructornode;
  1055. dovariant : boolean;
  1056. begin
  1057. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  1058. { only pass left tree, right tree contains next construct if any }
  1059. if assigned(left) then
  1060. begin
  1061. hp:=self;
  1062. while assigned(hp) do
  1063. begin
  1064. typecheckpass(hp.left);
  1065. { Insert typeconvs for array of const }
  1066. if dovariant then
  1067. { at this time C varargs are no longer an arrayconstructornode }
  1068. insert_varargstypeconv(hp.left,false);
  1069. hp:=tarrayconstructornode(hp.right);
  1070. end;
  1071. end;
  1072. end;
  1073. function tarrayconstructornode.pass_1 : tnode;
  1074. var
  1075. hp : tarrayconstructornode;
  1076. do_variant,
  1077. do_managed_variant:boolean;
  1078. begin
  1079. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  1080. do_managed_variant:=
  1081. do_variant and
  1082. (target_info.system in systems_managed_vm);
  1083. result:=nil;
  1084. { Insert required type convs, this must be
  1085. done in pass 1, because the call must be
  1086. typecheckpassed already }
  1087. if assigned(left) then
  1088. begin
  1089. insert_typeconvs;
  1090. { call firstpass for all nodes }
  1091. hp:=self;
  1092. while assigned(hp) do
  1093. begin
  1094. if hp.left<>nil then
  1095. begin
  1096. {This check is pessimistic; a call will happen depending
  1097. on the location in which the elements will be found in
  1098. pass 2.}
  1099. if not do_variant then
  1100. include(current_procinfo.flags,pi_do_call);
  1101. firstpass(hp.left);
  1102. if do_managed_variant then
  1103. wrapmanagedvarrec(hp.left);
  1104. end;
  1105. hp:=tarrayconstructornode(hp.right);
  1106. end;
  1107. end;
  1108. { set the elementdef to the correct type in case of a variant array }
  1109. if do_variant then
  1110. tarraydef(resultdef).elementdef:=search_system_type('TVARREC').typedef;
  1111. expectloc:=LOC_CREFERENCE;
  1112. end;
  1113. function tarrayconstructornode.docompare(p: tnode): boolean;
  1114. begin
  1115. docompare:=inherited docompare(p);
  1116. end;
  1117. {*****************************************************************************
  1118. TTYPENODE
  1119. *****************************************************************************}
  1120. constructor ttypenode.create(def:tdef);
  1121. begin
  1122. inherited create(typen);
  1123. typedef:=def;
  1124. typesym:=def.typesym;
  1125. allowed:=false;
  1126. helperallowed:=false;
  1127. end;
  1128. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1129. begin
  1130. inherited ppuload(t,ppufile);
  1131. ppufile.getderef(typedefderef);
  1132. ppufile.getderef(typesymderef);
  1133. allowed:=ppufile.getboolean;
  1134. helperallowed:=ppufile.getboolean;
  1135. end;
  1136. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  1137. begin
  1138. inherited ppuwrite(ppufile);
  1139. ppufile.putderef(typedefderef);
  1140. ppufile.putderef(typesymderef);
  1141. ppufile.putboolean(allowed);
  1142. ppufile.putboolean(helperallowed);
  1143. end;
  1144. procedure ttypenode.buildderefimpl;
  1145. begin
  1146. inherited buildderefimpl;
  1147. typedefderef.build(typedef);
  1148. typesymderef.build(typesym);
  1149. end;
  1150. procedure ttypenode.derefimpl;
  1151. begin
  1152. inherited derefimpl;
  1153. typedef:=tdef(typedefderef.resolve);
  1154. typesym:=tsym(typesymderef.resolve);
  1155. end;
  1156. function ttypenode.pass_typecheck:tnode;
  1157. begin
  1158. result:=nil;
  1159. resultdef:=typedef;
  1160. { check if it's valid }
  1161. if typedef.typ = errordef then
  1162. CGMessage(parser_e_illegal_expression);
  1163. end;
  1164. function ttypenode.pass_1 : tnode;
  1165. begin
  1166. result:=nil;
  1167. expectloc:=LOC_VOID;
  1168. { a typenode can't generate code, so we give here
  1169. an error. Else it'll be an abstract error in pass_generate_code.
  1170. Only when the allowed flag is set we don't generate
  1171. an error }
  1172. if not allowed then
  1173. CGMessage(parser_e_no_type_not_allowed_here);
  1174. if not helperallowed and is_objectpascal_helper(typedef) then
  1175. CGMessage(parser_e_no_category_as_types);
  1176. end;
  1177. function ttypenode.dogetcopy : tnode;
  1178. var
  1179. n : ttypenode;
  1180. begin
  1181. n:=ttypenode(inherited dogetcopy);
  1182. n.allowed:=allowed;
  1183. n.typedef:=typedef;
  1184. n.helperallowed:=helperallowed;
  1185. result:=n;
  1186. end;
  1187. function ttypenode.docompare(p: tnode): boolean;
  1188. begin
  1189. docompare :=
  1190. inherited docompare(p) and
  1191. (typedef=ttypenode(p).typedef) and
  1192. (allowed=ttypenode(p).allowed) and
  1193. (helperallowed=ttypenode(p).helperallowed);
  1194. end;
  1195. {*****************************************************************************
  1196. TRTTINODE
  1197. *****************************************************************************}
  1198. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  1199. begin
  1200. inherited create(rttin);
  1201. rttidef:=def;
  1202. rttitype:=rt;
  1203. rttidatatype:=dt;
  1204. end;
  1205. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1206. begin
  1207. inherited ppuload(t,ppufile);
  1208. ppufile.getderef(rttidefderef);
  1209. rttitype:=trttitype(ppufile.getbyte);
  1210. rttidatatype:=trttidatatype(ppufile.getbyte);
  1211. end;
  1212. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1213. begin
  1214. inherited ppuwrite(ppufile);
  1215. ppufile.putderef(rttidefderef);
  1216. ppufile.putbyte(byte(rttitype));
  1217. ppufile.putbyte(byte(rttidatatype));
  1218. end;
  1219. procedure trttinode.buildderefimpl;
  1220. begin
  1221. inherited buildderefimpl;
  1222. rttidefderef.build(rttidef);
  1223. end;
  1224. procedure trttinode.derefimpl;
  1225. begin
  1226. inherited derefimpl;
  1227. rttidef:=tstoreddef(rttidefderef.resolve);
  1228. end;
  1229. function trttinode.dogetcopy : tnode;
  1230. var
  1231. n : trttinode;
  1232. begin
  1233. n:=trttinode(inherited dogetcopy);
  1234. n.rttidef:=rttidef;
  1235. n.rttitype:=rttitype;
  1236. n.rttidatatype:=rttidatatype;
  1237. result:=n;
  1238. end;
  1239. function trttinode.pass_typecheck:tnode;
  1240. begin
  1241. { rtti information will be returned as a void pointer }
  1242. result:=nil;
  1243. resultdef:=voidpointertype;
  1244. end;
  1245. function trttinode.pass_1 : tnode;
  1246. begin
  1247. result:=nil;
  1248. expectloc:=LOC_CREFERENCE;
  1249. end;
  1250. function trttinode.docompare(p: tnode): boolean;
  1251. begin
  1252. docompare :=
  1253. inherited docompare(p) and
  1254. (rttidef = trttinode(p).rttidef) and
  1255. (rttitype = trttinode(p).rttitype) and
  1256. (rttidatatype = trttinode(p).rttidatatype);
  1257. end;
  1258. end.