pgenutil.pas 17 KB

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