nld.pas 53 KB

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