nld.pas 42 KB

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