ptype.pas 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing types for Free Pascal
  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 ptype;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,
  22. symtype,symdef,symbase;
  23. type
  24. TSingleTypeOption=(
  25. stoIsForwardDef, { foward declaration }
  26. stoAllowTypeDef, { allow type definitions }
  27. stoAllowSpecialization, { allow type specialization }
  28. stoParseClassParent { parse of parent class type }
  29. );
  30. TSingleTypeOptions=set of TSingleTypeOption;
  31. procedure resolve_forward_types;
  32. { reads a type identifier }
  33. procedure id_type(var def : tdef;isforwarddef:boolean);
  34. { reads a string, file type or a type identifier }
  35. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  36. { reads any type declaration, where the resulting type will get name as type identifier }
  37. procedure read_named_type(var def:tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  38. { reads any type declaration }
  39. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  40. { generate persistent type information like VMT, RTTI and inittables }
  41. procedure write_persistent_type_info(st:tsymtable);
  42. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean);
  43. implementation
  44. uses
  45. { common }
  46. cutils,
  47. { global }
  48. globals,tokens,verbose,constexp,
  49. systems,
  50. { target }
  51. paramgr,procinfo,
  52. { symtable }
  53. symconst,symsym,symtable,
  54. defutil,defcmp,
  55. { modules }
  56. fmodule,
  57. { pass 1 }
  58. node,ncgrtti,nobj,
  59. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  60. { parser }
  61. scanner,
  62. pbase,pexpr,pdecsub,pdecvar,pdecobj,pdecl;
  63. procedure resolve_forward_types;
  64. var
  65. i: longint;
  66. hpd,
  67. def : tdef;
  68. srsym : tsym;
  69. srsymtable : TSymtable;
  70. hs : string;
  71. begin
  72. for i:=0 to current_module.checkforwarddefs.Count-1 do
  73. begin
  74. def:=tdef(current_module.checkforwarddefs[i]);
  75. case def.typ of
  76. pointerdef,
  77. classrefdef :
  78. begin
  79. { classrefdef inherits from pointerdef }
  80. hpd:=tabstractpointerdef(def).pointeddef;
  81. { still a forward def ? }
  82. if hpd.typ=forwarddef then
  83. begin
  84. { try to resolve the forward }
  85. if not assigned(tforwarddef(hpd).tosymname) then
  86. internalerror(200211201);
  87. hs:=tforwarddef(hpd).tosymname^;
  88. searchsym(upper(hs),srsym,srsymtable);
  89. { we don't need the forwarddef anymore, dispose it }
  90. hpd.free;
  91. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  92. { was a type sym found ? }
  93. if assigned(srsym) and
  94. (srsym.typ=typesym) then
  95. begin
  96. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  97. { avoid wrong unused warnings web bug 801 PM }
  98. inc(ttypesym(srsym).refs);
  99. { we need a class type for classrefdef }
  100. if (def.typ=classrefdef) and
  101. not(is_class(ttypesym(srsym).typedef)) and
  102. not(is_objcclass(ttypesym(srsym).typedef)) then
  103. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  104. end
  105. else
  106. begin
  107. Message1(sym_e_forward_type_not_resolved,hs);
  108. { try to recover }
  109. tabstractpointerdef(def).pointeddef:=generrordef;
  110. end;
  111. end;
  112. end;
  113. objectdef :
  114. begin
  115. { give an error as the implementation may follow in an
  116. other type block which is allowed by FPC modes }
  117. if not(m_fpc in current_settings.modeswitches) and
  118. (oo_is_forward in tobjectdef(def).objectoptions) then
  119. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  120. end;
  121. else
  122. internalerror(200811071);
  123. end;
  124. end;
  125. current_module.checkforwarddefs.clear;
  126. end;
  127. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean);
  128. var
  129. st : TSymtable;
  130. srsym : tsym;
  131. pt2 : tnode;
  132. first,
  133. err : boolean;
  134. i : longint;
  135. sym : tsym;
  136. genericdef : tstoreddef;
  137. generictype : ttypesym;
  138. generictypelist : TFPObjectList;
  139. oldsymtablestack : tsymtablestack;
  140. hmodule : tmodule;
  141. pu : tused_unit;
  142. uspecializename,
  143. specializename : string;
  144. vmtbuilder : TVMTBuilder;
  145. onlyparsepara : boolean;
  146. specializest : tsymtable;
  147. item: psymtablestackitem;
  148. begin
  149. { retrieve generic def that we are going to replace }
  150. genericdef:=tstoreddef(tt);
  151. tt:=nil;
  152. onlyparsepara:=false;
  153. if not(df_generic in genericdef.defoptions) then
  154. begin
  155. Message(parser_e_special_onlygenerics);
  156. tt:=generrordef;
  157. onlyparsepara:=true;
  158. end;
  159. { only need to record the tokens, then we don't know the type yet ... }
  160. if parse_generic then
  161. begin
  162. { ... but we have to insert a def into the symtable else the deflist
  163. of generic and specialization might not be equally sized which
  164. is later assumed }
  165. tt:=tundefineddef.create;
  166. if parse_class_parent then
  167. tt:=genericdef;
  168. onlyparsepara:=true;
  169. end;
  170. { Only parse the parameters for recovery or
  171. for recording in genericbuf }
  172. if onlyparsepara then
  173. begin
  174. consume(_LSHARPBRACKET);
  175. repeat
  176. pt2:=factor(false,true);
  177. pt2.free;
  178. until not try_to_consume(_COMMA);
  179. consume(_RSHARPBRACKET);
  180. exit;
  181. end;
  182. if not try_to_consume(_LT) then
  183. consume(_LSHARPBRACKET);
  184. { Parse generic parameters, for each undefineddef in the symtable of
  185. the genericdef we need to have a new def }
  186. err:=false;
  187. first:=true;
  188. generictypelist:=TFPObjectList.create(false);
  189. case genericdef.typ of
  190. procdef:
  191. st:=genericdef.GetSymtable(gs_para);
  192. objectdef,
  193. recorddef:
  194. st:=genericdef.GetSymtable(gs_record);
  195. arraydef:
  196. st:=tarraydef(genericdef).symtable;
  197. procvardef:
  198. st:=genericdef.GetSymtable(gs_para);
  199. else
  200. internalerror(200511182);
  201. end;
  202. { Parse type parameters }
  203. if not assigned(genericdef.typesym) then
  204. internalerror(200710173);
  205. specializename:=genericdef.typesym.realname;
  206. for i:=0 to st.SymList.Count-1 do
  207. begin
  208. sym:=tsym(st.SymList[i]);
  209. if (sp_generic_para in sym.symoptions) then
  210. begin
  211. if not first then
  212. consume(_COMMA)
  213. else
  214. first:=false;
  215. pt2:=factor(false,true);
  216. if pt2.nodetype=typen then
  217. begin
  218. if df_generic in pt2.resultdef.defoptions then
  219. Message(parser_e_no_generics_as_params);
  220. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  221. generictypelist.add(generictype);
  222. if not assigned(pt2.resultdef.typesym) then
  223. message(type_e_generics_cannot_reference_itself)
  224. else
  225. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  226. end
  227. else
  228. begin
  229. Message(type_e_type_id_expected);
  230. err:=true;
  231. end;
  232. pt2.free;
  233. end;
  234. end;
  235. uspecializename:=upper(specializename);
  236. { force correct error location if too much type parameters are passed }
  237. if not (token in [_RSHARPBRACKET,_GT]) then
  238. consume(_RSHARPBRACKET);
  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. symtablestack:=tsymtablestack.create;
  268. if not assigned(genericdef) then
  269. internalerror(200705151);
  270. hmodule:=find_module_from_symtable(genericdef.owner);
  271. if hmodule=nil then
  272. internalerror(200705152);
  273. pu:=tused_unit(hmodule.used_units.first);
  274. while assigned(pu) do
  275. begin
  276. if not assigned(pu.u.globalsymtable) then
  277. internalerror(200705153);
  278. symtablestack.push(pu.u.globalsymtable);
  279. pu:=tused_unit(pu.next);
  280. end;
  281. if assigned(hmodule.globalsymtable) then
  282. symtablestack.push(hmodule.globalsymtable);
  283. { hacky, but necessary to insert the newly generated class properly }
  284. item:=oldsymtablestack.stack;
  285. while assigned(item) and (item^.symtable.symtablelevel>main_program_level) do
  286. item:=item^.next;
  287. if assigned(item) and (item^.symtable<>symtablestack.top) then
  288. symtablestack.push(item^.symtable);
  289. { Reparse the original type definition }
  290. if not err then
  291. begin
  292. { First a new typesym so we can reuse this specialization and
  293. references to this specialization can be handled }
  294. srsym:=ttypesym.create(specializename,generrordef);
  295. specializest.insert(srsym);
  296. if not assigned(genericdef.generictokenbuf) then
  297. internalerror(200511171);
  298. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  299. read_named_type(tt,specializename,genericdef,generictypelist,false);
  300. ttypesym(srsym).typedef:=tt;
  301. tt.typesym:=srsym;
  302. case tt.typ of
  303. { Build VMT indexes for classes }
  304. objectdef:
  305. begin
  306. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  307. vmtbuilder.generate_vmt;
  308. vmtbuilder.free;
  309. end;
  310. { handle params, calling convention, etc }
  311. procvardef:
  312. begin
  313. if not check_proc_directive(true) then
  314. begin
  315. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  316. consume(_SEMICOLON);
  317. end;
  318. parse_var_proc_directives(ttypesym(srsym));
  319. handle_calling_convention(tprocvardef(tt));
  320. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  321. consume(_SEMICOLON);
  322. end;
  323. end;
  324. { Consume the semicolon if it is also recorded }
  325. try_to_consume(_SEMICOLON);
  326. end;
  327. { Restore symtablestack }
  328. symtablestack.free;
  329. symtablestack:=oldsymtablestack;
  330. end
  331. else
  332. begin
  333. { There is comment few lines before ie 200512115
  334. saying "We are parsing the same objectdef, the def index numbers
  335. are the same". This is wrong (index numbers are not same)
  336. in case there is specialization (S2 in this case) inside
  337. specialized generic (G2 in this case) which is equal to
  338. some previous specialization (S1 in this case). In that case,
  339. new symbol is not added to currently specialized type
  340. (S in this case) for that specializations (S2 in this case),
  341. and this results in that specialization and generic definition
  342. don't have same number of elements in their object symbol tables.
  343. This patch adds undefined def to ensure that those
  344. two symbol tables will have same number of elements.
  345. }
  346. tundefineddef.create;
  347. end;
  348. generictypelist.free;
  349. if not try_to_consume(_GT) then
  350. consume(_RSHARPBRACKET);
  351. end;
  352. procedure id_type(var def : tdef;isforwarddef:boolean);
  353. { reads a type definition }
  354. { to a appropriating tdef, s gets the name of }
  355. { the type to allow name mangling }
  356. var
  357. is_unit_specific : boolean;
  358. pos : tfileposinfo;
  359. srsym : tsym;
  360. srsymtable : TSymtable;
  361. s,sorg : TIDString;
  362. t : ttoken;
  363. structdef : tabstractrecorddef;
  364. begin
  365. s:=pattern;
  366. sorg:=orgpattern;
  367. pos:=current_tokenpos;
  368. { use of current parsed object:
  369. - classes can be used also in classes
  370. - objects can be parameters }
  371. structdef:=current_structdef;
  372. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  373. begin
  374. if (structdef.objname^=pattern) then
  375. begin
  376. consume(_ID);
  377. def:=structdef;
  378. exit;
  379. end;
  380. structdef:=tabstractrecorddef(structdef.owner.defowner);
  381. end;
  382. { Use the special searchsym_type that ignores records and parameters }
  383. searchsym_type(s,srsym,srsymtable);
  384. { handle unit specification like System.Writeln }
  385. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t);
  386. consume(t);
  387. { Types are first defined with an error def before assigning
  388. the real type so check if it's an errordef. if so then
  389. give an error. Only check for typesyms in the current symbol
  390. table as forwarddef are not resolved directly }
  391. if assigned(srsym) and
  392. (srsym.typ=typesym) and
  393. (ttypesym(srsym).typedef.typ=errordef) then
  394. begin
  395. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  396. def:=generrordef;
  397. exit;
  398. end;
  399. { are we parsing a possible forward def ? }
  400. if isforwarddef and
  401. not(is_unit_specific) then
  402. begin
  403. def:=tforwarddef.create(sorg,pos);
  404. exit;
  405. end;
  406. { unknown sym ? }
  407. if not assigned(srsym) then
  408. begin
  409. Message1(sym_e_id_not_found,sorg);
  410. def:=generrordef;
  411. exit;
  412. end;
  413. { type sym ? }
  414. if (srsym.typ<>typesym) then
  415. begin
  416. Message(type_e_type_id_expected);
  417. def:=generrordef;
  418. exit;
  419. end;
  420. { Give an error when referring to an errordef }
  421. if (ttypesym(srsym).typedef.typ=errordef) then
  422. begin
  423. Message(sym_e_error_in_type_def);
  424. def:=generrordef;
  425. exit;
  426. end;
  427. def:=ttypesym(srsym).typedef;
  428. end;
  429. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  430. var
  431. t2 : tdef;
  432. dospecialize,
  433. again : boolean;
  434. begin
  435. dospecialize:=false;
  436. repeat
  437. again:=false;
  438. case token of
  439. _STRING:
  440. string_dec(def,stoAllowTypeDef in options);
  441. _FILE:
  442. begin
  443. consume(_FILE);
  444. if (token=_OF) then
  445. begin
  446. if not(stoAllowTypeDef in options) then
  447. Message(parser_e_no_local_para_def);
  448. consume(_OF);
  449. single_type(t2,[]);
  450. if is_managed_type(t2) then
  451. Message(parser_e_no_refcounted_typed_file);
  452. def:=tfiledef.createtyped(t2);
  453. end
  454. else
  455. def:=cfiletype;
  456. end;
  457. _ID:
  458. begin
  459. if try_to_consume(_SPECIALIZE) then
  460. begin
  461. if ([stoAllowSpecialization,stoAllowTypeDef] * options = []) then
  462. begin
  463. Message(parser_e_no_local_para_def);
  464. { try to recover }
  465. while token<>_SEMICOLON do
  466. consume(token);
  467. def:=generrordef;
  468. end
  469. else
  470. begin
  471. dospecialize:=true;
  472. again:=true;
  473. end;
  474. end
  475. else
  476. begin
  477. id_type(def,stoIsForwardDef in options);
  478. { handle types inside classes, e.g. TNode.TLongint }
  479. while (token=_POINT) do
  480. begin
  481. if parse_generic then
  482. begin
  483. consume(_POINT);
  484. consume(_ID);
  485. end
  486. else if is_class_or_object(def) or is_record(def) then
  487. begin
  488. symtablestack.push(tabstractrecorddef(def).symtable);
  489. consume(_POINT);
  490. id_type(t2,stoIsForwardDef in options);
  491. symtablestack.pop(tabstractrecorddef(def).symtable);
  492. def:=t2;
  493. end
  494. else
  495. break;
  496. end;
  497. end;
  498. end;
  499. else
  500. begin
  501. message(type_e_type_id_expected);
  502. def:=generrordef;
  503. end;
  504. end;
  505. until not again;
  506. if ([stoAllowSpecialization,stoAllowTypeDef] * options <> []) and
  507. (m_delphi in current_settings.modeswitches) then
  508. dospecialize:=token=_LSHARPBRACKET;
  509. if dospecialize then
  510. generate_specialization(def,stoParseClassParent in options)
  511. else
  512. begin
  513. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  514. begin
  515. def:=current_specializedef
  516. end
  517. else if (def=current_genericdef) then
  518. begin
  519. def:=current_genericdef
  520. end
  521. else if (df_generic in def.defoptions) then
  522. begin
  523. Message(parser_e_no_generics_as_types);
  524. def:=generrordef;
  525. end
  526. else if is_objccategory(def) then
  527. begin
  528. Message(parser_e_no_category_as_types);
  529. def:=generrordef
  530. end
  531. end;
  532. end;
  533. procedure parse_record_members;
  534. procedure maybe_parse_hint_directives(pd:tprocdef);
  535. var
  536. dummysymoptions : tsymoptions;
  537. deprecatedmsg : pshortstring;
  538. begin
  539. dummysymoptions:=[];
  540. deprecatedmsg:=nil;
  541. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  542. Consume(_SEMICOLON);
  543. if assigned(pd) then
  544. begin
  545. pd.symoptions:=pd.symoptions+dummysymoptions;
  546. pd.deprecatedmsg:=deprecatedmsg;
  547. end
  548. else
  549. stringdispose(deprecatedmsg);
  550. end;
  551. var
  552. pd : tprocdef;
  553. oldparse_only: boolean;
  554. member_blocktype : tblock_type;
  555. fields_allowed, is_classdef, classfields: boolean;
  556. vdoptions: tvar_dec_options;
  557. begin
  558. { empty record declaration ? }
  559. if (token=_SEMICOLON) then
  560. Exit;
  561. current_structdef.symtable.currentvisibility:=vis_public;
  562. fields_allowed:=true;
  563. is_classdef:=false;
  564. classfields:=false;
  565. member_blocktype:=bt_general;
  566. repeat
  567. case token of
  568. _TYPE :
  569. begin
  570. consume(_TYPE);
  571. member_blocktype:=bt_type;
  572. end;
  573. _VAR :
  574. begin
  575. consume(_VAR);
  576. fields_allowed:=true;
  577. member_blocktype:=bt_general;
  578. classfields:=is_classdef;
  579. is_classdef:=false;
  580. end;
  581. _CONST:
  582. begin
  583. consume(_CONST);
  584. member_blocktype:=bt_const;
  585. end;
  586. _ID, _CASE, _OPERATOR :
  587. begin
  588. case idtoken of
  589. _PRIVATE :
  590. begin
  591. consume(_PRIVATE);
  592. current_structdef.symtable.currentvisibility:=vis_private;
  593. include(current_structdef.objectoptions,oo_has_private);
  594. fields_allowed:=true;
  595. is_classdef:=false;
  596. classfields:=false;
  597. member_blocktype:=bt_general;
  598. end;
  599. _PROTECTED :
  600. begin
  601. consume(_PROTECTED);
  602. current_structdef.symtable.currentvisibility:=vis_protected;
  603. include(current_structdef.objectoptions,oo_has_protected);
  604. fields_allowed:=true;
  605. is_classdef:=false;
  606. classfields:=false;
  607. member_blocktype:=bt_general;
  608. end;
  609. _PUBLIC :
  610. begin
  611. consume(_PUBLIC);
  612. current_structdef.symtable.currentvisibility:=vis_public;
  613. fields_allowed:=true;
  614. is_classdef:=false;
  615. classfields:=false;
  616. member_blocktype:=bt_general;
  617. end;
  618. _PUBLISHED :
  619. begin
  620. Message(parser_e_no_record_published);
  621. consume(_PUBLISHED);
  622. current_structdef.symtable.currentvisibility:=vis_published;
  623. fields_allowed:=true;
  624. is_classdef:=false;
  625. classfields:=false;
  626. member_blocktype:=bt_general;
  627. end;
  628. _STRICT :
  629. begin
  630. consume(_STRICT);
  631. if token=_ID then
  632. begin
  633. case idtoken of
  634. _PRIVATE:
  635. begin
  636. consume(_PRIVATE);
  637. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  638. include(current_structdef.objectoptions,oo_has_strictprivate);
  639. end;
  640. _PROTECTED:
  641. begin
  642. consume(_PROTECTED);
  643. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  644. include(current_structdef.objectoptions,oo_has_strictprotected);
  645. end;
  646. else
  647. message(parser_e_protected_or_private_expected);
  648. end;
  649. end
  650. else
  651. message(parser_e_protected_or_private_expected);
  652. fields_allowed:=true;
  653. is_classdef:=false;
  654. classfields:=false;
  655. member_blocktype:=bt_general;
  656. end
  657. else
  658. if is_classdef and (idtoken=_OPERATOR) then
  659. begin
  660. oldparse_only:=parse_only;
  661. parse_only:=true;
  662. pd:=parse_proc_dec(is_classdef,current_structdef);
  663. { this is for error recovery as well as forward }
  664. { interface mappings, i.e. mapping to a method }
  665. { which isn't declared yet }
  666. if assigned(pd) then
  667. begin
  668. parse_record_proc_directives(pd);
  669. handle_calling_convention(pd);
  670. { add definition to procsym }
  671. proc_add_definition(pd);
  672. end;
  673. maybe_parse_hint_directives(pd);
  674. parse_only:=oldparse_only;
  675. fields_allowed:=false;
  676. is_classdef:=false;
  677. end
  678. else
  679. begin
  680. if member_blocktype=bt_general then
  681. begin
  682. if (not fields_allowed) then
  683. Message(parser_e_field_not_allowed_here);
  684. vdoptions:=[vd_record];
  685. if classfields then
  686. include(vdoptions,vd_class);
  687. read_record_fields(vdoptions);
  688. end
  689. else if member_blocktype=bt_type then
  690. types_dec(true)
  691. else if member_blocktype=bt_const then
  692. consts_dec(true)
  693. else
  694. internalerror(201001110);
  695. end;
  696. end;
  697. end;
  698. _PROPERTY :
  699. begin
  700. struct_property_dec(is_classdef);
  701. fields_allowed:=false;
  702. is_classdef:=false;
  703. end;
  704. _CLASS:
  705. begin
  706. is_classdef:=false;
  707. { read class method/field/property }
  708. consume(_CLASS);
  709. { class modifier is only allowed for procedures, functions, }
  710. { constructors, destructors, fields and properties }
  711. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  712. not((token=_ID) and (idtoken=_OPERATOR)) then
  713. Message(parser_e_procedure_or_function_expected);
  714. is_classdef:=true;
  715. end;
  716. _PROCEDURE,
  717. _FUNCTION:
  718. begin
  719. oldparse_only:=parse_only;
  720. parse_only:=true;
  721. pd:=parse_proc_dec(is_classdef,current_structdef);
  722. { this is for error recovery as well as forward }
  723. { interface mappings, i.e. mapping to a method }
  724. { which isn't declared yet }
  725. if assigned(pd) then
  726. begin
  727. parse_record_proc_directives(pd);
  728. { since records have no inheritance don't allow non static
  729. class methods. delphi do so. }
  730. if is_classdef and not (po_staticmethod in pd.procoptions) then
  731. MessagePos(pd.fileinfo, parser_e_class_methods_only_static_in_records);
  732. handle_calling_convention(pd);
  733. { add definition to procsym }
  734. proc_add_definition(pd);
  735. end;
  736. maybe_parse_hint_directives(pd);
  737. parse_only:=oldparse_only;
  738. fields_allowed:=false;
  739. is_classdef:=false;
  740. end;
  741. _CONSTRUCTOR :
  742. begin
  743. if not is_classdef then
  744. Message(parser_e_no_constructor_in_records);
  745. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  746. Message(parser_w_constructor_should_be_public);
  747. { only 1 class constructor is allowed }
  748. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  749. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  750. oldparse_only:=parse_only;
  751. parse_only:=true;
  752. if is_classdef then
  753. pd:=class_constructor_head
  754. else
  755. pd:=constructor_head;
  756. parse_record_proc_directives(pd);
  757. handle_calling_convention(pd);
  758. { add definition to procsym }
  759. proc_add_definition(pd);
  760. maybe_parse_hint_directives(pd);
  761. parse_only:=oldparse_only;
  762. fields_allowed:=false;
  763. is_classdef:=false;
  764. end;
  765. _DESTRUCTOR :
  766. begin
  767. if not is_classdef then
  768. Message(parser_e_no_destructor_in_records);
  769. { only 1 class destructor is allowed }
  770. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  771. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  772. oldparse_only:=parse_only;
  773. parse_only:=true;
  774. if is_classdef then
  775. pd:=class_destructor_head
  776. else
  777. pd:=destructor_head;
  778. parse_record_proc_directives(pd);
  779. handle_calling_convention(pd);
  780. { add definition to procsym }
  781. proc_add_definition(pd);
  782. maybe_parse_hint_directives(pd);
  783. parse_only:=oldparse_only;
  784. fields_allowed:=false;
  785. is_classdef:=false;
  786. end;
  787. _END :
  788. begin
  789. consume(_END);
  790. break;
  791. end;
  792. else
  793. consume(_ID); { Give a ident expected message, like tp7 }
  794. end;
  795. until false;
  796. end;
  797. { reads a record declaration }
  798. function record_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  799. var
  800. old_current_structdef: tabstractrecorddef;
  801. old_current_genericdef,
  802. old_current_specializedef: tstoreddef;
  803. old_parse_generic: boolean;
  804. recst: trecordsymtable;
  805. begin
  806. old_current_structdef:=current_structdef;
  807. old_current_genericdef:=current_genericdef;
  808. old_current_specializedef:=current_specializedef;
  809. old_parse_generic:=parse_generic;
  810. current_genericdef:=nil;
  811. current_specializedef:=nil;
  812. { create recdef }
  813. recst:=trecordsymtable.create(n,current_settings.packrecords);
  814. current_structdef:=trecorddef.create(n,recst);
  815. result:=current_structdef;
  816. { insert in symtablestack }
  817. symtablestack.push(recst);
  818. { parse record }
  819. consume(_RECORD);
  820. { usage of specialized type inside its generic template }
  821. if assigned(genericdef) then
  822. current_specializedef:=current_structdef
  823. { reject declaration of generic class inside generic class }
  824. else if assigned(genericlist) then
  825. current_genericdef:=current_structdef;
  826. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  827. parse_generic:=(df_generic in current_structdef.defoptions);
  828. if m_advanced_records in current_settings.modeswitches then
  829. parse_record_members
  830. else
  831. begin
  832. read_record_fields([vd_record]);
  833. consume(_END);
  834. end;
  835. { make the record size aligned }
  836. recst.addalignmentpadding;
  837. { restore symtable stack }
  838. symtablestack.pop(recst);
  839. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  840. Message(type_e_no_packed_inittable);
  841. { restore old state }
  842. parse_generic:=old_parse_generic;
  843. current_structdef:=old_current_structdef;
  844. current_genericdef:=old_current_genericdef;
  845. current_specializedef:=old_current_specializedef;
  846. end;
  847. { reads a type definition and returns a pointer to it }
  848. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  849. var
  850. pt : tnode;
  851. tt2 : tdef;
  852. aktenumdef : tenumdef;
  853. s : TIDString;
  854. l,v : TConstExprInt;
  855. oldpackrecords : longint;
  856. defpos,storepos : tfileposinfo;
  857. procedure expr_type;
  858. var
  859. pt1,pt2 : tnode;
  860. lv,hv : TConstExprInt;
  861. old_block_type : tblock_type;
  862. dospecialize : boolean;
  863. structdef: tabstractrecorddef;
  864. begin
  865. old_block_type:=block_type;
  866. dospecialize:=false;
  867. { use of current parsed object:
  868. - classes can be used also in classes
  869. - objects can be parameters }
  870. if (token=_ID) then
  871. begin
  872. structdef:=current_structdef;
  873. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  874. begin
  875. if (structdef.objname^=pattern) then
  876. begin
  877. consume(_ID);
  878. def:=structdef;
  879. exit;
  880. end;
  881. structdef:=tabstractrecorddef(structdef.owner.defowner);
  882. end;
  883. end;
  884. { Generate a specialization in FPC mode? }
  885. dospecialize:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE);
  886. { we can't accept a equal in type }
  887. pt1:=comp_expr(false,true);
  888. if not dospecialize and
  889. try_to_consume(_POINTPOINT) then
  890. begin
  891. { get high value of range }
  892. pt2:=comp_expr(false,false);
  893. { make both the same type or give an error. This is not
  894. done when both are integer values, because typecasting
  895. between -3200..3200 will result in a signed-unsigned
  896. conflict and give a range check error (PFV) }
  897. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  898. inserttypeconv(pt1,pt2.resultdef);
  899. { both must be evaluated to constants now }
  900. if (pt1.nodetype=ordconstn) and
  901. (pt2.nodetype=ordconstn) then
  902. begin
  903. lv:=tordconstnode(pt1).value;
  904. hv:=tordconstnode(pt2).value;
  905. { Check bounds }
  906. if hv<lv then
  907. message(parser_e_upper_lower_than_lower)
  908. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  909. message(type_e_cant_eval_constant_expr)
  910. else
  911. begin
  912. { All checks passed, create the new def }
  913. case pt1.resultdef.typ of
  914. enumdef :
  915. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  916. orddef :
  917. begin
  918. if is_char(pt1.resultdef) then
  919. def:=torddef.create(uchar,lv,hv)
  920. else
  921. if is_boolean(pt1.resultdef) then
  922. def:=torddef.create(pasbool,lv,hv)
  923. else if is_signed(pt1.resultdef) then
  924. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  925. else
  926. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  927. end;
  928. end;
  929. end;
  930. end
  931. else
  932. Message(sym_e_error_in_type_def);
  933. pt2.free;
  934. end
  935. else
  936. begin
  937. { a simple type renaming or generic specialization }
  938. if (pt1.nodetype=typen) then
  939. begin
  940. def:=ttypenode(pt1).resultdef;
  941. { Delphi mode specialization? }
  942. if (m_delphi in current_settings.modeswitches) then
  943. dospecialize:=token=_LSHARPBRACKET;
  944. if dospecialize then
  945. generate_specialization(def,false)
  946. else
  947. begin
  948. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  949. begin
  950. def:=current_specializedef
  951. end
  952. else if (def=current_genericdef) then
  953. begin
  954. def:=current_genericdef
  955. end
  956. else if (df_generic in def.defoptions) then
  957. begin
  958. Message(parser_e_no_generics_as_types);
  959. def:=generrordef;
  960. end
  961. else if is_objccategory(def) then
  962. begin
  963. Message(parser_e_no_category_as_types);
  964. def:=generrordef
  965. end
  966. end;
  967. end
  968. else
  969. Message(sym_e_error_in_type_def);
  970. end;
  971. pt1.free;
  972. block_type:=old_block_type;
  973. end;
  974. procedure set_dec;
  975. begin
  976. consume(_SET);
  977. consume(_OF);
  978. read_anon_type(tt2,true);
  979. if assigned(tt2) then
  980. begin
  981. case tt2.typ of
  982. { don't forget that min can be negativ PM }
  983. enumdef :
  984. if (tenumdef(tt2).min>=0) and
  985. (tenumdef(tt2).max<=255) then
  986. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  987. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  988. else
  989. Message(sym_e_ill_type_decl_set);
  990. orddef :
  991. begin
  992. if (torddef(tt2).ordtype<>uvoid) and
  993. (torddef(tt2).ordtype<>uwidechar) and
  994. (torddef(tt2).low>=0) then
  995. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  996. if Torddef(tt2).high>int64(high(byte)) then
  997. message(sym_e_ill_type_decl_set)
  998. else
  999. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  1000. else
  1001. Message(sym_e_ill_type_decl_set);
  1002. end;
  1003. else
  1004. Message(sym_e_ill_type_decl_set);
  1005. end;
  1006. end
  1007. else
  1008. def:=generrordef;
  1009. end;
  1010. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:TFPObjectList);
  1011. var
  1012. lowval,
  1013. highval : TConstExprInt;
  1014. indexdef : tdef;
  1015. hdef : tdef;
  1016. arrdef : tarraydef;
  1017. procedure setdefdecl(def:tdef);
  1018. begin
  1019. case def.typ of
  1020. enumdef :
  1021. begin
  1022. lowval:=tenumdef(def).min;
  1023. highval:=tenumdef(def).max;
  1024. if (m_fpc in current_settings.modeswitches) and
  1025. (tenumdef(def).has_jumps) then
  1026. Message(type_e_array_index_enums_with_assign_not_possible);
  1027. indexdef:=def;
  1028. end;
  1029. orddef :
  1030. begin
  1031. if torddef(def).ordtype in [uchar,
  1032. u8bit,u16bit,
  1033. s8bit,s16bit,s32bit,
  1034. {$ifdef cpu64bitaddr}
  1035. u32bit,s64bit,
  1036. {$endif cpu64bitaddr}
  1037. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  1038. uwidechar] then
  1039. begin
  1040. lowval:=torddef(def).low;
  1041. highval:=torddef(def).high;
  1042. indexdef:=def;
  1043. end
  1044. else
  1045. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1046. end;
  1047. else
  1048. Message(sym_e_error_in_type_def);
  1049. end;
  1050. end;
  1051. var
  1052. old_current_genericdef,
  1053. old_current_specializedef: tstoreddef;
  1054. old_parse_generic: boolean;
  1055. begin
  1056. old_current_genericdef:=current_genericdef;
  1057. old_current_specializedef:=current_specializedef;
  1058. old_parse_generic:=parse_generic;
  1059. current_genericdef:=nil;
  1060. current_specializedef:=nil;
  1061. arrdef:=nil;
  1062. consume(_ARRAY);
  1063. { open array? }
  1064. if try_to_consume(_LECKKLAMMER) then
  1065. begin
  1066. { defaults }
  1067. indexdef:=generrordef;
  1068. { use defaults which don't overflow the compiler }
  1069. lowval:=0;
  1070. highval:=0;
  1071. repeat
  1072. { read the expression and check it, check apart if the
  1073. declaration is an enum declaration because that needs to
  1074. be parsed by readtype (PFV) }
  1075. if token=_LKLAMMER then
  1076. begin
  1077. read_anon_type(hdef,true);
  1078. setdefdecl(hdef);
  1079. end
  1080. else
  1081. begin
  1082. pt:=expr(true);
  1083. if pt.nodetype=typen then
  1084. setdefdecl(pt.resultdef)
  1085. else
  1086. begin
  1087. if (pt.nodetype=rangen) then
  1088. begin
  1089. if (trangenode(pt).left.nodetype=ordconstn) and
  1090. (trangenode(pt).right.nodetype=ordconstn) then
  1091. begin
  1092. { make both the same type or give an error. This is not
  1093. done when both are integer values, because typecasting
  1094. between -3200..3200 will result in a signed-unsigned
  1095. conflict and give a range check error (PFV) }
  1096. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1097. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1098. lowval:=tordconstnode(trangenode(pt).left).value;
  1099. highval:=tordconstnode(trangenode(pt).right).value;
  1100. if highval<lowval then
  1101. begin
  1102. Message(parser_e_array_lower_less_than_upper_bound);
  1103. highval:=lowval;
  1104. end
  1105. else if (lowval<int64(low(aint))) or
  1106. (highval > high(aint)) then
  1107. begin
  1108. Message(parser_e_array_range_out_of_bounds);
  1109. lowval :=0;
  1110. highval:=0;
  1111. end;
  1112. if is_integer(trangenode(pt).left.resultdef) then
  1113. range_to_type(lowval,highval,indexdef)
  1114. else
  1115. indexdef:=trangenode(pt).left.resultdef;
  1116. end
  1117. else
  1118. Message(type_e_cant_eval_constant_expr);
  1119. end
  1120. else
  1121. Message(sym_e_error_in_type_def)
  1122. end;
  1123. pt.free;
  1124. end;
  1125. { if the array is already created add the new arrray
  1126. as element of the existing array, otherwise create a new array }
  1127. if assigned(arrdef) then
  1128. begin
  1129. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1130. arrdef:=tarraydef(arrdef.elementdef);
  1131. end
  1132. else
  1133. begin
  1134. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1135. def:=arrdef;
  1136. end;
  1137. if is_packed then
  1138. include(arrdef.arrayoptions,ado_IsBitPacked);
  1139. if token=_COMMA then
  1140. consume(_COMMA)
  1141. else
  1142. break;
  1143. until false;
  1144. consume(_RECKKLAMMER);
  1145. end
  1146. else
  1147. begin
  1148. if is_packed then
  1149. Message(parser_e_packed_dynamic_open_array);
  1150. arrdef:=tarraydef.create(0,-1,s32inttype);
  1151. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1152. def:=arrdef;
  1153. end;
  1154. if assigned(arrdef) then
  1155. begin
  1156. { usage of specialized type inside its generic template }
  1157. if assigned(genericdef) then
  1158. current_specializedef:=arrdef
  1159. { reject declaration of generic class inside generic class }
  1160. else if assigned(genericlist) then
  1161. current_genericdef:=arrdef;
  1162. symtablestack.push(arrdef.symtable);
  1163. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1164. parse_generic:=(df_generic in arrdef.defoptions);
  1165. end;
  1166. consume(_OF);
  1167. read_anon_type(tt2,true);
  1168. { set element type of the last array definition }
  1169. if assigned(arrdef) then
  1170. begin
  1171. symtablestack.pop(arrdef.symtable);
  1172. arrdef.elementdef:=tt2;
  1173. if is_packed and
  1174. is_managed_type(tt2) then
  1175. Message(type_e_no_packed_inittable);
  1176. end;
  1177. { restore old state }
  1178. parse_generic:=old_parse_generic;
  1179. current_genericdef:=old_current_genericdef;
  1180. current_specializedef:=old_current_specializedef;
  1181. end;
  1182. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1183. var
  1184. is_func:boolean;
  1185. pd:tabstractprocdef;
  1186. newtype:ttypesym;
  1187. old_current_genericdef,
  1188. old_current_specializedef: tstoreddef;
  1189. old_parse_generic: boolean;
  1190. begin
  1191. old_current_genericdef:=current_genericdef;
  1192. old_current_specializedef:=current_specializedef;
  1193. old_parse_generic:=parse_generic;
  1194. current_genericdef:=nil;
  1195. current_specializedef:=nil;
  1196. is_func:=(token=_FUNCTION);
  1197. consume(token);
  1198. pd:=tprocvardef.create(normal_function_level);
  1199. { usage of specialized type inside its generic template }
  1200. if assigned(genericdef) then
  1201. current_specializedef:=pd
  1202. { reject declaration of generic class inside generic class }
  1203. else if assigned(genericlist) then
  1204. current_genericdef:=pd;
  1205. symtablestack.push(pd.parast);
  1206. insert_generic_parameter_types(pd,genericdef,genericlist);
  1207. parse_generic:=(df_generic in pd.defoptions);
  1208. { don't allow to add defs to the symtable - use it for type param search only }
  1209. tparasymtable(pd.parast).readonly:=true;
  1210. if token=_LKLAMMER then
  1211. parse_parameter_dec(pd);
  1212. if is_func then
  1213. begin
  1214. consume(_COLON);
  1215. single_type(pd.returndef,[]);
  1216. end;
  1217. if try_to_consume(_OF) then
  1218. begin
  1219. consume(_OBJECT);
  1220. include(pd.procoptions,po_methodpointer);
  1221. end
  1222. else if (m_nested_procvars in current_settings.modeswitches) and
  1223. try_to_consume(_IS) then
  1224. begin
  1225. consume(_NESTED);
  1226. pd.parast.symtablelevel:=normal_function_level+1;
  1227. pd.check_mark_as_nested;
  1228. end;
  1229. symtablestack.pop(pd.parast);
  1230. tparasymtable(pd.parast).readonly:=false;
  1231. result:=pd;
  1232. { possible proc directives }
  1233. if parseprocvardir then
  1234. begin
  1235. if check_proc_directive(true) then
  1236. begin
  1237. newtype:=ttypesym.create('unnamed',result);
  1238. parse_var_proc_directives(tsym(newtype));
  1239. newtype.typedef:=nil;
  1240. result.typesym:=nil;
  1241. newtype.free;
  1242. end;
  1243. { Add implicit hidden parameters and function result }
  1244. handle_calling_convention(pd);
  1245. end;
  1246. { restore old state }
  1247. parse_generic:=old_parse_generic;
  1248. current_genericdef:=old_current_genericdef;
  1249. current_specializedef:=old_current_specializedef;
  1250. end;
  1251. const
  1252. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1253. var
  1254. p : tnode;
  1255. hdef : tdef;
  1256. enumdupmsg, first, is_specialize : boolean;
  1257. oldlocalswitches : tlocalswitches;
  1258. bitpacking: boolean;
  1259. stitem: psymtablestackitem;
  1260. sym: tsym;
  1261. st: tsymtable;
  1262. begin
  1263. def:=nil;
  1264. case token of
  1265. _STRING,_FILE:
  1266. begin
  1267. single_type(def,[stoAllowTypeDef]);
  1268. end;
  1269. _LKLAMMER:
  1270. begin
  1271. consume(_LKLAMMER);
  1272. first:=true;
  1273. { allow negativ value_str }
  1274. l:=int64(-1);
  1275. enumdupmsg:=false;
  1276. { check that we are not adding an enum from specialization
  1277. we can't just use current_specializedef because of inner types
  1278. like specialize array of record }
  1279. is_specialize:=false;
  1280. stitem:=symtablestack.stack;
  1281. while assigned(stitem) do
  1282. begin
  1283. { check records, classes and arrays because they can be specialized }
  1284. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1285. begin
  1286. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1287. stitem:=stitem^.next;
  1288. end
  1289. else
  1290. break;
  1291. end;
  1292. if not is_specialize then
  1293. aktenumdef:=tenumdef.create
  1294. else
  1295. aktenumdef:=nil;
  1296. repeat
  1297. { if it is a specialization then search the first enum member
  1298. and get the member owner instead of just created enumdef }
  1299. if not assigned(aktenumdef) then
  1300. begin
  1301. searchsym(pattern,sym,st);
  1302. if sym.typ=enumsym then
  1303. aktenumdef:=tenumsym(sym).definition
  1304. else
  1305. internalerror(201101021);
  1306. end;
  1307. s:=orgpattern;
  1308. defpos:=current_tokenpos;
  1309. consume(_ID);
  1310. { only allow assigning of specific numbers under fpc mode }
  1311. if not(m_tp7 in current_settings.modeswitches) and
  1312. (
  1313. { in fpc mode also allow := to be compatible
  1314. with previous 1.0.x versions }
  1315. ((m_fpc in current_settings.modeswitches) and
  1316. try_to_consume(_ASSIGNMENT)) or
  1317. try_to_consume(_EQ)
  1318. ) then
  1319. begin
  1320. oldlocalswitches:=current_settings.localswitches;
  1321. include(current_settings.localswitches,cs_allow_enum_calc);
  1322. p:=comp_expr(true,false);
  1323. current_settings.localswitches:=oldlocalswitches;
  1324. if (p.nodetype=ordconstn) then
  1325. begin
  1326. { we expect an integer or an enum of the
  1327. same type }
  1328. if is_integer(p.resultdef) or
  1329. is_char(p.resultdef) or
  1330. equal_defs(p.resultdef,aktenumdef) then
  1331. v:=tordconstnode(p).value
  1332. else
  1333. IncompatibleTypes(p.resultdef,s32inttype);
  1334. end
  1335. else
  1336. Message(parser_e_illegal_expression);
  1337. p.free;
  1338. { please leave that a note, allows type save }
  1339. { declarations in the win32 units ! }
  1340. if (not first) and (v<=l) and (not enumdupmsg) then
  1341. begin
  1342. Message(parser_n_duplicate_enum);
  1343. enumdupmsg:=true;
  1344. end;
  1345. l:=v;
  1346. end
  1347. else
  1348. inc(l.svalue);
  1349. first:=false;
  1350. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1351. if not is_specialize then
  1352. begin
  1353. storepos:=current_tokenpos;
  1354. current_tokenpos:=defpos;
  1355. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1356. if not (cs_scopedenums in current_settings.localswitches) then
  1357. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1358. current_tokenpos:=storepos;
  1359. end;
  1360. until not try_to_consume(_COMMA);
  1361. def:=aktenumdef;
  1362. consume(_RKLAMMER);
  1363. end;
  1364. _ARRAY:
  1365. begin
  1366. array_dec(false,genericdef,genericlist);
  1367. end;
  1368. _SET:
  1369. begin
  1370. set_dec;
  1371. end;
  1372. _CARET:
  1373. begin
  1374. consume(_CARET);
  1375. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1376. def:=tpointerdef.create(tt2);
  1377. if tt2.typ=forwarddef then
  1378. current_module.checkforwarddefs.add(def);
  1379. end;
  1380. _RECORD:
  1381. begin
  1382. def:=record_dec(name,genericdef,genericlist);
  1383. end;
  1384. _PACKED,
  1385. _BITPACKED:
  1386. begin
  1387. bitpacking :=
  1388. (cs_bitpacking in current_settings.localswitches) or
  1389. (token = _BITPACKED);
  1390. consume(token);
  1391. if token=_ARRAY then
  1392. array_dec(bitpacking,genericdef,genericlist)
  1393. else if token=_SET then
  1394. set_dec
  1395. else if token=_FILE then
  1396. single_type(def,[stoAllowTypeDef])
  1397. else
  1398. begin
  1399. oldpackrecords:=current_settings.packrecords;
  1400. if (not bitpacking) or
  1401. (token in [_CLASS,_OBJECT]) then
  1402. current_settings.packrecords:=1
  1403. else
  1404. current_settings.packrecords:=bit_alignment;
  1405. case token of
  1406. _CLASS :
  1407. begin
  1408. consume(_CLASS);
  1409. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  1410. end;
  1411. _OBJECT :
  1412. begin
  1413. consume(_OBJECT);
  1414. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  1415. end;
  1416. else
  1417. def:=record_dec(name,genericdef,genericlist);
  1418. end;
  1419. current_settings.packrecords:=oldpackrecords;
  1420. end;
  1421. end;
  1422. _DISPINTERFACE :
  1423. begin
  1424. { need extra check here since interface is a keyword
  1425. in all pascal modes }
  1426. if not(m_class in current_settings.modeswitches) then
  1427. Message(parser_f_need_objfpc_or_delphi_mode);
  1428. consume(token);
  1429. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil);
  1430. end;
  1431. _CLASS :
  1432. begin
  1433. consume(token);
  1434. { Delphi only allows class of in type blocks }
  1435. if (token=_OF) and
  1436. (
  1437. not(m_delphi in current_settings.modeswitches) or
  1438. (block_type=bt_type)
  1439. ) then
  1440. begin
  1441. consume(_OF);
  1442. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1443. if is_class(hdef) or
  1444. is_objcclass(hdef) then
  1445. def:=tclassrefdef.create(hdef)
  1446. else
  1447. if hdef.typ=forwarddef then
  1448. begin
  1449. def:=tclassrefdef.create(hdef);
  1450. current_module.checkforwarddefs.add(def);
  1451. end
  1452. else
  1453. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1454. end
  1455. else
  1456. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  1457. end;
  1458. _CPPCLASS :
  1459. begin
  1460. consume(token);
  1461. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil);
  1462. end;
  1463. _OBJCCLASS :
  1464. begin
  1465. if not(m_objectivec1 in current_settings.modeswitches) then
  1466. Message(parser_f_need_objc);
  1467. consume(token);
  1468. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil);
  1469. end;
  1470. _INTERFACE :
  1471. begin
  1472. { need extra check here since interface is a keyword
  1473. in all pascal modes }
  1474. if not(m_class in current_settings.modeswitches) then
  1475. Message(parser_f_need_objfpc_or_delphi_mode);
  1476. consume(token);
  1477. if current_settings.interfacetype=it_interfacecom then
  1478. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil)
  1479. else {it_interfacecorba}
  1480. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil);
  1481. end;
  1482. _OBJCPROTOCOL :
  1483. begin
  1484. if not(m_objectivec1 in current_settings.modeswitches) then
  1485. Message(parser_f_need_objc);
  1486. consume(token);
  1487. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil);
  1488. end;
  1489. _OBJCCATEGORY :
  1490. begin
  1491. if not(m_objectivec1 in current_settings.modeswitches) then
  1492. Message(parser_f_need_objc);
  1493. consume(token);
  1494. def:=object_dec(odt_objccategory,name,genericdef,genericlist,nil);
  1495. end;
  1496. _OBJECT :
  1497. begin
  1498. consume(token);
  1499. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  1500. end;
  1501. _PROCEDURE,
  1502. _FUNCTION:
  1503. begin
  1504. def:=procvar_dec(genericdef,genericlist);
  1505. end;
  1506. else
  1507. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1508. begin
  1509. consume(_KLAMMERAFFE);
  1510. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1511. def:=tpointerdef.create(tt2);
  1512. if tt2.typ=forwarddef then
  1513. current_module.checkforwarddefs.add(def);
  1514. end
  1515. else
  1516. expr_type;
  1517. end;
  1518. if def=nil then
  1519. def:=generrordef;
  1520. end;
  1521. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1522. begin
  1523. read_named_type(def,'',nil,nil,parseprocvardir);
  1524. end;
  1525. procedure write_persistent_type_info(st:tsymtable);
  1526. var
  1527. i : longint;
  1528. def : tdef;
  1529. vmtwriter : TVMTWriter;
  1530. begin
  1531. for i:=0 to st.DefList.Count-1 do
  1532. begin
  1533. def:=tdef(st.DefList[i]);
  1534. case def.typ of
  1535. recorddef :
  1536. write_persistent_type_info(trecorddef(def).symtable);
  1537. objectdef :
  1538. begin
  1539. { Skip generics and forward defs }
  1540. if (df_generic in def.defoptions) or
  1541. (oo_is_forward in tobjectdef(def).objectoptions) then
  1542. continue;
  1543. write_persistent_type_info(tobjectdef(def).symtable);
  1544. { Write also VMT if not done yet }
  1545. if not(ds_vmt_written in def.defstates) then
  1546. begin
  1547. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1548. if is_interface(tobjectdef(def)) then
  1549. vmtwriter.writeinterfaceids;
  1550. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1551. vmtwriter.writevmt;
  1552. vmtwriter.free;
  1553. include(def.defstates,ds_vmt_written);
  1554. end;
  1555. end;
  1556. procdef :
  1557. begin
  1558. if assigned(tprocdef(def).localst) and
  1559. (tprocdef(def).localst.symtabletype=localsymtable) then
  1560. write_persistent_type_info(tprocdef(def).localst);
  1561. if assigned(tprocdef(def).parast) then
  1562. write_persistent_type_info(tprocdef(def).parast);
  1563. end;
  1564. end;
  1565. { generate always persistent tables for types in the interface so it can
  1566. be reused in other units and give always the same pointer location. }
  1567. { Init }
  1568. if (
  1569. assigned(def.typesym) and
  1570. (st.symtabletype=globalsymtable) and
  1571. not is_objc_class_or_protocol(def)
  1572. ) or
  1573. is_managed_type(def) or
  1574. (ds_init_table_used in def.defstates) then
  1575. RTTIWriter.write_rtti(def,initrtti);
  1576. { RTTI }
  1577. if (
  1578. assigned(def.typesym) and
  1579. (st.symtabletype=globalsymtable) and
  1580. not is_objc_class_or_protocol(def)
  1581. ) or
  1582. (ds_rtti_table_used in def.defstates) then
  1583. RTTIWriter.write_rtti(def,fullrtti);
  1584. end;
  1585. end;
  1586. end.