pdecl.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. { global }
  22. globals,
  23. { symtable }
  24. symsym,
  25. { pass_1 }
  26. node;
  27. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  28. procedure const_dec;
  29. procedure label_dec;
  30. procedure type_dec;
  31. procedure var_dec;
  32. procedure threadvar_dec;
  33. procedure property_dec;
  34. procedure resourcestring_dec;
  35. implementation
  36. uses
  37. { common }
  38. cutils,cclasses,
  39. { global }
  40. globtype,tokens,verbose,
  41. systems,
  42. { aasm }
  43. aasmbase,aasmtai,fmodule,
  44. { symtable }
  45. symconst,symbase,symtype,symdef,symtable,paramgr,
  46. { pass 1 }
  47. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nobj,
  48. { codegen }
  49. ncgutil,
  50. { parser }
  51. scanner,
  52. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,
  53. { cpu-information }
  54. cpuinfo
  55. ;
  56. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  57. var
  58. hp : tconstsym;
  59. p : tnode;
  60. ps : pconstset;
  61. pd : pbestreal;
  62. pg : pguid;
  63. sp : pchar;
  64. storetokenpos : tfileposinfo;
  65. begin
  66. readconstant:=nil;
  67. if orgname='' then
  68. internalerror(9584582);
  69. hp:=nil;
  70. p:=comp_expr(true);
  71. storetokenpos:=akttokenpos;
  72. akttokenpos:=filepos;
  73. case p.nodetype of
  74. ordconstn:
  75. begin
  76. if p.resulttype.def.deftype=pointerdef then
  77. hp:=tconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value,p.resulttype)
  78. else
  79. hp:=tconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resulttype);
  80. end;
  81. stringconstn:
  82. begin
  83. getmem(sp,tstringconstnode(p).len+1);
  84. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  85. hp:=tconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len);
  86. end;
  87. realconstn :
  88. begin
  89. new(pd);
  90. pd^:=trealconstnode(p).value_real;
  91. hp:=tconstsym.create_ptr(orgname,constreal,pd,p.resulttype);
  92. end;
  93. setconstn :
  94. begin
  95. new(ps);
  96. ps^:=tsetconstnode(p).value_set^;
  97. hp:=tconstsym.create_ptr(orgname,constset,ps,p.resulttype);
  98. end;
  99. pointerconstn :
  100. begin
  101. hp:=tconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resulttype);
  102. end;
  103. niln :
  104. begin
  105. hp:=tconstsym.create_ord(orgname,constnil,0,p.resulttype);
  106. end;
  107. typen :
  108. begin
  109. if is_interface(p.resulttype.def) then
  110. begin
  111. if assigned(tobjectdef(p.resulttype.def).iidguid) then
  112. begin
  113. new(pg);
  114. pg^:=tobjectdef(p.resulttype.def).iidguid^;
  115. hp:=tconstsym.create_ptr(orgname,constguid,pg,p.resulttype);
  116. end
  117. else
  118. Message1(parser_e_interface_has_no_guid,tobjectdef(p.resulttype.def).objrealname^);
  119. end
  120. else
  121. Message(parser_e_illegal_expression);
  122. end;
  123. else
  124. Message(parser_e_illegal_expression);
  125. end;
  126. akttokenpos:=storetokenpos;
  127. p.free;
  128. readconstant:=hp;
  129. end;
  130. procedure const_dec;
  131. var
  132. orgname : stringid;
  133. tt : ttype;
  134. sym : tsym;
  135. dummysymoptions : tsymoptions;
  136. storetokenpos,filepos : tfileposinfo;
  137. old_block_type : tblock_type;
  138. skipequal : boolean;
  139. begin
  140. consume(_CONST);
  141. old_block_type:=block_type;
  142. block_type:=bt_const;
  143. repeat
  144. orgname:=orgpattern;
  145. filepos:=akttokenpos;
  146. consume(_ID);
  147. case token of
  148. _EQUAL:
  149. begin
  150. consume(_EQUAL);
  151. sym:=readconstant(orgname,filepos);
  152. { Support hint directives }
  153. dummysymoptions:=[];
  154. try_consume_hintdirective(dummysymoptions);
  155. if assigned(sym) then
  156. begin
  157. sym.symoptions:=sym.symoptions+dummysymoptions;
  158. symtablestack.insert(sym);
  159. end;
  160. consume(_SEMICOLON);
  161. end;
  162. _COLON:
  163. begin
  164. { set the blocktype first so a consume also supports a
  165. caret, to support const s : ^string = nil }
  166. block_type:=bt_type;
  167. consume(_COLON);
  168. ignore_equal:=true;
  169. read_type(tt,'',false);
  170. ignore_equal:=false;
  171. block_type:=bt_const;
  172. skipequal:=false;
  173. { create symbol }
  174. storetokenpos:=akttokenpos;
  175. akttokenpos:=filepos;
  176. sym:=ttypedconstsym.createtype(orgname,tt,(cs_typed_const_writable in aktlocalswitches));
  177. akttokenpos:=storetokenpos;
  178. symtablestack.insert(sym);
  179. insertconstdata(ttypedconstsym(sym));
  180. { procvar can have proc directives, but not type references }
  181. if (tt.def.deftype=procvardef) and
  182. (tt.sym=nil) then
  183. begin
  184. { support p : procedure;stdcall=nil; }
  185. if try_to_consume(_SEMICOLON) then
  186. begin
  187. if check_proc_directive(true) then
  188. parse_var_proc_directives(sym)
  189. else
  190. begin
  191. Message(parser_e_proc_directive_expected);
  192. skipequal:=true;
  193. end;
  194. end
  195. else
  196. { support p : procedure stdcall=nil; }
  197. begin
  198. if check_proc_directive(true) then
  199. parse_var_proc_directives(sym);
  200. end;
  201. { add default calling convention }
  202. handle_calling_convention(tabstractprocdef(tt.def));
  203. end;
  204. if not skipequal then
  205. begin
  206. { get init value }
  207. consume(_EQUAL);
  208. readtypedconst(tt,ttypedconstsym(sym),(cs_typed_const_writable in aktlocalswitches));
  209. try_consume_hintdirective(sym.symoptions);
  210. consume(_SEMICOLON);
  211. end;
  212. end;
  213. else
  214. { generate an error }
  215. consume(_EQUAL);
  216. end;
  217. until token<>_ID;
  218. block_type:=old_block_type;
  219. end;
  220. procedure label_dec;
  221. var
  222. hl : tasmlabel;
  223. begin
  224. consume(_LABEL);
  225. if not(cs_support_goto in aktmoduleswitches) then
  226. Message(sym_e_goto_and_label_not_supported);
  227. repeat
  228. if not(token in [_ID,_INTCONST]) then
  229. consume(_ID)
  230. else
  231. begin
  232. if token=_ID then
  233. symtablestack.insert(tlabelsym.create(orgpattern))
  234. else
  235. symtablestack.insert(tlabelsym.create(pattern));
  236. consume(token);
  237. end;
  238. if token<>_SEMICOLON then consume(_COMMA);
  239. until not(token in [_ID,_INTCONST]);
  240. consume(_SEMICOLON);
  241. end;
  242. { search in symtablestack used, but not defined type }
  243. procedure resolve_type_forward(p : tnamedindexitem;arg:pointer);
  244. var
  245. hpd,pd : tdef;
  246. stpos : tfileposinfo;
  247. again : boolean;
  248. srsym : tsym;
  249. srsymtable : tsymtable;
  250. {$ifdef gdb_notused}
  251. stab_str:Pchar;
  252. {$endif gdb_notused}
  253. begin
  254. { Check only typesyms or record/object fields }
  255. case tsym(p).typ of
  256. typesym :
  257. pd:=ttypesym(p).restype.def;
  258. fieldvarsym :
  259. pd:=tfieldvarsym(p).vartype.def
  260. else
  261. exit;
  262. end;
  263. repeat
  264. again:=false;
  265. case pd.deftype of
  266. arraydef :
  267. begin
  268. { elementtype could also be defined using a forwarddef }
  269. pd:=tarraydef(pd).elementtype.def;
  270. again:=true;
  271. end;
  272. pointerdef,
  273. classrefdef :
  274. begin
  275. { classrefdef inherits from pointerdef }
  276. hpd:=tpointerdef(pd).pointertype.def;
  277. { still a forward def ? }
  278. if hpd.deftype=forwarddef then
  279. begin
  280. { try to resolve the forward }
  281. { get the correct position for it }
  282. stpos:=akttokenpos;
  283. akttokenpos:=tforwarddef(hpd).forwardpos;
  284. resolving_forward:=true;
  285. make_ref:=false;
  286. if not assigned(tforwarddef(hpd).tosymname) then
  287. internalerror(20021120);
  288. searchsym(tforwarddef(hpd).tosymname^,srsym,srsymtable);
  289. make_ref:=true;
  290. resolving_forward:=false;
  291. akttokenpos:=stpos;
  292. { we don't need the forwarddef anymore, dispose it }
  293. hpd.free;
  294. tpointerdef(pd).pointertype.def:=nil; { if error occurs }
  295. { was a type sym found ? }
  296. if assigned(srsym) and
  297. (srsym.typ=typesym) then
  298. begin
  299. tpointerdef(pd).pointertype.setsym(srsym);
  300. { avoid wrong unused warnings web bug 801 PM }
  301. inc(ttypesym(srsym).refs);
  302. {$ifdef GDB_UNUSED}
  303. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  304. (tsym(p).owner.symtabletype in [globalsymtable,staticsymtable]) then
  305. begin
  306. ttypesym(p).isusedinstab:=true;
  307. { ttypesym(p).concatstabto(debuglist);}
  308. {not stabs for forward defs }
  309. if not Ttypesym(p).isstabwritten then
  310. begin
  311. if Ttypesym(p).restype.def.typesym=p then
  312. Tstoreddef(Ttypesym(p).restype.def).concatstabto(debuglist)
  313. else
  314. begin
  315. stab_str:=Ttypesym(p).stabstring;
  316. if assigned(stab_str) then
  317. debuglist.concat(Tai_stabs.create(stab_str));
  318. Ttypesym(p).isstabwritten:=true;
  319. end;
  320. end;
  321. end;
  322. {$endif GDB_UNUSED}
  323. { we need a class type for classrefdef }
  324. if (pd.deftype=classrefdef) and
  325. not(is_class(ttypesym(srsym).restype.def)) then
  326. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename);
  327. end
  328. else
  329. begin
  330. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  331. { try to recover }
  332. tpointerdef(pd).pointertype:=generrortype;
  333. end;
  334. end;
  335. end;
  336. recorddef :
  337. trecorddef(pd).symtable.foreach_static(@resolve_type_forward,nil);
  338. objectdef :
  339. begin
  340. if not(m_fpc in aktmodeswitches) and
  341. (oo_is_forward in tobjectdef(pd).objectoptions) then
  342. begin
  343. { only give an error as the implementation may follow in an
  344. other type block which is allowed by FPC modes }
  345. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  346. end
  347. else
  348. begin
  349. { Check all fields of the object declaration, but don't
  350. check objectdefs in objects/records, because these
  351. can't exist (anonymous objects aren't allowed) }
  352. if not(tsym(p).owner.symtabletype in [objectsymtable,recordsymtable]) then
  353. tobjectdef(pd).symtable.foreach_static(@resolve_type_forward,nil);
  354. end;
  355. end;
  356. end;
  357. until not again;
  358. end;
  359. { reads a type declaration to the symbol table }
  360. procedure type_dec;
  361. var
  362. typename,orgtypename : stringid;
  363. newtype : ttypesym;
  364. sym : tsym;
  365. srsymtable : tsymtable;
  366. tt : ttype;
  367. oldfilepos,
  368. defpos,storetokenpos : tfileposinfo;
  369. old_block_type : tblock_type;
  370. ch : tclassheader;
  371. unique,istyperenaming : boolean;
  372. begin
  373. old_block_type:=block_type;
  374. block_type:=bt_type;
  375. consume(_TYPE);
  376. typecanbeforward:=true;
  377. repeat
  378. typename:=pattern;
  379. orgtypename:=orgpattern;
  380. defpos:=akttokenpos;
  381. istyperenaming:=false;
  382. consume(_ID);
  383. consume(_EQUAL);
  384. { support 'ttype=type word' syntax }
  385. unique:=try_to_consume(_TYPE);
  386. { is the type already defined? }
  387. searchsym(typename,sym,srsymtable);
  388. newtype:=nil;
  389. { found a symbol with this name? }
  390. if assigned(sym) then
  391. begin
  392. if (sym.typ=typesym) then
  393. begin
  394. if ((token=_CLASS) or
  395. (token=_INTERFACE)) and
  396. (assigned(ttypesym(sym).restype.def)) and
  397. is_class_or_interface(ttypesym(sym).restype.def) and
  398. (oo_is_forward in tobjectdef(ttypesym(sym).restype.def).objectoptions) then
  399. begin
  400. { we can ignore the result }
  401. { the definition is modified }
  402. object_dec(orgtypename,tobjectdef(ttypesym(sym).restype.def));
  403. newtype:=ttypesym(sym);
  404. tt:=newtype.restype;
  405. end
  406. else
  407. message1(parser_h_type_redef,orgtypename);
  408. end;
  409. end;
  410. { no old type reused ? Then insert this new type }
  411. if not assigned(newtype) then
  412. begin
  413. { insert the new type first with an errordef, so that
  414. referencing the type before it's really set it
  415. will give an error (PFV) }
  416. tt:=generrortype;
  417. storetokenpos:=akttokenpos;
  418. newtype:=ttypesym.create(orgtypename,tt);
  419. symtablestack.insert(newtype);
  420. akttokenpos:=defpos;
  421. akttokenpos:=storetokenpos;
  422. { read the type definition }
  423. read_type(tt,orgtypename,false);
  424. { update the definition of the type }
  425. newtype.restype:=tt;
  426. if assigned(tt.sym) then
  427. istyperenaming:=true
  428. else
  429. tt.sym:=newtype;
  430. if unique and assigned(tt.def) then
  431. begin
  432. tt.setdef(tstoreddef(tt.def).getcopy);
  433. include(tt.def.defoptions,df_unique);
  434. newtype.restype:=tt;
  435. end;
  436. if assigned(tt.def) and not assigned(tt.def.typesym) then
  437. tt.def.typesym:=newtype;
  438. { KAZ: handle TGUID declaration in system unit }
  439. if (cs_compilesystem in aktmoduleswitches) and not assigned(rec_tguid) and
  440. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  441. assigned(tt.def) and (tt.def.deftype=recorddef) and (tt.def.size=16) then
  442. rec_tguid:=trecorddef(tt.def);
  443. end;
  444. if assigned(tt.def) then
  445. begin
  446. case tt.def.deftype of
  447. pointerdef :
  448. begin
  449. consume(_SEMICOLON);
  450. if try_to_consume(_FAR) then
  451. begin
  452. tpointerdef(tt.def).is_far:=true;
  453. consume(_SEMICOLON);
  454. end;
  455. end;
  456. procvardef :
  457. begin
  458. { in case of type renaming, don't parse proc directives }
  459. if istyperenaming then
  460. consume(_SEMICOLON)
  461. else
  462. begin
  463. if not check_proc_directive(true) then
  464. consume(_SEMICOLON);
  465. parse_var_proc_directives(tsym(newtype));
  466. handle_calling_convention(tprocvardef(tt.def));
  467. end;
  468. end;
  469. objectdef,
  470. recorddef :
  471. begin
  472. try_consume_hintdirective(newtype.symoptions);
  473. consume(_SEMICOLON);
  474. end;
  475. else
  476. consume(_SEMICOLON);
  477. end;
  478. end;
  479. { Write tables if we are the typesym that defines
  480. this type. This will not be done for simple type renamings }
  481. if (tt.def.typesym=newtype) then
  482. begin
  483. { file position }
  484. oldfilepos:=aktfilepos;
  485. aktfilepos:=newtype.fileinfo;
  486. { generate persistent init/final tables when it's declared in the interface so it can
  487. be reused in other used }
  488. if current_module.in_interface and
  489. ((is_class(tt.def) and
  490. tobjectdef(tt.def).members_need_inittable) or
  491. tt.def.needs_inittable) then
  492. generate_inittable(newtype);
  493. { for objects we should write the vmt and interfaces.
  494. This need to be done after the rtti has been written, because
  495. it can contain a reference to that data (PFV)
  496. This is not for forward classes }
  497. if (tt.def.deftype=objectdef) then
  498. with Tobjectdef(tt.def) do
  499. begin
  500. if not(oo_is_forward in objectoptions) then
  501. begin
  502. ch:=tclassheader.create(tobjectdef(tt.def));
  503. { generate and check virtual methods, must be done
  504. before RTTI is written }
  505. ch.genvmt;
  506. { Generate RTTI for class }
  507. generate_rtti(newtype);
  508. if is_interface(tobjectdef(tt.def)) then
  509. ch.writeinterfaceids;
  510. if (oo_has_vmt in objectoptions) then
  511. ch.writevmt;
  512. ch.free;
  513. end;
  514. end
  515. else
  516. begin
  517. { Always generate RTTI info for all types. This is to have typeinfo() return
  518. the same pointer }
  519. generate_rtti(newtype);
  520. end;
  521. aktfilepos:=oldfilepos;
  522. end;
  523. until token<>_ID;
  524. typecanbeforward:=false;
  525. symtablestack.foreach_static(@resolve_type_forward,nil);
  526. block_type:=old_block_type;
  527. end;
  528. procedure var_dec;
  529. { parses variable declarations and inserts them in }
  530. { the top symbol table of symtablestack }
  531. begin
  532. consume(_VAR);
  533. read_var_decs(false,false,false);
  534. end;
  535. procedure property_dec;
  536. var
  537. old_block_type : tblock_type;
  538. begin
  539. consume(_PROPERTY);
  540. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  541. message(parser_e_resourcestring_only_sg);
  542. old_block_type:=block_type;
  543. block_type:=bt_const;
  544. repeat
  545. read_property_dec(nil);
  546. consume(_SEMICOLON);
  547. until token<>_ID;
  548. block_type:=old_block_type;
  549. end;
  550. procedure threadvar_dec;
  551. { parses thread variable declarations and inserts them in }
  552. { the top symbol table of symtablestack }
  553. begin
  554. consume(_THREADVAR);
  555. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  556. message(parser_e_threadvars_only_sg);
  557. read_var_decs(false,false,true);
  558. end;
  559. procedure resourcestring_dec;
  560. var
  561. orgname : stringid;
  562. p : tnode;
  563. dummysymoptions : tsymoptions;
  564. storetokenpos,filepos : tfileposinfo;
  565. old_block_type : tblock_type;
  566. sp : pchar;
  567. sym : tsym;
  568. begin
  569. consume(_RESOURCESTRING);
  570. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  571. message(parser_e_resourcestring_only_sg);
  572. old_block_type:=block_type;
  573. block_type:=bt_const;
  574. repeat
  575. orgname:=orgpattern;
  576. filepos:=akttokenpos;
  577. consume(_ID);
  578. case token of
  579. _EQUAL:
  580. begin
  581. consume(_EQUAL);
  582. p:=comp_expr(true);
  583. storetokenpos:=akttokenpos;
  584. akttokenpos:=filepos;
  585. sym:=nil;
  586. case p.nodetype of
  587. ordconstn:
  588. begin
  589. if is_constcharnode(p) then
  590. begin
  591. getmem(sp,2);
  592. sp[0]:=chr(tordconstnode(p).value);
  593. sp[1]:=#0;
  594. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  595. end
  596. else
  597. Message(parser_e_illegal_expression);
  598. end;
  599. stringconstn:
  600. with Tstringconstnode(p) do
  601. begin
  602. getmem(sp,len+1);
  603. move(value_str^,sp^,len+1);
  604. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  605. end;
  606. else
  607. Message(parser_e_illegal_expression);
  608. end;
  609. akttokenpos:=storetokenpos;
  610. { Support hint directives }
  611. dummysymoptions:=[];
  612. try_consume_hintdirective(dummysymoptions);
  613. if assigned(sym) then
  614. begin
  615. sym.symoptions:=sym.symoptions+dummysymoptions;
  616. symtablestack.insert(sym);
  617. end;
  618. consume(_SEMICOLON);
  619. p.free;
  620. end;
  621. else consume(_EQUAL);
  622. end;
  623. until token<>_ID;
  624. block_type:=old_block_type;
  625. end;
  626. end.