nld.pas 51 KB

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