ptype.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing types for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ptype;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,
  22. symtype,symdef,symbase;
  23. type
  24. TSingleTypeOption=(
  25. stoIsForwardDef, { foward declaration }
  26. stoAllowTypeDef, { allow type definitions }
  27. stoAllowSpecialization, { allow type specialization }
  28. stoParseClassParent { parse of parent class type }
  29. );
  30. TSingleTypeOptions=set of TSingleTypeOption;
  31. procedure resolve_forward_types;
  32. { reads a string, file type or a type identifier }
  33. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  34. { reads any type declaration, where the resulting type will get name as type identifier }
  35. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;parseprocvardir:boolean;var hadtypetoken:boolean);
  36. { reads any type declaration }
  37. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  38. { parse nested type declaration of the def (typedef) }
  39. procedure parse_nested_types(var def: tdef; isforwarddef,allowspecialization: boolean; currentstructstack: tfpobjectlist);
  40. { add a definition for a method to a record/objectdef that will contain
  41. all code for initialising typed constants (only for targets in
  42. systems.systems_typed_constants_node_init) }
  43. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  44. { parse hint directives (platform, deprecated, ...) for a procdef }
  45. procedure maybe_parse_hint_directives(pd:tprocdef);
  46. implementation
  47. uses
  48. { common }
  49. cutils,
  50. { global }
  51. globals,tokens,verbose,constexp,
  52. systems,
  53. { target }
  54. paramgr,procinfo,
  55. { symtable }
  56. symconst,symsym,symtable,symcreat,
  57. defutil,defcmp,objcdef,
  58. {$ifdef jvm}
  59. jvmdef,
  60. {$endif}
  61. { modules }
  62. fmodule,
  63. { pass 1 }
  64. node,
  65. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  66. { parser }
  67. scanner,
  68. pbase,pexpr,pdecsub,pdecvar,pdecobj,pdecl,pgenutil
  69. {$ifdef jvm}
  70. ,pjvm
  71. {$endif}
  72. ;
  73. procedure maybe_parse_hint_directives(pd:tprocdef);
  74. var
  75. dummysymoptions : tsymoptions;
  76. deprecatedmsg : pshortstring;
  77. begin
  78. if assigned(pd) then
  79. begin
  80. dummysymoptions:=pd.symoptions;
  81. deprecatedmsg:=pd.deprecatedmsg;
  82. end
  83. else
  84. begin
  85. dummysymoptions:=[];
  86. deprecatedmsg:=nil;
  87. end;
  88. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  89. consume(_SEMICOLON);
  90. if assigned(pd) then
  91. begin
  92. pd.symoptions:=pd.symoptions+dummysymoptions;
  93. if sp_has_deprecated_msg in dummysymoptions then
  94. pd.deprecatedmsg:=deprecatedmsg;
  95. end
  96. else
  97. stringdispose(deprecatedmsg);
  98. end;
  99. procedure resolve_forward_types;
  100. var
  101. i: longint;
  102. tmp,
  103. hpd,
  104. def : tdef;
  105. srsym : tsym;
  106. srsymtable : TSymtable;
  107. hs : string;
  108. fileinfo : tfileposinfo;
  109. begin
  110. for i:=0 to current_module.checkforwarddefs.Count-1 do
  111. begin
  112. def:=tdef(current_module.checkforwarddefs[i]);
  113. case def.typ of
  114. pointerdef,
  115. classrefdef :
  116. begin
  117. { classrefdef inherits from pointerdef }
  118. hpd:=tabstractpointerdef(def).pointeddef;
  119. { still a forward def ? }
  120. if hpd.typ=forwarddef then
  121. begin
  122. { try to resolve the forward }
  123. if not assigned(tforwarddef(hpd).tosymname) then
  124. internalerror(200211201);
  125. hs:=tforwarddef(hpd).tosymname^;
  126. searchsym(upper(hs),srsym,srsymtable);
  127. { we don't need the forwarddef anymore, dispose it }
  128. hpd.free;
  129. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  130. { was a type sym found ? }
  131. if assigned(srsym) and
  132. (srsym.typ=typesym) then
  133. begin
  134. if (sp_generic_dummy in srsym.symoptions) and
  135. not (ttypesym(srsym).typedef.typ=undefineddef) and
  136. assigned(def.owner.defowner) then
  137. begin
  138. { is the forward def part of a specialization? }
  139. tmp:=tdef(def.owner.defowner);
  140. while not tstoreddef(tmp).is_specialization and assigned(tmp.owner.defowner) do
  141. tmp:=tdef(tmp.owner.defowner);
  142. { if the genericdef of the specialization is the same as the
  143. def the dummy points to, then update the found symbol }
  144. if tstoreddef(tmp).is_specialization and
  145. (tstoreddef(tmp).genericdef=ttypesym(srsym).typedef) then
  146. srsym:=tstoreddef(tmp).typesym;
  147. end;
  148. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  149. { 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 (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,true,true,is_specialize)
  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(var 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 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. if dospecialize then
  506. begin
  507. if def.typ=forwarddef then
  508. def:=ttypesym(srsym).typedef;
  509. generate_specialization(def,stoParseClassParent in options,'');
  510. parse_nested_types(def,stoIsForwardDef in options,[stoAllowSpecialization,stoAllowTypeDef]*options<>[],nil);
  511. end
  512. else
  513. begin
  514. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  515. begin
  516. def:=current_specializedef
  517. end
  518. else if (def=current_genericdef) then
  519. begin
  520. def:=current_genericdef
  521. end
  522. { when parsing a nested specialization in non-Delphi mode it might
  523. use the name of the topmost generic without type paramaters, thus
  524. def will contain the generic definition, but we need a reference
  525. to the specialization of that generic }
  526. { TODO : only in non-Delphi modes? }
  527. else if assigned(current_structdef) and
  528. (df_specialization in current_structdef.defoptions) and
  529. return_specialization_of_generic(current_structdef,def,t2) then
  530. begin
  531. def:=t2
  532. end
  533. else if tstoreddef(def).is_generic and
  534. not
  535. (
  536. parse_generic and
  537. (
  538. { if this is a generic parameter than it has already been checked that this is
  539. a valid usage of a generic }
  540. (sp_generic_para in srsym.symoptions) or
  541. (
  542. (current_genericdef.typ in [recorddef,objectdef]) and
  543. (
  544. { if both defs belong to the same generic (e.g. both are
  545. subtypes) then we must allow the usage }
  546. defs_belong_to_same_generic(def,current_genericdef) or
  547. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  548. declarations inside a generic }
  549. sym_is_owned_by(srsym,tabstractrecorddef(current_genericdef).symtable)
  550. )
  551. )
  552. )
  553. )
  554. then
  555. begin
  556. srsym:=resolve_generic_dummysym(srsym.name);
  557. if assigned(srsym) and
  558. not (sp_generic_dummy in srsym.symoptions) and
  559. (srsym.typ=typesym) then
  560. def:=ttypesym(srsym).typedef
  561. else
  562. begin
  563. Message(parser_e_no_generics_as_types);
  564. def:=generrordef;
  565. end;
  566. end
  567. else if (def.typ=undefineddef) and
  568. (sp_generic_dummy in srsym.symoptions) then
  569. begin
  570. if parse_generic and
  571. (current_genericdef.typ in [recorddef,objectdef]) and
  572. (Pos(upper(srsym.realname),tabstractrecorddef(current_genericdef).objname^)=1) then
  573. begin
  574. if m_delphi in current_settings.modeswitches then
  575. begin
  576. srsym:=resolve_generic_dummysym(srsym.name);
  577. if assigned(srsym) and
  578. not (sp_generic_dummy in srsym.symoptions) and
  579. (srsym.typ=typesym) then
  580. def:=ttypesym(srsym).typedef
  581. else
  582. begin
  583. Message(parser_e_no_generics_as_types);
  584. def:=generrordef;
  585. end;
  586. end
  587. else
  588. def:=current_genericdef;
  589. end
  590. else
  591. begin
  592. Message(parser_e_no_generics_as_types);
  593. def:=generrordef;
  594. end;
  595. end
  596. else if is_classhelper(def) and
  597. not (stoParseClassParent in options) then
  598. begin
  599. Message(parser_e_no_category_as_types);
  600. def:=generrordef
  601. end
  602. end;
  603. end;
  604. procedure parse_record_members(recsym:tsym);
  605. function IsAnonOrLocal: Boolean;
  606. begin
  607. result:=(current_structdef.objname^='') or
  608. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]);
  609. end;
  610. var
  611. olddef : tdef;
  612. procedure set_typesym;
  613. begin
  614. if not assigned(recsym) then
  615. exit;
  616. if ttypesym(recsym).typedef=current_structdef then
  617. exit;
  618. ttypesym(recsym).typedef:=current_structdef;
  619. current_structdef.typesym:=recsym;
  620. end;
  621. procedure reset_typesym;
  622. begin
  623. if not assigned(recsym) then
  624. exit;
  625. if ttypesym(recsym).typedef<>current_structdef then
  626. exit;
  627. ttypesym(recsym).typedef:=olddef;
  628. current_structdef.typesym:=nil;
  629. end;
  630. var
  631. pd : tprocdef;
  632. oldparse_only: boolean;
  633. member_blocktype : tblock_type;
  634. hadgeneric,
  635. fields_allowed, is_classdef, classfields: boolean;
  636. vdoptions: tvar_dec_options;
  637. begin
  638. { empty record declaration ? }
  639. if (token=_SEMICOLON) then
  640. Exit;
  641. { the correct typesym<->def relationship is needed for example when
  642. parsing parameters that are specializations of the record or when
  643. using nested constants and such }
  644. if assigned(recsym) then
  645. olddef:=ttypesym(recsym).typedef
  646. else
  647. olddef:=nil;
  648. set_typesym;
  649. current_structdef.symtable.currentvisibility:=vis_public;
  650. fields_allowed:=true;
  651. is_classdef:=false;
  652. hadgeneric:=false;
  653. classfields:=false;
  654. member_blocktype:=bt_general;
  655. repeat
  656. case token of
  657. _TYPE :
  658. begin
  659. consume(_TYPE);
  660. member_blocktype:=bt_type;
  661. { local and anonymous records can not have inner types. skip top record symtable }
  662. if IsAnonOrLocal then
  663. Message(parser_e_no_types_in_local_anonymous_records);
  664. end;
  665. _VAR :
  666. begin
  667. consume(_VAR);
  668. fields_allowed:=true;
  669. member_blocktype:=bt_general;
  670. classfields:=is_classdef;
  671. is_classdef:=false;
  672. end;
  673. _CONST:
  674. begin
  675. consume(_CONST);
  676. member_blocktype:=bt_const;
  677. { local and anonymous records can not have constants. skip top record symtable }
  678. if IsAnonOrLocal then
  679. Message(parser_e_no_consts_in_local_anonymous_records);
  680. end;
  681. _ID, _CASE, _OPERATOR :
  682. begin
  683. case idtoken of
  684. _PRIVATE :
  685. begin
  686. consume(_PRIVATE);
  687. current_structdef.symtable.currentvisibility:=vis_private;
  688. include(current_structdef.objectoptions,oo_has_private);
  689. fields_allowed:=true;
  690. is_classdef:=false;
  691. classfields:=false;
  692. member_blocktype:=bt_general;
  693. end;
  694. _PROTECTED :
  695. begin
  696. Message1(parser_e_not_allowed_in_record,tokeninfo^[_PROTECTED].str);
  697. consume(_PROTECTED);
  698. current_structdef.symtable.currentvisibility:=vis_protected;
  699. include(current_structdef.objectoptions,oo_has_protected);
  700. fields_allowed:=true;
  701. is_classdef:=false;
  702. classfields:=false;
  703. member_blocktype:=bt_general;
  704. end;
  705. _PUBLIC :
  706. begin
  707. consume(_PUBLIC);
  708. current_structdef.symtable.currentvisibility:=vis_public;
  709. fields_allowed:=true;
  710. is_classdef:=false;
  711. classfields:=false;
  712. member_blocktype:=bt_general;
  713. end;
  714. _PUBLISHED :
  715. begin
  716. Message(parser_e_no_record_published);
  717. consume(_PUBLISHED);
  718. current_structdef.symtable.currentvisibility:=vis_published;
  719. fields_allowed:=true;
  720. is_classdef:=false;
  721. classfields:=false;
  722. member_blocktype:=bt_general;
  723. end;
  724. _STRICT :
  725. begin
  726. consume(_STRICT);
  727. if token=_ID then
  728. begin
  729. case idtoken of
  730. _PRIVATE:
  731. begin
  732. consume(_PRIVATE);
  733. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  734. include(current_structdef.objectoptions,oo_has_strictprivate);
  735. end;
  736. _PROTECTED:
  737. begin
  738. { "strict protected" is not allowed for records }
  739. Message1(parser_e_not_allowed_in_record,tokeninfo^[_STRICT].str+' '+tokeninfo^[_PROTECTED].str);
  740. consume(_PROTECTED);
  741. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  742. include(current_structdef.objectoptions,oo_has_strictprotected);
  743. end;
  744. else
  745. message(parser_e_protected_or_private_expected);
  746. end;
  747. end
  748. else
  749. message(parser_e_protected_or_private_expected);
  750. fields_allowed:=true;
  751. is_classdef:=false;
  752. classfields:=false;
  753. member_blocktype:=bt_general;
  754. end
  755. else
  756. if is_classdef and (idtoken=_OPERATOR) then
  757. begin
  758. pd:=parse_record_method_dec(current_structdef,is_classdef,false);
  759. fields_allowed:=false;
  760. is_classdef:=false;
  761. end
  762. else
  763. begin
  764. if member_blocktype=bt_general then
  765. begin
  766. if (idtoken=_GENERIC) and
  767. not (m_delphi in current_settings.modeswitches) and
  768. not fields_allowed then
  769. begin
  770. if hadgeneric then
  771. Message(parser_e_procedure_or_function_expected);
  772. consume(_ID);
  773. hadgeneric:=true;
  774. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  775. Message(parser_e_procedure_or_function_expected);
  776. end
  777. else
  778. begin
  779. if (not fields_allowed)and(idtoken<>_CASE) then
  780. Message(parser_e_field_not_allowed_here);
  781. vdoptions:=[vd_record];
  782. if classfields then
  783. include(vdoptions,vd_class);
  784. if not (m_delphi in current_settings.modeswitches) then
  785. include(vdoptions,vd_check_generic);
  786. read_record_fields(vdoptions,nil,nil,hadgeneric);
  787. end;
  788. end
  789. else if member_blocktype=bt_type then
  790. types_dec(true,hadgeneric)
  791. else if member_blocktype=bt_const then
  792. consts_dec(true,true,hadgeneric)
  793. else
  794. internalerror(201001110);
  795. end;
  796. end;
  797. end;
  798. _PROPERTY :
  799. begin
  800. if IsAnonOrLocal then
  801. Message(parser_e_no_properties_in_local_anonymous_records);
  802. struct_property_dec(is_classdef);
  803. fields_allowed:=false;
  804. is_classdef:=false;
  805. end;
  806. _CLASS:
  807. begin
  808. is_classdef:=false;
  809. { read class method/field/property }
  810. consume(_CLASS);
  811. { class modifier is only allowed for procedures, functions, }
  812. { constructors, destructors, fields and properties }
  813. if (hadgeneric and not (token in [_FUNCTION,_PROCEDURE])) or
  814. (not hadgeneric and (not (token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  815. not((token=_ID) and (idtoken=_OPERATOR)))) then
  816. Message(parser_e_procedure_or_function_expected);
  817. if IsAnonOrLocal then
  818. Message(parser_e_no_class_in_local_anonymous_records);
  819. is_classdef:=true;
  820. end;
  821. _PROCEDURE,
  822. _FUNCTION:
  823. begin
  824. if IsAnonOrLocal then
  825. Message(parser_e_no_methods_in_local_anonymous_records);
  826. pd:=parse_record_method_dec(current_structdef,is_classdef,hadgeneric);
  827. hadgeneric:=false;
  828. fields_allowed:=false;
  829. is_classdef:=false;
  830. end;
  831. _CONSTRUCTOR :
  832. begin
  833. if IsAnonOrLocal then
  834. Message(parser_e_no_methods_in_local_anonymous_records);
  835. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  836. Message(parser_w_constructor_should_be_public);
  837. { only 1 class constructor is allowed }
  838. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  839. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  840. oldparse_only:=parse_only;
  841. parse_only:=true;
  842. if is_classdef then
  843. pd:=class_constructor_head(current_structdef)
  844. else
  845. begin
  846. pd:=constructor_head;
  847. if pd.minparacount = 0 then
  848. MessagePos(pd.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  849. end;
  850. parse_only:=oldparse_only;
  851. fields_allowed:=false;
  852. is_classdef:=false;
  853. end;
  854. _DESTRUCTOR :
  855. begin
  856. if IsAnonOrLocal then
  857. Message(parser_e_no_methods_in_local_anonymous_records);
  858. if not is_classdef then
  859. Message(parser_e_no_destructor_in_records);
  860. { only 1 class destructor is allowed }
  861. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  862. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  863. oldparse_only:=parse_only;
  864. parse_only:=true;
  865. if is_classdef then
  866. pd:=class_destructor_head(current_structdef)
  867. else
  868. pd:=destructor_head;
  869. parse_only:=oldparse_only;
  870. fields_allowed:=false;
  871. is_classdef:=false;
  872. end;
  873. _END :
  874. begin
  875. {$ifdef jvm}
  876. add_java_default_record_methods_intf(trecorddef(current_structdef));
  877. {$endif}
  878. if target_info.system in systems_typed_constants_node_init then
  879. add_typedconst_init_routine(current_structdef);
  880. consume(_END);
  881. break;
  882. end;
  883. else
  884. consume(_ID); { Give a ident expected message, like tp7 }
  885. end;
  886. until false;
  887. reset_typesym;
  888. end;
  889. { reads a record declaration }
  890. function record_dec(const n:tidstring;recsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  891. var
  892. old_current_structdef: tabstractrecorddef;
  893. old_current_genericdef,
  894. old_current_specializedef: tstoreddef;
  895. old_parse_generic: boolean;
  896. recst: trecordsymtable;
  897. hadgendummy : boolean;
  898. begin
  899. old_current_structdef:=current_structdef;
  900. old_current_genericdef:=current_genericdef;
  901. old_current_specializedef:=current_specializedef;
  902. old_parse_generic:=parse_generic;
  903. current_genericdef:=nil;
  904. current_specializedef:=nil;
  905. { create recdef }
  906. if (n<>'') or
  907. not(target_info.system in systems_jvm) then
  908. begin
  909. recst:=trecordsymtable.create(n,current_settings.packrecords,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  910. { can't use recst.realname^ instead of n, because recst.realname is
  911. nil in case of an empty name }
  912. current_structdef:=crecorddef.create(n,recst);
  913. end
  914. else
  915. begin
  916. { for the JVM target records always need a name, because they are
  917. represented by a class }
  918. recst:=trecordsymtable.create(current_module.realmodulename^+'__fpc_intern_recname_'+tostr(current_module.deflist.count),
  919. current_settings.packrecords,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  920. current_structdef:=crecorddef.create(recst.name^,recst);
  921. end;
  922. result:=current_structdef;
  923. { insert in symtablestack }
  924. symtablestack.push(recst);
  925. { usage of specialized type inside its generic template }
  926. if assigned(genericdef) then
  927. current_specializedef:=current_structdef
  928. { reject declaration of generic class inside generic class }
  929. else if assigned(genericlist) then
  930. current_genericdef:=current_structdef;
  931. { nested types of specializations are specializations as well }
  932. if assigned(old_current_structdef) and
  933. (df_specialization in old_current_structdef.defoptions) then
  934. include(current_structdef.defoptions,df_specialization);
  935. if assigned(old_current_structdef) and
  936. (df_generic in old_current_structdef.defoptions) then
  937. include(current_structdef.defoptions,df_generic);
  938. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  939. { when we are parsing a generic already then this is a generic as
  940. well }
  941. if old_parse_generic then
  942. include(current_structdef.defoptions, df_generic);
  943. parse_generic:=(df_generic in current_structdef.defoptions);
  944. if parse_generic and not assigned(current_genericdef) then
  945. current_genericdef:=current_structdef;
  946. { in non-Delphi modes we need a strict private symbol without type
  947. count and type parameters in the name to simply resolving }
  948. maybe_insert_generic_rename_symbol(n,genericlist);
  949. if m_advanced_records in current_settings.modeswitches then
  950. begin
  951. parse_record_members(recsym);
  952. end
  953. else
  954. begin
  955. read_record_fields([vd_record],nil,nil,hadgendummy);
  956. {$ifdef jvm}
  957. { we need a constructor to create temps, a deep copy helper, ... }
  958. add_java_default_record_methods_intf(trecorddef(current_structdef));
  959. {$endif}
  960. if target_info.system in systems_typed_constants_node_init then
  961. add_typedconst_init_routine(current_structdef);
  962. consume(_END);
  963. end;
  964. { make the record size aligned (has to be done before inserting the
  965. parameters, because that may depend on the record's size) }
  966. recst.addalignmentpadding;
  967. { don't keep track of procdefs in a separate list, because the
  968. compiler may add additional procdefs (e.g. property wrappers for
  969. the jvm backend) }
  970. insert_record_hidden_paras(trecorddef(current_structdef));
  971. { restore symtable stack }
  972. symtablestack.pop(recst);
  973. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  974. Message(type_e_no_packed_inittable);
  975. { restore old state }
  976. parse_generic:=old_parse_generic;
  977. current_structdef:=old_current_structdef;
  978. current_genericdef:=old_current_genericdef;
  979. current_specializedef:=old_current_specializedef;
  980. end;
  981. { reads a type definition and returns a pointer to it }
  982. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;parseprocvardir:boolean;var hadtypetoken:boolean);
  983. var
  984. pt : tnode;
  985. tt2 : tdef;
  986. aktenumdef : tenumdef;
  987. s : TIDString;
  988. l,v : TConstExprInt;
  989. oldpackrecords : longint;
  990. defpos,storepos : tfileposinfo;
  991. name: TIDString;
  992. procedure expr_type;
  993. var
  994. pt1,pt2 : tnode;
  995. lv,hv : TConstExprInt;
  996. old_block_type : tblock_type;
  997. dospecialize : boolean;
  998. newdef : tdef;
  999. sym : tsym;
  1000. genstr : string;
  1001. gencount : longint;
  1002. begin
  1003. old_block_type:=block_type;
  1004. dospecialize:=false;
  1005. { use of current parsed object:
  1006. classes, objects, records can be used also in themself }
  1007. if (token=_ID) then
  1008. if try_parse_structdef_nested_type(def,current_structdef,false) then
  1009. exit;
  1010. { we can't accept a equal in type }
  1011. pt1:=comp_expr([ef_type_only]);
  1012. if try_to_consume(_POINTPOINT) then
  1013. begin
  1014. { get high value of range }
  1015. pt2:=comp_expr([]);
  1016. { make both the same type or give an error. This is not
  1017. done when both are integer values, because typecasting
  1018. between -3200..3200 will result in a signed-unsigned
  1019. conflict and give a range check error (PFV) }
  1020. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  1021. inserttypeconv(pt1,pt2.resultdef);
  1022. { both must be evaluated to constants now }
  1023. if (pt1.nodetype=ordconstn) and
  1024. (pt2.nodetype=ordconstn) then
  1025. begin
  1026. lv:=tordconstnode(pt1).value;
  1027. hv:=tordconstnode(pt2).value;
  1028. { Check bounds }
  1029. if hv<lv then
  1030. message(parser_e_upper_lower_than_lower)
  1031. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  1032. message(type_e_cant_eval_constant_expr)
  1033. else
  1034. begin
  1035. { All checks passed, create the new def }
  1036. case pt1.resultdef.typ of
  1037. enumdef :
  1038. def:=cenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  1039. orddef :
  1040. begin
  1041. if is_char(pt1.resultdef) then
  1042. def:=corddef.create(uchar,lv,hv,true)
  1043. else
  1044. if is_boolean(pt1.resultdef) then
  1045. def:=corddef.create(pasbool8,lv,hv,true)
  1046. else if is_signed(pt1.resultdef) then
  1047. def:=corddef.create(range_to_basetype(lv,hv),lv,hv,true)
  1048. else
  1049. def:=corddef.create(range_to_basetype(lv,hv),lv,hv,true);
  1050. end;
  1051. end;
  1052. end;
  1053. end
  1054. else
  1055. Message(sym_e_error_in_type_def);
  1056. pt2.free;
  1057. end
  1058. else
  1059. begin
  1060. { a simple type renaming or generic specialization }
  1061. if (pt1.nodetype=typen) then
  1062. begin
  1063. def:=ttypenode(pt1).resultdef;
  1064. { Delphi mode specialization? }
  1065. if (m_delphi in current_settings.modeswitches) then
  1066. dospecialize:=token=_LSHARPBRACKET
  1067. else
  1068. begin
  1069. dospecialize:=false;
  1070. { in non-Delphi modes we might get a inline specialization
  1071. without "specialize" or "<T>" of the same type we're
  1072. currently parsing, so we need to handle that special }
  1073. newdef:=nil;
  1074. end;
  1075. if not dospecialize and
  1076. assigned(ttypenode(pt1).typesym) and
  1077. (ttypenode(pt1).typesym.typ=typesym) and
  1078. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  1079. assigned(current_structdef) and
  1080. (
  1081. (
  1082. not (m_delphi in current_settings.modeswitches) and
  1083. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  1084. (df_generic in current_structdef.defoptions) and
  1085. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  1086. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  1087. ) or (
  1088. { this could be a nested specialization which uses
  1089. the type name of a surrounding generic to
  1090. reference the specialization of said surrounding
  1091. class }
  1092. (df_specialization in current_structdef.defoptions) and
  1093. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  1094. )
  1095. )
  1096. then
  1097. begin
  1098. if assigned(newdef) then
  1099. def:=newdef
  1100. else
  1101. def:=current_structdef;
  1102. if assigned(def) then
  1103. { handle nested types }
  1104. post_comp_expr_gendef(def)
  1105. else
  1106. def:=generrordef;
  1107. end;
  1108. if dospecialize then
  1109. begin
  1110. generate_specialization(def,false,name);
  1111. { handle nested types }
  1112. if assigned(def) then
  1113. post_comp_expr_gendef(def);
  1114. end
  1115. else
  1116. begin
  1117. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  1118. begin
  1119. def:=current_specializedef
  1120. end
  1121. else if (def=current_genericdef) then
  1122. begin
  1123. def:=current_genericdef
  1124. end
  1125. else if tstoreddef(def).is_generic and
  1126. { TODO : check once nested generics are allowed }
  1127. not
  1128. (
  1129. parse_generic and
  1130. (current_genericdef.typ in [recorddef,objectdef]) and
  1131. (def.typ in [recorddef,objectdef]) and
  1132. (
  1133. { if both defs belong to the same generic (e.g. both are
  1134. subtypes) then we must allow the usage }
  1135. defs_belong_to_same_generic(def,current_genericdef) or
  1136. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  1137. declarations inside a generic }
  1138. (
  1139. (ttypenode(pt1).typesym<>nil) and
  1140. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  1141. )
  1142. )
  1143. )
  1144. then
  1145. begin
  1146. if assigned(def.typesym) then
  1147. begin
  1148. if ttypesym(def.typesym).typedef.typ<>undefineddef then
  1149. { non-Delphi modes... }
  1150. split_generic_name(def.typesym.name,genstr,gencount)
  1151. else
  1152. genstr:=def.typesym.name;
  1153. sym:=resolve_generic_dummysym(genstr);
  1154. end
  1155. else
  1156. sym:=nil;
  1157. if assigned(sym) and
  1158. not (sp_generic_dummy in sym.symoptions) and
  1159. (sym.typ=typesym) then
  1160. def:=ttypesym(sym).typedef
  1161. else
  1162. begin
  1163. Message(parser_e_no_generics_as_types);
  1164. def:=generrordef;
  1165. end;
  1166. end
  1167. else if is_classhelper(def) then
  1168. begin
  1169. Message(parser_e_no_category_as_types);
  1170. def:=generrordef
  1171. end
  1172. end;
  1173. end
  1174. else
  1175. Message(sym_e_error_in_type_def);
  1176. end;
  1177. pt1.free;
  1178. block_type:=old_block_type;
  1179. end;
  1180. procedure set_dec;
  1181. begin
  1182. consume(_SET);
  1183. consume(_OF);
  1184. read_anon_type(tt2,true);
  1185. if assigned(tt2) then
  1186. begin
  1187. case tt2.typ of
  1188. { don't forget that min can be negativ PM }
  1189. enumdef :
  1190. if (tenumdef(tt2).min>=0) and
  1191. (tenumdef(tt2).max<=255) then
  1192. // !! def:=csetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max),true)
  1193. def:=csetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max,true)
  1194. else
  1195. Message(sym_e_ill_type_decl_set);
  1196. orddef :
  1197. begin
  1198. if (torddef(tt2).ordtype<>uvoid) and
  1199. (torddef(tt2).ordtype<>uwidechar) and
  1200. (torddef(tt2).low>=0) then
  1201. // !! def:=csetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high),true)
  1202. if Torddef(tt2).high>int64(high(byte)) then
  1203. message(sym_e_ill_type_decl_set)
  1204. else
  1205. def:=csetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue,true)
  1206. else
  1207. Message(sym_e_ill_type_decl_set);
  1208. end;
  1209. else
  1210. Message(sym_e_ill_type_decl_set);
  1211. end;
  1212. end
  1213. else
  1214. def:=generrordef;
  1215. end;
  1216. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1217. var
  1218. lowval,
  1219. highval : TConstExprInt;
  1220. indexdef : tdef;
  1221. hdef : tdef;
  1222. arrdef : tarraydef;
  1223. procedure setdefdecl(def:tdef);
  1224. begin
  1225. case def.typ of
  1226. enumdef :
  1227. begin
  1228. lowval:=tenumdef(def).min;
  1229. highval:=tenumdef(def).max;
  1230. if (m_fpc in current_settings.modeswitches) and
  1231. (tenumdef(def).has_jumps) then
  1232. Message(type_e_array_index_enums_with_assign_not_possible);
  1233. indexdef:=def;
  1234. end;
  1235. orddef :
  1236. begin
  1237. if torddef(def).ordtype in [uchar,
  1238. u8bit,
  1239. s8bit,s16bit,
  1240. {$if defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1241. u16bit,s32bit,
  1242. {$endif defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1243. {$ifdef cpu64bitaddr}
  1244. u32bit,s64bit,
  1245. {$endif cpu64bitaddr}
  1246. pasbool8,pasbool16,pasbool32,pasbool64,
  1247. bool8bit,bool16bit,bool32bit,bool64bit,
  1248. uwidechar] then
  1249. begin
  1250. lowval:=torddef(def).low;
  1251. highval:=torddef(def).high;
  1252. indexdef:=def;
  1253. end
  1254. else
  1255. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1256. end;
  1257. else
  1258. Message(sym_e_error_in_type_def);
  1259. end;
  1260. end;
  1261. var
  1262. old_current_genericdef,
  1263. old_current_specializedef: tstoreddef;
  1264. first,
  1265. old_parse_generic: boolean;
  1266. begin
  1267. old_current_genericdef:=current_genericdef;
  1268. old_current_specializedef:=current_specializedef;
  1269. old_parse_generic:=parse_generic;
  1270. current_genericdef:=nil;
  1271. current_specializedef:=nil;
  1272. first:=true;
  1273. arrdef:=carraydef.create(0,0,s32inttype);
  1274. consume(_ARRAY);
  1275. { usage of specialized type inside its generic template }
  1276. if assigned(genericdef) then
  1277. current_specializedef:=arrdef
  1278. { reject declaration of generic class inside generic class }
  1279. else if assigned(genericlist) then
  1280. current_genericdef:=arrdef;
  1281. symtablestack.push(arrdef.symtable);
  1282. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1283. { there are two possibilties for the following to be true:
  1284. * the array declaration itself is generic
  1285. * the array is declared inside a generic
  1286. in both cases we need "parse_generic" and "current_genericdef"
  1287. so that e.g. specializations of another generic inside the
  1288. current generic can be used (either inline ones or "type" ones) }
  1289. if old_parse_generic then
  1290. include(arrdef.defoptions,df_generic);
  1291. parse_generic:=(df_generic in arrdef.defoptions);
  1292. if parse_generic and not assigned(current_genericdef) then
  1293. current_genericdef:=old_current_genericdef;
  1294. { open array? }
  1295. if try_to_consume(_LECKKLAMMER) then
  1296. begin
  1297. { defaults }
  1298. indexdef:=generrordef;
  1299. { use defaults which don't overflow the compiler }
  1300. lowval:=0;
  1301. highval:=0;
  1302. repeat
  1303. { read the expression and check it, check apart if the
  1304. declaration is an enum declaration because that needs to
  1305. be parsed by readtype (PFV) }
  1306. if token=_LKLAMMER then
  1307. begin
  1308. read_anon_type(hdef,true);
  1309. setdefdecl(hdef);
  1310. end
  1311. else
  1312. begin
  1313. pt:=expr(true);
  1314. if pt.nodetype=typen then
  1315. setdefdecl(pt.resultdef)
  1316. else
  1317. begin
  1318. if pt.nodetype=rangen then
  1319. begin
  1320. { pure ordconstn expressions can be checked for
  1321. generics as well, but don't give an error in case
  1322. of parsing a generic if that isn't yet the case }
  1323. if (trangenode(pt).left.nodetype=ordconstn) and
  1324. (trangenode(pt).right.nodetype=ordconstn) then
  1325. begin
  1326. { make both the same type or give an error. This is not
  1327. done when both are integer values, because typecasting
  1328. between -3200..3200 will result in a signed-unsigned
  1329. conflict and give a range check error (PFV) }
  1330. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1331. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1332. lowval:=tordconstnode(trangenode(pt).left).value;
  1333. highval:=tordconstnode(trangenode(pt).right).value;
  1334. if highval<lowval then
  1335. begin
  1336. Message(parser_e_array_lower_less_than_upper_bound);
  1337. highval:=lowval;
  1338. end
  1339. else if (lowval<int64(low(asizeint))) or
  1340. (highval>high(asizeint)) then
  1341. begin
  1342. Message(parser_e_array_range_out_of_bounds);
  1343. lowval :=0;
  1344. highval:=0;
  1345. end;
  1346. if is_integer(trangenode(pt).left.resultdef) then
  1347. range_to_type(lowval,highval,indexdef)
  1348. else
  1349. indexdef:=trangenode(pt).left.resultdef;
  1350. end
  1351. else
  1352. if not parse_generic then
  1353. Message(type_e_cant_eval_constant_expr)
  1354. else
  1355. { we need a valid range for debug information }
  1356. range_to_type(lowval,highval,indexdef);
  1357. end
  1358. else
  1359. Message(sym_e_error_in_type_def)
  1360. end;
  1361. pt.free;
  1362. end;
  1363. { if we are not at the first dimension, add the new arrray
  1364. as element of the existing array, otherwise modify the existing array }
  1365. if not(first) then
  1366. begin
  1367. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1368. { push new symtable }
  1369. symtablestack.pop(arrdef.symtable);
  1370. arrdef:=tarraydef(arrdef.elementdef);
  1371. symtablestack.push(arrdef.symtable);
  1372. end
  1373. else
  1374. begin
  1375. arrdef.lowrange:=lowval.svalue;
  1376. arrdef.highrange:=highval.svalue;
  1377. arrdef.rangedef:=indexdef;
  1378. def:=arrdef;
  1379. first:=false;
  1380. end;
  1381. if is_packed then
  1382. include(arrdef.arrayoptions,ado_IsBitPacked);
  1383. if token=_COMMA then
  1384. consume(_COMMA)
  1385. else
  1386. break;
  1387. until false;
  1388. consume(_RECKKLAMMER);
  1389. end
  1390. else
  1391. begin
  1392. if is_packed then
  1393. Message(parser_e_packed_dynamic_open_array);
  1394. arrdef.lowrange:=0;
  1395. arrdef.highrange:=-1;
  1396. arrdef.rangedef:=s32inttype;
  1397. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1398. def:=arrdef;
  1399. end;
  1400. consume(_OF);
  1401. read_anon_type(tt2,true);
  1402. { set element type of the last array definition }
  1403. if assigned(arrdef) then
  1404. begin
  1405. symtablestack.pop(arrdef.symtable);
  1406. arrdef.elementdef:=tt2;
  1407. if is_packed and
  1408. is_managed_type(tt2) then
  1409. Message(type_e_no_packed_inittable);
  1410. end;
  1411. { restore old state }
  1412. parse_generic:=old_parse_generic;
  1413. current_genericdef:=old_current_genericdef;
  1414. current_specializedef:=old_current_specializedef;
  1415. end;
  1416. function procvar_dec(genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  1417. var
  1418. is_func:boolean;
  1419. pd:tabstractprocdef;
  1420. newtype:ttypesym;
  1421. old_current_genericdef,
  1422. old_current_specializedef: tstoreddef;
  1423. old_parse_generic: boolean;
  1424. begin
  1425. old_current_genericdef:=current_genericdef;
  1426. old_current_specializedef:=current_specializedef;
  1427. old_parse_generic:=parse_generic;
  1428. current_genericdef:=nil;
  1429. current_specializedef:=nil;
  1430. is_func:=(token=_FUNCTION);
  1431. consume(token);
  1432. pd:=cprocvardef.create(normal_function_level);
  1433. { usage of specialized type inside its generic template }
  1434. if assigned(genericdef) then
  1435. current_specializedef:=pd
  1436. { reject declaration of generic class inside generic class }
  1437. else if assigned(genericlist) then
  1438. current_genericdef:=pd;
  1439. symtablestack.push(pd.parast);
  1440. insert_generic_parameter_types(pd,genericdef,genericlist);
  1441. { there are two possibilties for the following to be true:
  1442. * the procvar declaration itself is generic
  1443. * the procvar is declared inside a generic
  1444. in both cases we need "parse_generic" and "current_genericdef"
  1445. so that e.g. specializations of another generic inside the
  1446. current generic can be used (either inline ones or "type" ones) }
  1447. if old_parse_generic then
  1448. include(pd.defoptions,df_generic);
  1449. parse_generic:=(df_generic in pd.defoptions);
  1450. if parse_generic and not assigned(current_genericdef) then
  1451. current_genericdef:=old_current_genericdef;
  1452. { don't allow to add defs to the symtable - use it for type param search only }
  1453. tparasymtable(pd.parast).readonly:=true;
  1454. if token=_LKLAMMER then
  1455. parse_parameter_dec(pd);
  1456. if is_func then
  1457. begin
  1458. consume(_COLON);
  1459. single_type(pd.returndef,[]);
  1460. end;
  1461. if try_to_consume(_OF) then
  1462. begin
  1463. consume(_OBJECT);
  1464. include(pd.procoptions,po_methodpointer);
  1465. end
  1466. else if (m_nested_procvars in current_settings.modeswitches) and
  1467. try_to_consume(_IS) then
  1468. begin
  1469. consume(_NESTED);
  1470. pd.parast.symtablelevel:=normal_function_level+1;
  1471. pd.check_mark_as_nested;
  1472. end;
  1473. symtablestack.pop(pd.parast);
  1474. tparasymtable(pd.parast).readonly:=false;
  1475. result:=pd;
  1476. { possible proc directives }
  1477. if parseprocvardir then
  1478. begin
  1479. if check_proc_directive(true) then
  1480. begin
  1481. newtype:=ctypesym.create('unnamed',result,true);
  1482. parse_var_proc_directives(tsym(newtype));
  1483. newtype.typedef:=nil;
  1484. result.typesym:=nil;
  1485. newtype.free;
  1486. end;
  1487. { Add implicit hidden parameters and function result }
  1488. handle_calling_convention(pd);
  1489. end;
  1490. { restore old state }
  1491. parse_generic:=old_parse_generic;
  1492. current_genericdef:=old_current_genericdef;
  1493. current_specializedef:=old_current_specializedef;
  1494. end;
  1495. const
  1496. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1497. var
  1498. p : tnode;
  1499. hdef : tdef;
  1500. enumdupmsg, first, is_specialize : boolean;
  1501. oldlocalswitches : tlocalswitches;
  1502. bitpacking: boolean;
  1503. stitem: psymtablestackitem;
  1504. sym: tsym;
  1505. st: tsymtable;
  1506. begin
  1507. def:=nil;
  1508. v:=0;
  1509. l:=0;
  1510. if assigned(newsym) then
  1511. name:=newsym.RealName
  1512. else
  1513. name:='';
  1514. case token of
  1515. _STRING,_FILE:
  1516. begin
  1517. single_type(def,[stoAllowTypeDef]);
  1518. end;
  1519. _LKLAMMER:
  1520. begin
  1521. consume(_LKLAMMER);
  1522. first:=true;
  1523. { allow negativ value_str }
  1524. l:=int64(-1);
  1525. enumdupmsg:=false;
  1526. { check that we are not adding an enum from specialization
  1527. we can't just use current_specializedef because of inner types
  1528. like specialize array of record }
  1529. is_specialize:=false;
  1530. stitem:=symtablestack.stack;
  1531. while assigned(stitem) do
  1532. begin
  1533. { check records, classes and arrays because they can be specialized }
  1534. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1535. begin
  1536. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1537. stitem:=stitem^.next;
  1538. end
  1539. else
  1540. break;
  1541. end;
  1542. if not is_specialize then
  1543. aktenumdef:=cenumdef.create
  1544. else
  1545. aktenumdef:=nil;
  1546. repeat
  1547. { if it is a specialization then search the first enum member
  1548. and get the member owner instead of just created enumdef }
  1549. if not assigned(aktenumdef) then
  1550. begin
  1551. searchsym(pattern,sym,st);
  1552. if sym.typ=enumsym then
  1553. aktenumdef:=tenumsym(sym).definition
  1554. else
  1555. internalerror(201101021);
  1556. end;
  1557. s:=orgpattern;
  1558. defpos:=current_tokenpos;
  1559. consume(_ID);
  1560. { only allow assigning of specific numbers under fpc mode }
  1561. if not(m_tp7 in current_settings.modeswitches) and
  1562. (
  1563. { in fpc mode also allow := to be compatible
  1564. with previous 1.0.x versions }
  1565. ((m_fpc in current_settings.modeswitches) and
  1566. try_to_consume(_ASSIGNMENT)) or
  1567. try_to_consume(_EQ)
  1568. ) then
  1569. begin
  1570. oldlocalswitches:=current_settings.localswitches;
  1571. include(current_settings.localswitches,cs_allow_enum_calc);
  1572. p:=comp_expr([ef_accept_equal]);
  1573. current_settings.localswitches:=oldlocalswitches;
  1574. if (p.nodetype=ordconstn) then
  1575. begin
  1576. { we expect an integer or an enum of the
  1577. same type }
  1578. if is_integer(p.resultdef) or
  1579. is_char(p.resultdef) or
  1580. equal_defs(p.resultdef,aktenumdef) then
  1581. v:=tordconstnode(p).value
  1582. else
  1583. IncompatibleTypes(p.resultdef,s32inttype);
  1584. end
  1585. else
  1586. Message(parser_e_illegal_expression);
  1587. p.free;
  1588. { please leave that a note, allows type save }
  1589. { declarations in the win32 units ! }
  1590. if (not first) and (v<=l) and (not enumdupmsg) then
  1591. begin
  1592. Message(parser_n_duplicate_enum);
  1593. enumdupmsg:=true;
  1594. end;
  1595. l:=v;
  1596. end
  1597. else
  1598. inc(l.svalue);
  1599. first:=false;
  1600. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1601. if not is_specialize then
  1602. begin
  1603. storepos:=current_tokenpos;
  1604. current_tokenpos:=defpos;
  1605. tenumsymtable(aktenumdef.symtable).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1606. if not (cs_scopedenums in current_settings.localswitches) then
  1607. tstoredsymtable(aktenumdef.owner).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1608. current_tokenpos:=storepos;
  1609. end;
  1610. until not try_to_consume(_COMMA);
  1611. def:=aktenumdef;
  1612. consume(_RKLAMMER);
  1613. {$ifdef jvm}
  1614. jvm_maybe_create_enum_class(name,def);
  1615. {$endif}
  1616. end;
  1617. _ARRAY:
  1618. begin
  1619. array_dec(false,genericdef,genericlist);
  1620. end;
  1621. _SET:
  1622. begin
  1623. set_dec;
  1624. end;
  1625. _CARET:
  1626. begin
  1627. consume(_CARET);
  1628. single_type(tt2,
  1629. SingleTypeOptionsInTypeBlock[block_type=bt_type]+[stoAllowSpecialization]
  1630. );
  1631. { in case of e.g. var or const sections we need to especially
  1632. check that we don't use a generic dummy symbol }
  1633. if (block_type<>bt_type) and
  1634. (tt2.typ=undefineddef) and
  1635. assigned(tt2.typesym) and
  1636. (sp_generic_dummy in tt2.typesym.symoptions) then
  1637. begin
  1638. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1639. if assigned(sym) and
  1640. not (sp_generic_dummy in sym.symoptions) and
  1641. (sym.typ=typesym) then
  1642. tt2:=ttypesym(sym).typedef
  1643. else
  1644. Message(parser_e_no_generics_as_types);
  1645. end;
  1646. { don't use cpointerdef.getreusable() here, since this is a type
  1647. declaration (-> must create new typedef) }
  1648. def:=cpointerdef.create(tt2);
  1649. if tt2.typ=forwarddef then
  1650. current_module.checkforwarddefs.add(def);
  1651. end;
  1652. _RECORD:
  1653. begin
  1654. consume(token);
  1655. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1656. begin
  1657. consume(_HELPER);
  1658. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1659. end
  1660. else
  1661. def:=record_dec(name,newsym,genericdef,genericlist);
  1662. end;
  1663. _PACKED,
  1664. _BITPACKED:
  1665. begin
  1666. bitpacking :=
  1667. (cs_bitpacking in current_settings.localswitches) or
  1668. (token = _BITPACKED);
  1669. consume(token);
  1670. if token=_ARRAY then
  1671. array_dec(bitpacking,genericdef,genericlist)
  1672. else if token=_SET then
  1673. set_dec
  1674. else if token=_FILE then
  1675. single_type(def,[stoAllowTypeDef])
  1676. else
  1677. begin
  1678. oldpackrecords:=current_settings.packrecords;
  1679. if (not bitpacking) or
  1680. (token in [_CLASS,_OBJECT]) then
  1681. current_settings.packrecords:=1
  1682. else
  1683. current_settings.packrecords:=bit_alignment;
  1684. case token of
  1685. _CLASS :
  1686. begin
  1687. consume(_CLASS);
  1688. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1689. end;
  1690. _OBJECT :
  1691. begin
  1692. consume(_OBJECT);
  1693. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1694. end;
  1695. else begin
  1696. consume(_RECORD);
  1697. def:=record_dec(name,newsym,genericdef,genericlist);
  1698. end;
  1699. end;
  1700. current_settings.packrecords:=oldpackrecords;
  1701. end;
  1702. end;
  1703. _DISPINTERFACE :
  1704. begin
  1705. { need extra check here since interface is a keyword
  1706. in all pascal modes }
  1707. if not(m_class in current_settings.modeswitches) then
  1708. Message(parser_f_need_objfpc_or_delphi_mode);
  1709. consume(token);
  1710. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1711. end;
  1712. _CLASS :
  1713. begin
  1714. consume(token);
  1715. { Delphi only allows class of in type blocks }
  1716. if (token=_OF) and
  1717. (
  1718. not(m_delphi in current_settings.modeswitches) or
  1719. (block_type=bt_type)
  1720. ) then
  1721. begin
  1722. consume(_OF);
  1723. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1724. if is_class(hdef) or
  1725. is_objcclass(hdef) or
  1726. is_javaclass(hdef) then
  1727. def:=cclassrefdef.create(hdef)
  1728. else
  1729. if hdef.typ=forwarddef then
  1730. begin
  1731. def:=cclassrefdef.create(hdef);
  1732. current_module.checkforwarddefs.add(def);
  1733. end
  1734. else
  1735. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1736. end
  1737. else
  1738. if (idtoken=_HELPER) then
  1739. begin
  1740. consume(_HELPER);
  1741. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1742. end
  1743. else
  1744. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1745. end;
  1746. _CPPCLASS :
  1747. begin
  1748. consume(token);
  1749. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1750. end;
  1751. _OBJCCLASS :
  1752. begin
  1753. if not(m_objectivec1 in current_settings.modeswitches) then
  1754. Message(parser_f_need_objc);
  1755. consume(token);
  1756. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1757. end;
  1758. _INTERFACE :
  1759. begin
  1760. { need extra check here since interface is a keyword
  1761. in all pascal modes }
  1762. if not(m_class in current_settings.modeswitches) then
  1763. Message(parser_f_need_objfpc_or_delphi_mode);
  1764. consume(token);
  1765. case current_settings.interfacetype of
  1766. it_interfacecom:
  1767. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1768. it_interfacecorba:
  1769. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1770. it_interfacejava:
  1771. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1772. else
  1773. internalerror(2010122612);
  1774. end;
  1775. end;
  1776. _OBJCPROTOCOL :
  1777. begin
  1778. if not(m_objectivec1 in current_settings.modeswitches) then
  1779. Message(parser_f_need_objc);
  1780. consume(token);
  1781. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1782. end;
  1783. _OBJCCATEGORY :
  1784. begin
  1785. if not(m_objectivec1 in current_settings.modeswitches) then
  1786. Message(parser_f_need_objc);
  1787. consume(token);
  1788. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1789. end;
  1790. _OBJECT :
  1791. begin
  1792. consume(token);
  1793. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1794. end;
  1795. _PROCEDURE,
  1796. _FUNCTION:
  1797. begin
  1798. def:=procvar_dec(genericdef,genericlist);
  1799. {$ifdef jvm}
  1800. jvm_create_procvar_class(name,def);
  1801. {$endif}
  1802. end;
  1803. _ID:
  1804. begin
  1805. case idtoken of
  1806. _HELPER:
  1807. begin
  1808. if hadtypetoken and
  1809. { don't allow "type helper" in mode delphi and require modeswitch typehelpers }
  1810. ([m_delphi,m_type_helpers]*current_settings.modeswitches=[m_type_helpers]) then
  1811. begin
  1812. { reset hadtypetoken, so that calling code knows that it should not be handled
  1813. as a "unique" type }
  1814. hadtypetoken:=false;
  1815. consume(_HELPER);
  1816. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  1817. end
  1818. else
  1819. expr_type
  1820. end;
  1821. _REFERENCE:
  1822. begin
  1823. if m_blocks in current_settings.modeswitches then
  1824. begin
  1825. consume(_REFERENCE);
  1826. consume(_TO);
  1827. def:=procvar_dec(genericdef,genericlist);
  1828. { could be errordef in case of a syntax error }
  1829. if assigned(def) and
  1830. (def.typ=procvardef) then
  1831. include(tprocvardef(def).procoptions,po_is_function_ref);
  1832. end
  1833. else
  1834. expr_type;
  1835. end;
  1836. else
  1837. expr_type;
  1838. end;
  1839. end
  1840. else
  1841. if (token=_KLAMMERAFFE) and (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1842. begin
  1843. consume(_KLAMMERAFFE);
  1844. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1845. def:=cpointerdef.create(tt2);
  1846. if tt2.typ=forwarddef then
  1847. current_module.checkforwarddefs.add(def);
  1848. end
  1849. else
  1850. expr_type;
  1851. end;
  1852. if def=nil then
  1853. def:=generrordef;
  1854. end;
  1855. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1856. var
  1857. hadtypetoken : boolean;
  1858. begin
  1859. hadtypetoken:=false;
  1860. read_named_type(def,nil,nil,nil,parseprocvardir,hadtypetoken);
  1861. end;
  1862. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1863. var
  1864. sstate: tscannerstate;
  1865. pd: tprocdef;
  1866. begin
  1867. replace_scanner('tcinit_routine',sstate);
  1868. { the typed constant initialization code is called from the class
  1869. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1870. know yet whether that will be necessary, because there may be
  1871. typed constants inside method bodies -> always force the addition
  1872. of a class constructor.
  1873. We cannot directly add the typed constant initialisations to the
  1874. class constructor, because when it's parsed not all method bodies
  1875. are necessarily already parsed }
  1876. pd:=def.find_procdef_bytype(potype_class_constructor);
  1877. { the class constructor }
  1878. if not assigned(pd) then
  1879. begin
  1880. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1881. pd.synthetickind:=tsk_empty
  1882. else
  1883. internalerror(2011040206);
  1884. end;
  1885. { the initialisation helper }
  1886. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1887. pd.synthetickind:=tsk_tcinit
  1888. else
  1889. internalerror(2011040207);
  1890. restore_scanner(sstate);
  1891. end;
  1892. end.