nld.pas 52 KB

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