ptype.pas 65 KB

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