pbase.pas 16 KB

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