pdecl.pas 41 KB

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