ptype.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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,true);
  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,name,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. { TODO : check once nested generics are allowed }
  802. not
  803. (
  804. parse_generic and
  805. (current_genericdef.typ in [recorddef,objectdef]) and
  806. (def.typ in [recorddef,objectdef]) and
  807. (ttypenode(pt1).typesym<>nil) and
  808. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  809. )
  810. then
  811. begin
  812. Message(parser_e_no_generics_as_types);
  813. def:=generrordef;
  814. end
  815. else if is_classhelper(def) then
  816. begin
  817. Message(parser_e_no_category_as_types);
  818. def:=generrordef
  819. end
  820. end;
  821. end
  822. else
  823. Message(sym_e_error_in_type_def);
  824. end;
  825. pt1.free;
  826. block_type:=old_block_type;
  827. end;
  828. procedure set_dec;
  829. begin
  830. consume(_SET);
  831. consume(_OF);
  832. read_anon_type(tt2,true);
  833. if assigned(tt2) then
  834. begin
  835. case tt2.typ of
  836. { don't forget that min can be negativ PM }
  837. enumdef :
  838. if (tenumdef(tt2).min>=0) and
  839. (tenumdef(tt2).max<=255) then
  840. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  841. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  842. else
  843. Message(sym_e_ill_type_decl_set);
  844. orddef :
  845. begin
  846. if (torddef(tt2).ordtype<>uvoid) and
  847. (torddef(tt2).ordtype<>uwidechar) and
  848. (torddef(tt2).low>=0) then
  849. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  850. if Torddef(tt2).high>int64(high(byte)) then
  851. message(sym_e_ill_type_decl_set)
  852. else
  853. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  854. else
  855. Message(sym_e_ill_type_decl_set);
  856. end;
  857. else
  858. Message(sym_e_ill_type_decl_set);
  859. end;
  860. end
  861. else
  862. def:=generrordef;
  863. end;
  864. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:TFPObjectList);
  865. var
  866. lowval,
  867. highval : TConstExprInt;
  868. indexdef : tdef;
  869. hdef : tdef;
  870. arrdef : tarraydef;
  871. procedure setdefdecl(def:tdef);
  872. begin
  873. case def.typ of
  874. enumdef :
  875. begin
  876. lowval:=tenumdef(def).min;
  877. highval:=tenumdef(def).max;
  878. if (m_fpc in current_settings.modeswitches) and
  879. (tenumdef(def).has_jumps) then
  880. Message(type_e_array_index_enums_with_assign_not_possible);
  881. indexdef:=def;
  882. end;
  883. orddef :
  884. begin
  885. if torddef(def).ordtype in [uchar,
  886. u8bit,u16bit,
  887. s8bit,s16bit,s32bit,
  888. {$ifdef cpu64bitaddr}
  889. u32bit,s64bit,
  890. {$endif cpu64bitaddr}
  891. pasbool8,pasbool16,pasbool32,pasbool64,
  892. bool8bit,bool16bit,bool32bit,bool64bit,
  893. uwidechar] then
  894. begin
  895. lowval:=torddef(def).low;
  896. highval:=torddef(def).high;
  897. indexdef:=def;
  898. end
  899. else
  900. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  901. end;
  902. else
  903. Message(sym_e_error_in_type_def);
  904. end;
  905. end;
  906. var
  907. old_current_genericdef,
  908. old_current_specializedef: tstoreddef;
  909. old_parse_generic: boolean;
  910. begin
  911. old_current_genericdef:=current_genericdef;
  912. old_current_specializedef:=current_specializedef;
  913. old_parse_generic:=parse_generic;
  914. current_genericdef:=nil;
  915. current_specializedef:=nil;
  916. arrdef:=nil;
  917. consume(_ARRAY);
  918. { open array? }
  919. if try_to_consume(_LECKKLAMMER) then
  920. begin
  921. { defaults }
  922. indexdef:=generrordef;
  923. { use defaults which don't overflow the compiler }
  924. lowval:=0;
  925. highval:=0;
  926. repeat
  927. { read the expression and check it, check apart if the
  928. declaration is an enum declaration because that needs to
  929. be parsed by readtype (PFV) }
  930. if token=_LKLAMMER then
  931. begin
  932. read_anon_type(hdef,true);
  933. setdefdecl(hdef);
  934. end
  935. else
  936. begin
  937. pt:=expr(true);
  938. if pt.nodetype=typen then
  939. setdefdecl(pt.resultdef)
  940. else
  941. begin
  942. if (pt.nodetype=rangen) then
  943. begin
  944. if (trangenode(pt).left.nodetype=ordconstn) and
  945. (trangenode(pt).right.nodetype=ordconstn) then
  946. begin
  947. { make both the same type or give an error. This is not
  948. done when both are integer values, because typecasting
  949. between -3200..3200 will result in a signed-unsigned
  950. conflict and give a range check error (PFV) }
  951. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  952. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  953. lowval:=tordconstnode(trangenode(pt).left).value;
  954. highval:=tordconstnode(trangenode(pt).right).value;
  955. if highval<lowval then
  956. begin
  957. Message(parser_e_array_lower_less_than_upper_bound);
  958. highval:=lowval;
  959. end
  960. else if (lowval<int64(low(asizeint))) or
  961. (highval>high(asizeint)) then
  962. begin
  963. Message(parser_e_array_range_out_of_bounds);
  964. lowval :=0;
  965. highval:=0;
  966. end;
  967. if is_integer(trangenode(pt).left.resultdef) then
  968. range_to_type(lowval,highval,indexdef)
  969. else
  970. indexdef:=trangenode(pt).left.resultdef;
  971. end
  972. else
  973. Message(type_e_cant_eval_constant_expr);
  974. end
  975. else
  976. Message(sym_e_error_in_type_def)
  977. end;
  978. pt.free;
  979. end;
  980. { if the array is already created add the new arrray
  981. as element of the existing array, otherwise create a new array }
  982. if assigned(arrdef) then
  983. begin
  984. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  985. arrdef:=tarraydef(arrdef.elementdef);
  986. end
  987. else
  988. begin
  989. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  990. def:=arrdef;
  991. end;
  992. if is_packed then
  993. include(arrdef.arrayoptions,ado_IsBitPacked);
  994. if token=_COMMA then
  995. consume(_COMMA)
  996. else
  997. break;
  998. until false;
  999. consume(_RECKKLAMMER);
  1000. end
  1001. else
  1002. begin
  1003. if is_packed then
  1004. Message(parser_e_packed_dynamic_open_array);
  1005. arrdef:=tarraydef.create(0,-1,s32inttype);
  1006. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1007. def:=arrdef;
  1008. end;
  1009. if assigned(arrdef) then
  1010. begin
  1011. { usage of specialized type inside its generic template }
  1012. if assigned(genericdef) then
  1013. current_specializedef:=arrdef
  1014. { reject declaration of generic class inside generic class }
  1015. else if assigned(genericlist) then
  1016. current_genericdef:=arrdef;
  1017. symtablestack.push(arrdef.symtable);
  1018. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1019. parse_generic:=(df_generic in arrdef.defoptions);
  1020. end;
  1021. consume(_OF);
  1022. read_anon_type(tt2,true);
  1023. { set element type of the last array definition }
  1024. if assigned(arrdef) then
  1025. begin
  1026. symtablestack.pop(arrdef.symtable);
  1027. arrdef.elementdef:=tt2;
  1028. if is_packed and
  1029. is_managed_type(tt2) then
  1030. Message(type_e_no_packed_inittable);
  1031. end;
  1032. { restore old state }
  1033. parse_generic:=old_parse_generic;
  1034. current_genericdef:=old_current_genericdef;
  1035. current_specializedef:=old_current_specializedef;
  1036. end;
  1037. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1038. var
  1039. is_func:boolean;
  1040. pd:tabstractprocdef;
  1041. newtype:ttypesym;
  1042. old_current_genericdef,
  1043. old_current_specializedef: tstoreddef;
  1044. old_parse_generic: boolean;
  1045. begin
  1046. old_current_genericdef:=current_genericdef;
  1047. old_current_specializedef:=current_specializedef;
  1048. old_parse_generic:=parse_generic;
  1049. current_genericdef:=nil;
  1050. current_specializedef:=nil;
  1051. is_func:=(token=_FUNCTION);
  1052. consume(token);
  1053. pd:=tprocvardef.create(normal_function_level);
  1054. { usage of specialized type inside its generic template }
  1055. if assigned(genericdef) then
  1056. current_specializedef:=pd
  1057. { reject declaration of generic class inside generic class }
  1058. else if assigned(genericlist) then
  1059. current_genericdef:=pd;
  1060. symtablestack.push(pd.parast);
  1061. insert_generic_parameter_types(pd,genericdef,genericlist);
  1062. parse_generic:=(df_generic in pd.defoptions);
  1063. { don't allow to add defs to the symtable - use it for type param search only }
  1064. tparasymtable(pd.parast).readonly:=true;
  1065. if token=_LKLAMMER then
  1066. parse_parameter_dec(pd);
  1067. if is_func then
  1068. begin
  1069. consume(_COLON);
  1070. single_type(pd.returndef,[]);
  1071. end;
  1072. if try_to_consume(_OF) then
  1073. begin
  1074. consume(_OBJECT);
  1075. include(pd.procoptions,po_methodpointer);
  1076. end
  1077. else if (m_nested_procvars in current_settings.modeswitches) and
  1078. try_to_consume(_IS) then
  1079. begin
  1080. consume(_NESTED);
  1081. pd.parast.symtablelevel:=normal_function_level+1;
  1082. pd.check_mark_as_nested;
  1083. end;
  1084. symtablestack.pop(pd.parast);
  1085. tparasymtable(pd.parast).readonly:=false;
  1086. result:=pd;
  1087. { possible proc directives }
  1088. if parseprocvardir then
  1089. begin
  1090. if check_proc_directive(true) then
  1091. begin
  1092. newtype:=ttypesym.create('unnamed',result);
  1093. parse_var_proc_directives(tsym(newtype));
  1094. newtype.typedef:=nil;
  1095. result.typesym:=nil;
  1096. newtype.free;
  1097. end;
  1098. { Add implicit hidden parameters and function result }
  1099. handle_calling_convention(pd);
  1100. end;
  1101. { restore old state }
  1102. parse_generic:=old_parse_generic;
  1103. current_genericdef:=old_current_genericdef;
  1104. current_specializedef:=old_current_specializedef;
  1105. end;
  1106. const
  1107. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1108. var
  1109. p : tnode;
  1110. hdef : tdef;
  1111. enumdupmsg, first, is_specialize : boolean;
  1112. oldlocalswitches : tlocalswitches;
  1113. bitpacking: boolean;
  1114. stitem: psymtablestackitem;
  1115. sym: tsym;
  1116. st: tsymtable;
  1117. begin
  1118. def:=nil;
  1119. case token of
  1120. _STRING,_FILE:
  1121. begin
  1122. single_type(def,[stoAllowTypeDef]);
  1123. end;
  1124. _LKLAMMER:
  1125. begin
  1126. consume(_LKLAMMER);
  1127. first:=true;
  1128. { allow negativ value_str }
  1129. l:=int64(-1);
  1130. enumdupmsg:=false;
  1131. { check that we are not adding an enum from specialization
  1132. we can't just use current_specializedef because of inner types
  1133. like specialize array of record }
  1134. is_specialize:=false;
  1135. stitem:=symtablestack.stack;
  1136. while assigned(stitem) do
  1137. begin
  1138. { check records, classes and arrays because they can be specialized }
  1139. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1140. begin
  1141. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1142. stitem:=stitem^.next;
  1143. end
  1144. else
  1145. break;
  1146. end;
  1147. if not is_specialize then
  1148. aktenumdef:=tenumdef.create
  1149. else
  1150. aktenumdef:=nil;
  1151. repeat
  1152. { if it is a specialization then search the first enum member
  1153. and get the member owner instead of just created enumdef }
  1154. if not assigned(aktenumdef) then
  1155. begin
  1156. searchsym(pattern,sym,st);
  1157. if sym.typ=enumsym then
  1158. aktenumdef:=tenumsym(sym).definition
  1159. else
  1160. internalerror(201101021);
  1161. end;
  1162. s:=orgpattern;
  1163. defpos:=current_tokenpos;
  1164. consume(_ID);
  1165. { only allow assigning of specific numbers under fpc mode }
  1166. if not(m_tp7 in current_settings.modeswitches) and
  1167. (
  1168. { in fpc mode also allow := to be compatible
  1169. with previous 1.0.x versions }
  1170. ((m_fpc in current_settings.modeswitches) and
  1171. try_to_consume(_ASSIGNMENT)) or
  1172. try_to_consume(_EQ)
  1173. ) then
  1174. begin
  1175. oldlocalswitches:=current_settings.localswitches;
  1176. include(current_settings.localswitches,cs_allow_enum_calc);
  1177. p:=comp_expr(true,false);
  1178. current_settings.localswitches:=oldlocalswitches;
  1179. if (p.nodetype=ordconstn) then
  1180. begin
  1181. { we expect an integer or an enum of the
  1182. same type }
  1183. if is_integer(p.resultdef) or
  1184. is_char(p.resultdef) or
  1185. equal_defs(p.resultdef,aktenumdef) then
  1186. v:=tordconstnode(p).value
  1187. else
  1188. IncompatibleTypes(p.resultdef,s32inttype);
  1189. end
  1190. else
  1191. Message(parser_e_illegal_expression);
  1192. p.free;
  1193. { please leave that a note, allows type save }
  1194. { declarations in the win32 units ! }
  1195. if (not first) and (v<=l) and (not enumdupmsg) then
  1196. begin
  1197. Message(parser_n_duplicate_enum);
  1198. enumdupmsg:=true;
  1199. end;
  1200. l:=v;
  1201. end
  1202. else
  1203. inc(l.svalue);
  1204. first:=false;
  1205. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1206. if not is_specialize then
  1207. begin
  1208. storepos:=current_tokenpos;
  1209. current_tokenpos:=defpos;
  1210. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1211. if not (cs_scopedenums in current_settings.localswitches) then
  1212. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1213. current_tokenpos:=storepos;
  1214. end;
  1215. until not try_to_consume(_COMMA);
  1216. def:=aktenumdef;
  1217. consume(_RKLAMMER);
  1218. end;
  1219. _ARRAY:
  1220. begin
  1221. array_dec(false,genericdef,genericlist);
  1222. end;
  1223. _SET:
  1224. begin
  1225. set_dec;
  1226. end;
  1227. _CARET:
  1228. begin
  1229. consume(_CARET);
  1230. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1231. def:=tpointerdef.create(tt2);
  1232. if tt2.typ=forwarddef then
  1233. current_module.checkforwarddefs.add(def);
  1234. end;
  1235. _RECORD:
  1236. begin
  1237. consume(token);
  1238. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1239. begin
  1240. consume(_HELPER);
  1241. def:=object_dec(odt_helper,name,genericdef,genericlist,nil,ht_record);
  1242. end
  1243. else
  1244. def:=record_dec(name,genericdef,genericlist);
  1245. end;
  1246. _PACKED,
  1247. _BITPACKED:
  1248. begin
  1249. bitpacking :=
  1250. (cs_bitpacking in current_settings.localswitches) or
  1251. (token = _BITPACKED);
  1252. consume(token);
  1253. if token=_ARRAY then
  1254. array_dec(bitpacking,genericdef,genericlist)
  1255. else if token=_SET then
  1256. set_dec
  1257. else if token=_FILE then
  1258. single_type(def,[stoAllowTypeDef])
  1259. else
  1260. begin
  1261. oldpackrecords:=current_settings.packrecords;
  1262. if (not bitpacking) or
  1263. (token in [_CLASS,_OBJECT]) then
  1264. current_settings.packrecords:=1
  1265. else
  1266. current_settings.packrecords:=bit_alignment;
  1267. case token of
  1268. _CLASS :
  1269. begin
  1270. consume(_CLASS);
  1271. def:=object_dec(odt_class,name,genericdef,genericlist,nil,ht_none);
  1272. end;
  1273. _OBJECT :
  1274. begin
  1275. consume(_OBJECT);
  1276. def:=object_dec(odt_object,name,genericdef,genericlist,nil,ht_none);
  1277. end;
  1278. else begin
  1279. consume(_RECORD);
  1280. def:=record_dec(name,genericdef,genericlist);
  1281. end;
  1282. end;
  1283. current_settings.packrecords:=oldpackrecords;
  1284. end;
  1285. end;
  1286. _DISPINTERFACE :
  1287. begin
  1288. { need extra check here since interface is a keyword
  1289. in all pascal modes }
  1290. if not(m_class in current_settings.modeswitches) then
  1291. Message(parser_f_need_objfpc_or_delphi_mode);
  1292. consume(token);
  1293. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil,ht_none);
  1294. end;
  1295. _CLASS :
  1296. begin
  1297. consume(token);
  1298. { Delphi only allows class of in type blocks }
  1299. if (token=_OF) and
  1300. (
  1301. not(m_delphi in current_settings.modeswitches) or
  1302. (block_type=bt_type)
  1303. ) then
  1304. begin
  1305. consume(_OF);
  1306. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1307. if is_class(hdef) or
  1308. is_objcclass(hdef) then
  1309. def:=tclassrefdef.create(hdef)
  1310. else
  1311. if hdef.typ=forwarddef then
  1312. begin
  1313. def:=tclassrefdef.create(hdef);
  1314. current_module.checkforwarddefs.add(def);
  1315. end
  1316. else
  1317. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1318. end
  1319. else
  1320. if (idtoken=_HELPER) then
  1321. begin
  1322. consume(_HELPER);
  1323. def:=object_dec(odt_helper,name,genericdef,genericlist,nil,ht_class);
  1324. end
  1325. else
  1326. def:=object_dec(odt_class,name,genericdef,genericlist,nil,ht_none);
  1327. end;
  1328. _CPPCLASS :
  1329. begin
  1330. consume(token);
  1331. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil,ht_none);
  1332. end;
  1333. _OBJCCLASS :
  1334. begin
  1335. if not(m_objectivec1 in current_settings.modeswitches) then
  1336. Message(parser_f_need_objc);
  1337. consume(token);
  1338. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil,ht_none);
  1339. end;
  1340. _INTERFACE :
  1341. begin
  1342. { need extra check here since interface is a keyword
  1343. in all pascal modes }
  1344. if not(m_class in current_settings.modeswitches) then
  1345. Message(parser_f_need_objfpc_or_delphi_mode);
  1346. consume(token);
  1347. if current_settings.interfacetype=it_interfacecom then
  1348. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil,ht_none)
  1349. else {it_interfacecorba}
  1350. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil,ht_none);
  1351. end;
  1352. _OBJCPROTOCOL :
  1353. begin
  1354. if not(m_objectivec1 in current_settings.modeswitches) then
  1355. Message(parser_f_need_objc);
  1356. consume(token);
  1357. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil,ht_none);
  1358. end;
  1359. _OBJCCATEGORY :
  1360. begin
  1361. if not(m_objectivec1 in current_settings.modeswitches) then
  1362. Message(parser_f_need_objc);
  1363. consume(token);
  1364. def:=object_dec(odt_objccategory,name,genericdef,genericlist,nil,ht_none);
  1365. end;
  1366. _OBJECT :
  1367. begin
  1368. consume(token);
  1369. def:=object_dec(odt_object,name,genericdef,genericlist,nil,ht_none);
  1370. end;
  1371. _PROCEDURE,
  1372. _FUNCTION:
  1373. begin
  1374. def:=procvar_dec(genericdef,genericlist);
  1375. end;
  1376. else
  1377. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1378. begin
  1379. consume(_KLAMMERAFFE);
  1380. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1381. def:=tpointerdef.create(tt2);
  1382. if tt2.typ=forwarddef then
  1383. current_module.checkforwarddefs.add(def);
  1384. end
  1385. else
  1386. expr_type;
  1387. end;
  1388. if def=nil then
  1389. def:=generrordef;
  1390. end;
  1391. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1392. begin
  1393. read_named_type(def,'',nil,nil,parseprocvardir);
  1394. end;
  1395. procedure write_persistent_type_info(st:tsymtable);
  1396. var
  1397. i : longint;
  1398. def : tdef;
  1399. vmtwriter : TVMTWriter;
  1400. begin
  1401. for i:=0 to st.DefList.Count-1 do
  1402. begin
  1403. def:=tdef(st.DefList[i]);
  1404. case def.typ of
  1405. recorddef :
  1406. write_persistent_type_info(trecorddef(def).symtable);
  1407. objectdef :
  1408. begin
  1409. { Skip generics and forward defs }
  1410. if (df_generic in def.defoptions) or
  1411. (oo_is_forward in tobjectdef(def).objectoptions) then
  1412. continue;
  1413. write_persistent_type_info(tobjectdef(def).symtable);
  1414. { Write also VMT if not done yet }
  1415. if not(ds_vmt_written in def.defstates) then
  1416. begin
  1417. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1418. if is_interface(tobjectdef(def)) then
  1419. vmtwriter.writeinterfaceids;
  1420. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1421. vmtwriter.writevmt;
  1422. vmtwriter.free;
  1423. include(def.defstates,ds_vmt_written);
  1424. end;
  1425. end;
  1426. procdef :
  1427. begin
  1428. if assigned(tprocdef(def).localst) and
  1429. (tprocdef(def).localst.symtabletype=localsymtable) then
  1430. write_persistent_type_info(tprocdef(def).localst);
  1431. if assigned(tprocdef(def).parast) then
  1432. write_persistent_type_info(tprocdef(def).parast);
  1433. end;
  1434. end;
  1435. { generate always persistent tables for types in the interface so it can
  1436. be reused in other units and give always the same pointer location. }
  1437. { Init }
  1438. if (
  1439. assigned(def.typesym) and
  1440. (st.symtabletype=globalsymtable) and
  1441. not is_objc_class_or_protocol(def)
  1442. ) or
  1443. is_managed_type(def) or
  1444. (ds_init_table_used in def.defstates) then
  1445. RTTIWriter.write_rtti(def,initrtti);
  1446. { RTTI }
  1447. if (
  1448. assigned(def.typesym) and
  1449. (st.symtabletype=globalsymtable) and
  1450. not is_objc_class_or_protocol(def)
  1451. ) or
  1452. (ds_rtti_table_used in def.defstates) then
  1453. RTTIWriter.write_rtti(def,fullrtti);
  1454. end;
  1455. end;
  1456. end.