ptype.pas 67 KB

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