nld.pas 42 KB

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