pdecl.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. end;
  400. p.free;
  401. consume(_RECKKLAMMER);
  402. end;
  403. procedure add_synthetic_rtti_funtion_declarations(rtti_attributesdef: trtti_attributesdef; name: shortstring);
  404. var
  405. i: Integer;
  406. sstate: tscannerstate;
  407. attribute: trtti_attribute;
  408. pd: tprocdef;
  409. begin
  410. for i := 0 to rtti_attributesdef.get_attribute_count-1 do
  411. begin
  412. attribute := trtti_attribute(rtti_attributesdef.rtti_attributes[i]);
  413. replace_scanner('rtti_class_attributes',sstate);
  414. if str_parse_method_dec('function rtti_'+name+'_'+IntToStr(i)+':'+ attribute.typesym.Name +';',potype_function,false,tabstractrecorddef(ttypesym(attribute.typesym).typedef),pd) then
  415. pd.synthetickind:=tsk_get_rttiattribute
  416. else
  417. internalerror(2012052601);
  418. pd.skpara:=attribute;
  419. attribute.symbolname:=pd.mangledname;
  420. restore_scanner(sstate);
  421. end;
  422. end;
  423. procedure types_dec(in_structure: boolean);
  424. procedure finalize_class_external_status(od: tobjectdef);
  425. begin
  426. if [oo_is_external,oo_is_forward] <= od.objectoptions then
  427. begin
  428. { formal definition: x = objcclass external; }
  429. exclude(od.objectoptions,oo_is_forward);
  430. include(od.objectoptions,oo_is_formal);
  431. end;
  432. end;
  433. var
  434. typename,orgtypename,
  435. gentypename,genorgtypename : TIDString;
  436. newtype : ttypesym;
  437. sym : tsym;
  438. hdef : tdef;
  439. defpos,storetokenpos : tfileposinfo;
  440. old_block_type : tblock_type;
  441. old_checkforwarddefs: TFPObjectList;
  442. objecttype : tobjecttyp;
  443. isgeneric,
  444. isunique,
  445. istyperenaming : boolean;
  446. generictypelist : TFPObjectList;
  447. generictokenbuf : tdynamicarray;
  448. vmtbuilder : TVMTBuilder;
  449. p:tnode;
  450. gendef : tstoreddef;
  451. s : shortstring;
  452. pd: tprocdef;
  453. hashedid : thashedidstring;
  454. begin
  455. old_block_type:=block_type;
  456. { save unit container of forward declarations -
  457. we can be inside nested class type block }
  458. old_checkforwarddefs:=current_module.checkforwarddefs;
  459. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  460. block_type:=bt_type;
  461. repeat
  462. defpos:=current_tokenpos;
  463. istyperenaming:=false;
  464. generictypelist:=nil;
  465. generictokenbuf:=nil;
  466. { class attribute definitions? }
  467. while token=_LECKKLAMMER do
  468. begin
  469. parse_rttiattributes(current_rtticlassattributesdef);
  470. end;
  471. { fpc generic declaration? }
  472. isgeneric:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  473. typename:=pattern;
  474. orgtypename:=orgpattern;
  475. consume(_ID);
  476. { delphi generic declaration? }
  477. if (m_delphi in current_settings.modeswitches) then
  478. isgeneric:=token=_LSHARPBRACKET;
  479. { Generic type declaration? }
  480. if isgeneric then
  481. begin
  482. if assigned(current_genericdef) then
  483. Message(parser_f_no_generic_inside_generic);
  484. consume(_LSHARPBRACKET);
  485. generictypelist:=parse_generic_parameters;
  486. consume(_RSHARPBRACKET);
  487. str(generictypelist.Count,s);
  488. gentypename:=typename+'$'+s;
  489. genorgtypename:=orgtypename+'$'+s;
  490. end
  491. else
  492. begin
  493. gentypename:=typename;
  494. genorgtypename:=orgtypename;
  495. end;
  496. consume(_EQ);
  497. { support 'ttype=type word' syntax }
  498. isunique:=try_to_consume(_TYPE);
  499. { MacPas object model is more like Delphi's than like TP's, but }
  500. { uses the object keyword instead of class }
  501. if (m_mac in current_settings.modeswitches) and
  502. (token = _OBJECT) then
  503. token := _CLASS;
  504. { Start recording a generic template }
  505. if assigned(generictypelist) then
  506. begin
  507. generictokenbuf:=tdynamicarray.create(256);
  508. current_scanner.startrecordtokens(generictokenbuf);
  509. end;
  510. { is the type already defined? -- must be in the current symtable,
  511. not in a nested symtable or one higher up the stack -> don't
  512. use searchsym & frinds! }
  513. sym:=tsym(symtablestack.top.find(gentypename));
  514. newtype:=nil;
  515. { found a symbol with this name? }
  516. if assigned(sym) then
  517. begin
  518. if (sym.typ=typesym) and
  519. { this should not be a symbol that was created by a generic
  520. that was declared earlier }
  521. not (
  522. (ttypesym(sym).typedef.typ=undefineddef) and
  523. (sp_generic_dummy in sym.symoptions)
  524. ) then
  525. begin
  526. if ((token=_CLASS) or
  527. (token=_INTERFACE) or
  528. (token=_DISPINTERFACE) or
  529. (token=_OBJCCLASS) or
  530. (token=_OBJCPROTOCOL) or
  531. (token=_OBJCCATEGORY)) and
  532. (assigned(ttypesym(sym).typedef)) and
  533. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  534. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  535. begin
  536. case token of
  537. _CLASS :
  538. objecttype:=default_class_type;
  539. _INTERFACE :
  540. case current_settings.interfacetype of
  541. it_interfacecom:
  542. objecttype:=odt_interfacecom;
  543. it_interfacecorba:
  544. objecttype:=odt_interfacecorba;
  545. it_interfacejava:
  546. objecttype:=odt_interfacejava;
  547. else
  548. internalerror(2010122611);
  549. end;
  550. _DISPINTERFACE :
  551. objecttype:=odt_dispinterface;
  552. _OBJCCLASS,
  553. _OBJCCATEGORY :
  554. objecttype:=odt_objcclass;
  555. _OBJCPROTOCOL :
  556. objecttype:=odt_objcprotocol;
  557. else
  558. internalerror(200811072);
  559. end;
  560. consume(token);
  561. { we can ignore the result, the definition is modified }
  562. object_dec(objecttype,genorgtypename,newtype,nil,nil,tobjectdef(ttypesym(sym).typedef),ht_none);
  563. newtype:=ttypesym(sym);
  564. hdef:=newtype.typedef;
  565. end
  566. else
  567. message1(parser_h_type_redef,genorgtypename);
  568. end;
  569. end;
  570. { no old type reused ? Then insert this new type }
  571. if not assigned(newtype) then
  572. begin
  573. { insert the new type first with an errordef, so that
  574. referencing the type before it's really set it
  575. will give an error (PFV) }
  576. hdef:=generrordef;
  577. gendef:=nil;
  578. storetokenpos:=current_tokenpos;
  579. if isgeneric then
  580. begin
  581. { for generics we need to check whether a non-generic type
  582. already exists and if not we need to insert a symbol with
  583. the non-generic name (available in (org)typename) that is a
  584. undefineddef, so that inline specializations can be used }
  585. sym:=tsym(symtablestack.top.Find(typename));
  586. if not assigned(sym) then
  587. begin
  588. sym:=ttypesym.create(orgtypename,tundefineddef.create);
  589. Include(sym.symoptions,sp_generic_dummy);
  590. ttypesym(sym).typedef.typesym:=sym;
  591. sym.visibility:=symtablestack.top.currentvisibility;
  592. symtablestack.top.insert(sym);
  593. ttypesym(sym).typedef.owner:=sym.owner;
  594. end
  595. else
  596. { this is not allowed in non-Delphi modes }
  597. if not (m_delphi in current_settings.modeswitches) then
  598. Message1(sym_e_duplicate_id,genorgtypename)
  599. else
  600. { we need to find this symbol even if it's a variable or
  601. something else when doing an inline specialization }
  602. Include(sym.symoptions,sp_generic_dummy);
  603. end
  604. else
  605. begin
  606. if assigned(sym) and (sym.typ=typesym) and
  607. (ttypesym(sym).typedef.typ=undefineddef) and
  608. (sp_generic_dummy in sym.symoptions) then
  609. begin
  610. { this is a symbol that was added by an earlier generic
  611. declaration, reuse it }
  612. newtype:=ttypesym(sym);
  613. newtype.typedef:=hdef;
  614. sym:=nil;
  615. end;
  616. { check whether this is a declaration of a type inside a
  617. specialization }
  618. if assigned(current_structdef) and
  619. (df_specialization in current_structdef.defoptions) then
  620. begin
  621. if not assigned(current_structdef.genericdef) or
  622. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  623. internalerror(2011052301);
  624. hashedid.id:=gentypename;
  625. { we could be inside a method of the specialization
  626. instead of its declaration, so check that first (as
  627. local nested types aren't allowed we don't need to
  628. walk the symtablestack to find the localsymtable) }
  629. if symtablestack.top.symtabletype=localsymtable then
  630. begin
  631. { we are in a method }
  632. if not assigned(symtablestack.top.defowner) or
  633. (symtablestack.top.defowner.typ<>procdef) then
  634. internalerror(2011120701);
  635. pd:=tprocdef(symtablestack.top.defowner);
  636. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  637. internalerror(2011120702);
  638. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  639. end
  640. else
  641. sym:=nil;
  642. if not assigned(sym) or not (sym.typ=typesym) then
  643. begin
  644. { now search in the declaration of the generic }
  645. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  646. if not assigned(sym) or not (sym.typ=typesym) then
  647. internalerror(2011052302);
  648. end;
  649. { use the corresponding type in the generic's symtable as
  650. genericdef for the specialized type }
  651. gendef:=tstoreddef(ttypesym(sym).typedef);
  652. end;
  653. end;
  654. { insert a new type if we don't reuse an existing symbol }
  655. if not assigned(newtype) then
  656. begin
  657. newtype:=ttypesym.create(genorgtypename,hdef);
  658. newtype.visibility:=symtablestack.top.currentvisibility;
  659. symtablestack.top.insert(newtype);
  660. end;
  661. current_tokenpos:=defpos;
  662. current_tokenpos:=storetokenpos;
  663. { read the type definition }
  664. read_named_type(hdef,newtype,gendef,generictypelist,false);
  665. { update the definition of the type }
  666. if assigned(hdef) then
  667. begin
  668. if assigned(hdef.typesym) then
  669. begin
  670. istyperenaming:=true;
  671. include(newtype.symoptions,sp_explicitrename);
  672. end;
  673. if isunique then
  674. begin
  675. if is_objc_class_or_protocol(hdef) or
  676. is_java_class_or_interface(hdef) then
  677. Message(parser_e_unique_unsupported);
  678. hdef:=tstoreddef(hdef).getcopy;
  679. { check if it is an ansistirng(codepage) declaration }
  680. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  681. begin
  682. p:=comp_expr(true,false);
  683. consume(_RKLAMMER);
  684. if not is_constintnode(p) then
  685. begin
  686. Message(parser_e_illegal_expression);
  687. { error recovery }
  688. end
  689. else
  690. begin
  691. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  692. begin
  693. Message(parser_e_invalid_codepage);
  694. tordconstnode(p).value:=0;
  695. end;
  696. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  697. end;
  698. p.free;
  699. end;
  700. { fix name, it is used e.g. for tables }
  701. if is_class_or_interface_or_dispinterface(hdef) then
  702. with tobjectdef(hdef) do
  703. begin
  704. stringdispose(objname);
  705. stringdispose(objrealname);
  706. objrealname:=stringdup(genorgtypename);
  707. objname:=stringdup(upper(genorgtypename));
  708. end;
  709. include(hdef.defoptions,df_unique);
  710. if (hdef.typ in [pointerdef,classrefdef]) and
  711. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  712. current_module.checkforwarddefs.add(hdef);
  713. end;
  714. if not assigned(hdef.typesym) then
  715. hdef.typesym:=newtype;
  716. end;
  717. { in non-Delphi modes we need a reference to the generic def
  718. without the generic suffix, so it can be found easily when
  719. parsing method implementations }
  720. if isgeneric and assigned(sym) and
  721. not (m_delphi in current_settings.modeswitches) and
  722. (ttypesym(sym).typedef.typ=undefineddef) then
  723. { don't free the undefineddef as the defids rely on the count
  724. of the defs in the def list of the module}
  725. ttypesym(sym).typedef:=hdef;
  726. newtype.typedef:=hdef;
  727. { KAZ: handle TGUID declaration in system unit }
  728. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  729. (gentypename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  730. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  731. rec_tguid:=trecorddef(hdef);
  732. end;
  733. if assigned(hdef) then
  734. begin
  735. case hdef.typ of
  736. pointerdef :
  737. begin
  738. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  739. consume(_SEMICOLON);
  740. if try_to_consume(_FAR) then
  741. begin
  742. tpointerdef(hdef).is_far:=true;
  743. consume(_SEMICOLON);
  744. end;
  745. end;
  746. procvardef :
  747. begin
  748. { in case of type renaming, don't parse proc directives }
  749. if istyperenaming then
  750. begin
  751. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  752. consume(_SEMICOLON);
  753. end
  754. else
  755. begin
  756. if not check_proc_directive(true) then
  757. begin
  758. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  759. consume(_SEMICOLON);
  760. end;
  761. parse_var_proc_directives(tsym(newtype));
  762. handle_calling_convention(tprocvardef(hdef));
  763. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  764. consume(_SEMICOLON);
  765. end;
  766. end;
  767. objectdef :
  768. begin
  769. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  770. consume(_SEMICOLON);
  771. { change a forward and external class declaration into
  772. formal external definition, so the compiler does not
  773. expect an real definition later }
  774. if is_objc_class_or_protocol(hdef) or
  775. is_java_class_or_interface(hdef) then
  776. finalize_class_external_status(tobjectdef(hdef));
  777. { Build VMT indexes, skip for type renaming and forward classes }
  778. if (hdef.typesym=newtype) and
  779. not(oo_is_forward in tobjectdef(hdef).objectoptions) and
  780. not(df_generic in hdef.defoptions) then
  781. begin
  782. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  783. vmtbuilder.generate_vmt;
  784. vmtbuilder.free;
  785. end;
  786. { If there are attribute-properties available, bind them to
  787. this object }
  788. if assigned(current_rtticlassattributesdef) then
  789. begin
  790. add_synthetic_rtti_funtion_declarations(current_rtticlassattributesdef,hdef.typesym.Name);
  791. tobjectdef(hdef).rtti_attributesdef:=current_rtticlassattributesdef;
  792. current_rtticlassattributesdef := nil;
  793. end;
  794. { In case of an objcclass, verify that all methods have a message
  795. name set. We only check this now, because message names can be set
  796. during the protocol (interface) mapping. At the same time, set the
  797. mangled names (these depend on the "external" name of the class),
  798. and mark private fields of external classes as "used" (to avoid
  799. bogus notes about them being unused)
  800. }
  801. { watch out for crashes in case of errors }
  802. if is_objc_class_or_protocol(hdef) and
  803. (not is_objccategory(hdef) or
  804. assigned(tobjectdef(hdef).childof)) then
  805. tobjectdef(hdef).finish_objc_data;
  806. if is_cppclass(hdef) then
  807. tobjectdef(hdef).finish_cpp_data;
  808. end;
  809. recorddef :
  810. begin
  811. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  812. consume(_SEMICOLON);
  813. end;
  814. else
  815. begin
  816. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  817. consume(_SEMICOLON);
  818. end;
  819. end;
  820. end;
  821. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  822. or is_objectpascal_helper(hdef)) then
  823. message(parser_e_cant_create_generics_of_this_type);
  824. { Stop recording a generic template }
  825. if assigned(generictypelist) then
  826. begin
  827. current_scanner.stoprecordtokens;
  828. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  829. { Generic is never a type renaming }
  830. hdef.typesym:=newtype;
  831. generictypelist.free;
  832. end;
  833. if assigned(current_rtticlassattributesdef) and (current_rtticlassattributesdef.get_attribute_count>0) then
  834. Message1(scan_e_unresolved_attribute,trtti_attribute(current_rtticlassattributesdef.rtti_attributes[0]).typesym.prettyname);
  835. until ((token<>_ID) and (token<>_LECKKLAMMER)) or
  836. (in_structure and
  837. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  838. ((m_final_fields in current_settings.modeswitches) and
  839. (idtoken=_FINAL))));
  840. { resolve type block forward declarations and restore a unit
  841. container for them }
  842. resolve_forward_types;
  843. current_module.checkforwarddefs.free;
  844. current_module.checkforwarddefs:=old_checkforwarddefs;
  845. block_type:=old_block_type;
  846. end;
  847. { reads a type declaration to the symbol table }
  848. procedure type_dec;
  849. begin
  850. consume(_TYPE);
  851. types_dec(false);
  852. end;
  853. procedure var_dec;
  854. { parses variable declarations and inserts them in }
  855. { the top symbol table of symtablestack }
  856. begin
  857. consume(_VAR);
  858. read_var_decls([]);
  859. end;
  860. procedure property_dec;
  861. { parses a global property (fpc mode feature) }
  862. var
  863. old_block_type: tblock_type;
  864. begin
  865. consume(_PROPERTY);
  866. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  867. message(parser_e_resourcestring_only_sg);
  868. old_block_type:=block_type;
  869. block_type:=bt_const;
  870. repeat
  871. read_property_dec(false, nil);
  872. consume(_SEMICOLON);
  873. until token<>_ID;
  874. block_type:=old_block_type;
  875. end;
  876. procedure threadvar_dec;
  877. { parses thread variable declarations and inserts them in }
  878. { the top symbol table of symtablestack }
  879. begin
  880. consume(_THREADVAR);
  881. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  882. message(parser_e_threadvars_only_sg);
  883. read_var_decls([vd_threadvar]);
  884. end;
  885. procedure resourcestring_dec;
  886. var
  887. orgname : TIDString;
  888. p : tnode;
  889. dummysymoptions : tsymoptions;
  890. deprecatedmsg : pshortstring;
  891. storetokenpos,filepos : tfileposinfo;
  892. old_block_type : tblock_type;
  893. sp : pchar;
  894. sym : tsym;
  895. begin
  896. if target_info.system in systems_managed_vm then
  897. message(parser_e_feature_unsupported_for_vm);
  898. consume(_RESOURCESTRING);
  899. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  900. message(parser_e_resourcestring_only_sg);
  901. old_block_type:=block_type;
  902. block_type:=bt_const;
  903. repeat
  904. orgname:=orgpattern;
  905. filepos:=current_tokenpos;
  906. consume(_ID);
  907. case token of
  908. _EQ:
  909. begin
  910. consume(_EQ);
  911. p:=comp_expr(true,false);
  912. storetokenpos:=current_tokenpos;
  913. current_tokenpos:=filepos;
  914. sym:=nil;
  915. case p.nodetype of
  916. ordconstn:
  917. begin
  918. if is_constcharnode(p) then
  919. begin
  920. getmem(sp,2);
  921. sp[0]:=chr(tordconstnode(p).value.svalue);
  922. sp[1]:=#0;
  923. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  924. end
  925. else
  926. Message(parser_e_illegal_expression);
  927. end;
  928. stringconstn:
  929. with Tstringconstnode(p) do
  930. begin
  931. getmem(sp,len+1);
  932. move(value_str^,sp^,len+1);
  933. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  934. end;
  935. else
  936. Message(parser_e_illegal_expression);
  937. end;
  938. current_tokenpos:=storetokenpos;
  939. { Support hint directives }
  940. dummysymoptions:=[];
  941. deprecatedmsg:=nil;
  942. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  943. if assigned(sym) then
  944. begin
  945. sym.symoptions:=sym.symoptions+dummysymoptions;
  946. sym.deprecatedmsg:=deprecatedmsg;
  947. symtablestack.top.insert(sym);
  948. end
  949. else
  950. stringdispose(deprecatedmsg);
  951. consume(_SEMICOLON);
  952. p.free;
  953. end;
  954. else consume(_EQ);
  955. end;
  956. until token<>_ID;
  957. block_type:=old_block_type;
  958. end;
  959. end.