ptype.pas 68 KB

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