ptype.pas 67 KB

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