2
0

ptype.pas 86 KB

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