pdecl.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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(out had_generic:boolean);
  31. procedure consts_dec(in_structure, allow_typed_const: boolean;out had_generic:boolean);
  32. procedure label_dec;
  33. procedure type_dec(out had_generic:boolean);
  34. procedure types_dec(in_structure: boolean;out had_generic:boolean);
  35. procedure var_dec(out had_generic:boolean);
  36. procedure threadvar_dec(out had_generic:boolean);
  37. procedure property_dec;
  38. procedure resourcestring_dec(out had_generic:boolean);
  39. implementation
  40. uses
  41. { common }
  42. cutils,
  43. { global }
  44. globals,tokens,verbose,widestr,constexp,
  45. systems,aasmdata,fmodule,
  46. { symtable }
  47. symconst,symbase,symtype,symcpu,symtable,symcreat,defutil,
  48. { pass 1 }
  49. htypechk,ninl,ncon,nobj,ngenutil,
  50. { parser }
  51. scanner,
  52. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,pgenutil,
  53. {$ifdef jvm}
  54. pjvm,
  55. {$endif}
  56. { cpu-information }
  57. cpuinfo
  58. ;
  59. function readconstant(const orgname:string;const filepos:tfileposinfo; out nodetype: tnodetype):tconstsym;
  60. var
  61. hp : tconstsym;
  62. p : tnode;
  63. ps : pconstset;
  64. pd : pbestreal;
  65. pg : pguid;
  66. sp : pchar;
  67. pw : pcompilerwidestring;
  68. storetokenpos : tfileposinfo;
  69. begin
  70. readconstant:=nil;
  71. if orgname='' then
  72. internalerror(9584582);
  73. hp:=nil;
  74. p:=comp_expr([ef_accept_equal]);
  75. nodetype:=p.nodetype;
  76. storetokenpos:=current_tokenpos;
  77. current_tokenpos:=filepos;
  78. case p.nodetype of
  79. ordconstn:
  80. begin
  81. if p.resultdef.typ=pointerdef then
  82. hp:=cconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value.uvalue,p.resultdef)
  83. else
  84. hp:=cconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resultdef);
  85. end;
  86. stringconstn:
  87. begin
  88. if is_wide_or_unicode_string(p.resultdef) then
  89. begin
  90. initwidestring(pw);
  91. copywidestring(pcompilerwidestring(tstringconstnode(p).value_str),pw);
  92. hp:=cconstsym.create_wstring(orgname,constwstring,pw);
  93. end
  94. else
  95. begin
  96. getmem(sp,tstringconstnode(p).len+1);
  97. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  98. { if a non-default ansistring code page has been specified,
  99. keep it }
  100. if is_ansistring(p.resultdef) and
  101. (tstringdef(p.resultdef).encoding<>0) then
  102. hp:=cconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len,p.resultdef)
  103. else
  104. hp:=cconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len,nil);
  105. end;
  106. end;
  107. realconstn :
  108. begin
  109. new(pd);
  110. pd^:=trealconstnode(p).value_real;
  111. hp:=cconstsym.create_ptr(orgname,constreal,pd,p.resultdef);
  112. end;
  113. setconstn :
  114. begin
  115. new(ps);
  116. ps^:=tsetconstnode(p).value_set^;
  117. hp:=cconstsym.create_ptr(orgname,constset,ps,p.resultdef);
  118. end;
  119. pointerconstn :
  120. begin
  121. hp:=cconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resultdef);
  122. end;
  123. niln :
  124. begin
  125. hp:=cconstsym.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:=cconstsym.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:=cconstsym.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(out had_generic:boolean);
  170. begin
  171. consume(_CONST);
  172. consts_dec(false,true,had_generic);
  173. end;
  174. procedure consts_dec(in_structure, allow_typed_const: boolean;out had_generic: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. first,
  185. isgeneric,
  186. skipequal : boolean;
  187. tclist : tasmlist;
  188. varspez : tvarspez;
  189. begin
  190. old_block_type:=block_type;
  191. block_type:=bt_const;
  192. had_generic:=false;
  193. first:=true;
  194. repeat
  195. orgname:=orgpattern;
  196. filepos:=current_tokenpos;
  197. isgeneric:=not (m_delphi in current_settings.modeswitches) and (token=_ID) and (idtoken=_GENERIC);
  198. consume(_ID);
  199. case token of
  200. _EQ:
  201. begin
  202. consume(_EQ);
  203. sym:=readconstant(orgname,filepos,nodetype);
  204. { Support hint directives }
  205. dummysymoptions:=[];
  206. deprecatedmsg:=nil;
  207. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  208. if assigned(sym) then
  209. begin
  210. sym.symoptions:=sym.symoptions+dummysymoptions;
  211. sym.deprecatedmsg:=deprecatedmsg;
  212. sym.visibility:=symtablestack.top.currentvisibility;
  213. symtablestack.top.insert(sym);
  214. {$ifdef jvm}
  215. { for the JVM target, some constants need to be
  216. initialized at run time (enums, sets) -> create fake
  217. typed const to do so (at least if they are visible
  218. outside this routine, since we won't directly access
  219. these symbols in the generated code) }
  220. if (symtablestack.top.symtablelevel<normal_function_level) and
  221. assigned(tconstsym(sym).constdef) and
  222. (tconstsym(sym).constdef.typ in [enumdef,setdef]) then
  223. jvm_add_typed_const_initializer(tconstsym(sym));
  224. {$endif}
  225. end
  226. else
  227. stringdispose(deprecatedmsg);
  228. consume(_SEMICOLON);
  229. end;
  230. _COLON:
  231. begin
  232. if not allow_typed_const then
  233. begin
  234. Message(parser_e_no_typed_const);
  235. consume_all_until(_SEMICOLON);
  236. end;
  237. { set the blocktype first so a consume also supports a
  238. caret, to support const s : ^string = nil }
  239. block_type:=bt_const_type;
  240. consume(_COLON);
  241. read_anon_type(hdef,false);
  242. block_type:=bt_const;
  243. skipequal:=false;
  244. { create symbol }
  245. storetokenpos:=current_tokenpos;
  246. current_tokenpos:=filepos;
  247. if not (cs_typed_const_writable in current_settings.localswitches) then
  248. varspez:=vs_const
  249. else
  250. varspez:=vs_value;
  251. { if we are dealing with structure const then we need to handle it as a
  252. structure static variable: create a symbol in unit symtable and a reference
  253. to it from the structure or linking will fail }
  254. if symtablestack.top.symtabletype in [recordsymtable,ObjectSymtable] then
  255. begin
  256. sym:=cfieldvarsym.create(orgname,varspez,hdef,[],true);
  257. symtablestack.top.insert(sym);
  258. sym:=make_field_static(symtablestack.top,tfieldvarsym(sym));
  259. end
  260. else
  261. begin
  262. sym:=cstaticvarsym.create(orgname,varspez,hdef,[],true);
  263. sym.visibility:=symtablestack.top.currentvisibility;
  264. symtablestack.top.insert(sym);
  265. end;
  266. current_tokenpos:=storetokenpos;
  267. { procvar can have proc directives, but not type references }
  268. if (hdef.typ=procvardef) and
  269. (hdef.typesym=nil) then
  270. begin
  271. { support p : procedure;stdcall=nil; }
  272. if try_to_consume(_SEMICOLON) then
  273. begin
  274. if check_proc_directive(true) then
  275. parse_var_proc_directives(sym)
  276. else
  277. begin
  278. Message(parser_e_proc_directive_expected);
  279. skipequal:=true;
  280. end;
  281. end
  282. else
  283. { support p : procedure stdcall=nil; }
  284. begin
  285. if check_proc_directive(true) then
  286. parse_var_proc_directives(sym);
  287. end;
  288. { add default calling convention }
  289. handle_calling_convention(tabstractprocdef(hdef));
  290. end;
  291. if not skipequal then
  292. begin
  293. { get init value }
  294. consume(_EQ);
  295. if (cs_typed_const_writable in current_settings.localswitches) then
  296. tclist:=current_asmdata.asmlists[al_typedconsts]
  297. else
  298. tclist:=current_asmdata.asmlists[al_rotypedconsts];
  299. read_typed_const(tclist,tstaticvarsym(sym),in_structure);
  300. end;
  301. end;
  302. else
  303. if not first and isgeneric and (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  304. begin
  305. had_generic:=true;
  306. break;
  307. end
  308. else
  309. { generate an error }
  310. consume(_EQ);
  311. end;
  312. first:=false;
  313. until (token<>_ID) or
  314. (in_structure and
  315. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  316. ((m_final_fields in current_settings.modeswitches) and
  317. (idtoken=_FINAL))));
  318. block_type:=old_block_type;
  319. end;
  320. procedure label_dec;
  321. var
  322. labelsym : tlabelsym;
  323. begin
  324. consume(_LABEL);
  325. if not(cs_support_goto in current_settings.moduleswitches) then
  326. Message(sym_e_goto_and_label_not_supported);
  327. repeat
  328. if not(token in [_ID,_INTCONST]) then
  329. consume(_ID)
  330. else
  331. begin
  332. if token=_ID then
  333. labelsym:=clabelsym.create(orgpattern)
  334. else
  335. labelsym:=clabelsym.create(pattern);
  336. symtablestack.top.insert(labelsym);
  337. if m_non_local_goto in current_settings.modeswitches then
  338. begin
  339. if symtablestack.top.symtabletype=localsymtable then
  340. begin
  341. labelsym.jumpbuf:=clocalvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[],true);
  342. symtablestack.top.insert(labelsym.jumpbuf);
  343. end
  344. else
  345. begin
  346. labelsym.jumpbuf:=cstaticvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[],true);
  347. symtablestack.top.insert(labelsym.jumpbuf);
  348. cnodeutils.insertbssdata(tstaticvarsym(labelsym.jumpbuf));
  349. end;
  350. include(labelsym.jumpbuf.symoptions,sp_internal);
  351. { the buffer will be setup later, but avoid a hint }
  352. tabstractvarsym(labelsym.jumpbuf).varstate:=vs_written;
  353. end;
  354. consume(token);
  355. end;
  356. if token<>_SEMICOLON then consume(_COMMA);
  357. until not(token in [_ID,_INTCONST]);
  358. consume(_SEMICOLON);
  359. end;
  360. procedure types_dec(in_structure: boolean;out had_generic:boolean);
  361. function determine_generic_def(name:tidstring):tstoreddef;
  362. var
  363. hashedid : THashedIDString;
  364. pd : tprocdef;
  365. sym : tsym;
  366. begin
  367. result:=nil;
  368. { check whether this is a declaration of a type inside a
  369. specialization }
  370. if assigned(current_structdef) and
  371. (df_specialization in current_structdef.defoptions) then
  372. begin
  373. if not assigned(current_structdef.genericdef) or
  374. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  375. internalerror(2011052301);
  376. hashedid.id:=name;
  377. { we could be inside a method of the specialization
  378. instead of its declaration, so check that first (as
  379. local nested types aren't allowed we don't need to
  380. walk the symtablestack to find the localsymtable) }
  381. if symtablestack.top.symtabletype=localsymtable then
  382. begin
  383. { we are in a method }
  384. if not assigned(symtablestack.top.defowner) or
  385. (symtablestack.top.defowner.typ<>procdef) then
  386. internalerror(2011120701);
  387. pd:=tprocdef(symtablestack.top.defowner);
  388. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  389. internalerror(2011120702);
  390. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  391. end
  392. else
  393. sym:=nil;
  394. if not assigned(sym) or not (sym.typ=typesym) then
  395. begin
  396. { now search in the declaration of the generic }
  397. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  398. if not assigned(sym) or not (sym.typ=typesym) then
  399. internalerror(2011052302);
  400. end;
  401. { use the corresponding type in the generic's symtable as
  402. genericdef for the specialized type }
  403. result:=tstoreddef(ttypesym(sym).typedef);
  404. end;
  405. end;
  406. procedure finalize_class_external_status(od: tobjectdef);
  407. begin
  408. if [oo_is_external,oo_is_forward] <= od.objectoptions then
  409. begin
  410. { formal definition: x = objcclass external; }
  411. exclude(od.objectoptions,oo_is_forward);
  412. include(od.objectoptions,oo_is_formal);
  413. end;
  414. end;
  415. var
  416. typename,orgtypename,
  417. gentypename,genorgtypename : TIDString;
  418. newtype : ttypesym;
  419. sym : tsym;
  420. hdef : tdef;
  421. defpos,storetokenpos : tfileposinfo;
  422. old_block_type : tblock_type;
  423. old_checkforwarddefs: TFPObjectList;
  424. objecttype : tobjecttyp;
  425. first,
  426. isgeneric,
  427. isunique,
  428. istyperenaming : boolean;
  429. generictypelist : tfphashobjectlist;
  430. generictokenbuf : tdynamicarray;
  431. vmtbuilder : TVMTBuilder;
  432. p:tnode;
  433. gendef : tstoreddef;
  434. s : shortstring;
  435. i : longint;
  436. {$ifdef x86}
  437. segment_register: string;
  438. {$endif x86}
  439. begin
  440. old_block_type:=block_type;
  441. { save unit container of forward declarations -
  442. we can be inside nested class type block }
  443. old_checkforwarddefs:=current_module.checkforwarddefs;
  444. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  445. block_type:=bt_type;
  446. hdef:=nil;
  447. first:=true;
  448. repeat
  449. defpos:=current_tokenpos;
  450. istyperenaming:=false;
  451. generictypelist:=nil;
  452. generictokenbuf:=nil;
  453. { fpc generic declaration? }
  454. if first then
  455. had_generic:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  456. isgeneric:=had_generic;
  457. typename:=pattern;
  458. orgtypename:=orgpattern;
  459. consume(_ID);
  460. { delphi generic declaration? }
  461. if (m_delphi in current_settings.modeswitches) then
  462. isgeneric:=token=_LSHARPBRACKET;
  463. { Generic type declaration? }
  464. if isgeneric then
  465. begin
  466. if assigned(current_genericdef) then
  467. Message(parser_f_no_generic_inside_generic);
  468. consume(_LSHARPBRACKET);
  469. generictypelist:=parse_generic_parameters(true);
  470. consume(_RSHARPBRACKET);
  471. { we are not freeing the type parameters, so register them }
  472. for i:=0 to generictypelist.count-1 do
  473. begin
  474. ttypesym(generictypelist[i]).register_sym;
  475. tstoreddef(ttypesym(generictypelist[i]).typedef).register_def;
  476. end;
  477. str(generictypelist.Count,s);
  478. gentypename:=typename+'$'+s;
  479. genorgtypename:=orgtypename+'$'+s;
  480. end
  481. else
  482. begin
  483. gentypename:=typename;
  484. genorgtypename:=orgtypename;
  485. end;
  486. consume(_EQ);
  487. { support 'ttype=type word' syntax }
  488. isunique:=try_to_consume(_TYPE);
  489. { MacPas object model is more like Delphi's than like TP's, but }
  490. { uses the object keyword instead of class }
  491. if (m_mac in current_settings.modeswitches) and
  492. (token = _OBJECT) then
  493. token := _CLASS;
  494. { Start recording a generic template }
  495. if assigned(generictypelist) then
  496. begin
  497. generictokenbuf:=tdynamicarray.create(256);
  498. current_scanner.startrecordtokens(generictokenbuf);
  499. end;
  500. { is the type already defined? -- must be in the current symtable,
  501. not in a nested symtable or one higher up the stack -> don't
  502. use searchsym & frinds! }
  503. sym:=tsym(symtablestack.top.find(gentypename));
  504. newtype:=nil;
  505. { found a symbol with this name? }
  506. if assigned(sym) then
  507. begin
  508. if (sym.typ=typesym) and
  509. { this should not be a symbol that was created by a generic
  510. that was declared earlier }
  511. not (
  512. (ttypesym(sym).typedef.typ=undefineddef) and
  513. (sp_generic_dummy in sym.symoptions)
  514. ) then
  515. begin
  516. if ((token=_CLASS) or
  517. (token=_INTERFACE) or
  518. (token=_DISPINTERFACE) or
  519. (token=_OBJCCLASS) or
  520. (token=_OBJCPROTOCOL) or
  521. (token=_OBJCCATEGORY)) and
  522. (assigned(ttypesym(sym).typedef)) and
  523. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  524. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  525. begin
  526. case token of
  527. _CLASS :
  528. objecttype:=default_class_type;
  529. _INTERFACE :
  530. case current_settings.interfacetype of
  531. it_interfacecom:
  532. objecttype:=odt_interfacecom;
  533. it_interfacecorba:
  534. objecttype:=odt_interfacecorba;
  535. it_interfacejava:
  536. objecttype:=odt_interfacejava;
  537. else
  538. internalerror(2010122611);
  539. end;
  540. _DISPINTERFACE :
  541. objecttype:=odt_dispinterface;
  542. _OBJCCLASS,
  543. _OBJCCATEGORY :
  544. objecttype:=odt_objcclass;
  545. _OBJCPROTOCOL :
  546. objecttype:=odt_objcprotocol;
  547. else
  548. internalerror(200811072);
  549. end;
  550. consume(token);
  551. { determine the generic def in case we are in a nested type
  552. of a specialization }
  553. gendef:=determine_generic_def(gentypename);
  554. { we can ignore the result, the definition is modified }
  555. object_dec(objecttype,genorgtypename,newtype,gendef,generictypelist,tobjectdef(ttypesym(sym).typedef),ht_none);
  556. newtype:=ttypesym(sym);
  557. hdef:=newtype.typedef;
  558. end
  559. else
  560. message1(parser_h_type_redef,genorgtypename);
  561. end;
  562. end;
  563. { no old type reused ? Then insert this new type }
  564. if not assigned(newtype) then
  565. begin
  566. { insert the new type first with an errordef, so that
  567. referencing the type before it's really set it
  568. will give an error (PFV) }
  569. hdef:=generrordef;
  570. gendef:=nil;
  571. storetokenpos:=current_tokenpos;
  572. if isgeneric then
  573. begin
  574. { for generics we need to check whether a non-generic type
  575. already exists and if not we need to insert a symbol with
  576. the non-generic name (available in (org)typename) that is a
  577. undefineddef, so that inline specializations can be used }
  578. sym:=tsym(symtablestack.top.Find(typename));
  579. if not assigned(sym) then
  580. begin
  581. sym:=ctypesym.create(orgtypename,cundefineddef.create(true),true);
  582. Include(sym.symoptions,sp_generic_dummy);
  583. ttypesym(sym).typedef.typesym:=sym;
  584. sym.visibility:=symtablestack.top.currentvisibility;
  585. symtablestack.top.insert(sym);
  586. ttypesym(sym).typedef.owner:=sym.owner;
  587. end
  588. else
  589. { this is not allowed in non-Delphi modes }
  590. if not (m_delphi in current_settings.modeswitches) then
  591. Message1(sym_e_duplicate_id,genorgtypename)
  592. else
  593. { we need to find this symbol even if it's a variable or
  594. something else when doing an inline specialization }
  595. Include(sym.symoptions,sp_generic_dummy);
  596. end
  597. else
  598. begin
  599. if assigned(sym) and (sym.typ=typesym) and
  600. (ttypesym(sym).typedef.typ=undefineddef) and
  601. (sp_generic_dummy in sym.symoptions) then
  602. begin
  603. { this is a symbol that was added by an earlier generic
  604. declaration, reuse it }
  605. newtype:=ttypesym(sym);
  606. newtype.typedef:=hdef;
  607. sym:=nil;
  608. end;
  609. { determine the generic def in case we are in a nested type
  610. of a specialization }
  611. gendef:=determine_generic_def(gentypename);
  612. end;
  613. { insert a new type if we don't reuse an existing symbol }
  614. if not assigned(newtype) then
  615. begin
  616. newtype:=ctypesym.create(genorgtypename,hdef,true);
  617. newtype.visibility:=symtablestack.top.currentvisibility;
  618. symtablestack.top.insert(newtype);
  619. end;
  620. current_tokenpos:=defpos;
  621. current_tokenpos:=storetokenpos;
  622. { read the type definition }
  623. read_named_type(hdef,newtype,gendef,generictypelist,false,isunique);
  624. { update the definition of the type }
  625. if assigned(hdef) then
  626. begin
  627. if df_generic in hdef.defoptions then
  628. { flag parent symtables that they now contain a generic }
  629. hdef.owner.includeoption(sto_has_generic);
  630. if assigned(hdef.typesym) then
  631. begin
  632. istyperenaming:=true;
  633. include(newtype.symoptions,sp_explicitrename);
  634. end;
  635. if isunique then
  636. begin
  637. if is_objc_class_or_protocol(hdef) or
  638. is_java_class_or_interface(hdef) then
  639. Message(parser_e_unique_unsupported);
  640. hdef:=tstoreddef(hdef).getcopy;
  641. { check if it is an ansistirng(codepage) declaration }
  642. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  643. begin
  644. p:=comp_expr([ef_accept_equal]);
  645. consume(_RKLAMMER);
  646. if not is_constintnode(p) then
  647. begin
  648. Message(parser_e_illegal_expression);
  649. { error recovery }
  650. end
  651. else
  652. begin
  653. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  654. begin
  655. Message(parser_e_invalid_codepage);
  656. tordconstnode(p).value:=0;
  657. end;
  658. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  659. end;
  660. p.free;
  661. end;
  662. { fix name, it is used e.g. for tables }
  663. if is_class_or_interface_or_dispinterface(hdef) then
  664. with tobjectdef(hdef) do
  665. begin
  666. stringdispose(objname);
  667. stringdispose(objrealname);
  668. objrealname:=stringdup(genorgtypename);
  669. objname:=stringdup(upper(genorgtypename));
  670. end;
  671. include(hdef.defoptions,df_unique);
  672. if (hdef.typ in [pointerdef,classrefdef]) and
  673. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  674. current_module.checkforwarddefs.add(hdef);
  675. end;
  676. if not assigned(hdef.typesym) then
  677. hdef.typesym:=newtype;
  678. end;
  679. { in non-Delphi modes we need a reference to the generic def
  680. without the generic suffix, so it can be found easily when
  681. parsing method implementations }
  682. if isgeneric and assigned(sym) and
  683. not (m_delphi in current_settings.modeswitches) and
  684. (ttypesym(sym).typedef.typ=undefineddef) then
  685. { don't free the undefineddef as the defids rely on the count
  686. of the defs in the def list of the module}
  687. ttypesym(sym).typedef:=hdef;
  688. newtype.typedef:=hdef;
  689. { KAZ: handle TGUID declaration in system unit }
  690. if (cs_compilesystem in current_settings.moduleswitches) and
  691. assigned(hdef) and
  692. (hdef.typ=recorddef) then
  693. begin
  694. if not assigned(rec_tguid) and
  695. (gentypename='TGUID') and
  696. (hdef.size=16) then
  697. rec_tguid:=trecorddef(hdef)
  698. else if not assigned(rec_jmp_buf) and
  699. (gentypename='JMP_BUF') then
  700. rec_jmp_buf:=trecorddef(hdef)
  701. else if not assigned(rec_exceptaddr) and
  702. (gentypename='TEXCEPTADDR') then
  703. rec_exceptaddr:=trecorddef(hdef);
  704. end;
  705. end;
  706. if assigned(hdef) then
  707. begin
  708. case hdef.typ of
  709. pointerdef :
  710. begin
  711. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  712. consume(_SEMICOLON);
  713. {$ifdef x86}
  714. {$ifdef i8086}
  715. if try_to_consume(_HUGE) then
  716. begin
  717. tcpupointerdef(hdef).x86pointertyp:=x86pt_huge;
  718. consume(_SEMICOLON);
  719. end
  720. else
  721. {$endif i8086}
  722. if try_to_consume(_FAR) then
  723. begin
  724. {$if defined(i8086)}
  725. tcpupointerdef(hdef).x86pointertyp:=x86pt_far;
  726. {$elseif defined(i386)}
  727. tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  728. {$elseif defined(x86_64)}
  729. { for compatibility with previous versions of fpc,
  730. far pointer = regular pointer on x86_64 }
  731. Message1(parser_w_ptr_type_ignored,'FAR');
  732. {$endif}
  733. consume(_SEMICOLON);
  734. end
  735. else
  736. if try_to_consume(_NEAR) then
  737. begin
  738. if token <> _SEMICOLON then
  739. begin
  740. segment_register:=get_stringconst;
  741. case UpCase(segment_register) of
  742. 'CS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_cs;
  743. 'DS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ds;
  744. 'SS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ss;
  745. 'ES': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_es;
  746. 'FS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  747. 'GS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_gs;
  748. else
  749. Message(asmr_e_invalid_register);
  750. end;
  751. end
  752. else
  753. tcpupointerdef(hdef).x86pointertyp:=x86pt_near;
  754. consume(_SEMICOLON);
  755. end;
  756. {$else x86}
  757. { Previous versions of FPC support declaring a pointer as
  758. far even on non-x86 platforms. }
  759. if try_to_consume(_FAR) then
  760. begin
  761. Message1(parser_w_ptr_type_ignored,'FAR');
  762. consume(_SEMICOLON);
  763. end;
  764. {$endif x86}
  765. end;
  766. procvardef :
  767. begin
  768. { in case of type renaming, don't parse proc directives }
  769. if istyperenaming then
  770. begin
  771. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  772. consume(_SEMICOLON);
  773. end
  774. else
  775. begin
  776. if not check_proc_directive(true) then
  777. begin
  778. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  779. consume(_SEMICOLON);
  780. end;
  781. parse_var_proc_directives(tsym(newtype));
  782. if po_is_function_ref in tprocvardef(hdef).procoptions then
  783. begin
  784. { these always support everything, no "of object" or
  785. "is_nested" is allowed }
  786. if is_nested_pd(tprocvardef(hdef)) or
  787. is_methodpointer(hdef) then
  788. cgmessage(type_e_function_reference_kind)
  789. else
  790. begin
  791. if (po_hascallingconvention in tprocvardef(hdef).procoptions) and
  792. (tprocvardef(hdef).proccalloption=pocall_cdecl) then
  793. begin
  794. include(tprocvardef(hdef).procoptions,po_is_block);
  795. { can't check yet whether the parameter types
  796. are valid for a block, since some of them
  797. may still be forwarddefs }
  798. end
  799. else
  800. { a regular anonymous function type: not yet supported }
  801. { the }
  802. Comment(V_Error,'Function references are not yet supported, only C blocks (add "cdecl;" at the end)');
  803. end
  804. end;
  805. handle_calling_convention(tprocvardef(hdef));
  806. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  807. consume(_SEMICOLON);
  808. end;
  809. end;
  810. objectdef :
  811. begin
  812. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  813. consume(_SEMICOLON);
  814. { change a forward and external class declaration into
  815. formal external definition, so the compiler does not
  816. expect an real definition later }
  817. if is_objc_class_or_protocol(hdef) or
  818. is_java_class_or_interface(hdef) then
  819. finalize_class_external_status(tobjectdef(hdef));
  820. { Build VMT indexes, skip for type renaming and forward classes }
  821. if (hdef.typesym=newtype) and
  822. not(oo_is_forward in tobjectdef(hdef).objectoptions) then
  823. begin
  824. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  825. vmtbuilder.generate_vmt;
  826. vmtbuilder.free;
  827. end;
  828. { In case of an objcclass, verify that all methods have a message
  829. name set. We only check this now, because message names can be set
  830. during the protocol (interface) mapping. At the same time, set the
  831. mangled names (these depend on the "external" name of the class),
  832. and mark private fields of external classes as "used" (to avoid
  833. bogus notes about them being unused)
  834. }
  835. { watch out for crashes in case of errors }
  836. if is_objc_class_or_protocol(hdef) and
  837. (not is_objccategory(hdef) or
  838. assigned(tobjectdef(hdef).childof)) then
  839. tobjectdef(hdef).finish_objc_data;
  840. if is_cppclass(hdef) then
  841. tobjectdef(hdef).finish_cpp_data;
  842. end;
  843. recorddef :
  844. begin
  845. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  846. consume(_SEMICOLON);
  847. end;
  848. else
  849. begin
  850. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  851. consume(_SEMICOLON);
  852. end;
  853. end;
  854. end;
  855. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  856. or is_objectpascal_helper(hdef)) then
  857. message(parser_e_cant_create_generics_of_this_type);
  858. { Stop recording a generic template }
  859. if assigned(generictypelist) then
  860. begin
  861. current_scanner.stoprecordtokens;
  862. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  863. { Generic is never a type renaming }
  864. hdef.typesym:=newtype;
  865. generictypelist.free;
  866. end;
  867. if not (m_delphi in current_settings.modeswitches) and
  868. (token=_ID) and (idtoken=_GENERIC) then
  869. begin
  870. had_generic:=true;
  871. consume(_ID);
  872. if token in [_PROCEDURE,_FUNCTION,_CLASS] then
  873. break;
  874. end
  875. else
  876. had_generic:=false;
  877. first:=false;
  878. until (token<>_ID) or
  879. (in_structure and
  880. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  881. ((m_final_fields in current_settings.modeswitches) and
  882. (idtoken=_FINAL))));
  883. { resolve type block forward declarations and restore a unit
  884. container for them }
  885. resolve_forward_types;
  886. current_module.checkforwarddefs.free;
  887. current_module.checkforwarddefs:=old_checkforwarddefs;
  888. block_type:=old_block_type;
  889. end;
  890. { reads a type declaration to the symbol table }
  891. procedure type_dec(out had_generic:boolean);
  892. begin
  893. consume(_TYPE);
  894. types_dec(false,had_generic);
  895. end;
  896. procedure var_dec(out had_generic:boolean);
  897. { parses variable declarations and inserts them in }
  898. { the top symbol table of symtablestack }
  899. begin
  900. consume(_VAR);
  901. read_var_decls([vd_check_generic],had_generic);
  902. end;
  903. procedure property_dec;
  904. { parses a global property (fpc mode feature) }
  905. var
  906. old_block_type: tblock_type;
  907. begin
  908. consume(_PROPERTY);
  909. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  910. message(parser_e_property_only_sgr);
  911. old_block_type:=block_type;
  912. block_type:=bt_const;
  913. repeat
  914. read_property_dec(false, nil);
  915. consume(_SEMICOLON);
  916. until token<>_ID;
  917. block_type:=old_block_type;
  918. end;
  919. procedure threadvar_dec(out had_generic:boolean);
  920. { parses thread variable declarations and inserts them in }
  921. { the top symbol table of symtablestack }
  922. begin
  923. consume(_THREADVAR);
  924. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  925. message(parser_e_threadvars_only_sg);
  926. if f_threading in features then
  927. read_var_decls([vd_threadvar,vd_check_generic],had_generic)
  928. else
  929. begin
  930. Message1(parser_f_unsupported_feature,featurestr[f_threading]);
  931. read_var_decls([vd_check_generic],had_generic);
  932. end;
  933. end;
  934. procedure resourcestring_dec(out had_generic:boolean);
  935. var
  936. orgname : TIDString;
  937. p : tnode;
  938. dummysymoptions : tsymoptions;
  939. deprecatedmsg : pshortstring;
  940. storetokenpos,filepos : tfileposinfo;
  941. old_block_type : tblock_type;
  942. sp : pchar;
  943. sym : tsym;
  944. first,
  945. isgeneric : boolean;
  946. begin
  947. if target_info.system in systems_managed_vm then
  948. message(parser_e_feature_unsupported_for_vm);
  949. consume(_RESOURCESTRING);
  950. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  951. message(parser_e_resourcestring_only_sg);
  952. first:=true;
  953. had_generic:=false;
  954. old_block_type:=block_type;
  955. block_type:=bt_const;
  956. repeat
  957. orgname:=orgpattern;
  958. filepos:=current_tokenpos;
  959. isgeneric:=not (m_delphi in current_settings.modeswitches) and (token=_ID) and (idtoken=_GENERIC);
  960. consume(_ID);
  961. case token of
  962. _EQ:
  963. begin
  964. consume(_EQ);
  965. p:=comp_expr([ef_accept_equal]);
  966. storetokenpos:=current_tokenpos;
  967. current_tokenpos:=filepos;
  968. sym:=nil;
  969. case p.nodetype of
  970. ordconstn:
  971. begin
  972. if is_constcharnode(p) then
  973. begin
  974. getmem(sp,2);
  975. sp[0]:=chr(tordconstnode(p).value.svalue);
  976. sp[1]:=#0;
  977. sym:=cconstsym.create_string(orgname,constresourcestring,sp,1,nil);
  978. end
  979. else
  980. Message(parser_e_illegal_expression);
  981. end;
  982. stringconstn:
  983. with Tstringconstnode(p) do
  984. begin
  985. { resourcestrings are currently always single byte }
  986. if cst_type in [cst_widestring,cst_unicodestring] then
  987. changestringtype(getansistringdef);
  988. getmem(sp,len+1);
  989. move(value_str^,sp^,len+1);
  990. sym:=cconstsym.create_string(orgname,constresourcestring,sp,len,nil);
  991. end;
  992. else
  993. Message(parser_e_illegal_expression);
  994. end;
  995. current_tokenpos:=storetokenpos;
  996. { Support hint directives }
  997. dummysymoptions:=[];
  998. deprecatedmsg:=nil;
  999. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  1000. if assigned(sym) then
  1001. begin
  1002. sym.symoptions:=sym.symoptions+dummysymoptions;
  1003. sym.deprecatedmsg:=deprecatedmsg;
  1004. symtablestack.top.insert(sym);
  1005. end
  1006. else
  1007. stringdispose(deprecatedmsg);
  1008. consume(_SEMICOLON);
  1009. p.free;
  1010. end;
  1011. else
  1012. if not first and isgeneric and
  1013. (token in [_PROCEDURE, _FUNCTION, _CLASS]) then
  1014. begin
  1015. had_generic:=true;
  1016. break;
  1017. end
  1018. else
  1019. consume(_EQ);
  1020. end;
  1021. first:=false;
  1022. until token<>_ID;
  1023. block_type:=old_block_type;
  1024. end;
  1025. end.