pgenutil.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. {
  2. Copyright (c) 2011
  3. Contains different functions that are used in the context of
  4. parsing generics.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pgenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { symtable }
  25. symtype,symdef,symbase;
  26. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string);
  27. function parse_generic_parameters:TFPObjectList;
  28. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  29. type
  30. tspecializationstate = record
  31. oldsymtablestack : tsymtablestack;
  32. oldextendeddefs : TFPHashObjectList;
  33. end;
  34. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  35. procedure specialization_done(var state:tspecializationstate);
  36. implementation
  37. uses
  38. { common }
  39. cutils,fpccrc,
  40. { global }
  41. globals,globtype,tokens,verbose,
  42. { symtable }
  43. symconst,symsym,symtable,
  44. { modules }
  45. fmodule,
  46. { pass 1 }
  47. htypechk,
  48. node,nobj,nmem,
  49. { parser }
  50. scanner,
  51. pbase,pexpr,pdecsub,ptype;
  52. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string);
  53. var
  54. st : TSymtable;
  55. srsym : tsym;
  56. pt2 : tnode;
  57. found,
  58. first,
  59. err : boolean;
  60. i,
  61. gencount,crc : longint;
  62. genericdef,def : tstoreddef;
  63. generictype : ttypesym;
  64. genericdeflist : TFPObjectList;
  65. generictypelist : TFPObjectList;
  66. prettyname,specializename : ansistring;
  67. ufinalspecializename,
  68. countstr,genname,ugenname,finalspecializename : string;
  69. vmtbuilder : TVMTBuilder;
  70. specializest : tsymtable;
  71. item : tobject;
  72. old_current_structdef : tabstractrecorddef;
  73. old_current_genericdef,old_current_specializedef : tstoreddef;
  74. tempst : tglobalsymtable;
  75. old_block_type: tblock_type;
  76. hashedid: thashedidstring;
  77. state : tspecializationstate;
  78. begin
  79. { retrieve generic def that we are going to replace }
  80. genericdef:=tstoreddef(tt);
  81. tt:=nil;
  82. { either symname must be given or genericdef needs to be valid }
  83. if (symname='') and
  84. (not assigned(genericdef) or
  85. not assigned(genericdef.typesym) or
  86. (genericdef.typesym.typ<>typesym)) then
  87. internalerror(2011042701);
  88. { Only parse the parameters for recovery or
  89. for recording in genericbuf }
  90. if parse_generic then
  91. begin
  92. if not try_to_consume(_LT) then
  93. consume(_LSHARPBRACKET);
  94. gencount:=0;
  95. repeat
  96. pt2:=factor(false,true);
  97. pt2.free;
  98. inc(gencount);
  99. until not try_to_consume(_COMMA);
  100. if not try_to_consume(_GT) then
  101. consume(_RSHARPBRACKET);
  102. { we need to return a def that can later pass some checks like
  103. whether it's an interface or not }
  104. if not assigned(tt) or (tt.typ=undefineddef) then
  105. begin
  106. if (symname='') and (df_generic in genericdef.defoptions) then
  107. { this happens in non-Delphi modes }
  108. tt:=genericdef
  109. else
  110. begin
  111. { find the corresponding generic symbol so that any checks
  112. done on the returned def will be handled correctly }
  113. str(gencount,countstr);
  114. if symname='' then
  115. genname:=ttypesym(genericdef.typesym).realname
  116. else
  117. genname:=symname;
  118. genname:=genname+'$'+countstr;
  119. ugenname:=upper(genname);
  120. { first check whether the found name is the same as that of
  121. the current def or one of its (generic) surrounding defs;
  122. this is necessary as the symbol of the generic can not yet
  123. be used for lookup as it still contains a reference to an
  124. errordef) }
  125. def:=current_genericdef;
  126. repeat
  127. if def.typ in [objectdef,recorddef] then
  128. if tabstractrecorddef(def).objname^=ugenname then
  129. begin
  130. tt:=def;
  131. break;
  132. end;
  133. def:=tstoreddef(def.owner.defowner);
  134. until not assigned(def) or not (df_generic in def.defoptions);
  135. { it's not part of the current object hierarchy, so search
  136. for the symbol }
  137. if not assigned(tt) then
  138. begin
  139. if not searchsym(ugenname,srsym,st) or
  140. (srsym.typ<>typesym) then
  141. begin
  142. identifier_not_found(genname);
  143. exit;
  144. end;
  145. tt:=ttypesym(srsym).typedef;
  146. { this happens in non-Delphi modes if we encounter a
  147. specialization of the generic class or record we're
  148. currently parsing }
  149. if (tt.typ=errordef) and assigned(current_structdef) and
  150. (current_structdef.objname^=ugenname) then
  151. tt:=current_structdef;
  152. end;
  153. end;
  154. end;
  155. exit;
  156. end;
  157. if not assigned(parsedtype) and not try_to_consume(_LT) then
  158. consume(_LSHARPBRACKET);
  159. generictypelist:=TFPObjectList.create(false);
  160. genericdeflist:=TFPObjectList.Create(false);
  161. { Parse type parameters }
  162. err:=false;
  163. { set the block type to type, so that the parsed type are returned as
  164. ttypenode (e.g. classes are in non type-compatible blocks returned as
  165. tloadvmtaddrnode) }
  166. old_block_type:=block_type;
  167. { if parsedtype is set, then the first type identifer was already parsed
  168. (happens in inline specializations) and thus we only need to parse
  169. the remaining types and do as if the first one was already given }
  170. first:=not assigned(parsedtype);
  171. if assigned(parsedtype) then
  172. begin
  173. genericdeflist.Add(parsedtype);
  174. specializename:='$'+parsedtype.typename;
  175. prettyname:=parsedtype.typesym.prettyname;
  176. end
  177. else
  178. begin
  179. specializename:='';
  180. prettyname:='';
  181. end;
  182. while not (token in [_GT,_RSHARPBRACKET]) do
  183. begin
  184. { "first" is set to false at the end of the loop! }
  185. if not first then
  186. consume(_COMMA);
  187. block_type:=bt_type;
  188. pt2:=factor(false,true);
  189. if pt2.nodetype=typen then
  190. begin
  191. if df_generic in pt2.resultdef.defoptions then
  192. Message(parser_e_no_generics_as_params);
  193. genericdeflist.Add(pt2.resultdef);
  194. if not assigned(pt2.resultdef.typesym) then
  195. message(type_e_generics_cannot_reference_itself)
  196. else
  197. begin
  198. specializename:=specializename+'$'+pt2.resultdef.typename;
  199. if first then
  200. prettyname:=prettyname+pt2.resultdef.typesym.prettyname
  201. else
  202. prettyname:=prettyname+','+pt2.resultdef.typesym.prettyname;
  203. end;
  204. end
  205. else
  206. begin
  207. Message(type_e_type_id_expected);
  208. err:=true;
  209. end;
  210. pt2.free;
  211. first:=false;
  212. end;
  213. block_type:=old_block_type;
  214. if err then
  215. begin
  216. try_to_consume(_RSHARPBRACKET);
  217. exit;
  218. end;
  219. { search a generic with the given count of params }
  220. countstr:='';
  221. str(genericdeflist.Count,countstr);
  222. { use the name of the symbol as procvars return a user friendly version
  223. of the name }
  224. if symname='' then
  225. genname:=ttypesym(genericdef.typesym).realname
  226. else
  227. genname:=symname;
  228. { in case of non-Delphi mode the type name could already be a generic
  229. def (but maybe the wrong one) }
  230. if assigned(genericdef) and (df_generic in genericdef.defoptions) then
  231. begin
  232. { remove the type count suffix from the generic's name }
  233. for i:=Length(genname) downto 1 do
  234. if genname[i]='$' then
  235. begin
  236. genname:=copy(genname,1,i-1);
  237. break;
  238. end;
  239. end;
  240. genname:=genname+'$'+countstr;
  241. ugenname:=upper(genname);
  242. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  243. begin
  244. if genericdef.owner.symtabletype = objectsymtable then
  245. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,false)
  246. else
  247. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  248. end
  249. else
  250. found:=searchsym(ugenname,srsym,st);
  251. if not found or (srsym.typ<>typesym) then
  252. begin
  253. identifier_not_found(genname);
  254. genericdeflist.Free;
  255. generictypelist.Free;
  256. exit;
  257. end;
  258. { we've found the correct def }
  259. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  260. { build the new type's name }
  261. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  262. finalspecializename:=genname+'$crc'+hexstr(crc,8);
  263. ufinalspecializename:=upper(finalspecializename);
  264. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  265. { select the symtable containing the params }
  266. case genericdef.typ of
  267. procdef:
  268. st:=genericdef.GetSymtable(gs_para);
  269. objectdef,
  270. recorddef:
  271. st:=genericdef.GetSymtable(gs_record);
  272. arraydef:
  273. st:=tarraydef(genericdef).symtable;
  274. procvardef:
  275. st:=genericdef.GetSymtable(gs_para);
  276. else
  277. internalerror(200511182);
  278. end;
  279. { build the list containing the types for the generic params }
  280. gencount:=0;
  281. for i:=0 to st.SymList.Count-1 do
  282. begin
  283. srsym:=tsym(st.SymList[i]);
  284. if sp_generic_para in srsym.symoptions then
  285. begin
  286. if gencount=genericdeflist.Count then
  287. internalerror(2011042702);
  288. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  289. generictypelist.add(generictype);
  290. inc(gencount);
  291. end;
  292. end;
  293. { Special case if we are referencing the current defined object }
  294. if assigned(current_structdef) and
  295. (current_structdef.objname^=ufinalspecializename) then
  296. tt:=current_structdef;
  297. { decide in which symtable to put the specialization }
  298. if current_module.is_unit and current_module.in_interface then
  299. specializest:=current_module.globalsymtable
  300. else
  301. specializest:=current_module.localsymtable;
  302. { Can we reuse an already specialized type? }
  303. { for this first check whether we are currently specializing a nested
  304. type of the current (main) specialization (this is necessary, because
  305. during that time the symbol of the main specialization will still
  306. contain a reference to an errordef) }
  307. if not assigned(tt) and assigned(current_specializedef) then
  308. begin
  309. def:=current_specializedef;
  310. repeat
  311. if def.typ in [objectdef,recorddef] then
  312. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  313. tt:=def;
  314. break;
  315. end;
  316. def:=tstoreddef(def.owner.defowner);
  317. until not assigned(def) or not (df_specialization in def.defoptions);
  318. end;
  319. { now check whether there is a specialization somewhere else }
  320. if not assigned(tt) then
  321. begin
  322. hashedid.id:=ufinalspecializename;
  323. srsym:=tsym(specializest.findwithhash(hashedid));
  324. if assigned(srsym) then
  325. begin
  326. if srsym.typ<>typesym then
  327. internalerror(200710171);
  328. tt:=ttypesym(srsym).typedef;
  329. end
  330. else
  331. { the generic could have been specialized in the globalsymtable
  332. already, so search there as well }
  333. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  334. begin
  335. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  336. if assigned(srsym) then
  337. begin
  338. if srsym.typ<>typesym then
  339. internalerror(2011121101);
  340. tt:=ttypesym(srsym).typedef;
  341. end;
  342. end;
  343. end;
  344. if not assigned(tt) then
  345. begin
  346. specialization_init(genericdef,state);
  347. { push a temporary global symtable so that the specialization is
  348. added to the correct symtable; this symtable does not contain
  349. any other symbols, so that the type resolution can not be
  350. influenced by symbols in the current unit }
  351. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  352. symtablestack.push(tempst);
  353. { Reparse the original type definition }
  354. if not err then
  355. begin
  356. if parse_class_parent then
  357. begin
  358. old_current_structdef:=current_structdef;
  359. old_current_genericdef:=current_genericdef;
  360. old_current_specializedef:=current_specializedef;
  361. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  362. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  363. else
  364. current_structdef:=nil;
  365. current_genericdef:=nil;
  366. current_specializedef:=nil;
  367. end;
  368. { First a new typesym so we can reuse this specialization and
  369. references to this specialization can be handled }
  370. srsym:=ttypesym.create(finalspecializename,generrordef);
  371. specializest.insert(srsym);
  372. { specializations are declarations as such it is the wisest to
  373. declare set the blocktype to "type"; otherwise we'll
  374. experience unexpected side effects like the addition of
  375. classrefdefs if we have a generic that's derived from another
  376. generic }
  377. old_block_type:=block_type;
  378. block_type:=bt_type;
  379. if not assigned(genericdef.generictokenbuf) then
  380. internalerror(200511171);
  381. current_scanner.startreplaytokens(genericdef.generictokenbuf,
  382. genericdef.change_endian);
  383. read_named_type(tt,finalspecializename,genericdef,generictypelist,false);
  384. ttypesym(srsym).typedef:=tt;
  385. tt.typesym:=srsym;
  386. if _prettyname<>'' then
  387. ttypesym(tt.typesym).fprettyname:=_prettyname
  388. else
  389. ttypesym(tt.typesym).fprettyname:=prettyname;
  390. { Note regarding hint directives:
  391. There is no need to remove the flags for them from the
  392. specialized generic symbol, because hint directives that
  393. follow the specialization are handled by the code in
  394. pdecl.types_dec and added to the type symbol.
  395. E.g.: TFoo = TBar<Blubb> deprecated;
  396. Here the symbol TBar$1$Blubb will contain the
  397. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  398. case tt.typ of
  399. { Build VMT indexes for classes and read hint directives }
  400. objectdef:
  401. begin
  402. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  403. consume(_SEMICOLON);
  404. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  405. vmtbuilder.generate_vmt;
  406. vmtbuilder.free;
  407. end;
  408. { handle params, calling convention, etc }
  409. procvardef:
  410. begin
  411. if not check_proc_directive(true) then
  412. begin
  413. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  414. consume(_SEMICOLON);
  415. end;
  416. parse_var_proc_directives(ttypesym(srsym));
  417. handle_calling_convention(tprocvardef(tt));
  418. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  419. consume(_SEMICOLON);
  420. end;
  421. else
  422. { parse hint directives for records and arrays }
  423. begin
  424. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  425. consume(_SEMICOLON);
  426. end;
  427. end;
  428. { Consume the semicolon if it is also recorded }
  429. try_to_consume(_SEMICOLON);
  430. block_type:=old_block_type;
  431. if parse_class_parent then
  432. begin
  433. current_structdef:=old_current_structdef;
  434. current_genericdef:=old_current_genericdef;
  435. current_specializedef:=old_current_specializedef;
  436. end;
  437. end;
  438. { extract all created symbols and defs from the temporary symtable
  439. and add them to the specializest }
  440. for i:=0 to tempst.SymList.Count-1 do
  441. begin
  442. item:=tempst.SymList.Items[i];
  443. specializest.SymList.Add(tempst.SymList.NameOfIndex(i),item);
  444. tsym(item).Owner:=specializest;
  445. tempst.SymList.Extract(item);
  446. end;
  447. for i:=0 to tempst.DefList.Count-1 do
  448. begin
  449. item:=tempst.DefList.Items[i];
  450. specializest.DefList.Add(item);
  451. tdef(item).owner:=specializest;
  452. tempst.DefList.Extract(item);
  453. end;
  454. tempst.free;
  455. specialization_done(state);
  456. end;
  457. if not (token in [_GT, _RSHARPBRACKET]) then
  458. begin
  459. consume(_RSHARPBRACKET);
  460. exit;
  461. end
  462. else
  463. consume(token);
  464. genericdeflist.free;
  465. generictypelist.free;
  466. if assigned(genericdef) then
  467. begin
  468. { check the hints of the found generic symbol }
  469. srsym:=genericdef.typesym;
  470. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  471. end;
  472. end;
  473. function parse_generic_parameters:TFPObjectList;
  474. var
  475. generictype : ttypesym;
  476. begin
  477. result:=TFPObjectList.Create(false);
  478. repeat
  479. if token=_ID then
  480. begin
  481. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  482. include(generictype.symoptions,sp_generic_para);
  483. result.add(generictype);
  484. end;
  485. consume(_ID);
  486. until not try_to_consume(_COMMA) ;
  487. end;
  488. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  489. var
  490. i: longint;
  491. generictype: ttypesym;
  492. st: tsymtable;
  493. begin
  494. def.genericdef:=genericdef;
  495. if not assigned(genericlist) then
  496. exit;
  497. case def.typ of
  498. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  499. arraydef: st:=tarraydef(def).symtable;
  500. procvardef,procdef: st:=tabstractprocdef(def).parast;
  501. else
  502. internalerror(201101020);
  503. end;
  504. for i:=0 to genericlist.count-1 do
  505. begin
  506. generictype:=ttypesym(genericlist[i]);
  507. if generictype.typedef.typ=undefineddef then
  508. include(def.defoptions,df_generic)
  509. else
  510. include(def.defoptions,df_specialization);
  511. st.insert(generictype);
  512. end;
  513. end;
  514. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  515. var
  516. pu : tused_unit;
  517. hmodule : tmodule;
  518. unitsyms : TFPHashObjectList;
  519. sym : tsym;
  520. i : Integer;
  521. begin
  522. if not assigned(genericdef) then
  523. internalerror(200705151);
  524. { Setup symtablestack at definition time
  525. to get types right, however this is not perfect, we should probably record
  526. the resolved symbols }
  527. state.oldsymtablestack:=symtablestack;
  528. state.oldextendeddefs:=current_module.extendeddefs;
  529. current_module.extendeddefs:=TFPHashObjectList.create(true);
  530. symtablestack:=tdefawaresymtablestack.create;
  531. hmodule:=find_module_from_symtable(genericdef.owner);
  532. if hmodule=nil then
  533. internalerror(200705152);
  534. { collect all unit syms in the generic's unit as we need to establish
  535. their unitsym.module link again so that unit identifiers can be used }
  536. unitsyms:=tfphashobjectlist.create(false);
  537. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  538. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  539. begin
  540. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  541. if sym.typ=unitsym then
  542. unitsyms.add(upper(sym.realname),sym);
  543. end;
  544. { add all interface units to the new symtable stack }
  545. pu:=tused_unit(hmodule.used_units.first);
  546. while assigned(pu) do
  547. begin
  548. if not assigned(pu.u.globalsymtable) then
  549. internalerror(200705153);
  550. symtablestack.push(pu.u.globalsymtable);
  551. sym:=tsym(unitsyms.find(pu.u.modulename^));
  552. if assigned(sym) and not assigned(tunitsym(sym).module) then
  553. tunitsym(sym).module:=pu.u;
  554. pu:=tused_unit(pu.next);
  555. end;
  556. unitsyms.free;
  557. if assigned(hmodule.globalsymtable) then
  558. symtablestack.push(hmodule.globalsymtable);
  559. { push the localsymtable if needed }
  560. if (hmodule<>current_module) or not current_module.in_interface then
  561. symtablestack.push(hmodule.localsymtable);
  562. end;
  563. procedure specialization_done(var state: tspecializationstate);
  564. begin
  565. { Restore symtablestack }
  566. current_module.extendeddefs.free;
  567. current_module.extendeddefs:=state.oldextendeddefs;
  568. symtablestack.free;
  569. symtablestack:=state.oldsymtablestack;
  570. { clear the state record to be on the safe side }
  571. fillchar(state, sizeof(state), 0);
  572. end;
  573. end.