nld.pas 43 KB

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