ptype.pas 59 KB

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