pbase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. begin
  114. if current_scanner.had_multiline_string then
  115. Message2(scan_f_unterminated_multiline_string,
  116. tostr(current_scanner.multiline_start_line),
  117. tostr(current_scanner.multiline_start_column))
  118. else if token=_id then
  119. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  120. else
  121. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str);
  122. end
  123. else
  124. begin
  125. if token=_END then
  126. last_endtoken_filepos:=current_tokenpos;
  127. current_scanner.readtoken(true);
  128. end;
  129. end;
  130. procedure consume_last_dot;
  131. begin
  132. if (token<>_POINT) then
  133. begin
  134. if token=_id then
  135. Message2(scan_f_syn_expected,tokeninfo^[_POINT].str,'identifier '+pattern)
  136. else
  137. Message2(scan_f_syn_expected,tokeninfo^[_POINT].str,tokeninfo^[token].str)
  138. end
  139. else if c<>#0 then
  140. current_scanner.readtoken(true);
  141. end;
  142. function try_to_consume(i:Ttoken):boolean;
  143. begin
  144. try_to_consume:=false;
  145. if (token=i) or (idtoken=i) then
  146. begin
  147. try_to_consume:=true;
  148. if token=_END then
  149. last_endtoken_filepos:=current_tokenpos;
  150. current_scanner.readtoken(true);
  151. end;
  152. end;
  153. procedure consume_all_until(atoken : ttoken);
  154. begin
  155. while (token<>atoken) and (idtoken<>atoken) do
  156. begin
  157. Consume(token);
  158. if token=_EOF then
  159. begin
  160. Consume(atoken);
  161. if current_scanner.had_multiline_string then
  162. Message2(scan_f_unterminated_multiline_string,
  163. tostr(current_scanner.multiline_start_line),
  164. tostr(current_scanner.multiline_start_column))
  165. else
  166. Message(scan_f_end_of_file);
  167. exit;
  168. end;
  169. end;
  170. end;
  171. procedure consume_emptystats;
  172. begin
  173. repeat
  174. until not try_to_consume(_SEMICOLON);
  175. end;
  176. { check if a symbol contains the hint directive, and if so gives out a hint
  177. if required.
  178. If this code is changed, it's likly that consume_sym_orgid and factor_read_id
  179. must be changed as well (FK)
  180. }
  181. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  182. var
  183. t : ttoken;
  184. begin
  185. { first check for identifier }
  186. if token<>_ID then
  187. begin
  188. consume(_ID);
  189. srsym:=generrorsym;
  190. srsymtable:=nil;
  191. result:=false;
  192. exit;
  193. end;
  194. searchsym(pattern,srsym,srsymtable);
  195. { handle unit specification like System.Writeln }
  196. try_consume_unitsym_no_specialize(srsym,srsymtable,t,[cuf_consume_id],pattern);
  197. { if nothing found give error and return errorsym }
  198. if assigned(srsym) then
  199. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  200. else
  201. begin
  202. identifier_not_found(orgpattern);
  203. srsym:=generrorsym;
  204. srsymtable:=nil;
  205. end;
  206. consume(t);
  207. result:=assigned(srsym);
  208. end;
  209. { check if a symbol contains the hint directive, and if so gives out a hint
  210. if required and returns the id with it's original casing
  211. }
  212. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  213. var
  214. t : ttoken;
  215. begin
  216. { first check for identifier }
  217. if token<>_ID then
  218. begin
  219. consume(_ID);
  220. srsym:=generrorsym;
  221. srsymtable:=nil;
  222. result:=false;
  223. exit;
  224. end;
  225. searchsym(pattern,srsym,srsymtable);
  226. { handle unit specification like System.Writeln }
  227. try_consume_unitsym_no_specialize(srsym,srsymtable,t,[cuf_consume_id],pattern);
  228. { if nothing found give error and return errorsym }
  229. if assigned(srsym) then
  230. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  231. else
  232. begin
  233. identifier_not_found(orgpattern);
  234. srsym:=generrorsym;
  235. srsymtable:=nil;
  236. end;
  237. s:=orgpattern;
  238. consume(t);
  239. result:=assigned(srsym);
  240. end;
  241. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;out is_specialize:boolean;sympattern:TSymStr):boolean;
  242. var
  243. hmodule: tmodule;
  244. ns:ansistring;
  245. nssym:tsym;
  246. nsitem : TCmdStrListItem;
  247. procedure consume_namespace;
  248. begin
  249. while assigned(srsym) and (srsym.typ=namespacesym) do
  250. begin
  251. { we have a namespace. the next identifier should be either a namespace or a unit }
  252. searchsym_in_module(hmodule,ns+'.'+pattern,srsym,srsymtable);
  253. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  254. begin
  255. ns:=ns+'.'+pattern;
  256. nssym:=srsym;
  257. consume(_ID);
  258. consume(_POINT);
  259. end;
  260. end;
  261. { check if there is a hidden unit with this pattern in the namespace }
  262. if not assigned(srsym) and
  263. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  264. srsym:=tnamespacesym(nssym).unitsym;
  265. end;
  266. begin
  267. result:=false;
  268. tokentoconsume:=_ID;
  269. is_specialize:=false;
  270. if not assigned(srsym) and (pattern<>'') and (namespacelist.count>0) then
  271. begin
  272. hmodule:=get_module(current_filepos.moduleindex);
  273. if not assigned(hmodule) then
  274. internalerror(2018050301);
  275. nsitem:=TCmdStrListItem(namespacelist.first);
  276. while assigned(nsitem) do
  277. begin
  278. ns:=upper(nsitem.str)+'.'+sympattern;
  279. if searchsym_in_module(hmodule,ns,srsym,srsymtable) and
  280. (srsym.typ in [unitsym,namespacesym]) then
  281. break;
  282. nsitem:=TCmdStrListItem(nsitem.next);
  283. end;
  284. end;
  285. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  286. begin
  287. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  288. internalerror(2005011503);
  289. { only allow unit.symbol access if the name was
  290. found in the current module
  291. we can use iscurrentunit because generic specializations does not
  292. change current_unit variable }
  293. hmodule:=find_module_from_symtable(srsym.Owner);
  294. if not Assigned(hmodule) then
  295. internalerror(2010011201);
  296. if hmodule.unit_index=current_filepos.moduleindex then
  297. begin
  298. if cuf_consume_id in flags then
  299. consume(_ID);
  300. consume(_POINT);
  301. if srsym.typ=namespacesym then
  302. begin
  303. ns:=srsym.name;
  304. nssym:=srsym;
  305. consume_namespace;
  306. if not assigned(srsym) and (namespacelist.count>0) then
  307. begin
  308. nsitem:=TCmdStrListItem(namespacelist.first);
  309. while assigned(nsitem) do
  310. begin
  311. ns:=upper(nsitem.str)+'.'+nssym.name;
  312. if searchsym_in_module(hmodule,ns,srsym,srsymtable) and
  313. (srsym.typ in [unitsym,namespacesym]) then
  314. begin
  315. consume_namespace;
  316. break;
  317. end;
  318. nsitem:=TCmdStrListItem(nsitem.next);
  319. end;
  320. end;
  321. if assigned(srsym) and (srsym.typ<>unitsym) then
  322. internalerror(2011082601);
  323. if not assigned(srsym) then
  324. begin
  325. result:=true;
  326. srsymtable:=nil;
  327. exit;
  328. end;
  329. end;
  330. case token of
  331. _ID:
  332. begin
  333. if cuf_check_attr_suffix in flags then
  334. begin
  335. if searchsym_in_module(tunitsym(srsym).module,pattern+custom_attribute_suffix,srsym,srsymtable) then
  336. exit(true);
  337. end;
  338. { system.char? (char=widechar comes from the implicit
  339. uachar/uuchar unit -> override) }
  340. if (pattern='CHAR') and
  341. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  342. begin
  343. if m_default_unicodestring in current_settings.modeswitches then
  344. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  345. else
  346. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  347. end
  348. else
  349. if (cuf_allow_specialize in flags) and (idtoken=_SPECIALIZE) then
  350. begin
  351. consume(_ID);
  352. is_specialize:=true;
  353. if token=_ID then
  354. begin
  355. if (cuf_check_attr_suffix in flags) and
  356. searchsym_in_module(tunitsym(srsym).module,pattern+custom_attribute_suffix,srsym,srsymtable) then
  357. exit(true);
  358. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  359. end;
  360. end
  361. else
  362. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  363. end;
  364. _STRING:
  365. begin
  366. if cs_compilesystem in current_settings.moduleswitches then
  367. Message(parser_e_nostringaliasinsystem);
  368. { system.string? }
  369. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  370. begin
  371. if cs_refcountedstrings in current_settings.localswitches then
  372. begin
  373. if m_default_unicodestring in current_settings.modeswitches then
  374. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  375. else
  376. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  377. end
  378. else
  379. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  380. tokentoconsume:=_STRING;
  381. end;
  382. end
  383. else
  384. ;
  385. end;
  386. end
  387. else
  388. begin
  389. srsym:=nil;
  390. srsymtable:=nil;
  391. end;
  392. result:=true;
  393. end;
  394. end;
  395. function try_consume_unitsym_no_specialize(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;sympattern:TSymStr):boolean;
  396. var
  397. dummy: Boolean;
  398. begin
  399. exclude(flags,cuf_allow_specialize);
  400. result:=try_consume_unitsym(srsym,srsymtable,tokentoconsume,flags,dummy,sympattern);
  401. end;
  402. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  403. var
  404. last_is_deprecated:boolean;
  405. begin
  406. try_consume_hintdirective:=false;
  407. if not(m_hintdirective in current_settings.modeswitches) then
  408. exit;
  409. repeat
  410. last_is_deprecated:=false;
  411. case idtoken of
  412. _LIBRARY:
  413. begin
  414. if sp_hint_library in symopt then
  415. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  416. else
  417. include(symopt,sp_hint_library);
  418. try_consume_hintdirective:=true;
  419. end;
  420. _DEPRECATED:
  421. begin
  422. if sp_hint_deprecated in symopt then
  423. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  424. else
  425. include(symopt,sp_hint_deprecated);
  426. try_consume_hintdirective:=true;
  427. last_is_deprecated:=true;
  428. end;
  429. _EXPERIMENTAL:
  430. begin
  431. if sp_hint_experimental in symopt then
  432. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  433. else
  434. include(symopt,sp_hint_experimental);
  435. try_consume_hintdirective:=true;
  436. end;
  437. _PLATFORM:
  438. begin
  439. if sp_hint_platform in symopt then
  440. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  441. else
  442. include(symopt,sp_hint_platform);
  443. try_consume_hintdirective:=true;
  444. end;
  445. _UNIMPLEMENTED:
  446. begin
  447. if sp_hint_unimplemented in symopt then
  448. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  449. else
  450. include(symopt,sp_hint_unimplemented);
  451. try_consume_hintdirective:=true;
  452. end;
  453. else
  454. break;
  455. end;
  456. consume(Token);
  457. { handle deprecated message }
  458. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  459. begin
  460. if not assigned(deprecatedmsg) then
  461. begin
  462. if token=_CSTRING then
  463. deprecatedmsg:=stringdup(cstringpattern)
  464. else
  465. deprecatedmsg:=stringdup(pattern);
  466. end;
  467. consume(token);
  468. include(symopt,sp_has_deprecated_msg);
  469. end;
  470. until false;
  471. end;
  472. end.