nld.pas 45 KB

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