pdecl.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does declaration (but not type) parsing 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 pdecl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,
  25. { symtable }
  26. symsym,symdef,
  27. { pass_1 }
  28. node;
  29. function readconstant(const orgname:string;const filepos:tfileposinfo; out nodetype: tnodetype):tconstsym;
  30. procedure const_dec;
  31. procedure consts_dec(in_structure, allow_typed_const: boolean);
  32. procedure label_dec;
  33. procedure type_dec;
  34. procedure types_dec(in_structure: boolean);
  35. procedure var_dec;
  36. procedure threadvar_dec;
  37. procedure property_dec;
  38. procedure resourcestring_dec;
  39. implementation
  40. uses
  41. SysUtils,
  42. { common }
  43. cutils,
  44. { global }
  45. globals,tokens,verbose,widestr,constexp,
  46. systems,
  47. { aasm }
  48. aasmbase,aasmtai,aasmdata,fmodule,
  49. { symtable }
  50. symconst,symbase,symtype,symtable,symcreat,paramgr,defutil,
  51. { pass 1 }
  52. htypechk,
  53. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nobj,
  54. { codegen }
  55. ncgutil,ngenutil,
  56. { parser }
  57. scanner,
  58. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,pgenutil,
  59. {$ifdef jvm}
  60. pjvm,
  61. {$endif}
  62. { cpu-information }
  63. cpuinfo
  64. ;
  65. function readconstant(const orgname:string;const filepos:tfileposinfo; out nodetype: tnodetype):tconstsym;
  66. var
  67. hp : tconstsym;
  68. p : tnode;
  69. ps : pconstset;
  70. pd : pbestreal;
  71. pg : pguid;
  72. sp : pchar;
  73. pw : pcompilerwidestring;
  74. storetokenpos : tfileposinfo;
  75. begin
  76. readconstant:=nil;
  77. if orgname='' then
  78. internalerror(9584582);
  79. hp:=nil;
  80. p:=comp_expr(true,false);
  81. nodetype:=p.nodetype;
  82. storetokenpos:=current_tokenpos;
  83. current_tokenpos:=filepos;
  84. case p.nodetype of
  85. ordconstn:
  86. begin
  87. if p.resultdef.typ=pointerdef then
  88. hp:=tconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value.uvalue,p.resultdef)
  89. else
  90. hp:=tconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resultdef);
  91. end;
  92. stringconstn:
  93. begin
  94. if is_wide_or_unicode_string(p.resultdef) then
  95. begin
  96. initwidestring(pw);
  97. copywidestring(pcompilerwidestring(tstringconstnode(p).value_str),pw);
  98. hp:=tconstsym.create_wstring(orgname,constwstring,pw);
  99. end
  100. else
  101. begin
  102. getmem(sp,tstringconstnode(p).len+1);
  103. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  104. hp:=tconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len);
  105. end;
  106. end;
  107. realconstn :
  108. begin
  109. new(pd);
  110. pd^:=trealconstnode(p).value_real;
  111. hp:=tconstsym.create_ptr(orgname,constreal,pd,p.resultdef);
  112. end;
  113. setconstn :
  114. begin
  115. new(ps);
  116. ps^:=tsetconstnode(p).value_set^;
  117. hp:=tconstsym.create_ptr(orgname,constset,ps,p.resultdef);
  118. end;
  119. pointerconstn :
  120. begin
  121. hp:=tconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resultdef);
  122. end;
  123. niln :
  124. begin
  125. hp:=tconstsym.create_ord(orgname,constnil,0,p.resultdef);
  126. end;
  127. typen :
  128. begin
  129. if is_interface(p.resultdef) then
  130. begin
  131. if assigned(tobjectdef(p.resultdef).iidguid) then
  132. begin
  133. new(pg);
  134. pg^:=tobjectdef(p.resultdef).iidguid^;
  135. hp:=tconstsym.create_ptr(orgname,constguid,pg,p.resultdef);
  136. end
  137. else
  138. Message1(parser_e_interface_has_no_guid,tobjectdef(p.resultdef).objrealname^);
  139. end
  140. else
  141. Message(parser_e_illegal_expression);
  142. end;
  143. inlinen:
  144. begin
  145. { this situation only happens if a intrinsic is parsed that has a
  146. generic type as its argument. As we don't know certain
  147. information about the final type yet, we need to use safe
  148. values (mostly 0) }
  149. if not parse_generic then
  150. Message(parser_e_illegal_expression);
  151. case tinlinenode(p).inlinenumber of
  152. in_sizeof_x,
  153. in_bitsizeof_x:
  154. begin
  155. hp:=tconstsym.create_ord(orgname,constord,0,p.resultdef);
  156. end;
  157. { add other cases here if necessary }
  158. else
  159. Message(parser_e_illegal_expression);
  160. end;
  161. end;
  162. else
  163. Message(parser_e_illegal_expression);
  164. end;
  165. current_tokenpos:=storetokenpos;
  166. p.free;
  167. readconstant:=hp;
  168. end;
  169. procedure const_dec;
  170. begin
  171. consume(_CONST);
  172. consts_dec(false,true);
  173. end;
  174. procedure consts_dec(in_structure, allow_typed_const: boolean);
  175. var
  176. orgname : TIDString;
  177. hdef : tdef;
  178. sym : tsym;
  179. dummysymoptions : tsymoptions;
  180. deprecatedmsg : pshortstring;
  181. storetokenpos,filepos : tfileposinfo;
  182. nodetype : tnodetype;
  183. old_block_type : tblock_type;
  184. skipequal : boolean;
  185. tclist : tasmlist;
  186. varspez : tvarspez;
  187. begin
  188. old_block_type:=block_type;
  189. block_type:=bt_const;
  190. repeat
  191. orgname:=orgpattern;
  192. filepos:=current_tokenpos;
  193. consume(_ID);
  194. case token of
  195. _EQ:
  196. begin
  197. consume(_EQ);
  198. sym:=readconstant(orgname,filepos,nodetype);
  199. { Support hint directives }
  200. dummysymoptions:=[];
  201. deprecatedmsg:=nil;
  202. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  203. if assigned(sym) then
  204. begin
  205. sym.symoptions:=sym.symoptions+dummysymoptions;
  206. sym.deprecatedmsg:=deprecatedmsg;
  207. sym.visibility:=symtablestack.top.currentvisibility;
  208. symtablestack.top.insert(sym);
  209. {$ifdef jvm}
  210. { for the JVM target, some constants need to be
  211. initialized at run time (enums, sets) -> create fake
  212. typed const to do so }
  213. if assigned(tconstsym(sym).constdef) and
  214. (tconstsym(sym).constdef.typ in [enumdef,setdef]) then
  215. jvm_add_typed_const_initializer(tconstsym(sym));
  216. {$endif}
  217. end
  218. else
  219. stringdispose(deprecatedmsg);
  220. consume(_SEMICOLON);
  221. end;
  222. _COLON:
  223. begin
  224. if not allow_typed_const then
  225. begin
  226. Message(parser_e_no_typed_const);
  227. consume_all_until(_SEMICOLON);
  228. end;
  229. { set the blocktype first so a consume also supports a
  230. caret, to support const s : ^string = nil }
  231. block_type:=bt_const_type;
  232. consume(_COLON);
  233. read_anon_type(hdef,false);
  234. block_type:=bt_const;
  235. skipequal:=false;
  236. { create symbol }
  237. storetokenpos:=current_tokenpos;
  238. current_tokenpos:=filepos;
  239. if not (cs_typed_const_writable in current_settings.localswitches) then
  240. varspez:=vs_const
  241. else
  242. varspez:=vs_value;
  243. { if we are dealing with structure const then we need to handle it as a
  244. structure static variable: create a symbol in unit symtable and a reference
  245. to it from the structure or linking will fail }
  246. if symtablestack.top.symtabletype in [recordsymtable,ObjectSymtable] then
  247. begin
  248. sym:=tfieldvarsym.create(orgname,varspez,hdef,[]);
  249. symtablestack.top.insert(sym);
  250. sym:=make_field_static(symtablestack.top,tfieldvarsym(sym));
  251. end
  252. else
  253. begin
  254. sym:=tstaticvarsym.create(orgname,varspez,hdef,[]);
  255. sym.visibility:=symtablestack.top.currentvisibility;
  256. symtablestack.top.insert(sym);
  257. end;
  258. current_tokenpos:=storetokenpos;
  259. { procvar can have proc directives, but not type references }
  260. if (hdef.typ=procvardef) and
  261. (hdef.typesym=nil) then
  262. begin
  263. { support p : procedure;stdcall=nil; }
  264. if try_to_consume(_SEMICOLON) then
  265. begin
  266. if check_proc_directive(true) then
  267. parse_var_proc_directives(sym)
  268. else
  269. begin
  270. Message(parser_e_proc_directive_expected);
  271. skipequal:=true;
  272. end;
  273. end
  274. else
  275. { support p : procedure stdcall=nil; }
  276. begin
  277. if check_proc_directive(true) then
  278. parse_var_proc_directives(sym);
  279. end;
  280. { add default calling convention }
  281. handle_calling_convention(tabstractprocdef(hdef));
  282. end;
  283. if not skipequal then
  284. begin
  285. { get init value }
  286. consume(_EQ);
  287. if (cs_typed_const_writable in current_settings.localswitches) then
  288. tclist:=current_asmdata.asmlists[al_typedconsts]
  289. else
  290. tclist:=current_asmdata.asmlists[al_rotypedconsts];
  291. read_typed_const(tclist,tstaticvarsym(sym),in_structure);
  292. end;
  293. end;
  294. else
  295. { generate an error }
  296. consume(_EQ);
  297. end;
  298. until (token<>_ID) or
  299. (in_structure and
  300. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  301. ((m_final_fields in current_settings.modeswitches) and
  302. (idtoken=_FINAL))));
  303. block_type:=old_block_type;
  304. end;
  305. procedure label_dec;
  306. var
  307. labelsym : tlabelsym;
  308. begin
  309. consume(_LABEL);
  310. if not(cs_support_goto in current_settings.moduleswitches) then
  311. Message(sym_e_goto_and_label_not_supported);
  312. repeat
  313. if not(token in [_ID,_INTCONST]) then
  314. consume(_ID)
  315. else
  316. begin
  317. if token=_ID then
  318. labelsym:=tlabelsym.create(orgpattern)
  319. else
  320. labelsym:=tlabelsym.create(pattern);
  321. symtablestack.top.insert(labelsym);
  322. if m_non_local_goto in current_settings.modeswitches then
  323. begin
  324. if symtablestack.top.symtabletype=localsymtable then
  325. begin
  326. labelsym.jumpbuf:=tlocalvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[]);
  327. symtablestack.top.insert(labelsym.jumpbuf);
  328. end
  329. else
  330. begin
  331. labelsym.jumpbuf:=tstaticvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[]);
  332. symtablestack.top.insert(labelsym.jumpbuf);
  333. cnodeutils.insertbssdata(tstaticvarsym(labelsym.jumpbuf));
  334. end;
  335. include(labelsym.jumpbuf.symoptions,sp_internal);
  336. { the buffer will be setup later, but avoid a hint }
  337. tabstractvarsym(labelsym.jumpbuf).varstate:=vs_written;
  338. end;
  339. consume(token);
  340. end;
  341. if token<>_SEMICOLON then consume(_COMMA);
  342. until not(token in [_ID,_INTCONST]);
  343. consume(_SEMICOLON);
  344. end;
  345. procedure types_dec(in_structure: boolean);
  346. procedure finalize_class_external_status(od: tobjectdef);
  347. begin
  348. if [oo_is_external,oo_is_forward] <= od.objectoptions then
  349. begin
  350. { formal definition: x = objcclass external; }
  351. exclude(od.objectoptions,oo_is_forward);
  352. include(od.objectoptions,oo_is_formal);
  353. end;
  354. end;
  355. var
  356. typename,orgtypename,
  357. gentypename,genorgtypename : TIDString;
  358. newtype : ttypesym;
  359. sym : tsym;
  360. hdef : tdef;
  361. defpos,storetokenpos : tfileposinfo;
  362. old_block_type : tblock_type;
  363. old_checkforwarddefs: TFPObjectList;
  364. objecttype : tobjecttyp;
  365. isgeneric,
  366. isunique,
  367. istyperenaming : boolean;
  368. generictypelist : TFPObjectList;
  369. generictokenbuf : tdynamicarray;
  370. vmtbuilder : TVMTBuilder;
  371. p:tnode;
  372. gendef : tstoreddef;
  373. s : shortstring;
  374. pd: tprocdef;
  375. hashedid : thashedidstring;
  376. begin
  377. old_block_type:=block_type;
  378. { save unit container of forward declarations -
  379. we can be inside nested class type block }
  380. old_checkforwarddefs:=current_module.checkforwarddefs;
  381. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  382. block_type:=bt_type;
  383. repeat
  384. defpos:=current_tokenpos;
  385. istyperenaming:=false;
  386. generictypelist:=nil;
  387. generictokenbuf:=nil;
  388. { fpc generic declaration? }
  389. isgeneric:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  390. typename:=pattern;
  391. orgtypename:=orgpattern;
  392. consume(_ID);
  393. { delphi generic declaration? }
  394. if (m_delphi in current_settings.modeswitches) then
  395. isgeneric:=token=_LSHARPBRACKET;
  396. { Generic type declaration? }
  397. if isgeneric then
  398. begin
  399. if assigned(current_genericdef) then
  400. Message(parser_f_no_generic_inside_generic);
  401. consume(_LSHARPBRACKET);
  402. generictypelist:=parse_generic_parameters;
  403. consume(_RSHARPBRACKET);
  404. str(generictypelist.Count,s);
  405. gentypename:=typename+'$'+s;
  406. genorgtypename:=orgtypename+'$'+s;
  407. end
  408. else
  409. begin
  410. gentypename:=typename;
  411. genorgtypename:=orgtypename;
  412. end;
  413. consume(_EQ);
  414. { support 'ttype=type word' syntax }
  415. isunique:=try_to_consume(_TYPE);
  416. { MacPas object model is more like Delphi's than like TP's, but }
  417. { uses the object keyword instead of class }
  418. if (m_mac in current_settings.modeswitches) and
  419. (token = _OBJECT) then
  420. token := _CLASS;
  421. { Start recording a generic template }
  422. if assigned(generictypelist) then
  423. begin
  424. generictokenbuf:=tdynamicarray.create(256);
  425. current_scanner.startrecordtokens(generictokenbuf);
  426. end;
  427. { is the type already defined? -- must be in the current symtable,
  428. not in a nested symtable or one higher up the stack -> don't
  429. use searchsym & frinds! }
  430. sym:=tsym(symtablestack.top.find(gentypename));
  431. newtype:=nil;
  432. { found a symbol with this name? }
  433. if assigned(sym) then
  434. begin
  435. if (sym.typ=typesym) and
  436. { this should not be a symbol that was created by a generic
  437. that was declared earlier }
  438. not (
  439. (ttypesym(sym).typedef.typ=undefineddef) and
  440. (sp_generic_dummy in sym.symoptions)
  441. ) then
  442. begin
  443. if ((token=_CLASS) or
  444. (token=_INTERFACE) or
  445. (token=_DISPINTERFACE) or
  446. (token=_OBJCCLASS) or
  447. (token=_OBJCPROTOCOL) or
  448. (token=_OBJCCATEGORY)) and
  449. (assigned(ttypesym(sym).typedef)) and
  450. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  451. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  452. begin
  453. case token of
  454. _CLASS :
  455. objecttype:=default_class_type;
  456. _INTERFACE :
  457. case current_settings.interfacetype of
  458. it_interfacecom:
  459. objecttype:=odt_interfacecom;
  460. it_interfacecorba:
  461. objecttype:=odt_interfacecorba;
  462. it_interfacejava:
  463. objecttype:=odt_interfacejava;
  464. else
  465. internalerror(2010122611);
  466. end;
  467. _DISPINTERFACE :
  468. objecttype:=odt_dispinterface;
  469. _OBJCCLASS,
  470. _OBJCCATEGORY :
  471. objecttype:=odt_objcclass;
  472. _OBJCPROTOCOL :
  473. objecttype:=odt_objcprotocol;
  474. else
  475. internalerror(200811072);
  476. end;
  477. consume(token);
  478. { we can ignore the result, the definition is modified }
  479. object_dec(objecttype,genorgtypename,newtype,nil,nil,tobjectdef(ttypesym(sym).typedef),ht_none);
  480. newtype:=ttypesym(sym);
  481. hdef:=newtype.typedef;
  482. end
  483. else
  484. message1(parser_h_type_redef,genorgtypename);
  485. end;
  486. end;
  487. { no old type reused ? Then insert this new type }
  488. if not assigned(newtype) then
  489. begin
  490. { insert the new type first with an errordef, so that
  491. referencing the type before it's really set it
  492. will give an error (PFV) }
  493. hdef:=generrordef;
  494. gendef:=nil;
  495. storetokenpos:=current_tokenpos;
  496. if isgeneric then
  497. begin
  498. { for generics we need to check whether a non-generic type
  499. already exists and if not we need to insert a symbol with
  500. the non-generic name (available in (org)typename) that is a
  501. undefineddef, so that inline specializations can be used }
  502. sym:=tsym(symtablestack.top.Find(typename));
  503. if not assigned(sym) then
  504. begin
  505. sym:=ttypesym.create(orgtypename,tundefineddef.create);
  506. Include(sym.symoptions,sp_generic_dummy);
  507. ttypesym(sym).typedef.typesym:=sym;
  508. sym.visibility:=symtablestack.top.currentvisibility;
  509. symtablestack.top.insert(sym);
  510. ttypesym(sym).typedef.owner:=sym.owner;
  511. end
  512. else
  513. { this is not allowed in non-Delphi modes }
  514. if not (m_delphi in current_settings.modeswitches) then
  515. Message1(sym_e_duplicate_id,genorgtypename)
  516. else
  517. { we need to find this symbol even if it's a variable or
  518. something else when doing an inline specialization }
  519. Include(sym.symoptions,sp_generic_dummy);
  520. end
  521. else
  522. begin
  523. if assigned(sym) and (sym.typ=typesym) and
  524. (ttypesym(sym).typedef.typ=undefineddef) and
  525. (sp_generic_dummy in sym.symoptions) then
  526. begin
  527. { this is a symbol that was added by an earlier generic
  528. declaration, reuse it }
  529. newtype:=ttypesym(sym);
  530. newtype.typedef:=hdef;
  531. sym:=nil;
  532. end;
  533. { check whether this is a declaration of a type inside a
  534. specialization }
  535. if assigned(current_structdef) and
  536. (df_specialization in current_structdef.defoptions) then
  537. begin
  538. if not assigned(current_structdef.genericdef) or
  539. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  540. internalerror(2011052301);
  541. hashedid.id:=gentypename;
  542. { we could be inside a method of the specialization
  543. instead of its declaration, so check that first (as
  544. local nested types aren't allowed we don't need to
  545. walk the symtablestack to find the localsymtable) }
  546. if symtablestack.top.symtabletype=localsymtable then
  547. begin
  548. { we are in a method }
  549. if not assigned(symtablestack.top.defowner) or
  550. (symtablestack.top.defowner.typ<>procdef) then
  551. internalerror(2011120701);
  552. pd:=tprocdef(symtablestack.top.defowner);
  553. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  554. internalerror(2011120702);
  555. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  556. end
  557. else
  558. sym:=nil;
  559. if not assigned(sym) or not (sym.typ=typesym) then
  560. begin
  561. { now search in the declaration of the generic }
  562. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  563. if not assigned(sym) or not (sym.typ=typesym) then
  564. internalerror(2011052302);
  565. end;
  566. { use the corresponding type in the generic's symtable as
  567. genericdef for the specialized type }
  568. gendef:=tstoreddef(ttypesym(sym).typedef);
  569. end;
  570. end;
  571. { insert a new type if we don't reuse an existing symbol }
  572. if not assigned(newtype) then
  573. begin
  574. newtype:=ttypesym.create(genorgtypename,hdef);
  575. newtype.visibility:=symtablestack.top.currentvisibility;
  576. symtablestack.top.insert(newtype);
  577. end;
  578. current_tokenpos:=defpos;
  579. current_tokenpos:=storetokenpos;
  580. { read the type definition }
  581. read_named_type(hdef,newtype,gendef,generictypelist,false);
  582. { update the definition of the type }
  583. if assigned(hdef) then
  584. begin
  585. if df_generic in hdef.defoptions then
  586. { flag parent symtables that they now contain a generic }
  587. hdef.owner.includeoption(sto_has_generic);
  588. if assigned(hdef.typesym) then
  589. begin
  590. istyperenaming:=true;
  591. include(newtype.symoptions,sp_explicitrename);
  592. end;
  593. if isunique then
  594. begin
  595. if is_objc_class_or_protocol(hdef) or
  596. is_java_class_or_interface(hdef) then
  597. Message(parser_e_unique_unsupported);
  598. hdef:=tstoreddef(hdef).getcopy;
  599. { check if it is an ansistirng(codepage) declaration }
  600. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  601. begin
  602. p:=comp_expr(true,false);
  603. consume(_RKLAMMER);
  604. if not is_constintnode(p) then
  605. begin
  606. Message(parser_e_illegal_expression);
  607. { error recovery }
  608. end
  609. else
  610. begin
  611. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  612. begin
  613. Message(parser_e_invalid_codepage);
  614. tordconstnode(p).value:=0;
  615. end;
  616. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  617. end;
  618. p.free;
  619. end;
  620. { fix name, it is used e.g. for tables }
  621. if is_class_or_interface_or_dispinterface(hdef) then
  622. with tobjectdef(hdef) do
  623. begin
  624. stringdispose(objname);
  625. stringdispose(objrealname);
  626. objrealname:=stringdup(genorgtypename);
  627. objname:=stringdup(upper(genorgtypename));
  628. end;
  629. include(hdef.defoptions,df_unique);
  630. if (hdef.typ in [pointerdef,classrefdef]) and
  631. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  632. current_module.checkforwarddefs.add(hdef);
  633. end;
  634. if not assigned(hdef.typesym) then
  635. hdef.typesym:=newtype;
  636. end;
  637. { in non-Delphi modes we need a reference to the generic def
  638. without the generic suffix, so it can be found easily when
  639. parsing method implementations }
  640. if isgeneric and assigned(sym) and
  641. not (m_delphi in current_settings.modeswitches) and
  642. (ttypesym(sym).typedef.typ=undefineddef) then
  643. { don't free the undefineddef as the defids rely on the count
  644. of the defs in the def list of the module}
  645. ttypesym(sym).typedef:=hdef;
  646. newtype.typedef:=hdef;
  647. { KAZ: handle TGUID declaration in system unit }
  648. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  649. (gentypename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  650. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  651. rec_tguid:=trecorddef(hdef);
  652. end;
  653. if assigned(hdef) then
  654. begin
  655. case hdef.typ of
  656. pointerdef :
  657. begin
  658. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  659. consume(_SEMICOLON);
  660. if try_to_consume(_FAR) then
  661. begin
  662. tpointerdef(hdef).is_far:=true;
  663. consume(_SEMICOLON);
  664. end;
  665. end;
  666. procvardef :
  667. begin
  668. { in case of type renaming, don't parse proc directives }
  669. if istyperenaming then
  670. begin
  671. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  672. consume(_SEMICOLON);
  673. end
  674. else
  675. begin
  676. if not check_proc_directive(true) then
  677. begin
  678. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  679. consume(_SEMICOLON);
  680. end;
  681. parse_var_proc_directives(tsym(newtype));
  682. handle_calling_convention(tprocvardef(hdef));
  683. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  684. consume(_SEMICOLON);
  685. end;
  686. end;
  687. objectdef :
  688. begin
  689. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  690. consume(_SEMICOLON);
  691. { change a forward and external class declaration into
  692. formal external definition, so the compiler does not
  693. expect an real definition later }
  694. if is_objc_class_or_protocol(hdef) or
  695. is_java_class_or_interface(hdef) then
  696. finalize_class_external_status(tobjectdef(hdef));
  697. { Build VMT indexes, skip for type renaming and forward classes }
  698. if (hdef.typesym=newtype) and
  699. not(oo_is_forward in tobjectdef(hdef).objectoptions) and
  700. not(df_generic in hdef.defoptions) then
  701. begin
  702. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  703. vmtbuilder.generate_vmt;
  704. vmtbuilder.free;
  705. end;
  706. { In case of an objcclass, verify that all methods have a message
  707. name set. We only check this now, because message names can be set
  708. during the protocol (interface) mapping. At the same time, set the
  709. mangled names (these depend on the "external" name of the class),
  710. and mark private fields of external classes as "used" (to avoid
  711. bogus notes about them being unused)
  712. }
  713. { watch out for crashes in case of errors }
  714. if is_objc_class_or_protocol(hdef) and
  715. (not is_objccategory(hdef) or
  716. assigned(tobjectdef(hdef).childof)) then
  717. tobjectdef(hdef).finish_objc_data;
  718. if is_cppclass(hdef) then
  719. tobjectdef(hdef).finish_cpp_data;
  720. end;
  721. recorddef :
  722. begin
  723. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  724. consume(_SEMICOLON);
  725. end;
  726. else
  727. begin
  728. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  729. consume(_SEMICOLON);
  730. end;
  731. end;
  732. end;
  733. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  734. or is_objectpascal_helper(hdef)) then
  735. message(parser_e_cant_create_generics_of_this_type);
  736. { Stop recording a generic template }
  737. if assigned(generictypelist) then
  738. begin
  739. current_scanner.stoprecordtokens;
  740. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  741. { Generic is never a type renaming }
  742. hdef.typesym:=newtype;
  743. generictypelist.free;
  744. end;
  745. until (token<>_ID) or
  746. (in_structure and
  747. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  748. ((m_final_fields in current_settings.modeswitches) and
  749. (idtoken=_FINAL))));
  750. { resolve type block forward declarations and restore a unit
  751. container for them }
  752. resolve_forward_types;
  753. current_module.checkforwarddefs.free;
  754. current_module.checkforwarddefs:=old_checkforwarddefs;
  755. block_type:=old_block_type;
  756. end;
  757. { reads a type declaration to the symbol table }
  758. procedure type_dec;
  759. begin
  760. consume(_TYPE);
  761. types_dec(false);
  762. end;
  763. procedure var_dec;
  764. { parses variable declarations and inserts them in }
  765. { the top symbol table of symtablestack }
  766. begin
  767. consume(_VAR);
  768. read_var_decls([]);
  769. end;
  770. procedure property_dec;
  771. { parses a global property (fpc mode feature) }
  772. var
  773. old_block_type: tblock_type;
  774. begin
  775. consume(_PROPERTY);
  776. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  777. message(parser_e_resourcestring_only_sg);
  778. old_block_type:=block_type;
  779. block_type:=bt_const;
  780. repeat
  781. read_property_dec(false, nil);
  782. consume(_SEMICOLON);
  783. until token<>_ID;
  784. block_type:=old_block_type;
  785. end;
  786. procedure threadvar_dec;
  787. { parses thread variable declarations and inserts them in }
  788. { the top symbol table of symtablestack }
  789. begin
  790. consume(_THREADVAR);
  791. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  792. message(parser_e_threadvars_only_sg);
  793. read_var_decls([vd_threadvar]);
  794. end;
  795. procedure resourcestring_dec;
  796. var
  797. orgname : TIDString;
  798. p : tnode;
  799. dummysymoptions : tsymoptions;
  800. deprecatedmsg : pshortstring;
  801. storetokenpos,filepos : tfileposinfo;
  802. old_block_type : tblock_type;
  803. sp : pchar;
  804. sym : tsym;
  805. begin
  806. if target_info.system in systems_managed_vm then
  807. message(parser_e_feature_unsupported_for_vm);
  808. consume(_RESOURCESTRING);
  809. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  810. message(parser_e_resourcestring_only_sg);
  811. old_block_type:=block_type;
  812. block_type:=bt_const;
  813. repeat
  814. orgname:=orgpattern;
  815. filepos:=current_tokenpos;
  816. consume(_ID);
  817. case token of
  818. _EQ:
  819. begin
  820. consume(_EQ);
  821. p:=comp_expr(true,false);
  822. storetokenpos:=current_tokenpos;
  823. current_tokenpos:=filepos;
  824. sym:=nil;
  825. case p.nodetype of
  826. ordconstn:
  827. begin
  828. if is_constcharnode(p) then
  829. begin
  830. getmem(sp,2);
  831. sp[0]:=chr(tordconstnode(p).value.svalue);
  832. sp[1]:=#0;
  833. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  834. end
  835. else
  836. Message(parser_e_illegal_expression);
  837. end;
  838. stringconstn:
  839. with Tstringconstnode(p) do
  840. begin
  841. getmem(sp,len+1);
  842. move(value_str^,sp^,len+1);
  843. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  844. end;
  845. else
  846. Message(parser_e_illegal_expression);
  847. end;
  848. current_tokenpos:=storetokenpos;
  849. { Support hint directives }
  850. dummysymoptions:=[];
  851. deprecatedmsg:=nil;
  852. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  853. if assigned(sym) then
  854. begin
  855. sym.symoptions:=sym.symoptions+dummysymoptions;
  856. sym.deprecatedmsg:=deprecatedmsg;
  857. symtablestack.top.insert(sym);
  858. end
  859. else
  860. stringdispose(deprecatedmsg);
  861. consume(_SEMICOLON);
  862. p.free;
  863. end;
  864. else consume(_EQ);
  865. end;
  866. until token<>_ID;
  867. block_type:=old_block_type;
  868. end;
  869. end.