nld.pas 53 KB

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