nld.pas 43 KB

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