ptype.pas 70 KB

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