ptype.pas 82 KB

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