nld.pas 43 KB

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