nld.pas 51 KB

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