nld.pas 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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. current_procinfo.set_needs_parentfp(tprocdef(symtable.defowner).parast.symtablelevel);
  316. { reference in nested procedures, variable needs to be in memory }
  317. { and behaves as if its address escapes its parent block }
  318. make_not_regable(self,[ra_different_scope]);
  319. end;
  320. resultdef:=tabstractvarsym(symtableentry).vardef;
  321. { e.g. self for objects is passed as var-parameter on the caller
  322. side, but on the callee-side we use it as a pointer ->
  323. adjust }
  324. if (loadnf_load_addr in loadnodeflags) then
  325. resultdef:=cpointerdef.getreusable(resultdef);
  326. if (vo_is_self in tabstractvarsym(symtableentry).varoptions) and (resultdef=objc_idtype) and
  327. (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  328. resultdef:=cclassrefdef.create(tprocdef(symtableentry.owner.defowner).struct)
  329. end;
  330. procsym :
  331. begin
  332. { Return the first procdef. In case of overloaded
  333. procdefs the matching procdef will be choosen
  334. when the expected procvardef is known, see get_information
  335. in htypechk.pas (PFV) }
  336. if not assigned(fprocdef) then
  337. fprocdef:=tprocdef(tprocsym(symtableentry).ProcdefList[0])
  338. else if po_kylixlocal in fprocdef.procoptions then
  339. CGMessage(type_e_cant_take_address_of_local_subroutine);
  340. { the result is a fprocdef, addrn and proc_to_procvar
  341. typeconvn need this as resultdef so they know
  342. that the address needs to be returned }
  343. resultdef:=fprocdef;
  344. if is_nested_pd(fprocdef) and is_nested_pd(current_procinfo.procdef) then
  345. current_procinfo.set_needs_parentfp(tprocdef(fprocdef.owner.defowner).parast.symtablelevel);
  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 arm}
  663. {$ifdef xtensa}
  664. and not((FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) xor
  665. (FPUXTENSA_DOUBLE in fpu_capabilities[current_settings.fputype]))
  666. {$endif}
  667. then
  668. begin
  669. if not(nf_internal in flags) then
  670. check_ranges(fileinfo,right,left.resultdef);
  671. end
  672. else
  673. begin
  674. { check if the assignment may cause a range check error }
  675. if not(nf_internal in flags) then
  676. check_ranges(fileinfo,right,left.resultdef);
  677. { beginners might be confused about an error message like
  678. Incompatible types: got "untyped" expected "LongInt"
  679. when trying to assign the result of a procedure, so give
  680. a better error message, see also #19122 }
  681. if (left.resultdef.typ<>procvardef) and
  682. (right.nodetype=calln) and is_void(right.resultdef) then
  683. CGMessage(type_e_procedures_return_no_value)
  684. else if nf_internal in flags then
  685. inserttypeconv_internal(right,left.resultdef)
  686. else
  687. inserttypeconv(right,left.resultdef);
  688. end;
  689. { call helpers for interface }
  690. if is_interfacecom_or_dispinterface(left.resultdef) then
  691. begin
  692. { Normal interface assignments are handled by the generic refcount incr/decr }
  693. if not def_is_related(right.resultdef,left.resultdef) then
  694. begin
  695. { remove property flag to avoid errors, see comments for }
  696. { tf_winlikewidestring assignments below }
  697. exclude(left.flags,nf_isproperty);
  698. hp:=
  699. ccallparanode.create(
  700. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  701. ccallparanode.create(
  702. ctypeconvnode.create_internal(right,voidpointertype),
  703. ccallparanode.create(
  704. ctypeconvnode.create_internal(left,voidpointertype),
  705. nil)));
  706. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  707. left:=nil;
  708. right:=nil;
  709. exit;
  710. end;
  711. end;
  712. { check if local proc/func is assigned to procvar }
  713. if right.resultdef.typ=procvardef then
  714. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  715. end;
  716. function tassignmentnode.pass_1 : tnode;
  717. var
  718. hp: tnode;
  719. oldassignmentnode : tassignmentnode;
  720. hdef: tdef;
  721. hs: string;
  722. needrtti: boolean;
  723. begin
  724. result:=nil;
  725. expectloc:=LOC_VOID;
  726. firstpass(left);
  727. { Optimize the reuse of the destination of the assingment in left.
  728. Allow the use of the left inside the tree generated on the right.
  729. This is especially useful for string routines where the destination
  730. is pushed as a parameter. Using the final destination of left directly
  731. save a temp allocation and copy of data (PFV) }
  732. oldassignmentnode:=aktassignmentnode;
  733. aktassignmentnode:=self;
  734. firstpass(right);
  735. aktassignmentnode:=oldassignmentnode;
  736. if nf_assign_done_in_right in flags then
  737. begin
  738. result:=right;
  739. right:=nil;
  740. exit;
  741. end;
  742. if codegenerror then
  743. exit;
  744. { assignment to refcounted variable -> inc/decref }
  745. if is_managed_type(left.resultdef) then
  746. include(current_procinfo.flags,pi_do_call);
  747. needrtti:=false;
  748. if (is_shortstring(left.resultdef)) then
  749. begin
  750. if right.resultdef.typ=stringdef then
  751. begin
  752. if (right.nodetype<>stringconstn) or
  753. (tstringconstnode(right).len<>0) then
  754. begin
  755. { remove property flag to avoid errors, see comments for }
  756. { tf_winlikewidestring assignments below }
  757. exclude(left.flags, nf_isproperty);
  758. hp:=ccallparanode.create
  759. (right,
  760. ccallparanode.create(left,nil));
  761. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  762. firstpass(result);
  763. left:=nil;
  764. right:=nil;
  765. end;
  766. end;
  767. exit;
  768. end
  769. { call helpers for composite types containing automated types }
  770. else if is_managed_type(left.resultdef) and
  771. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  772. not is_interfacecom_or_dispinterface(left.resultdef) and
  773. not is_dynamic_array(left.resultdef) and
  774. not is_const(left) and
  775. not(target_info.system in systems_garbage_collected_managed_types) then
  776. begin
  777. hp:=ccallparanode.create(caddrnode.create_internal(
  778. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  779. ccallparanode.create(ctypeconvnode.create_internal(
  780. caddrnode.create_internal(left),voidpointertype),
  781. ccallparanode.create(ctypeconvnode.create_internal(
  782. caddrnode.create_internal(right),voidpointertype),
  783. nil)));
  784. result:=ccallnode.createintern('fpc_copy_proc',hp);
  785. firstpass(result);
  786. left:=nil;
  787. right:=nil;
  788. exit;
  789. end
  790. { call helpers for variant, they can contain non ref. counted types like
  791. vararrays which must be really copied }
  792. else if (left.resultdef.typ=variantdef) and
  793. not(is_const(left)) and
  794. not(target_info.system in systems_garbage_collected_managed_types) then
  795. begin
  796. { remove property flag to avoid errors, see comments for }
  797. { tf_winlikewidestring assignments below }
  798. exclude(left.flags,nf_isproperty);
  799. hdef:=search_system_type('TVARDATA').typedef;
  800. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  801. right,hdef),
  802. ccallparanode.create(ctypeconvnode.create_internal(
  803. left,hdef),
  804. nil));
  805. result:=ccallnode.createintern('fpc_variant_copy',hp);
  806. firstpass(result);
  807. left:=nil;
  808. right:=nil;
  809. exit;
  810. end
  811. else if not(target_info.system in systems_garbage_collected_managed_types) and
  812. not(is_const(left)) then
  813. begin
  814. { call helpers for pointer-sized managed types }
  815. if is_widestring(left.resultdef) then
  816. hs:='fpc_widestr_assign'
  817. else if is_ansistring(left.resultdef) then
  818. hs:='fpc_ansistr_assign'
  819. else if is_unicodestring(left.resultdef) then
  820. hs:='fpc_unicodestr_assign'
  821. else if is_interfacecom_or_dispinterface(left.resultdef) then
  822. hs:='fpc_intf_assign'
  823. else if is_dynamic_array(left.resultdef) then
  824. begin
  825. hs:='fpc_dynarray_assign';
  826. needrtti:=true;
  827. end
  828. else
  829. exit;
  830. end
  831. else
  832. exit;
  833. { The first argument of these procedures is a var parameter. Properties cannot }
  834. { be passed to var or out parameters, because in that case setters/getters are not }
  835. { used. Further, if we would allow it in case there are no getters or setters, you }
  836. { would need source changes in case these are introduced later on, thus defeating }
  837. { part of the transparency advantages of properties. In this particular case, }
  838. { however: }
  839. { a) if there is a setter, this code will not be used since then the assignment }
  840. { will be converted to a procedure call }
  841. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  842. { the refcount of the field to which we are writing }
  843. { c) source code changes are not required if a setter is added/removed, because }
  844. { this transformation is handled at compile time }
  845. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  846. { is a property which refers to a field without a setter call, we will not get }
  847. { an error about trying to pass a property as a var parameter }
  848. exclude(left.flags,nf_isproperty);
  849. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  850. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  851. nil));
  852. if needrtti then
  853. hp:=ccallparanode.create(
  854. caddrnode.create_internal(
  855. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  856. hp);
  857. result:=ccallnode.createintern(hs,hp);
  858. firstpass(result);
  859. left:=nil;
  860. right:=nil;
  861. end;
  862. function tassignmentnode.docompare(p: tnode): boolean;
  863. begin
  864. docompare :=
  865. inherited docompare(p) and
  866. (assigntype = tassignmentnode(p).assigntype);
  867. end;
  868. {$ifdef state_tracking}
  869. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  870. var se:Tstate_entry;
  871. begin
  872. track_state_pass:=false;
  873. if exec_known then
  874. begin
  875. track_state_pass:=right.track_state_pass(exec_known);
  876. {Force a new resultdef pass.}
  877. right.resultdef:=nil;
  878. do_typecheckpass(right);
  879. typecheckpass(right);
  880. aktstate.store_fact(left.getcopy,right.getcopy);
  881. end
  882. else
  883. aktstate.delete_fact(left);
  884. end;
  885. {$endif}
  886. {$ifdef DEBUG_NODE_XML}
  887. procedure TAssignmentNode.XMLPrintNodeData(var T: Text);
  888. begin
  889. { For assignments, put the left and right branches on the same level for clarity }
  890. XMLPrintNode(T, Left);
  891. XMLPrintNode(T, Right);
  892. PrintNodeUnindent;
  893. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  894. end;
  895. {$endif DEBUG_NODE_XML}
  896. {*****************************************************************************
  897. TARRAYCONSTRUCTORRANGENODE
  898. *****************************************************************************}
  899. constructor tarrayconstructorrangenode.create(l,r : tnode);
  900. begin
  901. inherited create(arrayconstructorrangen,l,r);
  902. end;
  903. function tarrayconstructorrangenode.pass_typecheck:tnode;
  904. begin
  905. result:=nil;
  906. typecheckpass(left);
  907. typecheckpass(right);
  908. set_varstate(left,vs_read,[vsf_must_be_valid]);
  909. set_varstate(right,vs_read,[vsf_must_be_valid]);
  910. if codegenerror then
  911. exit;
  912. resultdef:=left.resultdef;
  913. end;
  914. function tarrayconstructorrangenode.pass_1 : tnode;
  915. begin
  916. result:=nil;
  917. CGMessage(parser_e_illegal_expression);
  918. end;
  919. {****************************************************************************
  920. TARRAYCONSTRUCTORNODE
  921. *****************************************************************************}
  922. constructor tarrayconstructornode.create(l,r : tnode);
  923. begin
  924. inherited create(arrayconstructorn,l,r);
  925. allow_array_constructor:=false;
  926. end;
  927. function tarrayconstructornode.dogetcopy : tnode;
  928. var
  929. n : tarrayconstructornode;
  930. begin
  931. n:=tarrayconstructornode(inherited dogetcopy);
  932. result:=n;
  933. end;
  934. function tarrayconstructornode.has_range_node:boolean;
  935. var
  936. n : tarrayconstructornode;
  937. begin
  938. result:=false;
  939. n:=self;
  940. while assigned(n) do
  941. begin
  942. if assigned(n.left) and (n.left.nodetype=arrayconstructorrangen) then
  943. begin
  944. result:=true;
  945. break;
  946. end;
  947. n:=tarrayconstructornode(n.right);
  948. end;
  949. end;
  950. function tarrayconstructornode.isempty:boolean;
  951. begin
  952. result:=not(assigned(left)) and not(assigned(right));
  953. end;
  954. function tarrayconstructornode.pass_typecheck:tnode;
  955. var
  956. hdef : tdef;
  957. hp : tarrayconstructornode;
  958. len : longint;
  959. varia : boolean;
  960. eq : tequaltype;
  961. hnodetype : tnodetype;
  962. begin
  963. result:=nil;
  964. { are we allowing array constructor? Then convert it to a set.
  965. Do this only if we didn't convert the arrayconstructor yet. This
  966. is needed for the cases where the resultdef is forced for a second
  967. run }
  968. if not allow_array_constructor or has_range_node then
  969. begin
  970. hp:=tarrayconstructornode(getcopy);
  971. arrayconstructor_to_set(tnode(hp));
  972. result:=hp;
  973. exit;
  974. end;
  975. { only pass left tree, right tree contains next construct if any }
  976. hdef:=nil;
  977. hnodetype:=errorn;
  978. len:=0;
  979. varia:=false;
  980. if assigned(left) then
  981. begin
  982. hp:=self;
  983. while assigned(hp) do
  984. begin
  985. typecheckpass(hp.left);
  986. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  987. if (hdef=nil) then
  988. begin
  989. hdef:=hp.left.resultdef;
  990. hnodetype:=hp.left.nodetype;
  991. end
  992. else
  993. begin
  994. { If we got a niln we don't know the type yet and need to take the
  995. type of the next array element.
  996. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  997. if hnodetype=niln then
  998. begin
  999. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  1000. if eq>te_incompatible then
  1001. begin
  1002. hdef:=hp.left.resultdef;
  1003. hnodetype:=hp.left.nodetype;
  1004. end;
  1005. end
  1006. else
  1007. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  1008. if (not varia) and (eq<te_equal) then
  1009. begin
  1010. { If both are integers we need to take the type that can hold both
  1011. defs }
  1012. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  1013. begin
  1014. if is_in_limit(hdef,hp.left.resultdef) then
  1015. hdef:=hp.left.resultdef;
  1016. end
  1017. else
  1018. if (nf_novariaallowed in flags) then
  1019. varia:=true;
  1020. end;
  1021. end;
  1022. inc(len);
  1023. hp:=tarrayconstructornode(hp.right);
  1024. end;
  1025. end;
  1026. { Set the type of empty or varia arrays to void. Also
  1027. do this if the type is array of const/open array
  1028. because those can't be used with setelementdef }
  1029. if not assigned(hdef) or
  1030. varia or
  1031. is_array_of_const(hdef) or
  1032. is_open_array(hdef) then
  1033. hdef:=voidtype;
  1034. resultdef:=carraydef.create(0,len-1,s32inttype);
  1035. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  1036. if varia then
  1037. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  1038. tarraydef(resultdef).elementdef:=hdef;
  1039. end;
  1040. procedure tarrayconstructornode.force_type(def:tdef);
  1041. var
  1042. hp : tarrayconstructornode;
  1043. begin
  1044. tarraydef(resultdef).elementdef:=def;
  1045. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  1046. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  1047. if assigned(left) then
  1048. begin
  1049. hp:=self;
  1050. while assigned(hp) do
  1051. begin
  1052. inserttypeconv(hp.left,def);
  1053. hp:=tarrayconstructornode(hp.right);
  1054. end;
  1055. end;
  1056. end;
  1057. procedure tarrayconstructornode.insert_typeconvs;
  1058. var
  1059. hp : tarrayconstructornode;
  1060. dovariant : boolean;
  1061. begin
  1062. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  1063. { only pass left tree, right tree contains next construct if any }
  1064. if assigned(left) then
  1065. begin
  1066. hp:=self;
  1067. while assigned(hp) do
  1068. begin
  1069. typecheckpass(hp.left);
  1070. { Insert typeconvs for array of const }
  1071. if dovariant then
  1072. { at this time C varargs are no longer an arrayconstructornode }
  1073. insert_varargstypeconv(hp.left,false);
  1074. hp:=tarrayconstructornode(hp.right);
  1075. end;
  1076. end;
  1077. end;
  1078. function tarrayconstructornode.pass_1 : tnode;
  1079. var
  1080. hp : tarrayconstructornode;
  1081. do_variant,
  1082. do_managed_variant:boolean;
  1083. begin
  1084. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  1085. do_managed_variant:=
  1086. do_variant and
  1087. (target_info.system in systems_managed_vm);
  1088. result:=nil;
  1089. { Insert required type convs, this must be
  1090. done in pass 1, because the call must be
  1091. typecheckpassed already }
  1092. if assigned(left) then
  1093. begin
  1094. insert_typeconvs;
  1095. { call firstpass for all nodes }
  1096. hp:=self;
  1097. while assigned(hp) do
  1098. begin
  1099. if hp.left<>nil then
  1100. begin
  1101. {This check is pessimistic; a call will happen depending
  1102. on the location in which the elements will be found in
  1103. pass 2.}
  1104. if not do_variant then
  1105. include(current_procinfo.flags,pi_do_call);
  1106. firstpass(hp.left);
  1107. if do_managed_variant then
  1108. wrapmanagedvarrec(hp.left);
  1109. end;
  1110. hp:=tarrayconstructornode(hp.right);
  1111. end;
  1112. end;
  1113. { set the elementdef to the correct type in case of a variant array }
  1114. if do_variant then
  1115. tarraydef(resultdef).elementdef:=search_system_type('TVARREC').typedef;
  1116. expectloc:=LOC_CREFERENCE;
  1117. inc(current_procinfo.estimatedtempsize,(tarraydef(resultdef).highrange+1)*tarraydef(resultdef).elementdef.size);
  1118. end;
  1119. function tarrayconstructornode.docompare(p: tnode): boolean;
  1120. begin
  1121. docompare:=inherited docompare(p);
  1122. end;
  1123. {*****************************************************************************
  1124. TTYPENODE
  1125. *****************************************************************************}
  1126. constructor ttypenode.create(def:tdef);
  1127. begin
  1128. inherited create(typen);
  1129. typedef:=def;
  1130. typesym:=def.typesym;
  1131. allowed:=false;
  1132. helperallowed:=false;
  1133. end;
  1134. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1135. begin
  1136. inherited ppuload(t,ppufile);
  1137. ppufile.getderef(typedefderef);
  1138. ppufile.getderef(typesymderef);
  1139. allowed:=ppufile.getboolean;
  1140. helperallowed:=ppufile.getboolean;
  1141. end;
  1142. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  1143. begin
  1144. inherited ppuwrite(ppufile);
  1145. ppufile.putderef(typedefderef);
  1146. ppufile.putderef(typesymderef);
  1147. ppufile.putboolean(allowed);
  1148. ppufile.putboolean(helperallowed);
  1149. end;
  1150. procedure ttypenode.buildderefimpl;
  1151. begin
  1152. inherited buildderefimpl;
  1153. typedefderef.build(typedef);
  1154. typesymderef.build(typesym);
  1155. end;
  1156. procedure ttypenode.derefimpl;
  1157. begin
  1158. inherited derefimpl;
  1159. typedef:=tdef(typedefderef.resolve);
  1160. typesym:=tsym(typesymderef.resolve);
  1161. end;
  1162. function ttypenode.pass_typecheck:tnode;
  1163. begin
  1164. result:=nil;
  1165. resultdef:=typedef;
  1166. { check if it's valid }
  1167. if typedef.typ = errordef then
  1168. CGMessage(parser_e_illegal_expression);
  1169. end;
  1170. function ttypenode.pass_1 : tnode;
  1171. begin
  1172. result:=nil;
  1173. expectloc:=LOC_VOID;
  1174. { a typenode can't generate code, so we give here
  1175. an error. Else it'll be an abstract error in pass_generate_code.
  1176. Only when the allowed flag is set we don't generate
  1177. an error }
  1178. if not allowed then
  1179. CGMessage(parser_e_no_type_not_allowed_here);
  1180. if not helperallowed and is_objectpascal_helper(typedef) then
  1181. CGMessage(parser_e_no_category_as_types);
  1182. end;
  1183. function ttypenode.dogetcopy : tnode;
  1184. var
  1185. n : ttypenode;
  1186. begin
  1187. n:=ttypenode(inherited dogetcopy);
  1188. n.allowed:=allowed;
  1189. n.typedef:=typedef;
  1190. n.helperallowed:=helperallowed;
  1191. result:=n;
  1192. end;
  1193. function ttypenode.docompare(p: tnode): boolean;
  1194. begin
  1195. docompare :=
  1196. inherited docompare(p) and
  1197. (typedef=ttypenode(p).typedef) and
  1198. (allowed=ttypenode(p).allowed) and
  1199. (helperallowed=ttypenode(p).helperallowed);
  1200. end;
  1201. {*****************************************************************************
  1202. TRTTINODE
  1203. *****************************************************************************}
  1204. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  1205. begin
  1206. inherited create(rttin);
  1207. rttidef:=def;
  1208. rttitype:=rt;
  1209. rttidatatype:=dt;
  1210. end;
  1211. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1212. begin
  1213. inherited ppuload(t,ppufile);
  1214. ppufile.getderef(rttidefderef);
  1215. rttitype:=trttitype(ppufile.getbyte);
  1216. rttidatatype:=trttidatatype(ppufile.getbyte);
  1217. end;
  1218. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1219. begin
  1220. inherited ppuwrite(ppufile);
  1221. ppufile.putderef(rttidefderef);
  1222. ppufile.putbyte(byte(rttitype));
  1223. ppufile.putbyte(byte(rttidatatype));
  1224. end;
  1225. procedure trttinode.buildderefimpl;
  1226. begin
  1227. inherited buildderefimpl;
  1228. rttidefderef.build(rttidef);
  1229. end;
  1230. procedure trttinode.derefimpl;
  1231. begin
  1232. inherited derefimpl;
  1233. rttidef:=tstoreddef(rttidefderef.resolve);
  1234. end;
  1235. function trttinode.dogetcopy : tnode;
  1236. var
  1237. n : trttinode;
  1238. begin
  1239. n:=trttinode(inherited dogetcopy);
  1240. n.rttidef:=rttidef;
  1241. n.rttitype:=rttitype;
  1242. n.rttidatatype:=rttidatatype;
  1243. result:=n;
  1244. end;
  1245. function trttinode.pass_typecheck:tnode;
  1246. begin
  1247. { rtti information will be returned as a void pointer }
  1248. result:=nil;
  1249. resultdef:=voidpointertype;
  1250. end;
  1251. function trttinode.pass_1 : tnode;
  1252. begin
  1253. result:=nil;
  1254. expectloc:=LOC_CREFERENCE;
  1255. end;
  1256. function trttinode.docompare(p: tnode): boolean;
  1257. begin
  1258. docompare :=
  1259. inherited docompare(p) and
  1260. (rttidef = trttinode(p).rttidef) and
  1261. (rttitype = trttinode(p).rttitype) and
  1262. (rttidatatype = trttinode(p).rttidatatype);
  1263. end;
  1264. end.