ast::Variable is also used for formal parameters

Change-Id: Ie69fc400741fa9aadd0409eaf1aeb27762f17d0c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18700
Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
David Neto 2020-04-02 13:21:08 +00:00 committed by dan sinclair
parent afd66c9eed
commit 0e92735d25
1 changed files with 21 additions and 11 deletions

View File

@ -32,10 +32,10 @@ class DecoratedVariable;
/// A Variable statement.
///
/// An instance of this class represents one of two constructs in WGSL, "var" and
/// "const" declarations.
/// An instance of this class represents one of three constructs in WGSL: "var"
/// declaration, "const" declaration, or formal parameter to a function.
///
/// 1. A "var" declaration, is a name for typed storage. Examples:
/// 1. A "var" declaration is a name for typed storage. Examples:
///
/// // Declared outside a function, i.e. at module scope, requires
/// // a storage class.
@ -47,10 +47,16 @@ class DecoratedVariable;
/// var computed_depth : i32;
/// var area : i32 = compute_area(width, height);
///
/// 2. A "const" declaration, is a name for a typed value. Examples:
/// 2. A "const" declaration is a name for a typed value. Examples:
///
/// const twice_depth : i32 = width + width; // Must have initializer
///
/// 3. A formal parameter to a function is a name for a typed value to
/// be passed into a function. Example:
///
/// fn twice(a: i32) -> i32 { // "a:i32" is the formal parameter
/// return a + a;
/// }
///
/// From the WGSL draft, about "var"::
///
@ -62,12 +68,15 @@ class DecoratedVariable;
/// type (the type of the variable itself). If a variable has store type T
/// and storage class S, then its reference type is pointer-to-T-in-S.
///
/// This class uses the term "type" to refer to the value type of the "const",
/// This class uses the term "type" to refer to:
/// the value type of a "const",
/// the value type of the formal parameter,
/// or the store type of the "var".
///
/// The storage class for a "const" is always StorageClass::kNone.
/// The storage class for a "var" is StorageClass::kNone when using the
/// defaulting syntax for a "var" declared inside a function.
/// The storage class for a "const" is always StorageClass::kNone.
/// The storage class for a formal parameter is always StorageClass::kNone.
class Variable : public Node {
public:
/// Create a new empty variable statement
@ -75,13 +84,13 @@ class Variable : public Node {
/// Create a variable
/// @param name the variables name
/// @param sc the variable storage class
/// @param type the value type of the const, or the store type of the variable
/// @param type the value type
Variable(const std::string& name, StorageClass sc, type::Type* type);
/// Create a variable
/// @param source the variable source
/// @param name the variables name
/// @param sc the variable storage class
/// @param type the type of the const value, or store type of the variable
/// @param type the value type
Variable(const Source& source,
const std::string& name,
StorageClass sc,
@ -97,7 +106,8 @@ class Variable : public Node {
/// @returns the variable name
const std::string& name() { return name_; }
/// Sets the value type if a const, or the store type if a var
/// Sets the value type if a const or formal parameter, or the
/// store type if a var.
/// @param type the type
void set_type(type::Type* type) { type_ = type; }
/// @returns the variable's type.
@ -155,7 +165,7 @@ class Variable : public Node {
bool is_const_ = false;
std::string name_;
StorageClass storage_class_ = StorageClass::kNone;
// The value type if a const, and the store type if a var
// The value type if a const or formal paramter, and the store type if a var
type::Type* type_ = nullptr;
std::unique_ptr<Expression> constructor_;
};