nld.pas 50 KB

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