mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 19:32:40 +00:00
Add semantic analysis and semantics IR building for `if` expressions, and add the first parts of control flow handling to semantics IR. After discussion with @chandlerc, use [block arguments](https://en.wikipedia.org/wiki/Static_single-assignment_form#Block_arguments) to convey values from the two arms of the `if` to the result. For now, only a single block argument is supported, but we should revisit this as we explore more of the requirements of the Semantics IR form. Functions can now contain multiple code blocks, so grab the entry block up-front instead of assuming the entry block will be at the top of the block stack when we reach the end of function emission. Add trivial support for `bool` type literal, because without it we can't write testcases.
74 lines
3.0 KiB
Modula-2
74 lines
3.0 KiB
Modula-2
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// This is an X-macro header. It does not use `#include` guards, and instead is
|
|
// designed to be `#include`ed after the x-macro is defined in order for its
|
|
// inclusion to expand to the desired output. Macro definitions are cleaned up
|
|
// at the end of this file.
|
|
//
|
|
// Supported x-macros are:
|
|
// - CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name)
|
|
// Used as a fallback if other macros are missing. Used directly by Invalid
|
|
// only, which is defined last.
|
|
// - CARBON_SEMANTICS_BUILTIN_KIND(Name, Label)
|
|
// Defines a non-Invalid builtin type. The label is used for stringifying
|
|
// types.
|
|
//
|
|
// This tree represents the subset relationship between these macros, where if a
|
|
// specific x-macro isn't defined, it'll fall back to the parent macro.
|
|
|
|
#if !(defined(CARBON_SEMANTICS_BUILTIN_KIND_NAME) || \
|
|
defined(CARBON_SEMANTICS_BUILTIN_KIND))
|
|
#error \
|
|
"Must define CARBON_SEMANTICS_BUILTIN_KIND family x-macros to use this file."
|
|
#endif
|
|
|
|
// If CARBON_SEMANTICS_BUILTIN_KIND_NAME is undefined, ignore calls.
|
|
#ifndef CARBON_SEMANTICS_BUILTIN_KIND_NAME
|
|
#define CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name)
|
|
#endif
|
|
|
|
// If CARBON_SEMANTICS_BUILTIN_KIND is undefined, delegate calls to
|
|
// CARBON_SEMANTICS_BUILTIN_KIND_NAME.
|
|
#ifndef CARBON_SEMANTICS_BUILTIN_KIND
|
|
#define CARBON_SEMANTICS_BUILTIN_KIND(Name, ...) \
|
|
CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name)
|
|
#endif
|
|
|
|
// Tracks expressions which are valid as types.
|
|
// This has a deliberately self-referential type.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(TypeType, "Type")
|
|
|
|
// Used when a SemanticNode has an invalid type, which should then be ignored
|
|
// for future type checking.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(InvalidType, "<unknown>")
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// TODO: Below types are all placeholders. While the above may last, the below
|
|
// are expected to need to change in order to better reflect Carbon's design.
|
|
// Keeping distinct placeholders can help find usages for later fixes.
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// The type of bool literals and branch conditions, bool.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(BoolType, "bool")
|
|
|
|
// The type of integer values and integer literals, currently always i32.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(IntegerType, "i32")
|
|
|
|
// The type of floating point values and real literals, currently always f64.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(FloatingPointType, "f64")
|
|
|
|
// The type of string values and String literals.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(StringType, "String")
|
|
|
|
// The canonical empty tuple type.
|
|
CARBON_SEMANTICS_BUILTIN_KIND(EmptyTupleType, "() as Type")
|
|
|
|
// Keep invalid last, so that we can use values as array indices without needing
|
|
// an invalid entry.
|
|
CARBON_SEMANTICS_BUILTIN_KIND_NAME(Invalid)
|
|
|
|
#undef CARBON_SEMANTICS_BUILTIN_KIND_NAME
|
|
#undef CARBON_SEMANTICS_BUILTIN_KIND
|