nld.pas 44 KB

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