nld.pas 53 KB

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