nld.pas 55 KB

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