nld.pas 41 KB

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