nld.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. if (cs_create_pic in current_settings.moduleswitches) and
  308. not(symtableentry.typ in [paravarsym,localvarsym]) then
  309. include(current_procinfo.flags,pi_needs_got);
  310. case symtableentry.typ of
  311. absolutevarsym :
  312. ;
  313. constsym:
  314. begin
  315. if tconstsym(symtableentry).consttyp=constresourcestring then
  316. expectloc:=LOC_CREFERENCE;
  317. end;
  318. staticvarsym,
  319. localvarsym,
  320. paravarsym :
  321. begin
  322. if assigned(left) then
  323. firstpass(left);
  324. if not is_addr_param_load and
  325. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  326. expectloc:=tvarregable2tcgloc[tabstractvarsym(symtableentry).varregable]
  327. else
  328. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  329. expectloc:=LOC_CREFERENCE;
  330. if (target_info.system=system_powerpc_darwin) and
  331. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  332. include(current_procinfo.flags,pi_needs_got);
  333. { call to get address of threadvar }
  334. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  335. include(current_procinfo.flags,pi_do_call);
  336. if nf_write in flags then
  337. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  338. else
  339. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  340. { count variable references }
  341. if cg.t_times>1 then
  342. tabstractvarsym(symtableentry).IncRefCountBy(cg.t_times-1);
  343. end;
  344. procsym :
  345. begin
  346. { method pointer ? }
  347. if assigned(left) then
  348. begin
  349. expectloc:=LOC_CREFERENCE;
  350. firstpass(left);
  351. end;
  352. end;
  353. labelsym :
  354. ;
  355. else
  356. internalerror(200104143);
  357. end;
  358. end;
  359. function tloadnode.docompare(p: tnode): boolean;
  360. begin
  361. docompare :=
  362. inherited docompare(p) and
  363. (symtableentry = tloadnode(p).symtableentry) and
  364. (procdef = tloadnode(p).procdef) and
  365. (symtable = tloadnode(p).symtable);
  366. end;
  367. procedure tloadnode.printnodedata(var t:text);
  368. begin
  369. inherited printnodedata(t);
  370. write(t,printnodeindention,'symbol = ',symtableentry.name);
  371. if symtableentry.typ=procsym then
  372. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  373. writeln(t,'');
  374. end;
  375. procedure tloadnode.setprocdef(p : tprocdef);
  376. begin
  377. procdef:=p;
  378. resultdef:=p;
  379. if po_local in p.procoptions then
  380. CGMessage(type_e_cant_take_address_of_local_subroutine);
  381. end;
  382. {*****************************************************************************
  383. TASSIGNMENTNODE
  384. *****************************************************************************}
  385. constructor tassignmentnode.create(l,r : tnode);
  386. begin
  387. inherited create(assignn,l,r);
  388. l.mark_write;
  389. assigntype:=at_normal;
  390. end;
  391. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  392. begin
  393. inherited ppuload(t,ppufile);
  394. assigntype:=tassigntype(ppufile.getbyte);
  395. end;
  396. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  397. begin
  398. inherited ppuwrite(ppufile);
  399. ppufile.putbyte(byte(assigntype));
  400. end;
  401. function tassignmentnode.dogetcopy : tnode;
  402. var
  403. n : tassignmentnode;
  404. begin
  405. n:=tassignmentnode(inherited dogetcopy);
  406. n.assigntype:=assigntype;
  407. result:=n;
  408. end;
  409. function tassignmentnode.pass_typecheck:tnode;
  410. var
  411. hp : tnode;
  412. useshelper : boolean;
  413. begin
  414. result:=nil;
  415. resultdef:=voidtype;
  416. { must be made unique }
  417. set_unique(left);
  418. typecheckpass(left);
  419. typecheckpass(right);
  420. set_varstate(right,vs_read,[vsf_must_be_valid]);
  421. set_varstate(left,vs_written,[]);
  422. if codegenerror then
  423. exit;
  424. { tp procvar support, when we don't expect a procvar
  425. then we need to call the procvar }
  426. if (left.resultdef.typ<>procvardef) then
  427. maybe_call_procvar(right,true);
  428. { assignments to formaldefs and open arrays aren't allowed }
  429. if (left.resultdef.typ=formaldef) or
  430. is_open_array(left.resultdef) then
  431. CGMessage(type_e_assignment_not_allowed);
  432. { test if node can be assigned, properties are allowed }
  433. valid_for_assignment(left,true);
  434. { assigning nil to a dynamic array clears the array }
  435. if is_dynamic_array(left.resultdef) and
  436. (right.nodetype=niln) then
  437. begin
  438. { remove property flag to avoid errors, see comments for }
  439. { tf_winlikewidestring assignments below }
  440. exclude(left.flags,nf_isproperty);
  441. hp:=ccallparanode.create(caddrnode.create_internal
  442. (crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  443. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  444. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  445. left:=nil;
  446. exit;
  447. end;
  448. { shortstring helpers can do the conversion directly,
  449. so treat them separatly }
  450. if (is_shortstring(left.resultdef)) then
  451. begin
  452. { insert typeconv, except for chars that are handled in
  453. secondpass and except for ansi/wide string that can
  454. be converted immediatly }
  455. if not(is_char(right.resultdef) or
  456. (right.resultdef.typ=stringdef)) then
  457. inserttypeconv(right,left.resultdef);
  458. if right.resultdef.typ=stringdef then
  459. begin
  460. useshelper:=true;
  461. { convert constant strings to shortstrings. But
  462. skip empty constant strings, that will be handled
  463. in secondpass }
  464. if (right.nodetype=stringconstn) then
  465. begin
  466. { verify if range fits within shortstring }
  467. { just emit a warning, delphi gives an }
  468. { error, only if the type definition of }
  469. { of the string is less < 255 characters }
  470. if not is_open_string(left.resultdef) and
  471. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  472. cgmessage(type_w_string_too_long);
  473. inserttypeconv(right,left.resultdef);
  474. if (right.nodetype=stringconstn) and
  475. (tstringconstnode(right).len=0) then
  476. useshelper:=false;
  477. end;
  478. { rest is done in pass 1 (JM) }
  479. if useshelper then
  480. exit;
  481. end
  482. end
  483. else
  484. begin
  485. { check if the assignment may cause a range check error }
  486. check_ranges(fileinfo,right,left.resultdef);
  487. inserttypeconv(right,left.resultdef);
  488. end;
  489. { call helpers for interface }
  490. if is_interfacecom(left.resultdef) then
  491. begin
  492. { Normal interface assignments are handled by the generic refcount incr/decr }
  493. if not right.resultdef.is_related(left.resultdef) then
  494. begin
  495. { remove property flag to avoid errors, see comments for }
  496. { tf_winlikewidestring assignments below }
  497. exclude(left.flags,nf_isproperty);
  498. hp:=
  499. ccallparanode.create(
  500. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  501. ccallparanode.create(
  502. ctypeconvnode.create_internal(right,voidpointertype),
  503. ccallparanode.create(
  504. ctypeconvnode.create_internal(left,voidpointertype),
  505. nil)));
  506. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  507. left:=nil;
  508. right:=nil;
  509. exit;
  510. end;
  511. end
  512. { call helpers for variant, they can contain non ref. counted types like
  513. vararrays which must be really copied }
  514. else if left.resultdef.typ=variantdef then
  515. begin
  516. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  517. caddrnode.create_internal(right),voidpointertype),
  518. ccallparanode.create(ctypeconvnode.create_internal(
  519. caddrnode.create_internal(left),voidpointertype),
  520. nil));
  521. result:=ccallnode.createintern('fpc_variant_copy',hp);
  522. left:=nil;
  523. right:=nil;
  524. exit;
  525. end
  526. { call helpers for composite types containing automated types }
  527. else if (left.resultdef.needs_inittable) and
  528. (left.resultdef.typ in [arraydef,objectdef,recorddef]) then
  529. begin
  530. hp:=ccallparanode.create(caddrnode.create_internal(
  531. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  532. ccallparanode.create(ctypeconvnode.create_internal(
  533. caddrnode.create_internal(left),voidpointertype),
  534. ccallparanode.create(ctypeconvnode.create_internal(
  535. caddrnode.create_internal(right),voidpointertype),
  536. nil)));
  537. result:=ccallnode.createintern('fpc_copy',hp);
  538. left:=nil;
  539. right:=nil;
  540. exit;
  541. end
  542. { call helpers for windows widestrings, they aren't ref. counted }
  543. else if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  544. begin
  545. { The first argument of fpc_widestr_assign is a var parameter. Properties cannot }
  546. { be passed to var or out parameters, because in that case setters/getters are not }
  547. { used. Further, if we would allow it in case there are no getters or setters, you }
  548. { would need source changes in case these are introduced later on, thus defeating }
  549. { part of the transparency advantages of properties. In this particular case, }
  550. { however: }
  551. { a) if there is a setter, this code will not be used since then the assignment }
  552. { will be converted to a procedure call }
  553. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  554. { the refcount of the field to which we are writing }
  555. { c) source code changes are not required if a setter is added/removed, because }
  556. { this transformation is handled at compile time }
  557. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  558. { is a property which refers to a field without a setter call, we will not get }
  559. { an error about trying to pass a property as a var parameter }
  560. exclude(left.flags,nf_isproperty);
  561. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  562. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  563. nil));
  564. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  565. left:=nil;
  566. right:=nil;
  567. exit;
  568. end;
  569. { check if local proc/func is assigned to procvar }
  570. if right.resultdef.typ=procvardef then
  571. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  572. end;
  573. function tassignmentnode.pass_1 : tnode;
  574. var
  575. hp: tnode;
  576. oldassignmentnode : tassignmentnode;
  577. begin
  578. result:=nil;
  579. expectloc:=LOC_VOID;
  580. firstpass(left);
  581. { Optimize the reuse of the destination of the assingment in left.
  582. Allow the use of the left inside the tree generated on the right.
  583. This is especially usefull for string routines where the destination
  584. is pushed as a parameter. Using the final destination of left directly
  585. save a temp allocation and copy of data (PFV) }
  586. oldassignmentnode:=aktassignmentnode;
  587. aktassignmentnode:=self;
  588. firstpass(right);
  589. aktassignmentnode:=oldassignmentnode;
  590. if nf_assign_done_in_right in flags then
  591. begin
  592. result:=right;
  593. right:=nil;
  594. exit;
  595. end;
  596. if codegenerror then
  597. exit;
  598. { assignment to refcounted variable -> inc/decref }
  599. if (not is_class(left.resultdef) and
  600. left.resultdef.needs_inittable) then
  601. include(current_procinfo.flags,pi_do_call);
  602. if (is_shortstring(left.resultdef)) then
  603. begin
  604. if right.resultdef.typ=stringdef then
  605. begin
  606. if (right.nodetype<>stringconstn) or
  607. (tstringconstnode(right).len<>0) then
  608. begin
  609. hp:=ccallparanode.create
  610. (right,
  611. ccallparanode.create(cinlinenode.create
  612. (in_high_x,false,left.getcopy),nil));
  613. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp,left);
  614. firstpass(result);
  615. left:=nil;
  616. right:=nil;
  617. exit;
  618. end;
  619. end;
  620. end;
  621. end;
  622. function tassignmentnode.docompare(p: tnode): boolean;
  623. begin
  624. docompare :=
  625. inherited docompare(p) and
  626. (assigntype = tassignmentnode(p).assigntype);
  627. end;
  628. {$ifdef state_tracking}
  629. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  630. var se:Tstate_entry;
  631. begin
  632. track_state_pass:=false;
  633. if exec_known then
  634. begin
  635. track_state_pass:=right.track_state_pass(exec_known);
  636. {Force a new resultdef pass.}
  637. right.resultdef:=nil;
  638. do_typecheckpass(right);
  639. typecheckpass(right);
  640. aktstate.store_fact(left.getcopy,right.getcopy);
  641. end
  642. else
  643. aktstate.delete_fact(left);
  644. end;
  645. {$endif}
  646. {*****************************************************************************
  647. TARRAYCONSTRUCTORRANGENODE
  648. *****************************************************************************}
  649. constructor tarrayconstructorrangenode.create(l,r : tnode);
  650. begin
  651. inherited create(arrayconstructorrangen,l,r);
  652. end;
  653. function tarrayconstructorrangenode.pass_typecheck:tnode;
  654. begin
  655. result:=nil;
  656. typecheckpass(left);
  657. typecheckpass(right);
  658. set_varstate(left,vs_read,[vsf_must_be_valid]);
  659. set_varstate(right,vs_read,[vsf_must_be_valid]);
  660. if codegenerror then
  661. exit;
  662. resultdef:=left.resultdef;
  663. end;
  664. function tarrayconstructorrangenode.pass_1 : tnode;
  665. begin
  666. firstpass(left);
  667. firstpass(right);
  668. expectloc:=LOC_CREFERENCE;
  669. result:=nil;
  670. end;
  671. {****************************************************************************
  672. TARRAYCONSTRUCTORNODE
  673. *****************************************************************************}
  674. constructor tarrayconstructornode.create(l,r : tnode);
  675. begin
  676. inherited create(arrayconstructorn,l,r);
  677. end;
  678. function tarrayconstructornode.dogetcopy : tnode;
  679. var
  680. n : tarrayconstructornode;
  681. begin
  682. n:=tarrayconstructornode(inherited dogetcopy);
  683. result:=n;
  684. end;
  685. function tarrayconstructornode.pass_typecheck:tnode;
  686. var
  687. hdef : tdef;
  688. hp : tarrayconstructornode;
  689. len : longint;
  690. varia : boolean;
  691. eq : tequaltype;
  692. hnodetype : tnodetype;
  693. begin
  694. result:=nil;
  695. { are we allowing array constructor? Then convert it to a set.
  696. Do this only if we didn't convert the arrayconstructor yet. This
  697. is needed for the cases where the resultdef is forced for a second
  698. run }
  699. if (not allow_array_constructor) then
  700. begin
  701. hp:=tarrayconstructornode(getcopy);
  702. arrayconstructor_to_set(tnode(hp));
  703. result:=hp;
  704. exit;
  705. end;
  706. { only pass left tree, right tree contains next construct if any }
  707. hdef:=nil;
  708. hnodetype:=errorn;
  709. len:=0;
  710. varia:=false;
  711. if assigned(left) then
  712. begin
  713. hp:=self;
  714. while assigned(hp) do
  715. begin
  716. typecheckpass(hp.left);
  717. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  718. if (hdef=nil) then
  719. begin
  720. hdef:=hp.left.resultdef;
  721. hnodetype:=hp.left.nodetype;
  722. end
  723. else
  724. begin
  725. { If we got a niln we don't know the type yet and need to take the
  726. type of the next array element.
  727. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  728. if hnodetype=niln then
  729. begin
  730. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  731. if eq>te_incompatible then
  732. begin
  733. hdef:=hp.left.resultdef;
  734. hnodetype:=hp.left.nodetype;
  735. end;
  736. end
  737. else
  738. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  739. if (not varia) and (eq<te_equal) then
  740. begin
  741. { If both are integers we need to take the type that can hold both
  742. defs }
  743. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  744. begin
  745. if is_in_limit(hdef,hp.left.resultdef) then
  746. hdef:=hp.left.resultdef;
  747. end
  748. else
  749. if (nf_novariaallowed in flags) then
  750. varia:=true;
  751. end;
  752. end;
  753. inc(len);
  754. hp:=tarrayconstructornode(hp.right);
  755. end;
  756. end;
  757. { Set the type of empty or varia arrays to void. Also
  758. do this if the type is array of const/open array
  759. because those can't be used with setelementdef }
  760. if not assigned(hdef) or
  761. varia or
  762. is_array_of_const(hdef) or
  763. is_open_array(hdef) then
  764. hdef:=voidtype;
  765. resultdef:=tarraydef.create(0,len-1,s32inttype);
  766. tarraydef(resultdef).elementdef:=hdef;
  767. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  768. if varia then
  769. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  770. end;
  771. procedure tarrayconstructornode.force_type(def:tdef);
  772. var
  773. hp : tarrayconstructornode;
  774. begin
  775. tarraydef(resultdef).elementdef:=def;
  776. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  777. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  778. if assigned(left) then
  779. begin
  780. hp:=self;
  781. while assigned(hp) do
  782. begin
  783. inserttypeconv(hp.left,def);
  784. hp:=tarrayconstructornode(hp.right);
  785. end;
  786. end;
  787. end;
  788. procedure tarrayconstructornode.insert_typeconvs;
  789. var
  790. hp : tarrayconstructornode;
  791. dovariant : boolean;
  792. begin
  793. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  794. { only pass left tree, right tree contains next construct if any }
  795. if assigned(left) then
  796. begin
  797. hp:=self;
  798. while assigned(hp) do
  799. begin
  800. typecheckpass(hp.left);
  801. { Insert typeconvs for array of const }
  802. if dovariant then
  803. { at this time C varargs are no longer an arrayconstructornode }
  804. insert_varargstypeconv(hp.left,false);
  805. hp:=tarrayconstructornode(hp.right);
  806. end;
  807. end;
  808. end;
  809. function tarrayconstructornode.pass_1 : tnode;
  810. var
  811. hp : tarrayconstructornode;
  812. do_variant:boolean;
  813. begin
  814. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  815. result:=nil;
  816. { Insert required type convs, this must be
  817. done in pass 1, because the call must be
  818. typecheckpassed already }
  819. if assigned(left) then
  820. begin
  821. insert_typeconvs;
  822. { call firstpass for all nodes }
  823. hp:=self;
  824. while assigned(hp) do
  825. begin
  826. if hp.left<>nil then
  827. begin
  828. {This check is pessimistic; a call will happen depending
  829. on the location in which the elements will be found in
  830. pass 2.}
  831. if not do_variant then
  832. include(current_procinfo.flags,pi_do_call);
  833. firstpass(hp.left);
  834. end;
  835. hp:=tarrayconstructornode(hp.right);
  836. end;
  837. end;
  838. expectloc:=LOC_CREFERENCE;
  839. end;
  840. function tarrayconstructornode.docompare(p: tnode): boolean;
  841. begin
  842. docompare:=inherited docompare(p);
  843. end;
  844. {*****************************************************************************
  845. TTYPENODE
  846. *****************************************************************************}
  847. constructor ttypenode.create(def:tdef);
  848. begin
  849. inherited create(typen);
  850. typedef:=def;
  851. allowed:=false;
  852. end;
  853. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  854. begin
  855. inherited ppuload(t,ppufile);
  856. ppufile.getderef(typedefderef);
  857. allowed:=boolean(ppufile.getbyte);
  858. end;
  859. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  860. begin
  861. inherited ppuwrite(ppufile);
  862. ppufile.putderef(typedefderef);
  863. ppufile.putbyte(byte(allowed));
  864. end;
  865. procedure ttypenode.buildderefimpl;
  866. begin
  867. inherited buildderefimpl;
  868. typedefderef.build(typedef);
  869. end;
  870. procedure ttypenode.derefimpl;
  871. begin
  872. inherited derefimpl;
  873. typedef:=tdef(typedefderef.resolve);
  874. end;
  875. function ttypenode.pass_typecheck:tnode;
  876. begin
  877. result:=nil;
  878. resultdef:=typedef;
  879. { check if it's valid }
  880. if typedef.typ = errordef then
  881. CGMessage(parser_e_illegal_expression);
  882. end;
  883. function ttypenode.pass_1 : tnode;
  884. begin
  885. result:=nil;
  886. expectloc:=LOC_VOID;
  887. { a typenode can't generate code, so we give here
  888. an error. Else it'll be an abstract error in pass_generate_code.
  889. Only when the allowed flag is set we don't generate
  890. an error }
  891. if not allowed then
  892. Message(parser_e_no_type_not_allowed_here);
  893. end;
  894. function ttypenode.dogetcopy : tnode;
  895. var
  896. n : ttypenode;
  897. begin
  898. n:=ttypenode(inherited dogetcopy);
  899. n.allowed:=allowed;
  900. n.typedef:=typedef;
  901. result:=n;
  902. end;
  903. function ttypenode.docompare(p: tnode): boolean;
  904. begin
  905. docompare :=
  906. inherited docompare(p);
  907. end;
  908. {*****************************************************************************
  909. TRTTINODE
  910. *****************************************************************************}
  911. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  912. begin
  913. inherited create(rttin);
  914. rttidef:=def;
  915. rttitype:=rt;
  916. rttidatatype:=dt;
  917. end;
  918. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  919. begin
  920. inherited ppuload(t,ppufile);
  921. ppufile.getderef(rttidefderef);
  922. rttitype:=trttitype(ppufile.getbyte);
  923. end;
  924. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  925. begin
  926. inherited ppuwrite(ppufile);
  927. ppufile.putderef(rttidefderef);
  928. ppufile.putbyte(byte(rttitype));
  929. end;
  930. procedure trttinode.buildderefimpl;
  931. begin
  932. inherited buildderefimpl;
  933. rttidefderef.build(rttidef);
  934. end;
  935. procedure trttinode.derefimpl;
  936. begin
  937. inherited derefimpl;
  938. rttidef:=tstoreddef(rttidefderef.resolve);
  939. end;
  940. function trttinode.dogetcopy : tnode;
  941. var
  942. n : trttinode;
  943. begin
  944. n:=trttinode(inherited dogetcopy);
  945. n.rttidef:=rttidef;
  946. n.rttitype:=rttitype;
  947. result:=n;
  948. end;
  949. function trttinode.pass_typecheck:tnode;
  950. begin
  951. { rtti information will be returned as a void pointer }
  952. result:=nil;
  953. resultdef:=voidpointertype;
  954. end;
  955. function trttinode.pass_1 : tnode;
  956. begin
  957. result:=nil;
  958. expectloc:=LOC_CREFERENCE;
  959. end;
  960. function trttinode.docompare(p: tnode): boolean;
  961. begin
  962. docompare :=
  963. inherited docompare(p) and
  964. (rttidef = trttinode(p).rttidef) and
  965. (rttitype = trttinode(p).rttitype);
  966. end;
  967. begin
  968. cloadnode:=tloadnode;
  969. cassignmentnode:=tassignmentnode;
  970. carrayconstructorrangenode:=tarrayconstructorrangenode;
  971. carrayconstructornode:=tarrayconstructornode;
  972. ctypenode:=ttypenode;
  973. crttinode:=trttinode;
  974. end.