nld.pas 43 KB

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