nld.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 (tstringconstnode(right).len=0) then
  490. useshelper:=false;
  491. end;
  492. { rest is done in pass 1 (JM) }
  493. if useshelper then
  494. exit;
  495. end
  496. end
  497. else
  498. begin
  499. { check if the assignment may cause a range check error }
  500. check_ranges(fileinfo,right,left.resultdef);
  501. inserttypeconv(right,left.resultdef);
  502. end;
  503. { call helpers for interface }
  504. if is_interfacecom(left.resultdef) then
  505. begin
  506. { Normal interface assignments are handled by the generic refcount incr/decr }
  507. if not right.resultdef.is_related(left.resultdef) then
  508. begin
  509. { remove property flag to avoid errors, see comments for }
  510. { tf_winlikewidestring assignments below }
  511. exclude(left.flags,nf_isproperty);
  512. hp:=
  513. ccallparanode.create(
  514. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  515. ccallparanode.create(
  516. ctypeconvnode.create_internal(right,voidpointertype),
  517. ccallparanode.create(
  518. ctypeconvnode.create_internal(left,voidpointertype),
  519. nil)));
  520. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  521. left:=nil;
  522. right:=nil;
  523. exit;
  524. end;
  525. end
  526. { call helpers for variant, they can contain non ref. counted types like
  527. vararrays which must be really copied }
  528. else if left.resultdef.typ=variantdef then
  529. begin
  530. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  531. caddrnode.create_internal(right),voidpointertype),
  532. ccallparanode.create(ctypeconvnode.create_internal(
  533. caddrnode.create_internal(left),voidpointertype),
  534. nil));
  535. result:=ccallnode.createintern('fpc_variant_copy',hp);
  536. left:=nil;
  537. right:=nil;
  538. exit;
  539. end
  540. { call helpers for composite types containing automated types }
  541. else if (left.resultdef.needs_inittable) and
  542. (left.resultdef.typ in [arraydef,objectdef,recorddef]) then
  543. begin
  544. hp:=ccallparanode.create(caddrnode.create_internal(
  545. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  546. ccallparanode.create(ctypeconvnode.create_internal(
  547. caddrnode.create_internal(left),voidpointertype),
  548. ccallparanode.create(ctypeconvnode.create_internal(
  549. caddrnode.create_internal(right),voidpointertype),
  550. nil)));
  551. result:=ccallnode.createintern('fpc_copy',hp);
  552. left:=nil;
  553. right:=nil;
  554. exit;
  555. end
  556. { call helpers for windows widestrings, they aren't ref. counted }
  557. else if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  558. begin
  559. { The first argument of fpc_widestr_assign is a var parameter. Properties cannot }
  560. { be passed to var or out parameters, because in that case setters/getters are not }
  561. { used. Further, if we would allow it in case there are no getters or setters, you }
  562. { would need source changes in case these are introduced later on, thus defeating }
  563. { part of the transparency advantages of properties. In this particular case, }
  564. { however: }
  565. { a) if there is a setter, this code will not be used since then the assignment }
  566. { will be converted to a procedure call }
  567. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  568. { the refcount of the field to which we are writing }
  569. { c) source code changes are not required if a setter is added/removed, because }
  570. { this transformation is handled at compile time }
  571. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  572. { is a property which refers to a field without a setter call, we will not get }
  573. { an error about trying to pass a property as a var parameter }
  574. exclude(left.flags,nf_isproperty);
  575. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  576. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  577. nil));
  578. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  579. left:=nil;
  580. right:=nil;
  581. exit;
  582. end;
  583. { check if local proc/func is assigned to procvar }
  584. if right.resultdef.typ=procvardef then
  585. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  586. end;
  587. function tassignmentnode.pass_1 : tnode;
  588. var
  589. hp: tnode;
  590. oldassignmentnode : tassignmentnode;
  591. begin
  592. result:=nil;
  593. expectloc:=LOC_VOID;
  594. firstpass(left);
  595. { Optimize the reuse of the destination of the assingment in left.
  596. Allow the use of the left inside the tree generated on the right.
  597. This is especially usefull for string routines where the destination
  598. is pushed as a parameter. Using the final destination of left directly
  599. save a temp allocation and copy of data (PFV) }
  600. oldassignmentnode:=aktassignmentnode;
  601. if right.nodetype=addn then
  602. aktassignmentnode:=self
  603. else
  604. aktassignmentnode:=nil;
  605. firstpass(right);
  606. aktassignmentnode:=oldassignmentnode;
  607. if nf_assign_done_in_right in flags then
  608. begin
  609. result:=right;
  610. right:=nil;
  611. exit;
  612. end;
  613. if codegenerror then
  614. exit;
  615. { if right is a function call for which the address of the result }
  616. { is allocated by the caller and passed to the function via an }
  617. { invisible function result, try to pass the x in "x:=f(...)" as }
  618. { that function result instead. Condition: x cannot be accessible }
  619. { from within f. This is the case if x is a temp, or x is a local }
  620. { variable or value parameter of the current block and its address }
  621. { is not passed to f. One problem: what if someone takes the }
  622. { address of x, puts it in a pointer variable/field and then }
  623. { accesses it that way from within the function? This is solved }
  624. { (in a conservative way) using the ti_addr_taken/addr_taken flags }
  625. if (cs_opt_level1 in current_settings.optimizerswitches) and
  626. (right.nodetype = calln) and
  627. (right.resultdef=left.resultdef) and
  628. { left must be a temp, since otherwise as soon as you modify the }
  629. { result, the current left node is modified and that one may }
  630. { still be an argument to the function or even accessed in the }
  631. { function }
  632. (
  633. (
  634. (((left.nodetype = temprefn) and
  635. not(ti_addr_taken in ttemprefnode(left).tempinfo^.flags) and
  636. not(ti_may_be_in_reg in ttemprefnode(left).tempinfo^.flags)) or
  637. ((left.nodetype = loadn) and
  638. { nested procedures may access the current procedure's locals }
  639. (tcallnode(right).procdefinition.parast.symtablelevel=normal_function_level) and
  640. { must be a local variable or a value para }
  641. ((tloadnode(left).symtableentry.typ = localvarsym) or
  642. ((tloadnode(left).symtableentry.typ = paravarsym) and
  643. (tparavarsym(tloadnode(left).symtableentry).varspez = vs_value)
  644. )
  645. ) and
  646. { the address may not have been taken of the variable/parameter, because }
  647. { otherwise it's possible that the called function can access it via a }
  648. { global variable or other stored state }
  649. not(tabstractvarsym(tloadnode(left).symtableentry).addr_taken) and
  650. (tabstractvarsym(tloadnode(left).symtableentry).varregable in [vr_none,vr_addr])
  651. )
  652. ) and
  653. paramanager.ret_in_param(right.resultdef,tcallnode(right).procdefinition.proccalloption)
  654. ) or
  655. { there's special support for ansi/widestrings in the callnode }
  656. is_ansistring(right.resultdef) or
  657. is_widestring(right.resultdef)
  658. ) then
  659. begin
  660. if assigned(tcallnode(right).funcretnode) then
  661. internalerror(2007080201);
  662. tcallnode(right).funcretnode := left;
  663. result := right;
  664. left := nil;
  665. right := nil;
  666. exit;
  667. end;
  668. { assignment to refcounted variable -> inc/decref }
  669. if (not is_class(left.resultdef) and
  670. left.resultdef.needs_inittable) then
  671. include(current_procinfo.flags,pi_do_call);
  672. if (is_shortstring(left.resultdef)) then
  673. begin
  674. if right.resultdef.typ=stringdef then
  675. begin
  676. if (right.nodetype<>stringconstn) or
  677. (tstringconstnode(right).len<>0) then
  678. begin
  679. hp:=ccallparanode.create
  680. (right,
  681. ccallparanode.create(cinlinenode.create
  682. (in_high_x,false,left.getcopy),nil));
  683. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp,left);
  684. firstpass(result);
  685. left:=nil;
  686. right:=nil;
  687. exit;
  688. end;
  689. end;
  690. end;
  691. registersint:=left.registersint+right.registersint;
  692. registersfpu:=max(left.registersfpu,right.registersfpu);
  693. {$ifdef SUPPORT_MMX}
  694. registersmmx:=max(left.registersmmx,right.registersmmx);
  695. {$endif SUPPORT_MMX}
  696. end;
  697. function tassignmentnode.docompare(p: tnode): boolean;
  698. begin
  699. docompare :=
  700. inherited docompare(p) and
  701. (assigntype = tassignmentnode(p).assigntype);
  702. end;
  703. {$ifdef state_tracking}
  704. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  705. var se:Tstate_entry;
  706. begin
  707. track_state_pass:=false;
  708. if exec_known then
  709. begin
  710. track_state_pass:=right.track_state_pass(exec_known);
  711. {Force a new resultdef pass.}
  712. right.resultdef:=nil;
  713. do_typecheckpass(right);
  714. typecheckpass(right);
  715. aktstate.store_fact(left.getcopy,right.getcopy);
  716. end
  717. else
  718. aktstate.delete_fact(left);
  719. end;
  720. {$endif}
  721. {*****************************************************************************
  722. TARRAYCONSTRUCTORRANGENODE
  723. *****************************************************************************}
  724. constructor tarrayconstructorrangenode.create(l,r : tnode);
  725. begin
  726. inherited create(arrayconstructorrangen,l,r);
  727. end;
  728. function tarrayconstructorrangenode.pass_typecheck:tnode;
  729. begin
  730. result:=nil;
  731. typecheckpass(left);
  732. typecheckpass(right);
  733. set_varstate(left,vs_read,[vsf_must_be_valid]);
  734. set_varstate(right,vs_read,[vsf_must_be_valid]);
  735. if codegenerror then
  736. exit;
  737. resultdef:=left.resultdef;
  738. end;
  739. function tarrayconstructorrangenode.pass_1 : tnode;
  740. begin
  741. firstpass(left);
  742. firstpass(right);
  743. expectloc:=LOC_CREFERENCE;
  744. calcregisters(self,0,0,0);
  745. result:=nil;
  746. end;
  747. {****************************************************************************
  748. TARRAYCONSTRUCTORNODE
  749. *****************************************************************************}
  750. constructor tarrayconstructornode.create(l,r : tnode);
  751. begin
  752. inherited create(arrayconstructorn,l,r);
  753. end;
  754. function tarrayconstructornode.dogetcopy : tnode;
  755. var
  756. n : tarrayconstructornode;
  757. begin
  758. n:=tarrayconstructornode(inherited dogetcopy);
  759. result:=n;
  760. end;
  761. function tarrayconstructornode.pass_typecheck:tnode;
  762. var
  763. hdef : tdef;
  764. hp : tarrayconstructornode;
  765. len : longint;
  766. varia : boolean;
  767. eq : tequaltype;
  768. hnodetype : tnodetype;
  769. begin
  770. result:=nil;
  771. { are we allowing array constructor? Then convert it to a set.
  772. Do this only if we didn't convert the arrayconstructor yet. This
  773. is needed for the cases where the resultdef is forced for a second
  774. run }
  775. if (not allow_array_constructor) then
  776. begin
  777. hp:=tarrayconstructornode(getcopy);
  778. arrayconstructor_to_set(tnode(hp));
  779. result:=hp;
  780. exit;
  781. end;
  782. { only pass left tree, right tree contains next construct if any }
  783. hdef:=nil;
  784. hnodetype:=errorn;
  785. len:=0;
  786. varia:=false;
  787. if assigned(left) then
  788. begin
  789. hp:=self;
  790. while assigned(hp) do
  791. begin
  792. typecheckpass(hp.left);
  793. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  794. if (hdef=nil) then
  795. begin
  796. hdef:=hp.left.resultdef;
  797. hnodetype:=hp.left.nodetype;
  798. end
  799. else
  800. begin
  801. { If we got a niln we don't know the type yet and need to take the
  802. type of the next array element.
  803. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  804. if hnodetype=niln then
  805. begin
  806. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  807. if eq>te_incompatible then
  808. begin
  809. hdef:=hp.left.resultdef;
  810. hnodetype:=hp.left.nodetype;
  811. end;
  812. end
  813. else
  814. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  815. if (not varia) and (eq<te_equal) then
  816. begin
  817. { If both are integers we need to take the type that can hold both
  818. defs }
  819. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  820. begin
  821. if is_in_limit(hdef,hp.left.resultdef) then
  822. hdef:=hp.left.resultdef;
  823. end
  824. else
  825. if (nf_novariaallowed in flags) then
  826. varia:=true;
  827. end;
  828. end;
  829. inc(len);
  830. hp:=tarrayconstructornode(hp.right);
  831. end;
  832. end;
  833. { Set the type of empty or varia arrays to void. Also
  834. do this if the type is array of const/open array
  835. because those can't be used with setelementdef }
  836. if not assigned(hdef) or
  837. varia or
  838. is_array_of_const(hdef) or
  839. is_open_array(hdef) then
  840. hdef:=voidtype;
  841. resultdef:=tarraydef.create(0,len-1,s32inttype);
  842. tarraydef(resultdef).elementdef:=hdef;
  843. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  844. if varia then
  845. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  846. end;
  847. procedure tarrayconstructornode.force_type(def:tdef);
  848. var
  849. hp : tarrayconstructornode;
  850. begin
  851. tarraydef(resultdef).elementdef:=def;
  852. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  853. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  854. if assigned(left) then
  855. begin
  856. hp:=self;
  857. while assigned(hp) do
  858. begin
  859. inserttypeconv(hp.left,def);
  860. hp:=tarrayconstructornode(hp.right);
  861. end;
  862. end;
  863. end;
  864. procedure tarrayconstructornode.insert_typeconvs;
  865. var
  866. hp : tarrayconstructornode;
  867. dovariant : boolean;
  868. begin
  869. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  870. { only pass left tree, right tree contains next construct if any }
  871. if assigned(left) then
  872. begin
  873. hp:=self;
  874. while assigned(hp) do
  875. begin
  876. typecheckpass(hp.left);
  877. { Insert typeconvs for array of const }
  878. if dovariant then
  879. { at this time C varargs are no longer an arrayconstructornode }
  880. insert_varargstypeconv(hp.left,false);
  881. hp:=tarrayconstructornode(hp.right);
  882. end;
  883. end;
  884. end;
  885. function tarrayconstructornode.pass_1 : tnode;
  886. var
  887. hp : tarrayconstructornode;
  888. do_variant:boolean;
  889. begin
  890. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  891. result:=nil;
  892. { Insert required type convs, this must be
  893. done in pass 1, because the call must be
  894. typecheckpassed already }
  895. if assigned(left) then
  896. begin
  897. insert_typeconvs;
  898. { call firstpass for all nodes }
  899. hp:=self;
  900. while assigned(hp) do
  901. begin
  902. if hp.left<>nil then
  903. begin
  904. {This check is pessimistic; a call will happen depending
  905. on the location in which the elements will be found in
  906. pass 2.}
  907. if not do_variant then
  908. include(current_procinfo.flags,pi_do_call);
  909. firstpass(hp.left);
  910. end;
  911. hp:=tarrayconstructornode(hp.right);
  912. end;
  913. end;
  914. expectloc:=LOC_CREFERENCE;
  915. calcregisters(self,0,0,0);
  916. end;
  917. function tarrayconstructornode.docompare(p: tnode): boolean;
  918. begin
  919. docompare:=inherited docompare(p);
  920. end;
  921. {*****************************************************************************
  922. TTYPENODE
  923. *****************************************************************************}
  924. constructor ttypenode.create(def:tdef);
  925. begin
  926. inherited create(typen);
  927. typedef:=def;
  928. allowed:=false;
  929. end;
  930. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  931. begin
  932. inherited ppuload(t,ppufile);
  933. ppufile.getderef(typedefderef);
  934. allowed:=boolean(ppufile.getbyte);
  935. end;
  936. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  937. begin
  938. inherited ppuwrite(ppufile);
  939. ppufile.putderef(typedefderef);
  940. ppufile.putbyte(byte(allowed));
  941. end;
  942. procedure ttypenode.buildderefimpl;
  943. begin
  944. inherited buildderefimpl;
  945. typedefderef.build(typedef);
  946. end;
  947. procedure ttypenode.derefimpl;
  948. begin
  949. inherited derefimpl;
  950. typedef:=tdef(typedefderef.resolve);
  951. end;
  952. function ttypenode.pass_typecheck:tnode;
  953. begin
  954. result:=nil;
  955. resultdef:=typedef;
  956. { check if it's valid }
  957. if typedef.typ = errordef then
  958. CGMessage(parser_e_illegal_expression);
  959. end;
  960. function ttypenode.pass_1 : tnode;
  961. begin
  962. result:=nil;
  963. expectloc:=LOC_VOID;
  964. { a typenode can't generate code, so we give here
  965. an error. Else it'll be an abstract error in pass_generate_code.
  966. Only when the allowed flag is set we don't generate
  967. an error }
  968. if not allowed then
  969. Message(parser_e_no_type_not_allowed_here);
  970. end;
  971. function ttypenode.dogetcopy : tnode;
  972. var
  973. n : ttypenode;
  974. begin
  975. n:=ttypenode(inherited dogetcopy);
  976. n.allowed:=allowed;
  977. n.typedef:=typedef;
  978. result:=n;
  979. end;
  980. function ttypenode.docompare(p: tnode): boolean;
  981. begin
  982. docompare :=
  983. inherited docompare(p);
  984. end;
  985. {*****************************************************************************
  986. TRTTINODE
  987. *****************************************************************************}
  988. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  989. begin
  990. inherited create(rttin);
  991. rttidef:=def;
  992. rttitype:=rt;
  993. rttidatatype:=dt;
  994. end;
  995. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  996. begin
  997. inherited ppuload(t,ppufile);
  998. ppufile.getderef(rttidefderef);
  999. rttitype:=trttitype(ppufile.getbyte);
  1000. end;
  1001. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1002. begin
  1003. inherited ppuwrite(ppufile);
  1004. ppufile.putderef(rttidefderef);
  1005. ppufile.putbyte(byte(rttitype));
  1006. end;
  1007. procedure trttinode.buildderefimpl;
  1008. begin
  1009. inherited buildderefimpl;
  1010. rttidefderef.build(rttidef);
  1011. end;
  1012. procedure trttinode.derefimpl;
  1013. begin
  1014. inherited derefimpl;
  1015. rttidef:=tstoreddef(rttidefderef.resolve);
  1016. end;
  1017. function trttinode.dogetcopy : tnode;
  1018. var
  1019. n : trttinode;
  1020. begin
  1021. n:=trttinode(inherited dogetcopy);
  1022. n.rttidef:=rttidef;
  1023. n.rttitype:=rttitype;
  1024. result:=n;
  1025. end;
  1026. function trttinode.pass_typecheck:tnode;
  1027. begin
  1028. { rtti information will be returned as a void pointer }
  1029. result:=nil;
  1030. resultdef:=voidpointertype;
  1031. end;
  1032. function trttinode.pass_1 : tnode;
  1033. begin
  1034. result:=nil;
  1035. expectloc:=LOC_CREFERENCE;
  1036. end;
  1037. function trttinode.docompare(p: tnode): boolean;
  1038. begin
  1039. docompare :=
  1040. inherited docompare(p) and
  1041. (rttidef = trttinode(p).rttidef) and
  1042. (rttitype = trttinode(p).rttitype);
  1043. end;
  1044. begin
  1045. cloadnode:=tloadnode;
  1046. cassignmentnode:=tassignmentnode;
  1047. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1048. carrayconstructornode:=tarrayconstructornode;
  1049. ctypenode:=ttypenode;
  1050. crttinode:=trttinode;
  1051. end.