nld.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. ;
  379. else
  380. internalerror(200104143);
  381. end;
  382. end;
  383. function tloadnode.docompare(p: tnode): boolean;
  384. begin
  385. docompare :=
  386. inherited docompare(p) and
  387. (symtableentry = tloadnode(p).symtableentry) and
  388. (fprocdef = tloadnode(p).fprocdef) and
  389. (symtable = tloadnode(p).symtable);
  390. end;
  391. procedure tloadnode.printnodedata(var t:text);
  392. begin
  393. inherited printnodedata(t);
  394. write(t,printnodeindention,'symbol = ',symtableentry.name);
  395. if symtableentry.typ=procsym then
  396. write(t,printnodeindention,'procdef = ',fprocdef.mangledname);
  397. writeln(t,'');
  398. end;
  399. procedure tloadnode.setprocdef(p : tprocdef);
  400. begin
  401. fprocdef:=p;
  402. resultdef:=p;
  403. { nested procedure? }
  404. if assigned(p) and
  405. is_nested_pd(p) then
  406. begin
  407. if not(m_nested_procvars in current_settings.modeswitches) then
  408. CGMessage(type_e_cant_take_address_of_local_subroutine)
  409. else
  410. begin
  411. { parent frame pointer pointer as "self" }
  412. left.free;
  413. left:=cloadparentfpnode.create(tprocdef(p.owner.defowner));
  414. end;
  415. end
  416. { we should never go from nested to non-nested }
  417. else if assigned(left) and
  418. (left.nodetype=loadparentfpn) then
  419. internalerror(2010072201);
  420. end;
  421. {*****************************************************************************
  422. TASSIGNMENTNODE
  423. *****************************************************************************}
  424. constructor tassignmentnode.create(l,r : tnode);
  425. begin
  426. inherited create(assignn,l,r);
  427. l.mark_write;
  428. assigntype:=at_normal;
  429. if r.nodetype = typeconvn then
  430. ttypeconvnode(r).warn_pointer_to_signed:=false;
  431. end;
  432. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  433. begin
  434. inherited ppuload(t,ppufile);
  435. assigntype:=tassigntype(ppufile.getbyte);
  436. end;
  437. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  438. begin
  439. inherited ppuwrite(ppufile);
  440. ppufile.putbyte(byte(assigntype));
  441. end;
  442. function tassignmentnode.dogetcopy : tnode;
  443. var
  444. n : tassignmentnode;
  445. begin
  446. n:=tassignmentnode(inherited dogetcopy);
  447. n.assigntype:=assigntype;
  448. result:=n;
  449. end;
  450. function tassignmentnode.simplify(forinline : boolean) : tnode;
  451. begin
  452. result:=nil;
  453. { assignment nodes can perform several floating point }
  454. { type conversions directly, so no typeconversions }
  455. { are inserted in those cases. When inlining, a }
  456. { variable may be replaced by a constant which can be }
  457. { converted at compile time, so check for this case }
  458. if is_real(left.resultdef) and
  459. is_real(right.resultdef) and
  460. is_constrealnode(right) and
  461. not equal_defs(right.resultdef,left.resultdef) then
  462. inserttypeconv(right,left.resultdef);
  463. end;
  464. function tassignmentnode.pass_typecheck:tnode;
  465. var
  466. hp : tnode;
  467. useshelper : boolean;
  468. oldassignmentnode : tassignmentnode;
  469. begin
  470. result:=nil;
  471. resultdef:=voidtype;
  472. { must be made unique }
  473. set_unique(left);
  474. typecheckpass(left);
  475. { PI. This is needed to return correct resultdef of add nodes for ansistrings
  476. rawbytestring return needs to be replaced by left.resultdef }
  477. oldassignmentnode:=aktassignmentnode;
  478. aktassignmentnode:=self;
  479. typecheckpass(right);
  480. aktassignmentnode:=oldassignmentnode;
  481. set_varstate(right,vs_read,[vsf_must_be_valid]);
  482. set_varstate(left,vs_written,[]);
  483. if codegenerror then
  484. exit;
  485. { tp procvar support, when we don't expect a procvar
  486. then we need to call the procvar }
  487. if (left.resultdef.typ<>procvardef) then
  488. maybe_call_procvar(right,true);
  489. { assignments to formaldefs and open arrays aren't allowed }
  490. if (left.resultdef.typ=formaldef) or
  491. is_open_array(left.resultdef) then
  492. CGMessage(type_e_assignment_not_allowed);
  493. { test if node can be assigned, properties are allowed }
  494. valid_for_assignment(left,true);
  495. { assigning nil to a dynamic array clears the array }
  496. if is_dynamic_array(left.resultdef) and
  497. (right.nodetype=niln) then
  498. begin
  499. { remove property flag to avoid errors, see comments for }
  500. { tf_winlikewidestring assignments below }
  501. exclude(left.flags,nf_isproperty);
  502. hp:=ccallparanode.create(caddrnode.create_internal
  503. (crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  504. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  505. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  506. left:=nil;
  507. exit;
  508. end;
  509. { shortstring helpers can do the conversion directly,
  510. so treat them separatly }
  511. if (is_shortstring(left.resultdef)) then
  512. begin
  513. { insert typeconv, except for chars that are handled in
  514. secondpass and except for ansi/wide string that can
  515. be converted immediatly }
  516. if not(is_char(right.resultdef) or
  517. (right.resultdef.typ=stringdef)) then
  518. inserttypeconv(right,left.resultdef);
  519. if right.resultdef.typ=stringdef then
  520. begin
  521. useshelper:=true;
  522. { convert constant strings to shortstrings. But
  523. skip empty constant strings, that will be handled
  524. in secondpass }
  525. if (right.nodetype=stringconstn) then
  526. begin
  527. { verify if range fits within shortstring }
  528. { just emit a warning, delphi gives an }
  529. { error, only if the type definition of }
  530. { of the string is less < 255 characters }
  531. if not is_open_string(left.resultdef) and
  532. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  533. cgmessage(type_w_string_too_long);
  534. inserttypeconv(right,left.resultdef);
  535. if (right.nodetype=stringconstn) and
  536. (tstringconstnode(right).len=0) then
  537. useshelper:=false;
  538. end
  539. else if (tstringdef(right.resultdef).stringtype in [st_unicodestring,st_widestring]) then
  540. Message2(type_w_implicit_string_cast_loss,right.resultdef.typename,left.resultdef.typename);
  541. { rest is done in pass 1 (JM) }
  542. if useshelper then
  543. exit;
  544. end
  545. end
  546. { floating point assignments can also perform the conversion directly }
  547. else if is_real(left.resultdef) and is_real(right.resultdef) and
  548. not is_constrealnode(right)
  549. {$ifdef cpufpemu}
  550. { the emulator can't do this obviously }
  551. and not(current_settings.fputype in [fpu_libgcc,fpu_soft])
  552. {$endif cpufpemu}
  553. {$ifdef x86}
  554. { the assignment node code can't convert a double in an }
  555. { sse register to an extended value in memory more }
  556. { efficiently than a type conversion node, so don't }
  557. { bother implementing support for that }
  558. and (use_vectorfpu(left.resultdef) or not(use_vectorfpu(right.resultdef)))
  559. {$endif}
  560. {$ifdef arm}
  561. { the assignment node code can't convert a single in
  562. an interger register to a double in an mmregister or
  563. vice versa }
  564. and (use_vectorfpu(left.resultdef) and
  565. use_vectorfpu(right.resultdef) and
  566. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype))
  567. {$endif}
  568. then
  569. begin
  570. check_ranges(fileinfo,right,left.resultdef);
  571. end
  572. else
  573. begin
  574. { check if the assignment may cause a range check error }
  575. check_ranges(fileinfo,right,left.resultdef);
  576. { beginners might be confused about an error message like
  577. Incompatible types: got "untyped" expected "LongInt"
  578. when trying to assign the result of a procedure, so give
  579. a better error message, see also #19122 }
  580. if (left.resultdef.typ<>procvardef) and
  581. (right.nodetype=calln) and is_void(right.resultdef) then
  582. CGMessage(type_e_procedures_return_no_value)
  583. else
  584. inserttypeconv(right,left.resultdef);
  585. end;
  586. { call helpers for interface }
  587. if is_interfacecom_or_dispinterface(left.resultdef) then
  588. begin
  589. { Normal interface assignments are handled by the generic refcount incr/decr }
  590. if not right.resultdef.is_related(left.resultdef) then
  591. begin
  592. { remove property flag to avoid errors, see comments for }
  593. { tf_winlikewidestring assignments below }
  594. exclude(left.flags,nf_isproperty);
  595. hp:=
  596. ccallparanode.create(
  597. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  598. ccallparanode.create(
  599. ctypeconvnode.create_internal(right,voidpointertype),
  600. ccallparanode.create(
  601. ctypeconvnode.create_internal(left,voidpointertype),
  602. nil)));
  603. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  604. left:=nil;
  605. right:=nil;
  606. exit;
  607. end;
  608. end;
  609. { check if local proc/func is assigned to procvar }
  610. if right.resultdef.typ=procvardef then
  611. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  612. end;
  613. function tassignmentnode.pass_1 : tnode;
  614. var
  615. hp: tnode;
  616. oldassignmentnode : tassignmentnode;
  617. hdef: tdef;
  618. begin
  619. result:=nil;
  620. expectloc:=LOC_VOID;
  621. firstpass(left);
  622. { Optimize the reuse of the destination of the assingment in left.
  623. Allow the use of the left inside the tree generated on the right.
  624. This is especially useful for string routines where the destination
  625. is pushed as a parameter. Using the final destination of left directly
  626. save a temp allocation and copy of data (PFV) }
  627. oldassignmentnode:=aktassignmentnode;
  628. aktassignmentnode:=self;
  629. firstpass(right);
  630. aktassignmentnode:=oldassignmentnode;
  631. if nf_assign_done_in_right in flags then
  632. begin
  633. result:=right;
  634. right:=nil;
  635. exit;
  636. end;
  637. if codegenerror then
  638. exit;
  639. { assignment to refcounted variable -> inc/decref }
  640. if is_managed_type(left.resultdef) then
  641. include(current_procinfo.flags,pi_do_call);
  642. if (is_shortstring(left.resultdef)) then
  643. begin
  644. if right.resultdef.typ=stringdef then
  645. begin
  646. if (right.nodetype<>stringconstn) or
  647. (tstringconstnode(right).len<>0) then
  648. begin
  649. { remove property flag to avoid errors, see comments for }
  650. { tf_winlikewidestring assignments below }
  651. exclude(left.flags, nf_isproperty);
  652. hp:=ccallparanode.create
  653. (right,
  654. ccallparanode.create(left,nil));
  655. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  656. firstpass(result);
  657. left:=nil;
  658. right:=nil;
  659. exit;
  660. end;
  661. end;
  662. end
  663. { call helpers for composite types containing automated types }
  664. else if is_managed_type(left.resultdef) and
  665. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  666. not is_interfacecom_or_dispinterface(left.resultdef) and
  667. not is_dynamic_array(left.resultdef) then
  668. begin
  669. hp:=ccallparanode.create(caddrnode.create_internal(
  670. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  671. ccallparanode.create(ctypeconvnode.create_internal(
  672. caddrnode.create_internal(left),voidpointertype),
  673. ccallparanode.create(ctypeconvnode.create_internal(
  674. caddrnode.create_internal(right),voidpointertype),
  675. nil)));
  676. result:=ccallnode.createintern('fpc_copy_proc',hp);
  677. firstpass(result);
  678. left:=nil;
  679. right:=nil;
  680. exit;
  681. end
  682. { call helpers for variant, they can contain non ref. counted types like
  683. vararrays which must be really copied }
  684. else if left.resultdef.typ=variantdef then
  685. begin
  686. { remove property flag to avoid errors, see comments for }
  687. { tf_winlikewidestring assignments below }
  688. exclude(left.flags,nf_isproperty);
  689. hdef:=search_system_type('TVARDATA').typedef;
  690. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  691. right,hdef),
  692. ccallparanode.create(ctypeconvnode.create_internal(
  693. left,hdef),
  694. nil));
  695. result:=ccallnode.createintern('fpc_variant_copy',hp);
  696. firstpass(result);
  697. left:=nil;
  698. right:=nil;
  699. exit;
  700. end
  701. { call helpers for windows widestrings, they aren't ref. counted }
  702. else if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  703. begin
  704. { The first argument of fpc_widestr_assign is a var parameter. Properties cannot }
  705. { be passed to var or out parameters, because in that case setters/getters are not }
  706. { used. Further, if we would allow it in case there are no getters or setters, you }
  707. { would need source changes in case these are introduced later on, thus defeating }
  708. { part of the transparency advantages of properties. In this particular case, }
  709. { however: }
  710. { a) if there is a setter, this code will not be used since then the assignment }
  711. { will be converted to a procedure call }
  712. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  713. { the refcount of the field to which we are writing }
  714. { c) source code changes are not required if a setter is added/removed, because }
  715. { this transformation is handled at compile time }
  716. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  717. { is a property which refers to a field without a setter call, we will not get }
  718. { an error about trying to pass a property as a var parameter }
  719. exclude(left.flags,nf_isproperty);
  720. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  721. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  722. nil));
  723. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  724. firstpass(result);
  725. left:=nil;
  726. right:=nil;
  727. exit;
  728. end;
  729. end;
  730. function tassignmentnode.docompare(p: tnode): boolean;
  731. begin
  732. docompare :=
  733. inherited docompare(p) and
  734. (assigntype = tassignmentnode(p).assigntype);
  735. end;
  736. {$ifdef state_tracking}
  737. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  738. var se:Tstate_entry;
  739. begin
  740. track_state_pass:=false;
  741. if exec_known then
  742. begin
  743. track_state_pass:=right.track_state_pass(exec_known);
  744. {Force a new resultdef pass.}
  745. right.resultdef:=nil;
  746. do_typecheckpass(right);
  747. typecheckpass(right);
  748. aktstate.store_fact(left.getcopy,right.getcopy);
  749. end
  750. else
  751. aktstate.delete_fact(left);
  752. end;
  753. {$endif}
  754. {*****************************************************************************
  755. TARRAYCONSTRUCTORRANGENODE
  756. *****************************************************************************}
  757. constructor tarrayconstructorrangenode.create(l,r : tnode);
  758. begin
  759. inherited create(arrayconstructorrangen,l,r);
  760. end;
  761. function tarrayconstructorrangenode.pass_typecheck:tnode;
  762. begin
  763. result:=nil;
  764. typecheckpass(left);
  765. typecheckpass(right);
  766. set_varstate(left,vs_read,[vsf_must_be_valid]);
  767. set_varstate(right,vs_read,[vsf_must_be_valid]);
  768. if codegenerror then
  769. exit;
  770. resultdef:=left.resultdef;
  771. end;
  772. function tarrayconstructorrangenode.pass_1 : tnode;
  773. begin
  774. result:=nil;
  775. CGMessage(parser_e_illegal_expression);
  776. end;
  777. {****************************************************************************
  778. TARRAYCONSTRUCTORNODE
  779. *****************************************************************************}
  780. constructor tarrayconstructornode.create(l,r : tnode);
  781. begin
  782. inherited create(arrayconstructorn,l,r);
  783. end;
  784. function tarrayconstructornode.dogetcopy : tnode;
  785. var
  786. n : tarrayconstructornode;
  787. begin
  788. n:=tarrayconstructornode(inherited dogetcopy);
  789. result:=n;
  790. end;
  791. function tarrayconstructornode.pass_typecheck:tnode;
  792. var
  793. hdef : tdef;
  794. hp : tarrayconstructornode;
  795. len : longint;
  796. varia : boolean;
  797. eq : tequaltype;
  798. hnodetype : tnodetype;
  799. begin
  800. result:=nil;
  801. { are we allowing array constructor? Then convert it to a set.
  802. Do this only if we didn't convert the arrayconstructor yet. This
  803. is needed for the cases where the resultdef is forced for a second
  804. run }
  805. if not(allow_array_constructor) then
  806. begin
  807. hp:=tarrayconstructornode(getcopy);
  808. arrayconstructor_to_set(tnode(hp));
  809. result:=hp;
  810. exit;
  811. end;
  812. { only pass left tree, right tree contains next construct if any }
  813. hdef:=nil;
  814. hnodetype:=errorn;
  815. len:=0;
  816. varia:=false;
  817. if assigned(left) then
  818. begin
  819. hp:=self;
  820. while assigned(hp) do
  821. begin
  822. typecheckpass(hp.left);
  823. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  824. if (hdef=nil) then
  825. begin
  826. hdef:=hp.left.resultdef;
  827. hnodetype:=hp.left.nodetype;
  828. end
  829. else
  830. begin
  831. { If we got a niln we don't know the type yet and need to take the
  832. type of the next array element.
  833. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  834. if hnodetype=niln then
  835. begin
  836. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  837. if eq>te_incompatible then
  838. begin
  839. hdef:=hp.left.resultdef;
  840. hnodetype:=hp.left.nodetype;
  841. end;
  842. end
  843. else
  844. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  845. if (not varia) and (eq<te_equal) then
  846. begin
  847. { If both are integers we need to take the type that can hold both
  848. defs }
  849. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  850. begin
  851. if is_in_limit(hdef,hp.left.resultdef) then
  852. hdef:=hp.left.resultdef;
  853. end
  854. else
  855. if (nf_novariaallowed in flags) then
  856. varia:=true;
  857. end;
  858. end;
  859. inc(len);
  860. hp:=tarrayconstructornode(hp.right);
  861. end;
  862. end;
  863. { Set the type of empty or varia arrays to void. Also
  864. do this if the type is array of const/open array
  865. because those can't be used with setelementdef }
  866. if not assigned(hdef) or
  867. varia or
  868. is_array_of_const(hdef) or
  869. is_open_array(hdef) then
  870. hdef:=voidtype;
  871. resultdef:=tarraydef.create(0,len-1,s32inttype);
  872. tarraydef(resultdef).elementdef:=hdef;
  873. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  874. if varia then
  875. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  876. end;
  877. procedure tarrayconstructornode.force_type(def:tdef);
  878. var
  879. hp : tarrayconstructornode;
  880. begin
  881. tarraydef(resultdef).elementdef:=def;
  882. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  883. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  884. if assigned(left) then
  885. begin
  886. hp:=self;
  887. while assigned(hp) do
  888. begin
  889. inserttypeconv(hp.left,def);
  890. hp:=tarrayconstructornode(hp.right);
  891. end;
  892. end;
  893. end;
  894. procedure tarrayconstructornode.insert_typeconvs;
  895. var
  896. hp : tarrayconstructornode;
  897. dovariant : boolean;
  898. begin
  899. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  900. { only pass left tree, right tree contains next construct if any }
  901. if assigned(left) then
  902. begin
  903. hp:=self;
  904. while assigned(hp) do
  905. begin
  906. typecheckpass(hp.left);
  907. { Insert typeconvs for array of const }
  908. if dovariant then
  909. { at this time C varargs are no longer an arrayconstructornode }
  910. insert_varargstypeconv(hp.left,false);
  911. hp:=tarrayconstructornode(hp.right);
  912. end;
  913. end;
  914. end;
  915. function tarrayconstructornode.pass_1 : tnode;
  916. var
  917. hp : tarrayconstructornode;
  918. do_variant:boolean;
  919. begin
  920. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  921. result:=nil;
  922. { Insert required type convs, this must be
  923. done in pass 1, because the call must be
  924. typecheckpassed already }
  925. if assigned(left) then
  926. begin
  927. insert_typeconvs;
  928. { call firstpass for all nodes }
  929. hp:=self;
  930. while assigned(hp) do
  931. begin
  932. if hp.left<>nil then
  933. begin
  934. {This check is pessimistic; a call will happen depending
  935. on the location in which the elements will be found in
  936. pass 2.}
  937. if not do_variant then
  938. include(current_procinfo.flags,pi_do_call);
  939. firstpass(hp.left);
  940. end;
  941. hp:=tarrayconstructornode(hp.right);
  942. end;
  943. end;
  944. expectloc:=LOC_CREFERENCE;
  945. end;
  946. function tarrayconstructornode.docompare(p: tnode): boolean;
  947. begin
  948. docompare:=inherited docompare(p);
  949. end;
  950. {*****************************************************************************
  951. TTYPENODE
  952. *****************************************************************************}
  953. constructor ttypenode.create(def:tdef);
  954. begin
  955. inherited create(typen);
  956. typedef:=def;
  957. typesym:=def.typesym;
  958. allowed:=false;
  959. helperallowed:=false;
  960. end;
  961. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  962. begin
  963. inherited ppuload(t,ppufile);
  964. ppufile.getderef(typedefderef);
  965. ppufile.getderef(typesymderef);
  966. allowed:=boolean(ppufile.getbyte);
  967. helperallowed:=boolean(ppufile.getbyte);
  968. end;
  969. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  970. begin
  971. inherited ppuwrite(ppufile);
  972. ppufile.putderef(typedefderef);
  973. ppufile.putderef(typesymderef);
  974. ppufile.putbyte(byte(allowed));
  975. ppufile.putbyte(byte(helperallowed));
  976. end;
  977. procedure ttypenode.buildderefimpl;
  978. begin
  979. inherited buildderefimpl;
  980. typedefderef.build(typedef);
  981. typesymderef.build(typesym);
  982. end;
  983. procedure ttypenode.derefimpl;
  984. begin
  985. inherited derefimpl;
  986. typedef:=tdef(typedefderef.resolve);
  987. typesym:=tsym(typesymderef.resolve);
  988. end;
  989. function ttypenode.pass_typecheck:tnode;
  990. begin
  991. result:=nil;
  992. resultdef:=typedef;
  993. { check if it's valid }
  994. if typedef.typ = errordef then
  995. CGMessage(parser_e_illegal_expression);
  996. end;
  997. function ttypenode.pass_1 : tnode;
  998. begin
  999. result:=nil;
  1000. expectloc:=LOC_VOID;
  1001. { a typenode can't generate code, so we give here
  1002. an error. Else it'll be an abstract error in pass_generate_code.
  1003. Only when the allowed flag is set we don't generate
  1004. an error }
  1005. if not allowed then
  1006. Message(parser_e_no_type_not_allowed_here);
  1007. if not helperallowed and is_objectpascal_helper(typedef) then
  1008. Message(parser_e_no_category_as_types);
  1009. end;
  1010. function ttypenode.dogetcopy : tnode;
  1011. var
  1012. n : ttypenode;
  1013. begin
  1014. n:=ttypenode(inherited dogetcopy);
  1015. n.allowed:=allowed;
  1016. n.typedef:=typedef;
  1017. n.helperallowed:=helperallowed;
  1018. result:=n;
  1019. end;
  1020. function ttypenode.docompare(p: tnode): boolean;
  1021. begin
  1022. docompare :=
  1023. inherited docompare(p);
  1024. end;
  1025. {*****************************************************************************
  1026. TRTTINODE
  1027. *****************************************************************************}
  1028. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  1029. begin
  1030. inherited create(rttin);
  1031. rttidef:=def;
  1032. rttitype:=rt;
  1033. rttidatatype:=dt;
  1034. end;
  1035. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1036. begin
  1037. inherited ppuload(t,ppufile);
  1038. ppufile.getderef(rttidefderef);
  1039. rttitype:=trttitype(ppufile.getbyte);
  1040. rttidatatype:=trttidatatype(ppufile.getbyte);
  1041. end;
  1042. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1043. begin
  1044. inherited ppuwrite(ppufile);
  1045. ppufile.putderef(rttidefderef);
  1046. ppufile.putbyte(byte(rttitype));
  1047. ppufile.putbyte(byte(rttidatatype));
  1048. end;
  1049. procedure trttinode.buildderefimpl;
  1050. begin
  1051. inherited buildderefimpl;
  1052. rttidefderef.build(rttidef);
  1053. end;
  1054. procedure trttinode.derefimpl;
  1055. begin
  1056. inherited derefimpl;
  1057. rttidef:=tstoreddef(rttidefderef.resolve);
  1058. end;
  1059. function trttinode.dogetcopy : tnode;
  1060. var
  1061. n : trttinode;
  1062. begin
  1063. n:=trttinode(inherited dogetcopy);
  1064. n.rttidef:=rttidef;
  1065. n.rttitype:=rttitype;
  1066. n.rttidatatype:=rttidatatype;
  1067. result:=n;
  1068. end;
  1069. function trttinode.pass_typecheck:tnode;
  1070. begin
  1071. { rtti information will be returned as a void pointer }
  1072. result:=nil;
  1073. resultdef:=voidpointertype;
  1074. end;
  1075. function trttinode.pass_1 : tnode;
  1076. begin
  1077. result:=nil;
  1078. expectloc:=LOC_CREFERENCE;
  1079. end;
  1080. function trttinode.docompare(p: tnode): boolean;
  1081. begin
  1082. docompare :=
  1083. inherited docompare(p) and
  1084. (rttidef = trttinode(p).rttidef) and
  1085. (rttitype = trttinode(p).rttitype);
  1086. end;
  1087. end.