pbase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. var
  40. { for operators }
  41. optoken : ttoken;
  42. { true, if only routine headers should be parsed }
  43. parse_only : boolean;
  44. { true, if we found a name for a named arg }
  45. found_arg_name : boolean;
  46. { true, if we are parsing generic declaration }
  47. parse_generic : boolean;
  48. procedure identifier_not_found(const s:string);
  49. procedure identifier_not_found(const s:string;const filepos:tfileposinfo);
  50. { function tokenstring(i : ttoken):string;}
  51. { consumes token i, if the current token is unequal i }
  52. { a syntax error is written }
  53. procedure consume(i : ttoken);
  54. {Tries to consume the token i, and returns true if it was consumed:
  55. if token=i.}
  56. function try_to_consume(i:Ttoken):boolean;
  57. { consumes all tokens til atoken (for error recovering }
  58. procedure consume_all_until(atoken : ttoken);
  59. { consumes tokens while they are semicolons }
  60. procedure consume_emptystats;
  61. { reads a list of identifiers into a string list }
  62. { consume a symbol, if not found give an error and
  63. and return an errorsym }
  64. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  65. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  66. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;consume_id:boolean):boolean;
  67. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  68. { just for an accurate position of the end of a procedure (PM) }
  69. var
  70. last_endtoken_filepos: tfileposinfo;
  71. implementation
  72. uses
  73. globals,htypechk,scanner,systems,verbose,fmodule;
  74. {****************************************************************************
  75. Token Parsing
  76. ****************************************************************************}
  77. procedure identifier_not_found(const s:string);
  78. begin
  79. Message1(sym_e_id_not_found,s);
  80. { show a fatal that you need -S2 or -Sd, but only
  81. if we just parsed the a token that has m_class }
  82. if not(m_class in current_settings.modeswitches) and
  83. (Upper(s)=pattern) and
  84. (m_class in tokeninfo^[idtoken].keyword) then
  85. Message(parser_f_need_objfpc_or_delphi_mode);
  86. end;
  87. procedure identifier_not_found(const s:string;const filepos:tfileposinfo);
  88. begin
  89. MessagePos1(filepos,sym_e_id_not_found,s);
  90. { show a fatal that you need -S2 or -Sd, but only
  91. if we just parsed the a token that has m_class }
  92. if not(m_class in current_settings.modeswitches) and
  93. (Upper(s)=pattern) and
  94. (m_class in tokeninfo^[idtoken].keyword) then
  95. MessagePos(filepos,parser_f_need_objfpc_or_delphi_mode);
  96. end;
  97. { consumes token i, write error if token is different }
  98. procedure consume(i : ttoken);
  99. begin
  100. if (token<>i) and (idtoken<>i) then
  101. if token=_id then
  102. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  103. else
  104. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  105. else
  106. begin
  107. if token=_END then
  108. last_endtoken_filepos:=current_tokenpos;
  109. current_scanner.readtoken(true);
  110. end;
  111. end;
  112. function try_to_consume(i:Ttoken):boolean;
  113. begin
  114. try_to_consume:=false;
  115. if (token=i) or (idtoken=i) then
  116. begin
  117. try_to_consume:=true;
  118. if token=_END then
  119. last_endtoken_filepos:=current_tokenpos;
  120. current_scanner.readtoken(true);
  121. end;
  122. end;
  123. procedure consume_all_until(atoken : ttoken);
  124. begin
  125. while (token<>atoken) and (idtoken<>atoken) do
  126. begin
  127. Consume(token);
  128. if token=_EOF then
  129. begin
  130. Consume(atoken);
  131. Message(scan_f_end_of_file);
  132. exit;
  133. end;
  134. end;
  135. end;
  136. procedure consume_emptystats;
  137. begin
  138. repeat
  139. until not try_to_consume(_SEMICOLON);
  140. end;
  141. { check if a symbol contains the hint directive, and if so gives out a hint
  142. if required.
  143. If this code is changed, it's likly that consume_sym_orgid and factor_read_id
  144. must be changed as well (FK)
  145. }
  146. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  147. var
  148. t : ttoken;
  149. begin
  150. { first check for identifier }
  151. if token<>_ID then
  152. begin
  153. consume(_ID);
  154. srsym:=generrorsym;
  155. srsymtable:=nil;
  156. result:=false;
  157. exit;
  158. end;
  159. searchsym(pattern,srsym,srsymtable);
  160. { handle unit specification like System.Writeln }
  161. try_consume_unitsym(srsym,srsymtable,t,true);
  162. { if nothing found give error and return errorsym }
  163. if assigned(srsym) then
  164. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  165. else
  166. begin
  167. identifier_not_found(orgpattern);
  168. srsym:=generrorsym;
  169. srsymtable:=nil;
  170. end;
  171. consume(t);
  172. result:=assigned(srsym);
  173. end;
  174. { check if a symbol contains the hint directive, and if so gives out a hint
  175. if required and returns the id with it's original casing
  176. }
  177. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  178. var
  179. t : ttoken;
  180. begin
  181. { first check for identifier }
  182. if token<>_ID then
  183. begin
  184. consume(_ID);
  185. srsym:=generrorsym;
  186. srsymtable:=nil;
  187. result:=false;
  188. exit;
  189. end;
  190. searchsym(pattern,srsym,srsymtable);
  191. { handle unit specification like System.Writeln }
  192. try_consume_unitsym(srsym,srsymtable,t,true);
  193. { if nothing found give error and return errorsym }
  194. if assigned(srsym) then
  195. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  196. else
  197. begin
  198. identifier_not_found(orgpattern);
  199. srsym:=generrorsym;
  200. srsymtable:=nil;
  201. end;
  202. s:=orgpattern;
  203. consume(t);
  204. result:=assigned(srsym);
  205. end;
  206. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;consume_id:boolean):boolean;
  207. var
  208. hmodule: tmodule;
  209. ns:ansistring;
  210. nssym:tsym;
  211. begin
  212. result:=false;
  213. tokentoconsume:=_ID;
  214. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  215. begin
  216. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  217. internalerror(200501154);
  218. { only allow unit.symbol access if the name was
  219. found in the current module
  220. we can use iscurrentunit because generic specializations does not
  221. change current_unit variable }
  222. hmodule:=find_module_from_symtable(srsym.Owner);
  223. if not Assigned(hmodule) then
  224. internalerror(201001120);
  225. if hmodule.unit_index=current_filepos.moduleindex then
  226. begin
  227. if consume_id then
  228. consume(_ID);
  229. consume(_POINT);
  230. if srsym.typ=namespacesym then
  231. begin
  232. ns:=srsym.name;
  233. nssym:=srsym;
  234. while assigned(srsym) and (srsym.typ=namespacesym) do
  235. begin
  236. { we have a namespace. the next identifier should be either a namespace or a unit }
  237. searchsym_in_module(hmodule,ns+'.'+pattern,srsym,srsymtable);
  238. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  239. begin
  240. ns:=ns+'.'+pattern;
  241. nssym:=srsym;
  242. consume(_ID);
  243. consume(_POINT);
  244. end;
  245. end;
  246. { check if there is a hidden unit with this pattern in the namespace }
  247. if not assigned(srsym) and
  248. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  249. srsym:=tnamespacesym(nssym).unitsym;
  250. if assigned(srsym) and (srsym.typ<>unitsym) then
  251. internalerror(201108260);
  252. if not assigned(srsym) then
  253. begin
  254. result:=true;
  255. srsymtable:=nil;
  256. exit;
  257. end;
  258. end;
  259. case token of
  260. _ID:
  261. { system.char? (char=widechar comes from the implicit
  262. uuchar unit -> override) }
  263. if (pattern='CHAR') and
  264. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  265. begin
  266. if m_default_unicodestring in current_settings.modeswitches then
  267. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  268. else
  269. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  270. end
  271. else
  272. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  273. _STRING:
  274. begin
  275. { system.string? }
  276. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  277. begin
  278. if cs_refcountedstrings in current_settings.localswitches then
  279. begin
  280. if m_default_unicodestring in current_settings.modeswitches then
  281. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  282. else
  283. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  284. end
  285. else
  286. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  287. tokentoconsume:=_STRING;
  288. end;
  289. end
  290. end;
  291. end
  292. else
  293. begin
  294. srsym:=nil;
  295. srsymtable:=nil;
  296. end;
  297. result:=true;
  298. end;
  299. end;
  300. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  301. var
  302. last_is_deprecated:boolean;
  303. begin
  304. try_consume_hintdirective:=false;
  305. if not(m_hintdirective in current_settings.modeswitches) then
  306. exit;
  307. repeat
  308. last_is_deprecated:=false;
  309. case idtoken of
  310. _LIBRARY:
  311. begin
  312. if sp_hint_library in symopt then
  313. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  314. else
  315. include(symopt,sp_hint_library);
  316. try_consume_hintdirective:=true;
  317. end;
  318. _DEPRECATED:
  319. begin
  320. if sp_hint_deprecated in symopt then
  321. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  322. else
  323. include(symopt,sp_hint_deprecated);
  324. try_consume_hintdirective:=true;
  325. last_is_deprecated:=true;
  326. end;
  327. _EXPERIMENTAL:
  328. begin
  329. if sp_hint_experimental in symopt then
  330. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  331. else
  332. include(symopt,sp_hint_experimental);
  333. try_consume_hintdirective:=true;
  334. end;
  335. _PLATFORM:
  336. begin
  337. if sp_hint_platform in symopt then
  338. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  339. else
  340. include(symopt,sp_hint_platform);
  341. try_consume_hintdirective:=true;
  342. end;
  343. _UNIMPLEMENTED:
  344. begin
  345. if sp_hint_unimplemented in symopt then
  346. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  347. else
  348. include(symopt,sp_hint_unimplemented);
  349. try_consume_hintdirective:=true;
  350. end;
  351. else
  352. break;
  353. end;
  354. consume(Token);
  355. { handle deprecated message }
  356. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  357. begin
  358. if not assigned(deprecatedmsg) then
  359. begin
  360. if token=_CSTRING then
  361. deprecatedmsg:=stringdup(cstringpattern)
  362. else
  363. deprecatedmsg:=stringdup(pattern);
  364. end;
  365. consume(token);
  366. include(symopt,sp_has_deprecated_msg);
  367. end;
  368. until false;
  369. end;
  370. end.