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 (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 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_struct_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. else
  1074. internalerror(2019050527);
  1075. end;
  1076. end;
  1077. end
  1078. else
  1079. Message(sym_e_error_in_type_def);
  1080. pt2.free;
  1081. end
  1082. else
  1083. begin
  1084. { a simple type renaming or generic specialization }
  1085. if (pt1.nodetype=typen) then
  1086. begin
  1087. def:=ttypenode(pt1).resultdef;
  1088. { Delphi mode specialization? }
  1089. if (m_delphi in current_settings.modeswitches) then
  1090. dospecialize:=token=_LSHARPBRACKET
  1091. else
  1092. begin
  1093. dospecialize:=false;
  1094. { in non-Delphi modes we might get a inline specialization
  1095. without "specialize" or "<T>" of the same type we're
  1096. currently parsing, so we need to handle that special }
  1097. newdef:=nil;
  1098. end;
  1099. if not dospecialize and
  1100. assigned(ttypenode(pt1).typesym) and
  1101. (ttypenode(pt1).typesym.typ=typesym) and
  1102. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  1103. assigned(current_structdef) and
  1104. (
  1105. (
  1106. not (m_delphi in current_settings.modeswitches) and
  1107. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  1108. (df_generic in current_structdef.defoptions) and
  1109. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  1110. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  1111. ) or (
  1112. { this could be a nested specialization which uses
  1113. the type name of a surrounding generic to
  1114. reference the specialization of said surrounding
  1115. class }
  1116. (df_specialization in current_structdef.defoptions) and
  1117. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  1118. )
  1119. )
  1120. then
  1121. begin
  1122. if assigned(newdef) then
  1123. def:=newdef
  1124. else
  1125. def:=current_structdef;
  1126. if assigned(def) then
  1127. { handle nested types }
  1128. post_comp_expr_gendef(def)
  1129. else
  1130. def:=generrordef;
  1131. end;
  1132. if dospecialize then
  1133. begin
  1134. generate_specialization(def,false,name);
  1135. { handle nested types }
  1136. if assigned(def) then
  1137. post_comp_expr_gendef(def);
  1138. end
  1139. else
  1140. begin
  1141. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  1142. begin
  1143. def:=current_specializedef
  1144. end
  1145. else if (def=current_genericdef) then
  1146. begin
  1147. def:=current_genericdef
  1148. end
  1149. else if tstoreddef(def).is_generic and
  1150. { TODO : check once nested generics are allowed }
  1151. not
  1152. (
  1153. parse_generic and
  1154. (current_genericdef.typ in [recorddef,objectdef]) and
  1155. (def.typ in [recorddef,objectdef]) and
  1156. (
  1157. { if both defs belong to the same generic (e.g. both are
  1158. subtypes) then we must allow the usage }
  1159. defs_belong_to_same_generic(def,current_genericdef) or
  1160. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  1161. declarations inside a generic }
  1162. (
  1163. (ttypenode(pt1).typesym<>nil) and
  1164. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  1165. )
  1166. )
  1167. )
  1168. then
  1169. begin
  1170. if assigned(def.typesym) then
  1171. begin
  1172. if ttypesym(def.typesym).typedef.typ<>undefineddef then
  1173. { non-Delphi modes... }
  1174. split_generic_name(def.typesym.name,genstr,gencount)
  1175. else
  1176. genstr:=def.typesym.name;
  1177. sym:=resolve_generic_dummysym(genstr);
  1178. end
  1179. else
  1180. sym:=nil;
  1181. if assigned(sym) and
  1182. not (sp_generic_dummy in sym.symoptions) and
  1183. (sym.typ=typesym) then
  1184. def:=ttypesym(sym).typedef
  1185. else
  1186. begin
  1187. Message(parser_e_no_generics_as_types);
  1188. def:=generrordef;
  1189. end;
  1190. end
  1191. else if is_classhelper(def) then
  1192. begin
  1193. Message(parser_e_no_category_as_types);
  1194. def:=generrordef
  1195. end
  1196. end;
  1197. end
  1198. else
  1199. Message(sym_e_error_in_type_def);
  1200. end;
  1201. pt1.free;
  1202. block_type:=old_block_type;
  1203. end;
  1204. procedure set_dec;
  1205. begin
  1206. consume(_SET);
  1207. consume(_OF);
  1208. read_anon_type(tt2,true);
  1209. if assigned(tt2) then
  1210. begin
  1211. case tt2.typ of
  1212. { don't forget that min can be negativ PM }
  1213. enumdef :
  1214. if (tenumdef(tt2).min>=0) and
  1215. (tenumdef(tt2).max<=255) then
  1216. // !! def:=csetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max),true)
  1217. def:=csetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max,true)
  1218. else
  1219. Message(sym_e_ill_type_decl_set);
  1220. orddef :
  1221. begin
  1222. if (torddef(tt2).ordtype<>uvoid) and
  1223. (torddef(tt2).ordtype<>uwidechar) and
  1224. (torddef(tt2).low>=0) then
  1225. // !! def:=csetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high),true)
  1226. if Torddef(tt2).high>int64(high(byte)) then
  1227. message(sym_e_ill_type_decl_set)
  1228. else
  1229. def:=csetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue,true)
  1230. else
  1231. Message(sym_e_ill_type_decl_set);
  1232. end;
  1233. else
  1234. Message(sym_e_ill_type_decl_set);
  1235. end;
  1236. end
  1237. else
  1238. def:=generrordef;
  1239. end;
  1240. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1241. var
  1242. lowval,
  1243. highval : TConstExprInt;
  1244. indexdef : tdef;
  1245. hdef : tdef;
  1246. arrdef : tarraydef;
  1247. procedure setdefdecl(def:tdef);
  1248. begin
  1249. case def.typ of
  1250. enumdef :
  1251. begin
  1252. lowval:=tenumdef(def).min;
  1253. highval:=tenumdef(def).max;
  1254. if (m_fpc in current_settings.modeswitches) and
  1255. (tenumdef(def).has_jumps) then
  1256. Message(type_e_array_index_enums_with_assign_not_possible);
  1257. indexdef:=def;
  1258. end;
  1259. orddef :
  1260. begin
  1261. if torddef(def).ordtype in [uchar,
  1262. u8bit,
  1263. s8bit,s16bit,
  1264. {$if defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1265. u16bit,s32bit,
  1266. {$endif defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1267. {$ifdef cpu64bitaddr}
  1268. u32bit,s64bit,
  1269. {$endif cpu64bitaddr}
  1270. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
  1271. bool8bit,bool16bit,bool32bit,bool64bit,
  1272. uwidechar] then
  1273. begin
  1274. lowval:=torddef(def).low;
  1275. highval:=torddef(def).high;
  1276. indexdef:=def;
  1277. end
  1278. else
  1279. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1280. end;
  1281. { generic parameter? }
  1282. undefineddef:
  1283. begin
  1284. lowval:=0;
  1285. highval:=1;
  1286. indexdef:=def;
  1287. end;
  1288. else
  1289. Message(sym_e_error_in_type_def);
  1290. end;
  1291. end;
  1292. var
  1293. old_current_genericdef,
  1294. old_current_specializedef: tstoreddef;
  1295. first,
  1296. old_parse_generic: boolean;
  1297. begin
  1298. old_current_genericdef:=current_genericdef;
  1299. old_current_specializedef:=current_specializedef;
  1300. old_parse_generic:=parse_generic;
  1301. current_genericdef:=nil;
  1302. current_specializedef:=nil;
  1303. first:=true;
  1304. arrdef:=carraydef.create(0,0,s32inttype);
  1305. consume(_ARRAY);
  1306. { usage of specialized type inside its generic template }
  1307. if assigned(genericdef) then
  1308. current_specializedef:=arrdef
  1309. { reject declaration of generic class inside generic class }
  1310. else if assigned(genericlist) then
  1311. current_genericdef:=arrdef;
  1312. symtablestack.push(arrdef.symtable);
  1313. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1314. { there are two possibilties for the following to be true:
  1315. * the array declaration itself is generic
  1316. * the array is declared inside a generic
  1317. in both cases we need "parse_generic" and "current_genericdef"
  1318. so that e.g. specializations of another generic inside the
  1319. current generic can be used (either inline ones or "type" ones) }
  1320. if old_parse_generic then
  1321. include(arrdef.defoptions,df_generic);
  1322. parse_generic:=(df_generic in arrdef.defoptions);
  1323. if parse_generic and not assigned(current_genericdef) then
  1324. current_genericdef:=old_current_genericdef;
  1325. { open array? }
  1326. if try_to_consume(_LECKKLAMMER) then
  1327. begin
  1328. { defaults }
  1329. indexdef:=generrordef;
  1330. { use defaults which don't overflow the compiler }
  1331. lowval:=0;
  1332. highval:=0;
  1333. repeat
  1334. { read the expression and check it, check apart if the
  1335. declaration is an enum declaration because that needs to
  1336. be parsed by readtype (PFV) }
  1337. if token=_LKLAMMER then
  1338. begin
  1339. read_anon_type(hdef,true);
  1340. setdefdecl(hdef);
  1341. end
  1342. else
  1343. begin
  1344. pt:=expr(true);
  1345. if pt.nodetype=typen then
  1346. setdefdecl(pt.resultdef)
  1347. else
  1348. begin
  1349. if pt.nodetype=rangen then
  1350. begin
  1351. { pure ordconstn expressions can be checked for
  1352. generics as well, but don't give an error in case
  1353. of parsing a generic if that isn't yet the case }
  1354. if (trangenode(pt).left.nodetype=ordconstn) and
  1355. (trangenode(pt).right.nodetype=ordconstn) then
  1356. begin
  1357. { make both the same type or give an error. This is not
  1358. done when both are integer values, because typecasting
  1359. between -3200..3200 will result in a signed-unsigned
  1360. conflict and give a range check error (PFV) }
  1361. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1362. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1363. lowval:=tordconstnode(trangenode(pt).left).value;
  1364. highval:=tordconstnode(trangenode(pt).right).value;
  1365. if highval<lowval then
  1366. begin
  1367. Message(parser_e_array_lower_less_than_upper_bound);
  1368. highval:=lowval;
  1369. end
  1370. else if (lowval<int64(low(asizeint))) or
  1371. (highval>high(asizeint)) then
  1372. begin
  1373. Message(parser_e_array_range_out_of_bounds);
  1374. lowval :=0;
  1375. highval:=0;
  1376. end;
  1377. if is_integer(trangenode(pt).left.resultdef) then
  1378. range_to_type(lowval,highval,indexdef)
  1379. else
  1380. indexdef:=trangenode(pt).left.resultdef;
  1381. end
  1382. else
  1383. if not parse_generic then
  1384. Message(type_e_cant_eval_constant_expr)
  1385. else
  1386. { we need a valid range for debug information }
  1387. range_to_type(lowval,highval,indexdef);
  1388. end
  1389. else
  1390. Message(sym_e_error_in_type_def)
  1391. end;
  1392. pt.free;
  1393. end;
  1394. { if we are not at the first dimension, add the new arrray
  1395. as element of the existing array, otherwise modify the existing array }
  1396. if not(first) then
  1397. begin
  1398. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1399. { push new symtable }
  1400. symtablestack.pop(arrdef.symtable);
  1401. arrdef:=tarraydef(arrdef.elementdef);
  1402. symtablestack.push(arrdef.symtable);
  1403. end
  1404. else
  1405. begin
  1406. arrdef.lowrange:=lowval.svalue;
  1407. arrdef.highrange:=highval.svalue;
  1408. arrdef.rangedef:=indexdef;
  1409. def:=arrdef;
  1410. first:=false;
  1411. end;
  1412. if is_packed then
  1413. include(arrdef.arrayoptions,ado_IsBitPacked);
  1414. if token=_COMMA then
  1415. consume(_COMMA)
  1416. else
  1417. break;
  1418. until false;
  1419. consume(_RECKKLAMMER);
  1420. end
  1421. else
  1422. begin
  1423. if is_packed then
  1424. Message(parser_e_packed_dynamic_open_array);
  1425. arrdef.lowrange:=0;
  1426. arrdef.highrange:=-1;
  1427. arrdef.rangedef:=s32inttype;
  1428. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1429. def:=arrdef;
  1430. end;
  1431. consume(_OF);
  1432. read_anon_type(tt2,true);
  1433. { set element type of the last array definition }
  1434. if assigned(arrdef) then
  1435. begin
  1436. symtablestack.pop(arrdef.symtable);
  1437. arrdef.elementdef:=tt2;
  1438. if is_packed and
  1439. is_managed_type(tt2) then
  1440. Message(type_e_no_packed_inittable);
  1441. end;
  1442. { restore old state }
  1443. parse_generic:=old_parse_generic;
  1444. current_genericdef:=old_current_genericdef;
  1445. current_specializedef:=old_current_specializedef;
  1446. end;
  1447. function procvar_dec(genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  1448. var
  1449. is_func:boolean;
  1450. pd:tabstractprocdef;
  1451. newtype:ttypesym;
  1452. old_current_genericdef,
  1453. old_current_specializedef: tstoreddef;
  1454. old_parse_generic: boolean;
  1455. begin
  1456. old_current_genericdef:=current_genericdef;
  1457. old_current_specializedef:=current_specializedef;
  1458. old_parse_generic:=parse_generic;
  1459. current_genericdef:=nil;
  1460. current_specializedef:=nil;
  1461. is_func:=(token=_FUNCTION);
  1462. consume(token);
  1463. pd:=cprocvardef.create(normal_function_level);
  1464. { usage of specialized type inside its generic template }
  1465. if assigned(genericdef) then
  1466. current_specializedef:=pd
  1467. { reject declaration of generic class inside generic class }
  1468. else if assigned(genericlist) then
  1469. current_genericdef:=pd;
  1470. symtablestack.push(pd.parast);
  1471. insert_generic_parameter_types(pd,genericdef,genericlist);
  1472. { there are two possibilties for the following to be true:
  1473. * the procvar declaration itself is generic
  1474. * the procvar is declared inside a generic
  1475. in both cases we need "parse_generic" and "current_genericdef"
  1476. so that e.g. specializations of another generic inside the
  1477. current generic can be used (either inline ones or "type" ones) }
  1478. if old_parse_generic then
  1479. include(pd.defoptions,df_generic);
  1480. parse_generic:=(df_generic in pd.defoptions);
  1481. if parse_generic and not assigned(current_genericdef) then
  1482. current_genericdef:=old_current_genericdef;
  1483. { don't allow to add defs to the symtable - use it for type param search only }
  1484. tparasymtable(pd.parast).readonly:=true;
  1485. if token=_LKLAMMER then
  1486. parse_parameter_dec(pd);
  1487. if is_func then
  1488. begin
  1489. consume(_COLON);
  1490. single_type(pd.returndef,[]);
  1491. end;
  1492. if try_to_consume(_OF) then
  1493. begin
  1494. consume(_OBJECT);
  1495. include(pd.procoptions,po_methodpointer);
  1496. end
  1497. else if (m_nested_procvars in current_settings.modeswitches) and
  1498. try_to_consume(_IS) then
  1499. begin
  1500. consume(_NESTED);
  1501. pd.parast.symtablelevel:=normal_function_level+1;
  1502. pd.check_mark_as_nested;
  1503. end;
  1504. symtablestack.pop(pd.parast);
  1505. tparasymtable(pd.parast).readonly:=false;
  1506. result:=pd;
  1507. { possible proc directives }
  1508. if parseprocvardir then
  1509. begin
  1510. if check_proc_directive(true) then
  1511. begin
  1512. newtype:=ctypesym.create('unnamed',result,true);
  1513. parse_var_proc_directives(tsym(newtype));
  1514. newtype.typedef:=nil;
  1515. result.typesym:=nil;
  1516. newtype.free;
  1517. end;
  1518. { Add implicit hidden parameters and function result }
  1519. handle_calling_convention(pd,hcc_default_actions_intf);
  1520. end;
  1521. { restore old state }
  1522. parse_generic:=old_parse_generic;
  1523. current_genericdef:=old_current_genericdef;
  1524. current_specializedef:=old_current_specializedef;
  1525. end;
  1526. const
  1527. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1528. var
  1529. p : tnode;
  1530. hdef : tdef;
  1531. enumdupmsg, first, is_specialize : boolean;
  1532. oldlocalswitches : tlocalswitches;
  1533. bitpacking: boolean;
  1534. stitem: psymtablestackitem;
  1535. sym: tsym;
  1536. st: tsymtable;
  1537. begin
  1538. def:=nil;
  1539. v:=0;
  1540. l:=0;
  1541. if assigned(newsym) then
  1542. name:=newsym.RealName
  1543. else
  1544. name:='';
  1545. case token of
  1546. _STRING,_FILE:
  1547. begin
  1548. single_type(def,[stoAllowTypeDef]);
  1549. end;
  1550. _LKLAMMER:
  1551. begin
  1552. consume(_LKLAMMER);
  1553. first:=true;
  1554. { allow negativ value_str }
  1555. l:=int64(-1);
  1556. enumdupmsg:=false;
  1557. { check that we are not adding an enum from specialization
  1558. we can't just use current_specializedef because of inner types
  1559. like specialize array of record }
  1560. is_specialize:=false;
  1561. stitem:=symtablestack.stack;
  1562. while assigned(stitem) do
  1563. begin
  1564. { check records, classes and arrays because they can be specialized }
  1565. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1566. begin
  1567. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1568. stitem:=stitem^.next;
  1569. end
  1570. else
  1571. break;
  1572. end;
  1573. if not is_specialize then
  1574. aktenumdef:=cenumdef.create
  1575. else
  1576. aktenumdef:=nil;
  1577. repeat
  1578. { if it is a specialization then search the first enum member
  1579. and get the member owner instead of just created enumdef }
  1580. if not assigned(aktenumdef) then
  1581. begin
  1582. searchsym(pattern,sym,st);
  1583. if sym.typ=enumsym then
  1584. aktenumdef:=tenumsym(sym).definition
  1585. else
  1586. internalerror(201101021);
  1587. end;
  1588. s:=orgpattern;
  1589. defpos:=current_tokenpos;
  1590. consume(_ID);
  1591. { only allow assigning of specific numbers under fpc mode }
  1592. if not(m_tp7 in current_settings.modeswitches) and
  1593. (
  1594. { in fpc mode also allow := to be compatible
  1595. with previous 1.0.x versions }
  1596. ((m_fpc in current_settings.modeswitches) and
  1597. try_to_consume(_ASSIGNMENT)) or
  1598. try_to_consume(_EQ)
  1599. ) then
  1600. begin
  1601. oldlocalswitches:=current_settings.localswitches;
  1602. include(current_settings.localswitches,cs_allow_enum_calc);
  1603. p:=comp_expr([ef_accept_equal]);
  1604. current_settings.localswitches:=oldlocalswitches;
  1605. if (p.nodetype=ordconstn) then
  1606. begin
  1607. { we expect an integer or an enum of the
  1608. same type }
  1609. if is_integer(p.resultdef) or
  1610. is_char(p.resultdef) or
  1611. equal_defs(p.resultdef,aktenumdef) then
  1612. v:=tordconstnode(p).value
  1613. else
  1614. IncompatibleTypes(p.resultdef,s32inttype);
  1615. end
  1616. else
  1617. Message(parser_e_illegal_expression);
  1618. p.free;
  1619. { please leave that a note, allows type save }
  1620. { declarations in the win32 units ! }
  1621. if (not first) and (v<=l) and (not enumdupmsg) then
  1622. begin
  1623. Message(parser_n_duplicate_enum);
  1624. enumdupmsg:=true;
  1625. end;
  1626. l:=v;
  1627. end
  1628. else
  1629. inc(l.svalue);
  1630. first:=false;
  1631. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1632. if not is_specialize then
  1633. begin
  1634. storepos:=current_tokenpos;
  1635. current_tokenpos:=defpos;
  1636. tenumsymtable(aktenumdef.symtable).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1637. if not (cs_scopedenums in current_settings.localswitches) then
  1638. tstoredsymtable(aktenumdef.owner).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1639. current_tokenpos:=storepos;
  1640. end;
  1641. until not try_to_consume(_COMMA);
  1642. def:=aktenumdef;
  1643. consume(_RKLAMMER);
  1644. {$ifdef jvm}
  1645. jvm_maybe_create_enum_class(name,def);
  1646. {$endif}
  1647. end;
  1648. _ARRAY:
  1649. begin
  1650. array_dec(false,genericdef,genericlist);
  1651. end;
  1652. _SET:
  1653. begin
  1654. set_dec;
  1655. end;
  1656. _CARET:
  1657. begin
  1658. consume(_CARET);
  1659. single_type(tt2,
  1660. SingleTypeOptionsInTypeBlock[block_type=bt_type]+[stoAllowSpecialization]
  1661. );
  1662. { in case of e.g. var or const sections we need to especially
  1663. check that we don't use a generic dummy symbol }
  1664. if (block_type<>bt_type) and
  1665. (tt2.typ=undefineddef) and
  1666. assigned(tt2.typesym) and
  1667. (sp_generic_dummy in tt2.typesym.symoptions) then
  1668. begin
  1669. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1670. if assigned(sym) and
  1671. not (sp_generic_dummy in sym.symoptions) and
  1672. (sym.typ=typesym) then
  1673. tt2:=ttypesym(sym).typedef
  1674. else
  1675. Message(parser_e_no_generics_as_types);
  1676. end;
  1677. { don't use cpointerdef.getreusable() here, since this is a type
  1678. declaration (-> must create new typedef) }
  1679. def:=cpointerdef.create(tt2);
  1680. if tt2.typ=forwarddef then
  1681. current_module.checkforwarddefs.add(def);
  1682. end;
  1683. _RECORD:
  1684. begin
  1685. consume(token);
  1686. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1687. begin
  1688. consume(_HELPER);
  1689. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1690. end
  1691. else
  1692. def:=record_dec(name,newsym,genericdef,genericlist);
  1693. end;
  1694. _PACKED,
  1695. _BITPACKED:
  1696. begin
  1697. bitpacking :=
  1698. (cs_bitpacking in current_settings.localswitches) or
  1699. (token = _BITPACKED);
  1700. consume(token);
  1701. if token=_ARRAY then
  1702. array_dec(bitpacking,genericdef,genericlist)
  1703. else if token=_SET then
  1704. set_dec
  1705. else if token=_FILE then
  1706. single_type(def,[stoAllowTypeDef])
  1707. else
  1708. begin
  1709. oldpackrecords:=current_settings.packrecords;
  1710. if (not bitpacking) or
  1711. (token in [_CLASS,_OBJECT]) then
  1712. current_settings.packrecords:=1
  1713. else
  1714. current_settings.packrecords:=bit_alignment;
  1715. case token of
  1716. _CLASS :
  1717. begin
  1718. consume(_CLASS);
  1719. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1720. end;
  1721. _OBJECT :
  1722. begin
  1723. consume(_OBJECT);
  1724. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1725. end;
  1726. else begin
  1727. consume(_RECORD);
  1728. def:=record_dec(name,newsym,genericdef,genericlist);
  1729. end;
  1730. end;
  1731. current_settings.packrecords:=oldpackrecords;
  1732. end;
  1733. end;
  1734. _DISPINTERFACE :
  1735. begin
  1736. { need extra check here since interface is a keyword
  1737. in all pascal modes }
  1738. if not(m_class in current_settings.modeswitches) then
  1739. Message(parser_f_need_objfpc_or_delphi_mode);
  1740. consume(token);
  1741. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1742. end;
  1743. _CLASS :
  1744. begin
  1745. consume(token);
  1746. { Delphi only allows class of in type blocks }
  1747. if (token=_OF) and
  1748. (
  1749. not(m_delphi in current_settings.modeswitches) or
  1750. (block_type=bt_type)
  1751. ) then
  1752. begin
  1753. consume(_OF);
  1754. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1755. if is_class(hdef) or
  1756. is_objcclass(hdef) or
  1757. is_javaclass(hdef) then
  1758. def:=cclassrefdef.create(hdef)
  1759. else
  1760. if hdef.typ=forwarddef then
  1761. begin
  1762. def:=cclassrefdef.create(hdef);
  1763. current_module.checkforwarddefs.add(def);
  1764. end
  1765. else
  1766. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1767. end
  1768. else
  1769. if (idtoken=_HELPER) then
  1770. begin
  1771. consume(_HELPER);
  1772. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1773. end
  1774. else
  1775. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1776. end;
  1777. _CPPCLASS :
  1778. begin
  1779. consume(token);
  1780. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1781. end;
  1782. _OBJCCLASS :
  1783. begin
  1784. if not(m_objectivec1 in current_settings.modeswitches) then
  1785. Message(parser_f_need_objc);
  1786. consume(token);
  1787. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1788. end;
  1789. _INTERFACE :
  1790. begin
  1791. { need extra check here since interface is a keyword
  1792. in all pascal modes }
  1793. if not(m_class in current_settings.modeswitches) then
  1794. Message(parser_f_need_objfpc_or_delphi_mode);
  1795. consume(token);
  1796. case current_settings.interfacetype of
  1797. it_interfacecom:
  1798. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1799. it_interfacecorba:
  1800. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1801. it_interfacejava:
  1802. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  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.