pgenutil.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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;
  26. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;parsedtype:tdef;symname:string);
  27. function parse_generic_parameters:TFPObjectList;
  28. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  29. implementation
  30. uses
  31. { common }
  32. cutils,
  33. { global }
  34. globals,tokens,verbose,
  35. { symtable }
  36. symconst,symbase,symsym,symtable,
  37. { modules }
  38. fmodule,
  39. { pass 1 }
  40. htypechk,
  41. node,nobj,
  42. { parser }
  43. scanner,
  44. pbase,pexpr,pdecsub,ptype;
  45. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;parsedtype:tdef;symname:string);
  46. var
  47. st : TSymtable;
  48. srsym : tsym;
  49. pt2 : tnode;
  50. first,
  51. err : boolean;
  52. i,
  53. gencount : longint;
  54. genericdef : tstoreddef;
  55. generictype : ttypesym;
  56. genericdeflist : TFPObjectList;
  57. generictypelist : TFPObjectList;
  58. oldsymtablestack : tsymtablestack;
  59. oldextendeddefs : TFPHashObjectList;
  60. hmodule : tmodule;
  61. pu : tused_unit;
  62. uspecializename,
  63. countstr,genname,ugenname,specializename : string;
  64. vmtbuilder : TVMTBuilder;
  65. onlyparsepara : boolean;
  66. specializest : tsymtable;
  67. item: psymtablestackitem;
  68. begin
  69. { retrieve generic def that we are going to replace }
  70. genericdef:=tstoreddef(tt);
  71. tt:=nil;
  72. onlyparsepara:=false;
  73. { either symname must be given or genericdef needs to be valid }
  74. if (symname='') and
  75. (not assigned(genericdef) or
  76. not assigned(genericdef.typesym) or
  77. (genericdef.typesym.typ<>typesym)) then
  78. internalerror(2011042701);
  79. { only need to record the tokens, then we don't know the type yet ... }
  80. if parse_generic then
  81. begin
  82. { ... but we have to insert a def into the symtable else the deflist
  83. of generic and specialization might not be equally sized which
  84. is later assumed }
  85. tt:=tundefineddef.create;
  86. if parse_class_parent then
  87. tt:=genericdef;
  88. onlyparsepara:=true;
  89. end;
  90. { Only parse the parameters for recovery or
  91. for recording in genericbuf }
  92. if onlyparsepara then
  93. begin
  94. consume(_LSHARPBRACKET);
  95. gencount:=0;
  96. repeat
  97. pt2:=factor(false,true);
  98. pt2.free;
  99. inc(gencount);
  100. until not try_to_consume(_COMMA);
  101. consume(_RSHARPBRACKET);
  102. if parse_generic and parse_class_parent then
  103. begin
  104. if (symname='') and (df_generic in genericdef.defoptions) then
  105. { this happens in non-Delphi modes }
  106. tt:=genericdef
  107. else
  108. begin
  109. { find the corresponding generic symbol so that any checks
  110. done on the returned def will be handled correctly }
  111. str(gencount,countstr);
  112. if symname='' then
  113. genname:=ttypesym(genericdef.typesym).realname
  114. else
  115. genname:=symname;
  116. genname:=symname+'$'+countstr;
  117. ugenname:=upper(genname);
  118. if not searchsym(ugenname,srsym,st) or
  119. (srsym.typ<>typesym) then
  120. begin
  121. identifier_not_found(genname);
  122. exit;
  123. end;
  124. tt:=ttypesym(srsym).typedef;
  125. end;
  126. end;
  127. exit;
  128. end;
  129. if not assigned(parsedtype) and not try_to_consume(_LT) then
  130. consume(_LSHARPBRACKET);
  131. generictypelist:=TFPObjectList.create(false);
  132. genericdeflist:=TFPObjectList.Create(false);
  133. { Parse type parameters }
  134. err:=false;
  135. { if parsedtype is set, then the first type identifer was already parsed
  136. (happens in inline specializations) and thus we only need to parse
  137. the remaining types and do as if the first one was already given }
  138. first:=not assigned(parsedtype);
  139. if assigned(parsedtype) then
  140. begin
  141. genericdeflist.Add(parsedtype);
  142. specializename:='$'+parsedtype.typesym.realname;
  143. end
  144. else
  145. specializename:='';
  146. while not (token in [_GT,_RSHARPBRACKET]) do
  147. begin
  148. if not first then
  149. consume(_COMMA)
  150. else
  151. first:=false;
  152. pt2:=factor(false,true);
  153. if pt2.nodetype=typen then
  154. begin
  155. if df_generic in pt2.resultdef.defoptions then
  156. Message(parser_e_no_generics_as_params);
  157. genericdeflist.Add(pt2.resultdef);
  158. if not assigned(pt2.resultdef.typesym) then
  159. message(type_e_generics_cannot_reference_itself)
  160. else
  161. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  162. end
  163. else
  164. begin
  165. Message(type_e_type_id_expected);
  166. err:=true;
  167. end;
  168. pt2.free;
  169. end;
  170. if err then
  171. begin
  172. try_to_consume(_RSHARPBRACKET);
  173. exit;
  174. end;
  175. { search a generic with the given count of params }
  176. countstr:='';
  177. str(genericdeflist.Count,countstr);
  178. { use the name of the symbol as procvars return a user friendly version
  179. of the name }
  180. if symname='' then
  181. genname:=ttypesym(genericdef.typesym).realname
  182. else
  183. genname:=symname;
  184. { in case of non-Delphi mode the type name could already be a generic
  185. def (but maybe the wrong one) }
  186. if assigned(genericdef) and (df_generic in genericdef.defoptions) then
  187. begin
  188. { remove the type count suffix from the generic's name }
  189. for i:=Length(genname) downto 1 do
  190. if genname[i]='$' then
  191. begin
  192. genname:=copy(genname,1,i-1);
  193. break;
  194. end;
  195. end;
  196. genname:=genname+'$'+countstr;
  197. ugenname:=upper(genname);
  198. if not searchsym(ugenname,srsym,st)
  199. or (srsym.typ<>typesym) then
  200. begin
  201. identifier_not_found(genname);
  202. genericdeflist.Free;
  203. generictypelist.Free;
  204. exit;
  205. end;
  206. { we've found the correct def }
  207. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  208. { build the new type's name }
  209. specializename:=genname+specializename;
  210. uspecializename:=upper(specializename);
  211. { select the symtable containing the params }
  212. case genericdef.typ of
  213. procdef:
  214. st:=genericdef.GetSymtable(gs_para);
  215. objectdef,
  216. recorddef:
  217. st:=genericdef.GetSymtable(gs_record);
  218. arraydef:
  219. st:=tarraydef(genericdef).symtable;
  220. procvardef:
  221. st:=genericdef.GetSymtable(gs_para);
  222. else
  223. internalerror(200511182);
  224. end;
  225. { build the list containing the types for the generic params }
  226. gencount:=0;
  227. for i:=0 to st.SymList.Count-1 do
  228. begin
  229. srsym:=tsym(st.SymList[i]);
  230. if sp_generic_para in srsym.symoptions then
  231. begin
  232. if gencount=genericdeflist.Count then
  233. internalerror(2011042702);
  234. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  235. generictypelist.add(generictype);
  236. inc(gencount);
  237. end;
  238. end;
  239. { Special case if we are referencing the current defined object }
  240. if assigned(current_structdef) and
  241. (current_structdef.objname^=uspecializename) then
  242. tt:=current_structdef;
  243. { for units specializations can already be needed in the interface, therefor we
  244. will use the global symtable. Programs don't have a globalsymtable and there we
  245. use the localsymtable }
  246. if current_module.is_unit then
  247. specializest:=current_module.globalsymtable
  248. else
  249. specializest:=current_module.localsymtable;
  250. { Can we reuse an already specialized type? }
  251. if not assigned(tt) then
  252. begin
  253. srsym:=tsym(specializest.find(uspecializename));
  254. if assigned(srsym) then
  255. begin
  256. if srsym.typ<>typesym then
  257. internalerror(200710171);
  258. tt:=ttypesym(srsym).typedef;
  259. end;
  260. end;
  261. if not assigned(tt) then
  262. begin
  263. { Setup symtablestack at definition time
  264. to get types right, however this is not perfect, we should probably record
  265. the resolved symbols }
  266. oldsymtablestack:=symtablestack;
  267. oldextendeddefs:=current_module.extendeddefs;
  268. current_module.extendeddefs:=TFPHashObjectList.create(true);
  269. symtablestack:=tdefawaresymtablestack.create;
  270. if not assigned(genericdef) then
  271. internalerror(200705151);
  272. hmodule:=find_module_from_symtable(genericdef.owner);
  273. if hmodule=nil then
  274. internalerror(200705152);
  275. pu:=tused_unit(hmodule.used_units.first);
  276. while assigned(pu) do
  277. begin
  278. if not assigned(pu.u.globalsymtable) then
  279. internalerror(200705153);
  280. symtablestack.push(pu.u.globalsymtable);
  281. pu:=tused_unit(pu.next);
  282. end;
  283. if assigned(hmodule.globalsymtable) then
  284. symtablestack.push(hmodule.globalsymtable);
  285. { hacky, but necessary to insert the newly generated class properly }
  286. item:=oldsymtablestack.stack;
  287. while assigned(item) and (item^.symtable.symtablelevel>main_program_level) do
  288. item:=item^.next;
  289. if assigned(item) and (item^.symtable<>symtablestack.top) then
  290. symtablestack.push(item^.symtable);
  291. { Reparse the original type definition }
  292. if not err then
  293. begin
  294. { First a new typesym so we can reuse this specialization and
  295. references to this specialization can be handled }
  296. srsym:=ttypesym.create(specializename,generrordef);
  297. specializest.insert(srsym);
  298. if not assigned(genericdef.generictokenbuf) then
  299. internalerror(200511171);
  300. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  301. read_named_type(tt,specializename,genericdef,generictypelist,false);
  302. ttypesym(srsym).typedef:=tt;
  303. tt.typesym:=srsym;
  304. { Note regarding hint directives:
  305. There is no need to remove the flags for them from the
  306. specialized generic symbol, because hint directives that
  307. follow the specialization are handled by the code in
  308. pdecl.types_dec and added to the type symbol.
  309. E.g.: TFoo = TBar<Blubb> deprecated;
  310. Here the symbol TBar$1$Blubb will contain the
  311. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  312. case tt.typ of
  313. { Build VMT indexes for classes and read hint directives }
  314. objectdef:
  315. begin
  316. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  317. consume(_SEMICOLON);
  318. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  319. vmtbuilder.generate_vmt;
  320. vmtbuilder.free;
  321. end;
  322. { handle params, calling convention, etc }
  323. procvardef:
  324. begin
  325. if not check_proc_directive(true) then
  326. begin
  327. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  328. consume(_SEMICOLON);
  329. end;
  330. parse_var_proc_directives(ttypesym(srsym));
  331. handle_calling_convention(tprocvardef(tt));
  332. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  333. consume(_SEMICOLON);
  334. end;
  335. else
  336. { parse hint directives for records and arrays }
  337. begin
  338. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  339. consume(_SEMICOLON);
  340. end;
  341. end;
  342. { Consume the semicolon if it is also recorded }
  343. try_to_consume(_SEMICOLON);
  344. end;
  345. { Restore symtablestack }
  346. current_module.extendeddefs.free;
  347. current_module.extendeddefs:=oldextendeddefs;
  348. symtablestack.free;
  349. symtablestack:=oldsymtablestack;
  350. end
  351. else
  352. begin
  353. { There is comment few lines before ie 200512115
  354. saying "We are parsing the same objectdef, the def index numbers
  355. are the same". This is wrong (index numbers are not same)
  356. in case there is specialization (S2 in this case) inside
  357. specialized generic (G2 in this case) which is equal to
  358. some previous specialization (S1 in this case). In that case,
  359. new symbol is not added to currently specialized type
  360. (S in this case) for that specializations (S2 in this case),
  361. and this results in that specialization and generic definition
  362. don't have same number of elements in their object symbol tables.
  363. This patch adds undefined def to ensure that those
  364. two symbol tables will have same number of elements.
  365. }
  366. tundefineddef.create;
  367. end;
  368. if not (token in [_GT, _RSHARPBRACKET]) then
  369. begin
  370. consume(_RSHARPBRACKET);
  371. exit;
  372. end
  373. else
  374. consume(token);
  375. genericdeflist.free;
  376. generictypelist.free;
  377. if assigned(genericdef) then
  378. begin
  379. { check the hints of the found generic symbol }
  380. srsym:=genericdef.typesym;
  381. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  382. end;
  383. end;
  384. function parse_generic_parameters:TFPObjectList;
  385. var
  386. generictype : ttypesym;
  387. begin
  388. result:=TFPObjectList.Create(false);
  389. repeat
  390. if token=_ID then
  391. begin
  392. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  393. include(generictype.symoptions,sp_generic_para);
  394. result.add(generictype);
  395. end;
  396. consume(_ID);
  397. until not try_to_consume(_COMMA) ;
  398. end;
  399. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  400. var
  401. i: longint;
  402. generictype: ttypesym;
  403. st: tsymtable;
  404. begin
  405. def.genericdef:=genericdef;
  406. if not assigned(genericlist) then
  407. exit;
  408. case def.typ of
  409. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  410. arraydef: st:=tarraydef(def).symtable;
  411. procvardef,procdef: st:=tabstractprocdef(def).parast;
  412. else
  413. internalerror(201101020);
  414. end;
  415. for i:=0 to genericlist.count-1 do
  416. begin
  417. generictype:=ttypesym(genericlist[i]);
  418. if generictype.typedef.typ=undefineddef then
  419. include(def.defoptions,df_generic)
  420. else
  421. include(def.defoptions,df_specialization);
  422. st.insert(generictype);
  423. end;
  424. end;
  425. end.