nld.pas 40 KB

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