PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/compiler/ptype.pas

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