nld.pas 39 KB

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