pdecl.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 assigned(hdef.typesym) then
  586. begin
  587. istyperenaming:=true;
  588. include(newtype.symoptions,sp_explicitrename);
  589. end;
  590. if isunique then
  591. begin
  592. if is_objc_class_or_protocol(hdef) or
  593. is_java_class_or_interface(hdef) then
  594. Message(parser_e_unique_unsupported);
  595. hdef:=tstoreddef(hdef).getcopy;
  596. { check if it is an ansistirng(codepage) declaration }
  597. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  598. begin
  599. p:=comp_expr(true,false);
  600. consume(_RKLAMMER);
  601. if not is_constintnode(p) then
  602. begin
  603. Message(parser_e_illegal_expression);
  604. { error recovery }
  605. end
  606. else
  607. begin
  608. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  609. begin
  610. Message(parser_e_invalid_codepage);
  611. tordconstnode(p).value:=0;
  612. end;
  613. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  614. end;
  615. p.free;
  616. end;
  617. { fix name, it is used e.g. for tables }
  618. if is_class_or_interface_or_dispinterface(hdef) then
  619. with tobjectdef(hdef) do
  620. begin
  621. stringdispose(objname);
  622. stringdispose(objrealname);
  623. objrealname:=stringdup(genorgtypename);
  624. objname:=stringdup(upper(genorgtypename));
  625. end;
  626. include(hdef.defoptions,df_unique);
  627. if (hdef.typ in [pointerdef,classrefdef]) and
  628. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  629. current_module.checkforwarddefs.add(hdef);
  630. end;
  631. if not assigned(hdef.typesym) then
  632. hdef.typesym:=newtype;
  633. end;
  634. { in non-Delphi modes we need a reference to the generic def
  635. without the generic suffix, so it can be found easily when
  636. parsing method implementations }
  637. if isgeneric and assigned(sym) and
  638. not (m_delphi in current_settings.modeswitches) and
  639. (ttypesym(sym).typedef.typ=undefineddef) then
  640. { don't free the undefineddef as the defids rely on the count
  641. of the defs in the def list of the module}
  642. ttypesym(sym).typedef:=hdef;
  643. newtype.typedef:=hdef;
  644. { KAZ: handle TGUID declaration in system unit }
  645. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  646. (gentypename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  647. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  648. rec_tguid:=trecorddef(hdef);
  649. end;
  650. if assigned(hdef) then
  651. begin
  652. case hdef.typ of
  653. pointerdef :
  654. begin
  655. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  656. consume(_SEMICOLON);
  657. if try_to_consume(_FAR) then
  658. begin
  659. tpointerdef(hdef).is_far:=true;
  660. consume(_SEMICOLON);
  661. end;
  662. end;
  663. procvardef :
  664. begin
  665. { in case of type renaming, don't parse proc directives }
  666. if istyperenaming then
  667. begin
  668. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  669. consume(_SEMICOLON);
  670. end
  671. else
  672. begin
  673. if not check_proc_directive(true) then
  674. begin
  675. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  676. consume(_SEMICOLON);
  677. end;
  678. parse_var_proc_directives(tsym(newtype));
  679. handle_calling_convention(tprocvardef(hdef));
  680. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  681. consume(_SEMICOLON);
  682. end;
  683. end;
  684. objectdef :
  685. begin
  686. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  687. consume(_SEMICOLON);
  688. { change a forward and external class declaration into
  689. formal external definition, so the compiler does not
  690. expect an real definition later }
  691. if is_objc_class_or_protocol(hdef) or
  692. is_java_class_or_interface(hdef) then
  693. finalize_class_external_status(tobjectdef(hdef));
  694. { Build VMT indexes, skip for type renaming and forward classes }
  695. if (hdef.typesym=newtype) and
  696. not(oo_is_forward in tobjectdef(hdef).objectoptions) and
  697. not(df_generic in hdef.defoptions) then
  698. begin
  699. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  700. vmtbuilder.generate_vmt;
  701. vmtbuilder.free;
  702. end;
  703. { In case of an objcclass, verify that all methods have a message
  704. name set. We only check this now, because message names can be set
  705. during the protocol (interface) mapping. At the same time, set the
  706. mangled names (these depend on the "external" name of the class),
  707. and mark private fields of external classes as "used" (to avoid
  708. bogus notes about them being unused)
  709. }
  710. { watch out for crashes in case of errors }
  711. if is_objc_class_or_protocol(hdef) and
  712. (not is_objccategory(hdef) or
  713. assigned(tobjectdef(hdef).childof)) then
  714. tobjectdef(hdef).finish_objc_data;
  715. if is_cppclass(hdef) then
  716. tobjectdef(hdef).finish_cpp_data;
  717. end;
  718. recorddef :
  719. begin
  720. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  721. consume(_SEMICOLON);
  722. end;
  723. else
  724. begin
  725. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  726. consume(_SEMICOLON);
  727. end;
  728. end;
  729. end;
  730. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  731. or is_objectpascal_helper(hdef)) then
  732. message(parser_e_cant_create_generics_of_this_type);
  733. { Stop recording a generic template }
  734. if assigned(generictypelist) then
  735. begin
  736. current_scanner.stoprecordtokens;
  737. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  738. { Generic is never a type renaming }
  739. hdef.typesym:=newtype;
  740. generictypelist.free;
  741. end;
  742. until (token<>_ID) or
  743. (in_structure and
  744. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  745. ((m_final_fields in current_settings.modeswitches) and
  746. (idtoken=_FINAL))));
  747. { resolve type block forward declarations and restore a unit
  748. container for them }
  749. resolve_forward_types;
  750. current_module.checkforwarddefs.free;
  751. current_module.checkforwarddefs:=old_checkforwarddefs;
  752. block_type:=old_block_type;
  753. end;
  754. { reads a type declaration to the symbol table }
  755. procedure type_dec;
  756. begin
  757. consume(_TYPE);
  758. types_dec(false);
  759. end;
  760. procedure var_dec;
  761. { parses variable declarations and inserts them in }
  762. { the top symbol table of symtablestack }
  763. begin
  764. consume(_VAR);
  765. read_var_decls([]);
  766. end;
  767. procedure property_dec;
  768. { parses a global property (fpc mode feature) }
  769. var
  770. old_block_type: tblock_type;
  771. begin
  772. consume(_PROPERTY);
  773. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  774. message(parser_e_resourcestring_only_sg);
  775. old_block_type:=block_type;
  776. block_type:=bt_const;
  777. repeat
  778. read_property_dec(false, nil);
  779. consume(_SEMICOLON);
  780. until token<>_ID;
  781. block_type:=old_block_type;
  782. end;
  783. procedure threadvar_dec;
  784. { parses thread variable declarations and inserts them in }
  785. { the top symbol table of symtablestack }
  786. begin
  787. consume(_THREADVAR);
  788. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  789. message(parser_e_threadvars_only_sg);
  790. read_var_decls([vd_threadvar]);
  791. end;
  792. procedure resourcestring_dec;
  793. var
  794. orgname : TIDString;
  795. p : tnode;
  796. dummysymoptions : tsymoptions;
  797. deprecatedmsg : pshortstring;
  798. storetokenpos,filepos : tfileposinfo;
  799. old_block_type : tblock_type;
  800. sp : pchar;
  801. sym : tsym;
  802. begin
  803. if target_info.system in systems_managed_vm then
  804. message(parser_e_feature_unsupported_for_vm);
  805. consume(_RESOURCESTRING);
  806. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  807. message(parser_e_resourcestring_only_sg);
  808. old_block_type:=block_type;
  809. block_type:=bt_const;
  810. repeat
  811. orgname:=orgpattern;
  812. filepos:=current_tokenpos;
  813. consume(_ID);
  814. case token of
  815. _EQ:
  816. begin
  817. consume(_EQ);
  818. p:=comp_expr(true,false);
  819. storetokenpos:=current_tokenpos;
  820. current_tokenpos:=filepos;
  821. sym:=nil;
  822. case p.nodetype of
  823. ordconstn:
  824. begin
  825. if is_constcharnode(p) then
  826. begin
  827. getmem(sp,2);
  828. sp[0]:=chr(tordconstnode(p).value.svalue);
  829. sp[1]:=#0;
  830. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  831. end
  832. else
  833. Message(parser_e_illegal_expression);
  834. end;
  835. stringconstn:
  836. with Tstringconstnode(p) do
  837. begin
  838. getmem(sp,len+1);
  839. move(value_str^,sp^,len+1);
  840. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  841. end;
  842. else
  843. Message(parser_e_illegal_expression);
  844. end;
  845. current_tokenpos:=storetokenpos;
  846. { Support hint directives }
  847. dummysymoptions:=[];
  848. deprecatedmsg:=nil;
  849. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  850. if assigned(sym) then
  851. begin
  852. sym.symoptions:=sym.symoptions+dummysymoptions;
  853. sym.deprecatedmsg:=deprecatedmsg;
  854. symtablestack.top.insert(sym);
  855. end
  856. else
  857. stringdispose(deprecatedmsg);
  858. consume(_SEMICOLON);
  859. p.free;
  860. end;
  861. else consume(_EQ);
  862. end;
  863. until token<>_ID;
  864. block_type:=old_block_type;
  865. end;
  866. end.