pbase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains some helper routines for the parser
  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 pbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. tokens,globtype,
  23. symconst,symbase,symtype,symdef,symsym,symtable
  24. ;
  25. const
  26. { tokens that end a block or statement. And don't require
  27. a ; on the statement before }
  28. endtokens = [_SEMICOLON,_END,_ELSE,_UNTIL,_EXCEPT,_FINALLY];
  29. { true, if we are after an assignement }
  30. afterassignment : boolean = false;
  31. { true, if we are parsing arguments }
  32. in_args : boolean = false;
  33. { true, if we are parsing arguments allowing named parameters }
  34. named_args_allowed : boolean = false;
  35. { true, if we got an @ to get the address }
  36. got_addrn : boolean = false;
  37. { special for handling procedure vars }
  38. getprocvardef : tprocvardef = nil;
  39. { special for function reference vars }
  40. getfuncrefdef : tobjectdef = nil;
  41. var
  42. { for operators }
  43. optoken : ttoken;
  44. { true, if only routine headers should be parsed }
  45. parse_only : boolean;
  46. { true, if we found a name for a named arg }
  47. found_arg_name : boolean;
  48. { true, if we are parsing generic declaration }
  49. parse_generic : boolean;
  50. procedure identifier_not_found(const s:string);
  51. procedure identifier_not_found(const s:string;const filepos:tfileposinfo);
  52. { function tokenstring(i : ttoken):string;}
  53. { consumes token i, if the current token is unequal i }
  54. { a syntax error is written }
  55. procedure consume(i : ttoken);
  56. { Same as consume, but will not attempt to read next token if the token is a point }
  57. procedure consume_last_dot;
  58. {Tries to consume the token i, and returns true if it was consumed:
  59. if token=i.}
  60. function try_to_consume(i:Ttoken):boolean;
  61. { consumes all tokens til atoken (for error recovering }
  62. procedure consume_all_until(atoken : ttoken);
  63. { consumes tokens while they are semicolons }
  64. procedure consume_emptystats;
  65. { reads a list of identifiers into a string list }
  66. { consume a symbol, if not found give an error and
  67. and return an errorsym }
  68. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  69. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  70. type
  71. tconsume_unitsym_flag = (
  72. cuf_consume_id,
  73. cuf_allow_specialize,
  74. cuf_check_attr_suffix
  75. );
  76. tconsume_unitsym_flags = set of tconsume_unitsym_flag;
  77. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;out is_specialize:boolean;sympattern:TSymStr):boolean;
  78. function try_consume_unitsym_no_specialize(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;sympattern:TSymStr):boolean;
  79. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  80. { just for an accurate position of the end of a procedure (PM) }
  81. var
  82. last_endtoken_filepos: tfileposinfo;
  83. implementation
  84. uses
  85. globals,scanner,verbose,fmodule;
  86. {****************************************************************************
  87. Token Parsing
  88. ****************************************************************************}
  89. procedure identifier_not_found(const s:string);
  90. begin
  91. Message1(sym_e_id_not_found,s);
  92. { show a fatal that you need -S2 or -Sd, but only
  93. if we just parsed the a token that has m_class }
  94. if not(m_class in current_settings.modeswitches) and
  95. (Upper(s)=pattern) and
  96. (m_class in tokeninfo^[idtoken].keyword) then
  97. Message(parser_f_need_objfpc_or_delphi_mode);
  98. end;
  99. procedure identifier_not_found(const s:string;const filepos:tfileposinfo);
  100. begin
  101. MessagePos1(filepos,sym_e_id_not_found,s);
  102. { show a fatal that you need -S2 or -Sd, but only
  103. if we just parsed the a token that has m_class }
  104. if not(m_class in current_settings.modeswitches) and
  105. (Upper(s)=pattern) and
  106. (m_class in tokeninfo^[idtoken].keyword) then
  107. MessagePos(filepos,parser_f_need_objfpc_or_delphi_mode);
  108. end;
  109. { consumes token i, write error if token is different }
  110. procedure consume(i : ttoken);
  111. begin
  112. if (token<>i) and (idtoken<>i) then
  113. if token=_id then
  114. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  115. else
  116. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  117. else
  118. begin
  119. if token=_END then
  120. last_endtoken_filepos:=current_tokenpos;
  121. current_scanner.readtoken(true);
  122. end;
  123. end;
  124. procedure consume_last_dot;
  125. begin
  126. if (token<>_POINT) then
  127. begin
  128. if token=_id then
  129. Message2(scan_f_syn_expected,tokeninfo^[_POINT].str,'identifier '+pattern)
  130. else
  131. Message2(scan_f_syn_expected,tokeninfo^[_POINT].str,tokeninfo^[token].str)
  132. end
  133. else if c<>#0 then
  134. current_scanner.readtoken(true);
  135. end;
  136. function try_to_consume(i:Ttoken):boolean;
  137. begin
  138. try_to_consume:=false;
  139. if (token=i) or (idtoken=i) then
  140. begin
  141. try_to_consume:=true;
  142. if token=_END then
  143. last_endtoken_filepos:=current_tokenpos;
  144. current_scanner.readtoken(true);
  145. end;
  146. end;
  147. procedure consume_all_until(atoken : ttoken);
  148. begin
  149. while (token<>atoken) and (idtoken<>atoken) do
  150. begin
  151. Consume(token);
  152. if token=_EOF then
  153. begin
  154. Consume(atoken);
  155. Message(scan_f_end_of_file);
  156. exit;
  157. end;
  158. end;
  159. end;
  160. procedure consume_emptystats;
  161. begin
  162. repeat
  163. until not try_to_consume(_SEMICOLON);
  164. end;
  165. { check if a symbol contains the hint directive, and if so gives out a hint
  166. if required.
  167. If this code is changed, it's likly that consume_sym_orgid and factor_read_id
  168. must be changed as well (FK)
  169. }
  170. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  171. var
  172. t : ttoken;
  173. begin
  174. { first check for identifier }
  175. if token<>_ID then
  176. begin
  177. consume(_ID);
  178. srsym:=generrorsym;
  179. srsymtable:=nil;
  180. result:=false;
  181. exit;
  182. end;
  183. searchsym(pattern,srsym,srsymtable);
  184. { handle unit specification like System.Writeln }
  185. try_consume_unitsym_no_specialize(srsym,srsymtable,t,[cuf_consume_id],pattern);
  186. { if nothing found give error and return errorsym }
  187. if assigned(srsym) then
  188. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  189. else
  190. begin
  191. identifier_not_found(orgpattern);
  192. srsym:=generrorsym;
  193. srsymtable:=nil;
  194. end;
  195. consume(t);
  196. result:=assigned(srsym);
  197. end;
  198. { check if a symbol contains the hint directive, and if so gives out a hint
  199. if required and returns the id with it's original casing
  200. }
  201. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  202. var
  203. t : ttoken;
  204. begin
  205. { first check for identifier }
  206. if token<>_ID then
  207. begin
  208. consume(_ID);
  209. srsym:=generrorsym;
  210. srsymtable:=nil;
  211. result:=false;
  212. exit;
  213. end;
  214. searchsym(pattern,srsym,srsymtable);
  215. { handle unit specification like System.Writeln }
  216. try_consume_unitsym_no_specialize(srsym,srsymtable,t,[cuf_consume_id],pattern);
  217. { if nothing found give error and return errorsym }
  218. if assigned(srsym) then
  219. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  220. else
  221. begin
  222. identifier_not_found(orgpattern);
  223. srsym:=generrorsym;
  224. srsymtable:=nil;
  225. end;
  226. s:=orgpattern;
  227. consume(t);
  228. result:=assigned(srsym);
  229. end;
  230. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;out is_specialize:boolean;sympattern:TSymStr):boolean;
  231. var
  232. hmodule: tmodule;
  233. ns:ansistring;
  234. nssym:tsym;
  235. nsitem : TCmdStrListItem;
  236. procedure consume_namespace;
  237. begin
  238. while assigned(srsym) and (srsym.typ=namespacesym) do
  239. begin
  240. { we have a namespace. the next identifier should be either a namespace or a unit }
  241. searchsym_in_module(hmodule,ns+'.'+pattern,srsym,srsymtable);
  242. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  243. begin
  244. ns:=ns+'.'+pattern;
  245. nssym:=srsym;
  246. consume(_ID);
  247. consume(_POINT);
  248. end;
  249. end;
  250. { check if there is a hidden unit with this pattern in the namespace }
  251. if not assigned(srsym) and
  252. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  253. srsym:=tnamespacesym(nssym).unitsym;
  254. end;
  255. begin
  256. result:=false;
  257. tokentoconsume:=_ID;
  258. is_specialize:=false;
  259. if not assigned(srsym) and (pattern<>'') and (namespacelist.count>0) then
  260. begin
  261. hmodule:=get_module(current_filepos.moduleindex);
  262. if not assigned(hmodule) then
  263. internalerror(2018050301);
  264. nsitem:=TCmdStrListItem(namespacelist.first);
  265. while assigned(nsitem) do
  266. begin
  267. ns:=upper(nsitem.str)+'.'+sympattern;
  268. if searchsym_in_module(hmodule,ns,srsym,srsymtable) and
  269. (srsym.typ in [unitsym,namespacesym]) then
  270. break;
  271. nsitem:=TCmdStrListItem(nsitem.next);
  272. end;
  273. end;
  274. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  275. begin
  276. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  277. internalerror(2005011503);
  278. { only allow unit.symbol access if the name was
  279. found in the current module
  280. we can use iscurrentunit because generic specializations does not
  281. change current_unit variable }
  282. hmodule:=find_module_from_symtable(srsym.Owner);
  283. if not Assigned(hmodule) then
  284. internalerror(2010011201);
  285. if hmodule.unit_index=current_filepos.moduleindex then
  286. begin
  287. if cuf_consume_id in flags then
  288. consume(_ID);
  289. consume(_POINT);
  290. if srsym.typ=namespacesym then
  291. begin
  292. ns:=srsym.name;
  293. nssym:=srsym;
  294. consume_namespace;
  295. if not assigned(srsym) and (namespacelist.count>0) then
  296. begin
  297. nsitem:=TCmdStrListItem(namespacelist.first);
  298. while assigned(nsitem) do
  299. begin
  300. ns:=upper(nsitem.str)+'.'+nssym.name;
  301. if searchsym_in_module(hmodule,ns,srsym,srsymtable) and
  302. (srsym.typ in [unitsym,namespacesym]) then
  303. begin
  304. consume_namespace;
  305. break;
  306. end;
  307. nsitem:=TCmdStrListItem(nsitem.next);
  308. end;
  309. end;
  310. if assigned(srsym) and (srsym.typ<>unitsym) then
  311. internalerror(2011082601);
  312. if not assigned(srsym) then
  313. begin
  314. result:=true;
  315. srsymtable:=nil;
  316. exit;
  317. end;
  318. end;
  319. case token of
  320. _ID:
  321. begin
  322. if cuf_check_attr_suffix in flags then
  323. begin
  324. if searchsym_in_module(tunitsym(srsym).module,pattern+custom_attribute_suffix,srsym,srsymtable) then
  325. exit(true);
  326. end;
  327. { system.char? (char=widechar comes from the implicit
  328. uachar/uuchar unit -> override) }
  329. if (pattern='CHAR') and
  330. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  331. begin
  332. if m_default_unicodestring in current_settings.modeswitches then
  333. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  334. else
  335. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  336. end
  337. else
  338. if (cuf_allow_specialize in flags) and (idtoken=_SPECIALIZE) then
  339. begin
  340. consume(_ID);
  341. is_specialize:=true;
  342. if token=_ID then
  343. begin
  344. if (cuf_check_attr_suffix in flags) and
  345. searchsym_in_module(tunitsym(srsym).module,pattern+custom_attribute_suffix,srsym,srsymtable) then
  346. exit(true);
  347. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  348. end;
  349. end
  350. else
  351. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  352. end;
  353. _STRING:
  354. begin
  355. if cs_compilesystem in current_settings.moduleswitches then
  356. Message(parser_e_nostringaliasinsystem);
  357. { system.string? }
  358. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  359. begin
  360. if cs_refcountedstrings in current_settings.localswitches then
  361. begin
  362. if m_default_unicodestring in current_settings.modeswitches then
  363. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  364. else
  365. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  366. end
  367. else
  368. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  369. tokentoconsume:=_STRING;
  370. end;
  371. end
  372. else
  373. ;
  374. end;
  375. end
  376. else
  377. begin
  378. srsym:=nil;
  379. srsymtable:=nil;
  380. end;
  381. result:=true;
  382. end;
  383. end;
  384. function try_consume_unitsym_no_specialize(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;sympattern:TSymStr):boolean;
  385. var
  386. dummy: Boolean;
  387. begin
  388. exclude(flags,cuf_allow_specialize);
  389. result:=try_consume_unitsym(srsym,srsymtable,tokentoconsume,flags,dummy,sympattern);
  390. end;
  391. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  392. var
  393. last_is_deprecated:boolean;
  394. begin
  395. try_consume_hintdirective:=false;
  396. if not(m_hintdirective in current_settings.modeswitches) then
  397. exit;
  398. repeat
  399. last_is_deprecated:=false;
  400. case idtoken of
  401. _LIBRARY:
  402. begin
  403. if sp_hint_library in symopt then
  404. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  405. else
  406. include(symopt,sp_hint_library);
  407. try_consume_hintdirective:=true;
  408. end;
  409. _DEPRECATED:
  410. begin
  411. if sp_hint_deprecated in symopt then
  412. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  413. else
  414. include(symopt,sp_hint_deprecated);
  415. try_consume_hintdirective:=true;
  416. last_is_deprecated:=true;
  417. end;
  418. _EXPERIMENTAL:
  419. begin
  420. if sp_hint_experimental in symopt then
  421. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  422. else
  423. include(symopt,sp_hint_experimental);
  424. try_consume_hintdirective:=true;
  425. end;
  426. _PLATFORM:
  427. begin
  428. if sp_hint_platform in symopt then
  429. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  430. else
  431. include(symopt,sp_hint_platform);
  432. try_consume_hintdirective:=true;
  433. end;
  434. _UNIMPLEMENTED:
  435. begin
  436. if sp_hint_unimplemented in symopt then
  437. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  438. else
  439. include(symopt,sp_hint_unimplemented);
  440. try_consume_hintdirective:=true;
  441. end;
  442. else
  443. break;
  444. end;
  445. consume(Token);
  446. { handle deprecated message }
  447. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  448. begin
  449. if not assigned(deprecatedmsg) then
  450. begin
  451. if token=_CSTRING then
  452. deprecatedmsg:=stringdup(cstringpattern)
  453. else
  454. deprecatedmsg:=stringdup(pattern);
  455. end;
  456. consume(token);
  457. include(symopt,sp_has_deprecated_msg);
  458. end;
  459. until false;
  460. end;
  461. end.