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. 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_vectorfpu(left.resultdef) or not(use_vectorfpu(right.resultdef)))
  511. {$endif}
  512. {$ifdef arm}
  513. { the assignment node code can't convert a single in
  514. an interger register to a double in an mmregister or
  515. vice versa }
  516. and (use_vectorfpu(left.resultdef) and
  517. use_vectorfpu(right.resultdef) and
  518. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype))
  519. {$endif}
  520. then
  521. begin
  522. check_ranges(fileinfo,right,left.resultdef);
  523. end
  524. else
  525. begin
  526. { check if the assignment may cause a range check error }
  527. check_ranges(fileinfo,right,left.resultdef);
  528. inserttypeconv(right,left.resultdef);
  529. end;
  530. { call helpers for interface }
  531. if is_interfacecom(left.resultdef) then
  532. begin
  533. { Normal interface assignments are handled by the generic refcount incr/decr }
  534. if not right.resultdef.is_related(left.resultdef) then
  535. begin
  536. { remove property flag to avoid errors, see comments for }
  537. { tf_winlikewidestring assignments below }
  538. exclude(left.flags,nf_isproperty);
  539. hp:=
  540. ccallparanode.create(
  541. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  542. ccallparanode.create(
  543. ctypeconvnode.create_internal(right,voidpointertype),
  544. ccallparanode.create(
  545. ctypeconvnode.create_internal(left,voidpointertype),
  546. nil)));
  547. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  548. left:=nil;
  549. right:=nil;
  550. exit;
  551. end;
  552. end;
  553. { check if local proc/func is assigned to procvar }
  554. if right.resultdef.typ=procvardef then
  555. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  556. end;
  557. function tassignmentnode.pass_1 : tnode;
  558. var
  559. hp: tnode;
  560. oldassignmentnode : tassignmentnode;
  561. begin
  562. result:=nil;
  563. expectloc:=LOC_VOID;
  564. firstpass(left);
  565. { Optimize the reuse of the destination of the assingment in left.
  566. Allow the use of the left inside the tree generated on the right.
  567. This is especially usefull for string routines where the destination
  568. is pushed as a parameter. Using the final destination of left directly
  569. save a temp allocation and copy of data (PFV) }
  570. oldassignmentnode:=aktassignmentnode;
  571. aktassignmentnode:=self;
  572. firstpass(right);
  573. aktassignmentnode:=oldassignmentnode;
  574. if nf_assign_done_in_right in flags then
  575. begin
  576. result:=right;
  577. right:=nil;
  578. exit;
  579. end;
  580. if codegenerror then
  581. exit;
  582. { assignment to refcounted variable -> inc/decref }
  583. if (not is_class(left.resultdef) and
  584. left.resultdef.needs_inittable) 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 (left.resultdef.needs_inittable) 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.