ptype.pas 84 KB

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