ptype.pas 73 KB

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