nld.pas 41 KB

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