nld.pas 43 KB

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