ptype.pas 62 KB

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