nld.pas 43 KB

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