resolve_names.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "executable_semantics/interpreter/resolve_names.h"
  5. #include <set>
  6. #include "executable_semantics/ast/declaration.h"
  7. #include "executable_semantics/ast/expression.h"
  8. #include "executable_semantics/ast/pattern.h"
  9. #include "executable_semantics/ast/statement.h"
  10. #include "executable_semantics/ast/static_scope.h"
  11. #include "llvm/Support/Casting.h"
  12. #include "llvm/Support/Error.h"
  13. using llvm::cast;
  14. namespace Carbon {
  15. // Adds the names exposed by the given AST node to enclosing_scope.
  16. static auto AddExposedNames(const Declaration& declaration,
  17. StaticScope& enclosing_scope) -> ErrorOr<Success>;
  18. static auto AddExposedNames(const Declaration& declaration,
  19. StaticScope& enclosing_scope) -> ErrorOr<Success> {
  20. switch (declaration.kind()) {
  21. case DeclarationKind::InterfaceDeclaration: {
  22. auto& iface_decl = cast<InterfaceDeclaration>(declaration);
  23. RETURN_IF_ERROR(enclosing_scope.Add(iface_decl.name(), &iface_decl));
  24. break;
  25. }
  26. case DeclarationKind::ImplDeclaration: {
  27. // Nothing to do here
  28. break;
  29. }
  30. case DeclarationKind::FunctionDeclaration: {
  31. auto& func = cast<FunctionDeclaration>(declaration);
  32. RETURN_IF_ERROR(enclosing_scope.Add(func.name(), &func));
  33. break;
  34. }
  35. case DeclarationKind::ClassDeclaration: {
  36. auto& class_decl = cast<ClassDeclaration>(declaration);
  37. RETURN_IF_ERROR(enclosing_scope.Add(class_decl.name(), &class_decl));
  38. break;
  39. }
  40. case DeclarationKind::ChoiceDeclaration: {
  41. auto& choice = cast<ChoiceDeclaration>(declaration);
  42. RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice));
  43. break;
  44. }
  45. case DeclarationKind::VariableDeclaration:
  46. auto& var = cast<VariableDeclaration>(declaration);
  47. if (var.binding().name() != AnonymousName) {
  48. RETURN_IF_ERROR(
  49. enclosing_scope.Add(var.binding().name(), &var.binding()));
  50. }
  51. break;
  52. }
  53. return Success();
  54. }
  55. // Traverses the sub-AST rooted at the given node, resolving all names within
  56. // it using enclosing_scope, and updating enclosing_scope to add names to
  57. // it as they become available. In scopes where names are only visible below
  58. // their point of declaration (such as block scopes in C++), this is implemented
  59. // as a single pass, recursively calling ResolveNames on the elements of the
  60. // scope in order. In scopes where names are also visible above their point of
  61. // declaration (such as class scopes in C++), this requires two passes: first
  62. // calling AddExposedNames on each element of the scope to populate a
  63. // StaticScope, and then calling ResolveNames on each element, passing it the
  64. // already-populated StaticScope.
  65. static auto ResolveNames(Expression& expression,
  66. const StaticScope& enclosing_scope)
  67. -> ErrorOr<Success>;
  68. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  69. -> ErrorOr<Success>;
  70. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  71. -> ErrorOr<Success>;
  72. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
  73. -> ErrorOr<Success>;
  74. static auto ResolveNames(Expression& expression,
  75. const StaticScope& enclosing_scope)
  76. -> ErrorOr<Success> {
  77. switch (expression.kind()) {
  78. case ExpressionKind::CallExpression: {
  79. auto& call = cast<CallExpression>(expression);
  80. RETURN_IF_ERROR(ResolveNames(call.function(), enclosing_scope));
  81. RETURN_IF_ERROR(ResolveNames(call.argument(), enclosing_scope));
  82. break;
  83. }
  84. case ExpressionKind::FunctionTypeLiteral: {
  85. auto& fun_type = cast<FunctionTypeLiteral>(expression);
  86. RETURN_IF_ERROR(ResolveNames(fun_type.parameter(), enclosing_scope));
  87. RETURN_IF_ERROR(ResolveNames(fun_type.return_type(), enclosing_scope));
  88. break;
  89. }
  90. case ExpressionKind::FieldAccessExpression:
  91. RETURN_IF_ERROR(
  92. ResolveNames(cast<FieldAccessExpression>(expression).aggregate(),
  93. enclosing_scope));
  94. break;
  95. case ExpressionKind::IndexExpression: {
  96. auto& index = cast<IndexExpression>(expression);
  97. RETURN_IF_ERROR(ResolveNames(index.aggregate(), enclosing_scope));
  98. RETURN_IF_ERROR(ResolveNames(index.offset(), enclosing_scope));
  99. break;
  100. }
  101. case ExpressionKind::PrimitiveOperatorExpression:
  102. for (Nonnull<Expression*> operand :
  103. cast<PrimitiveOperatorExpression>(expression).arguments()) {
  104. RETURN_IF_ERROR(ResolveNames(*operand, enclosing_scope));
  105. }
  106. break;
  107. case ExpressionKind::TupleLiteral:
  108. for (Nonnull<Expression*> field :
  109. cast<TupleLiteral>(expression).fields()) {
  110. RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  111. }
  112. break;
  113. case ExpressionKind::StructLiteral:
  114. for (FieldInitializer& init : cast<StructLiteral>(expression).fields()) {
  115. RETURN_IF_ERROR(ResolveNames(init.expression(), enclosing_scope));
  116. }
  117. break;
  118. case ExpressionKind::StructTypeLiteral:
  119. for (FieldInitializer& init :
  120. cast<StructTypeLiteral>(expression).fields()) {
  121. RETURN_IF_ERROR(ResolveNames(init.expression(), enclosing_scope));
  122. }
  123. break;
  124. case ExpressionKind::IdentifierExpression: {
  125. auto& identifier = cast<IdentifierExpression>(expression);
  126. ASSIGN_OR_RETURN(
  127. const auto value_node,
  128. enclosing_scope.Resolve(identifier.name(), identifier.source_loc()));
  129. identifier.set_value_node(value_node);
  130. break;
  131. }
  132. case ExpressionKind::IntrinsicExpression:
  133. RETURN_IF_ERROR(ResolveNames(cast<IntrinsicExpression>(expression).args(),
  134. enclosing_scope));
  135. break;
  136. case ExpressionKind::IfExpression: {
  137. auto& if_expr = cast<IfExpression>(expression);
  138. RETURN_IF_ERROR(ResolveNames(*if_expr.condition(), enclosing_scope));
  139. RETURN_IF_ERROR(
  140. ResolveNames(*if_expr.then_expression(), enclosing_scope));
  141. RETURN_IF_ERROR(
  142. ResolveNames(*if_expr.else_expression(), enclosing_scope));
  143. break;
  144. }
  145. case ExpressionKind::BoolTypeLiteral:
  146. case ExpressionKind::BoolLiteral:
  147. case ExpressionKind::IntTypeLiteral:
  148. case ExpressionKind::ContinuationTypeLiteral:
  149. case ExpressionKind::IntLiteral:
  150. case ExpressionKind::StringLiteral:
  151. case ExpressionKind::StringTypeLiteral:
  152. case ExpressionKind::TypeTypeLiteral:
  153. break;
  154. case ExpressionKind::UnimplementedExpression:
  155. FATAL() << "Unimplemented";
  156. }
  157. return Success();
  158. }
  159. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  160. -> ErrorOr<Success> {
  161. switch (pattern.kind()) {
  162. case PatternKind::BindingPattern: {
  163. auto& binding = cast<BindingPattern>(pattern);
  164. RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
  165. if (binding.name() != AnonymousName) {
  166. RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  167. }
  168. break;
  169. }
  170. case PatternKind::TuplePattern:
  171. for (Nonnull<Pattern*> field : cast<TuplePattern>(pattern).fields()) {
  172. RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  173. }
  174. break;
  175. case PatternKind::AlternativePattern: {
  176. auto& alternative = cast<AlternativePattern>(pattern);
  177. RETURN_IF_ERROR(ResolveNames(alternative.choice_type(), enclosing_scope));
  178. RETURN_IF_ERROR(ResolveNames(alternative.arguments(), enclosing_scope));
  179. break;
  180. }
  181. case PatternKind::ExpressionPattern:
  182. RETURN_IF_ERROR(ResolveNames(
  183. cast<ExpressionPattern>(pattern).expression(), enclosing_scope));
  184. break;
  185. case PatternKind::AutoPattern:
  186. break;
  187. case PatternKind::VarPattern:
  188. RETURN_IF_ERROR(
  189. ResolveNames(cast<VarPattern>(pattern).pattern(), enclosing_scope));
  190. break;
  191. }
  192. return Success();
  193. }
  194. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  195. -> ErrorOr<Success> {
  196. switch (statement.kind()) {
  197. case StatementKind::ExpressionStatement:
  198. RETURN_IF_ERROR(ResolveNames(
  199. cast<ExpressionStatement>(statement).expression(), enclosing_scope));
  200. break;
  201. case StatementKind::Assign: {
  202. auto& assign = cast<Assign>(statement);
  203. RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope));
  204. RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope));
  205. break;
  206. }
  207. case StatementKind::VariableDefinition: {
  208. auto& def = cast<VariableDefinition>(statement);
  209. RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope));
  210. RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope));
  211. break;
  212. }
  213. case StatementKind::If: {
  214. auto& if_stmt = cast<If>(statement);
  215. RETURN_IF_ERROR(ResolveNames(if_stmt.condition(), enclosing_scope));
  216. RETURN_IF_ERROR(ResolveNames(if_stmt.then_block(), enclosing_scope));
  217. if (if_stmt.else_block().has_value()) {
  218. RETURN_IF_ERROR(ResolveNames(**if_stmt.else_block(), enclosing_scope));
  219. }
  220. break;
  221. }
  222. case StatementKind::Return:
  223. RETURN_IF_ERROR(
  224. ResolveNames(cast<Return>(statement).expression(), enclosing_scope));
  225. break;
  226. case StatementKind::Block: {
  227. auto& block = cast<Block>(statement);
  228. StaticScope block_scope;
  229. block_scope.AddParent(&enclosing_scope);
  230. for (Nonnull<Statement*> sub_statement : block.statements()) {
  231. RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope));
  232. }
  233. break;
  234. }
  235. case StatementKind::While: {
  236. auto& while_stmt = cast<While>(statement);
  237. RETURN_IF_ERROR(ResolveNames(while_stmt.condition(), enclosing_scope));
  238. RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope));
  239. break;
  240. }
  241. case StatementKind::Match: {
  242. auto& match = cast<Match>(statement);
  243. RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope));
  244. for (Match::Clause& clause : match.clauses()) {
  245. StaticScope clause_scope;
  246. clause_scope.AddParent(&enclosing_scope);
  247. RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope));
  248. RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope));
  249. }
  250. break;
  251. }
  252. case StatementKind::Continuation: {
  253. auto& continuation = cast<Continuation>(statement);
  254. RETURN_IF_ERROR(enclosing_scope.Add(continuation.name(), &continuation));
  255. StaticScope continuation_scope;
  256. continuation_scope.AddParent(&enclosing_scope);
  257. RETURN_IF_ERROR(ResolveNames(cast<Continuation>(statement).body(),
  258. continuation_scope));
  259. break;
  260. }
  261. case StatementKind::Run:
  262. RETURN_IF_ERROR(
  263. ResolveNames(cast<Run>(statement).argument(), enclosing_scope));
  264. break;
  265. case StatementKind::Await:
  266. case StatementKind::Break:
  267. case StatementKind::Continue:
  268. break;
  269. }
  270. return Success();
  271. }
  272. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
  273. -> ErrorOr<Success> {
  274. switch (declaration.kind()) {
  275. case DeclarationKind::InterfaceDeclaration: {
  276. auto& iface = cast<InterfaceDeclaration>(declaration);
  277. StaticScope iface_scope;
  278. iface_scope.AddParent(&enclosing_scope);
  279. RETURN_IF_ERROR(iface_scope.Add("Self", iface.self()));
  280. for (Nonnull<Declaration*> member : iface.members()) {
  281. RETURN_IF_ERROR(AddExposedNames(*member, iface_scope));
  282. }
  283. for (Nonnull<Declaration*> member : iface.members()) {
  284. RETURN_IF_ERROR(ResolveNames(*member, iface_scope));
  285. }
  286. break;
  287. }
  288. case DeclarationKind::ImplDeclaration: {
  289. auto& impl = cast<ImplDeclaration>(declaration);
  290. RETURN_IF_ERROR(ResolveNames(impl.interface(), enclosing_scope));
  291. RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), enclosing_scope));
  292. for (Nonnull<Declaration*> member : impl.members()) {
  293. RETURN_IF_ERROR(AddExposedNames(*member, enclosing_scope));
  294. }
  295. for (Nonnull<Declaration*> member : impl.members()) {
  296. RETURN_IF_ERROR(ResolveNames(*member, enclosing_scope));
  297. }
  298. break;
  299. }
  300. case DeclarationKind::FunctionDeclaration: {
  301. auto& function = cast<FunctionDeclaration>(declaration);
  302. StaticScope function_scope;
  303. function_scope.AddParent(&enclosing_scope);
  304. for (Nonnull<GenericBinding*> binding : function.deduced_parameters()) {
  305. RETURN_IF_ERROR(function_scope.Add(binding->name(), binding));
  306. RETURN_IF_ERROR(ResolveNames(binding->type(), function_scope));
  307. }
  308. if (function.is_method()) {
  309. RETURN_IF_ERROR(ResolveNames(function.me_pattern(), function_scope));
  310. }
  311. RETURN_IF_ERROR(ResolveNames(function.param_pattern(), function_scope));
  312. if (function.return_term().type_expression().has_value()) {
  313. RETURN_IF_ERROR(ResolveNames(**function.return_term().type_expression(),
  314. function_scope));
  315. }
  316. if (function.body().has_value()) {
  317. RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope));
  318. }
  319. break;
  320. }
  321. case DeclarationKind::ClassDeclaration: {
  322. auto& class_decl = cast<ClassDeclaration>(declaration);
  323. StaticScope class_scope;
  324. class_scope.AddParent(&enclosing_scope);
  325. RETURN_IF_ERROR(class_scope.Add(class_decl.name(), &class_decl));
  326. for (Nonnull<Declaration*> member : class_decl.members()) {
  327. RETURN_IF_ERROR(AddExposedNames(*member, class_scope));
  328. }
  329. for (Nonnull<Declaration*> member : class_decl.members()) {
  330. RETURN_IF_ERROR(ResolveNames(*member, class_scope));
  331. }
  332. break;
  333. }
  334. case DeclarationKind::ChoiceDeclaration: {
  335. auto& choice = cast<ChoiceDeclaration>(declaration);
  336. // Alternative names are never used unqualified, so we don't need to
  337. // add the alternatives to a scope, or introduce a new scope; we only
  338. // need to check for duplicates.
  339. std::set<std::string_view> alternative_names;
  340. for (Nonnull<AlternativeSignature*> alternative : choice.alternatives()) {
  341. RETURN_IF_ERROR(
  342. ResolveNames(alternative->signature(), enclosing_scope));
  343. if (!alternative_names.insert(alternative->name()).second) {
  344. return FATAL_COMPILATION_ERROR(alternative->source_loc())
  345. << "Duplicate name `" << alternative->name()
  346. << "` in choice type";
  347. }
  348. }
  349. break;
  350. }
  351. case DeclarationKind::VariableDeclaration: {
  352. auto& var = cast<VariableDeclaration>(declaration);
  353. RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope));
  354. if (var.has_initializer()) {
  355. RETURN_IF_ERROR(ResolveNames(var.initializer(), enclosing_scope));
  356. }
  357. break;
  358. }
  359. }
  360. return Success();
  361. }
  362. auto ResolveNames(AST& ast) -> ErrorOr<Success> {
  363. StaticScope file_scope;
  364. for (auto declaration : ast.declarations) {
  365. RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope));
  366. }
  367. for (auto declaration : ast.declarations) {
  368. RETURN_IF_ERROR(ResolveNames(*declaration, file_scope));
  369. }
  370. return ResolveNames(**ast.main_call, file_scope);
  371. }
  372. } // namespace Carbon