nld.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. hs: string;
  619. needrtti: boolean;
  620. begin
  621. result:=nil;
  622. expectloc:=LOC_VOID;
  623. firstpass(left);
  624. { Optimize the reuse of the destination of the assingment in left.
  625. Allow the use of the left inside the tree generated on the right.
  626. This is especially useful for string routines where the destination
  627. is pushed as a parameter. Using the final destination of left directly
  628. save a temp allocation and copy of data (PFV) }
  629. oldassignmentnode:=aktassignmentnode;
  630. aktassignmentnode:=self;
  631. firstpass(right);
  632. aktassignmentnode:=oldassignmentnode;
  633. if nf_assign_done_in_right in flags then
  634. begin
  635. result:=right;
  636. right:=nil;
  637. exit;
  638. end;
  639. if codegenerror then
  640. exit;
  641. { assignment to refcounted variable -> inc/decref }
  642. if is_managed_type(left.resultdef) then
  643. include(current_procinfo.flags,pi_do_call);
  644. needrtti:=false;
  645. if (is_shortstring(left.resultdef)) then
  646. begin
  647. if right.resultdef.typ=stringdef then
  648. begin
  649. if (right.nodetype<>stringconstn) or
  650. (tstringconstnode(right).len<>0) then
  651. begin
  652. { remove property flag to avoid errors, see comments for }
  653. { tf_winlikewidestring assignments below }
  654. exclude(left.flags, nf_isproperty);
  655. hp:=ccallparanode.create
  656. (right,
  657. ccallparanode.create(left,nil));
  658. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  659. firstpass(result);
  660. left:=nil;
  661. right:=nil;
  662. end;
  663. end;
  664. exit;
  665. end
  666. { call helpers for composite types containing automated types }
  667. else if is_managed_type(left.resultdef) and
  668. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  669. not is_interfacecom_or_dispinterface(left.resultdef) and
  670. not is_dynamic_array(left.resultdef) then
  671. begin
  672. hp:=ccallparanode.create(caddrnode.create_internal(
  673. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  674. ccallparanode.create(ctypeconvnode.create_internal(
  675. caddrnode.create_internal(left),voidpointertype),
  676. ccallparanode.create(ctypeconvnode.create_internal(
  677. caddrnode.create_internal(right),voidpointertype),
  678. nil)));
  679. result:=ccallnode.createintern('fpc_copy_proc',hp);
  680. firstpass(result);
  681. left:=nil;
  682. right:=nil;
  683. exit;
  684. end
  685. { call helpers for variant, they can contain non ref. counted types like
  686. vararrays which must be really copied }
  687. else if left.resultdef.typ=variantdef then
  688. begin
  689. { remove property flag to avoid errors, see comments for }
  690. { tf_winlikewidestring assignments below }
  691. exclude(left.flags,nf_isproperty);
  692. hdef:=search_system_type('TVARDATA').typedef;
  693. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  694. right,hdef),
  695. ccallparanode.create(ctypeconvnode.create_internal(
  696. left,hdef),
  697. nil));
  698. result:=ccallnode.createintern('fpc_variant_copy',hp);
  699. firstpass(result);
  700. left:=nil;
  701. right:=nil;
  702. exit;
  703. end
  704. { call helpers for pointer-sized managed types }
  705. else if is_widestring(left.resultdef) then
  706. hs:='fpc_widestr_assign'
  707. else if is_ansistring(left.resultdef) then
  708. hs:='fpc_ansistr_assign'
  709. else if is_unicodestring(left.resultdef) then
  710. hs:='fpc_unicodestr_assign'
  711. else if is_interfacecom_or_dispinterface(left.resultdef) then
  712. hs:='fpc_intf_assign'
  713. else if is_dynamic_array(left.resultdef) then
  714. begin
  715. hs:='fpc_dynarray_assign';
  716. needrtti:=true;
  717. end
  718. else
  719. exit;
  720. { The first argument of these procedures is a var parameter. Properties cannot }
  721. { be passed to var or out parameters, because in that case setters/getters are not }
  722. { used. Further, if we would allow it in case there are no getters or setters, you }
  723. { would need source changes in case these are introduced later on, thus defeating }
  724. { part of the transparency advantages of properties. In this particular case, }
  725. { however: }
  726. { a) if there is a setter, this code will not be used since then the assignment }
  727. { will be converted to a procedure call }
  728. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  729. { the refcount of the field to which we are writing }
  730. { c) source code changes are not required if a setter is added/removed, because }
  731. { this transformation is handled at compile time }
  732. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  733. { is a property which refers to a field without a setter call, we will not get }
  734. { an error about trying to pass a property as a var parameter }
  735. exclude(left.flags,nf_isproperty);
  736. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  737. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  738. nil));
  739. if needrtti then
  740. hp:=ccallparanode.create(
  741. caddrnode.create_internal(
  742. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  743. hp);
  744. result:=ccallnode.createintern(hs,hp);
  745. firstpass(result);
  746. left:=nil;
  747. right:=nil;
  748. end;
  749. function tassignmentnode.docompare(p: tnode): boolean;
  750. begin
  751. docompare :=
  752. inherited docompare(p) and
  753. (assigntype = tassignmentnode(p).assigntype);
  754. end;
  755. {$ifdef state_tracking}
  756. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  757. var se:Tstate_entry;
  758. begin
  759. track_state_pass:=false;
  760. if exec_known then
  761. begin
  762. track_state_pass:=right.track_state_pass(exec_known);
  763. {Force a new resultdef pass.}
  764. right.resultdef:=nil;
  765. do_typecheckpass(right);
  766. typecheckpass(right);
  767. aktstate.store_fact(left.getcopy,right.getcopy);
  768. end
  769. else
  770. aktstate.delete_fact(left);
  771. end;
  772. {$endif}
  773. {*****************************************************************************
  774. TARRAYCONSTRUCTORRANGENODE
  775. *****************************************************************************}
  776. constructor tarrayconstructorrangenode.create(l,r : tnode);
  777. begin
  778. inherited create(arrayconstructorrangen,l,r);
  779. end;
  780. function tarrayconstructorrangenode.pass_typecheck:tnode;
  781. begin
  782. result:=nil;
  783. typecheckpass(left);
  784. typecheckpass(right);
  785. set_varstate(left,vs_read,[vsf_must_be_valid]);
  786. set_varstate(right,vs_read,[vsf_must_be_valid]);
  787. if codegenerror then
  788. exit;
  789. resultdef:=left.resultdef;
  790. end;
  791. function tarrayconstructorrangenode.pass_1 : tnode;
  792. begin
  793. result:=nil;
  794. CGMessage(parser_e_illegal_expression);
  795. end;
  796. {****************************************************************************
  797. TARRAYCONSTRUCTORNODE
  798. *****************************************************************************}
  799. constructor tarrayconstructornode.create(l,r : tnode);
  800. begin
  801. inherited create(arrayconstructorn,l,r);
  802. end;
  803. function tarrayconstructornode.dogetcopy : tnode;
  804. var
  805. n : tarrayconstructornode;
  806. begin
  807. n:=tarrayconstructornode(inherited dogetcopy);
  808. result:=n;
  809. end;
  810. function tarrayconstructornode.pass_typecheck:tnode;
  811. var
  812. hdef : tdef;
  813. hp : tarrayconstructornode;
  814. len : longint;
  815. varia : boolean;
  816. eq : tequaltype;
  817. hnodetype : tnodetype;
  818. begin
  819. result:=nil;
  820. { are we allowing array constructor? Then convert it to a set.
  821. Do this only if we didn't convert the arrayconstructor yet. This
  822. is needed for the cases where the resultdef is forced for a second
  823. run }
  824. if not(allow_array_constructor) then
  825. begin
  826. hp:=tarrayconstructornode(getcopy);
  827. arrayconstructor_to_set(tnode(hp));
  828. result:=hp;
  829. exit;
  830. end;
  831. { only pass left tree, right tree contains next construct if any }
  832. hdef:=nil;
  833. hnodetype:=errorn;
  834. len:=0;
  835. varia:=false;
  836. if assigned(left) then
  837. begin
  838. hp:=self;
  839. while assigned(hp) do
  840. begin
  841. typecheckpass(hp.left);
  842. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  843. if (hdef=nil) then
  844. begin
  845. hdef:=hp.left.resultdef;
  846. hnodetype:=hp.left.nodetype;
  847. end
  848. else
  849. begin
  850. { If we got a niln we don't know the type yet and need to take the
  851. type of the next array element.
  852. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  853. if hnodetype=niln then
  854. begin
  855. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  856. if eq>te_incompatible then
  857. begin
  858. hdef:=hp.left.resultdef;
  859. hnodetype:=hp.left.nodetype;
  860. end;
  861. end
  862. else
  863. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  864. if (not varia) and (eq<te_equal) then
  865. begin
  866. { If both are integers we need to take the type that can hold both
  867. defs }
  868. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  869. begin
  870. if is_in_limit(hdef,hp.left.resultdef) then
  871. hdef:=hp.left.resultdef;
  872. end
  873. else
  874. if (nf_novariaallowed in flags) then
  875. varia:=true;
  876. end;
  877. end;
  878. inc(len);
  879. hp:=tarrayconstructornode(hp.right);
  880. end;
  881. end;
  882. { Set the type of empty or varia arrays to void. Also
  883. do this if the type is array of const/open array
  884. because those can't be used with setelementdef }
  885. if not assigned(hdef) or
  886. varia or
  887. is_array_of_const(hdef) or
  888. is_open_array(hdef) then
  889. hdef:=voidtype;
  890. resultdef:=tarraydef.create(0,len-1,s32inttype);
  891. tarraydef(resultdef).elementdef:=hdef;
  892. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  893. if varia then
  894. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  895. end;
  896. procedure tarrayconstructornode.force_type(def:tdef);
  897. var
  898. hp : tarrayconstructornode;
  899. begin
  900. tarraydef(resultdef).elementdef:=def;
  901. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  902. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  903. if assigned(left) then
  904. begin
  905. hp:=self;
  906. while assigned(hp) do
  907. begin
  908. inserttypeconv(hp.left,def);
  909. hp:=tarrayconstructornode(hp.right);
  910. end;
  911. end;
  912. end;
  913. procedure tarrayconstructornode.insert_typeconvs;
  914. var
  915. hp : tarrayconstructornode;
  916. dovariant : boolean;
  917. begin
  918. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  919. { only pass left tree, right tree contains next construct if any }
  920. if assigned(left) then
  921. begin
  922. hp:=self;
  923. while assigned(hp) do
  924. begin
  925. typecheckpass(hp.left);
  926. { Insert typeconvs for array of const }
  927. if dovariant then
  928. { at this time C varargs are no longer an arrayconstructornode }
  929. insert_varargstypeconv(hp.left,false);
  930. hp:=tarrayconstructornode(hp.right);
  931. end;
  932. end;
  933. end;
  934. function tarrayconstructornode.pass_1 : tnode;
  935. var
  936. hp : tarrayconstructornode;
  937. do_variant:boolean;
  938. begin
  939. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  940. result:=nil;
  941. { Insert required type convs, this must be
  942. done in pass 1, because the call must be
  943. typecheckpassed already }
  944. if assigned(left) then
  945. begin
  946. insert_typeconvs;
  947. { call firstpass for all nodes }
  948. hp:=self;
  949. while assigned(hp) do
  950. begin
  951. if hp.left<>nil then
  952. begin
  953. {This check is pessimistic; a call will happen depending
  954. on the location in which the elements will be found in
  955. pass 2.}
  956. if not do_variant then
  957. include(current_procinfo.flags,pi_do_call);
  958. firstpass(hp.left);
  959. end;
  960. hp:=tarrayconstructornode(hp.right);
  961. end;
  962. end;
  963. expectloc:=LOC_CREFERENCE;
  964. end;
  965. function tarrayconstructornode.docompare(p: tnode): boolean;
  966. begin
  967. docompare:=inherited docompare(p);
  968. end;
  969. {*****************************************************************************
  970. TTYPENODE
  971. *****************************************************************************}
  972. constructor ttypenode.create(def:tdef);
  973. begin
  974. inherited create(typen);
  975. typedef:=def;
  976. typesym:=def.typesym;
  977. allowed:=false;
  978. helperallowed:=false;
  979. end;
  980. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  981. begin
  982. inherited ppuload(t,ppufile);
  983. ppufile.getderef(typedefderef);
  984. ppufile.getderef(typesymderef);
  985. allowed:=boolean(ppufile.getbyte);
  986. helperallowed:=boolean(ppufile.getbyte);
  987. end;
  988. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  989. begin
  990. inherited ppuwrite(ppufile);
  991. ppufile.putderef(typedefderef);
  992. ppufile.putderef(typesymderef);
  993. ppufile.putbyte(byte(allowed));
  994. ppufile.putbyte(byte(helperallowed));
  995. end;
  996. procedure ttypenode.buildderefimpl;
  997. begin
  998. inherited buildderefimpl;
  999. typedefderef.build(typedef);
  1000. typesymderef.build(typesym);
  1001. end;
  1002. procedure ttypenode.derefimpl;
  1003. begin
  1004. inherited derefimpl;
  1005. typedef:=tdef(typedefderef.resolve);
  1006. typesym:=tsym(typesymderef.resolve);
  1007. end;
  1008. function ttypenode.pass_typecheck:tnode;
  1009. begin
  1010. result:=nil;
  1011. resultdef:=typedef;
  1012. { check if it's valid }
  1013. if typedef.typ = errordef then
  1014. CGMessage(parser_e_illegal_expression);
  1015. end;
  1016. function ttypenode.pass_1 : tnode;
  1017. begin
  1018. result:=nil;
  1019. expectloc:=LOC_VOID;
  1020. { a typenode can't generate code, so we give here
  1021. an error. Else it'll be an abstract error in pass_generate_code.
  1022. Only when the allowed flag is set we don't generate
  1023. an error }
  1024. if not allowed then
  1025. Message(parser_e_no_type_not_allowed_here);
  1026. if not helperallowed and is_objectpascal_helper(typedef) then
  1027. Message(parser_e_no_category_as_types);
  1028. end;
  1029. function ttypenode.dogetcopy : tnode;
  1030. var
  1031. n : ttypenode;
  1032. begin
  1033. n:=ttypenode(inherited dogetcopy);
  1034. n.allowed:=allowed;
  1035. n.typedef:=typedef;
  1036. n.helperallowed:=helperallowed;
  1037. result:=n;
  1038. end;
  1039. function ttypenode.docompare(p: tnode): boolean;
  1040. begin
  1041. docompare :=
  1042. inherited docompare(p);
  1043. end;
  1044. {*****************************************************************************
  1045. TRTTINODE
  1046. *****************************************************************************}
  1047. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  1048. begin
  1049. inherited create(rttin);
  1050. rttidef:=def;
  1051. rttitype:=rt;
  1052. rttidatatype:=dt;
  1053. end;
  1054. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1055. begin
  1056. inherited ppuload(t,ppufile);
  1057. ppufile.getderef(rttidefderef);
  1058. rttitype:=trttitype(ppufile.getbyte);
  1059. rttidatatype:=trttidatatype(ppufile.getbyte);
  1060. end;
  1061. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1062. begin
  1063. inherited ppuwrite(ppufile);
  1064. ppufile.putderef(rttidefderef);
  1065. ppufile.putbyte(byte(rttitype));
  1066. ppufile.putbyte(byte(rttidatatype));
  1067. end;
  1068. procedure trttinode.buildderefimpl;
  1069. begin
  1070. inherited buildderefimpl;
  1071. rttidefderef.build(rttidef);
  1072. end;
  1073. procedure trttinode.derefimpl;
  1074. begin
  1075. inherited derefimpl;
  1076. rttidef:=tstoreddef(rttidefderef.resolve);
  1077. end;
  1078. function trttinode.dogetcopy : tnode;
  1079. var
  1080. n : trttinode;
  1081. begin
  1082. n:=trttinode(inherited dogetcopy);
  1083. n.rttidef:=rttidef;
  1084. n.rttitype:=rttitype;
  1085. n.rttidatatype:=rttidatatype;
  1086. result:=n;
  1087. end;
  1088. function trttinode.pass_typecheck:tnode;
  1089. begin
  1090. { rtti information will be returned as a void pointer }
  1091. result:=nil;
  1092. resultdef:=voidpointertype;
  1093. end;
  1094. function trttinode.pass_1 : tnode;
  1095. begin
  1096. result:=nil;
  1097. expectloc:=LOC_CREFERENCE;
  1098. end;
  1099. function trttinode.docompare(p: tnode): boolean;
  1100. begin
  1101. docompare :=
  1102. inherited docompare(p) and
  1103. (rttidef = trttinode(p).rttidef) and
  1104. (rttitype = trttinode(p).rttitype);
  1105. end;
  1106. end.