nld.pas 54 KB

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