nld.pas 44 KB

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