nld.pas 44 KB

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