Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: src/ast/ast.h

Issue 1810943002: Add parsing for tuple types (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@types
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/parsing/parser.h » ('j') | src/parsing/parser-base.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_AST_AST_H_ 5 #ifndef V8_AST_AST_H_
6 #define V8_AST_AST_H_ 6 #define V8_AST_AST_H_
7 7
8 #include "src/assembler.h" 8 #include "src/assembler.h"
9 #include "src/ast/ast-value-factory.h" 9 #include "src/ast/ast-value-factory.h"
10 #include "src/ast/modules.h" 10 #include "src/ast/modules.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 EXPRESSION_NODE_LIST(V) 99 EXPRESSION_NODE_LIST(V)
100 100
101 // Optional type nodes are intentionally not in in AST_NODE_LIST. 101 // Optional type nodes are intentionally not in in AST_NODE_LIST.
102 // Visitors should not have to worry about them. 102 // Visitors should not have to worry about them.
103 #define TYPESYSTEM_NODE_LIST(V) \ 103 #define TYPESYSTEM_NODE_LIST(V) \
104 V(PredefinedType) \ 104 V(PredefinedType) \
105 V(ThisType) \ 105 V(ThisType) \
106 V(UnionType) \ 106 V(UnionType) \
107 V(IntersectionType) \ 107 V(IntersectionType) \
108 V(ArrayType) \ 108 V(ArrayType) \
109 V(TupleType) \
109 V(FunctionType) \ 110 V(FunctionType) \
110 V(TypeParameter) \ 111 V(TypeParameter) \
111 V(FormalParameter) \ 112 V(FormalParameter) \
112 V(TypeReference) \ 113 V(TypeReference) \
113 V(StringLiteralType) \ 114 V(StringLiteralType) \
114 V(QueryType) \ 115 V(QueryType) \
115 V(TypeOrParameters) 116 V(TypeOrParameters)
116 117
117 // Forward declarations 118 // Forward declarations
118 class AstNodeFactory; 119 class AstNodeFactory;
(...skipping 2809 matching lines...) Expand 10 before | Expand all | Expand 10 after
2928 private: 2929 private:
2929 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {} 2930 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2930 }; 2931 };
2931 2932
2932 2933
2933 // Nodes for the optional type system. 2934 // Nodes for the optional type system.
2934 namespace typesystem { 2935 namespace typesystem {
2935 2936
2936 class FormalParameter; 2937 class FormalParameter;
2937 2938
2938 class Type : public AstNode { 2939 class Type : public AstNode {
rossberg 2016/03/17 16:51:37 With all the new syntax it seems that this is now
nickie 2016/03/18 11:12:01 Done. I copied some information from the design d
2939 public: 2940 public:
2940 // Uncovers the case of a single, parenthesized valid type. 2941 // Uncovers the case of a single, parenthesized valid type.
2941 V8_INLINE Type* Uncover(bool* ok); 2942 V8_INLINE Type* Uncover(bool* ok);
2942 2943
2943 V8_INLINE ZoneList<FormalParameter*>* AsValidParameterList(Zone* zone, 2944 V8_INLINE ZoneList<FormalParameter*>* AsValidParameterList(Zone* zone,
2944 bool* ok) const; 2945 bool* ok) const;
2945 2946
2946 V8_INLINE bool IsSimpleIdentifier() const; 2947 V8_INLINE bool IsValidType() const;
2947 V8_INLINE const AstRawString* AsSimpleIdentifier() const; 2948 V8_INLINE bool IsValidBindingIdentifierOrPattern() const;
2948 2949
2949 protected: 2950 protected:
2950 explicit Type(Zone* zone, int position) : AstNode(position) {} 2951 explicit Type(Zone* zone, int position) : AstNode(position) {}
2951 }; 2952 };
2952 2953
2953 2954
2954 class PredefinedType : public Type { 2955 class PredefinedType : public Type {
2955 public: 2956 public:
2956 DECLARE_NODE_TYPE(PredefinedType) 2957 DECLARE_NODE_TYPE(PredefinedType)
2957 2958
2958 enum Kind { 2959 enum Kind {
2959 kAnyType, 2960 kAnyType,
2960 kNumberType, 2961 kNumberType,
2961 kBooleanType, 2962 kBooleanType,
2962 kStringType, 2963 kStringType,
2963 kSymbolType, 2964 kSymbolType,
2964 kVoidType 2965 kVoidType
2965 }; 2966 };
2966 2967
2967 Kind kind() const { return kind_; } 2968 Kind kind() const { return kind_; }
2969 bool IsValidBindingIdentifier() const { return kind_ != kVoidType; }
2968 2970
2969 protected: 2971 protected:
2970 PredefinedType(Zone* zone, Kind kind, int pos) 2972 PredefinedType(Zone* zone, Kind kind, int pos)
2971 : Type(zone, pos), kind_(kind) {} 2973 : Type(zone, pos), kind_(kind) {}
2972 2974
2973 private: 2975 private:
2974 Kind kind_; 2976 Kind kind_;
2975 }; 2977 };
2976 2978
2977 2979
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
3025 Type* base() const { return base_; } 3027 Type* base() const { return base_; }
3026 3028
3027 protected: 3029 protected:
3028 ArrayType(Zone* zone, Type* base, int pos) : Type(zone, pos), base_(base) {} 3030 ArrayType(Zone* zone, Type* base, int pos) : Type(zone, pos), base_(base) {}
3029 3031
3030 private: 3032 private:
3031 Type* base_; 3033 Type* base_;
3032 }; 3034 };
3033 3035
3034 3036
3037 class TupleType : public Type {
3038 public:
3039 DECLARE_NODE_TYPE(TupleType)
3040
3041 ZoneList<Type*>* elements() const { return elements_; }
3042 bool IsValidType() const { return valid_type_; }
3043 bool IsValidBindingPattern() const { return valid_binder_; }
3044 bool spread() const { return spread_; }
3045
3046 protected:
3047 TupleType(Zone* zone, ZoneList<Type*>* elements, bool valid_type,
3048 bool valid_binder, bool spread, int pos)
3049 : Type(zone, pos),
3050 elements_(elements),
3051 valid_type_(valid_type),
3052 valid_binder_(valid_binder),
3053 spread_(spread) {}
3054
3055 private:
3056 ZoneList<Type*>* elements_;
3057 bool valid_type_;
3058 bool valid_binder_;
3059 bool spread_;
3060 };
3061
3062
3035 class TypeParameter : public AstNode { 3063 class TypeParameter : public AstNode {
3036 public: 3064 public:
3037 DECLARE_NODE_TYPE(TypeParameter) 3065 DECLARE_NODE_TYPE(TypeParameter)
3038 3066
3039 const AstRawString* name() const { return name_; } 3067 const AstRawString* name() const { return name_; }
3040 Type* extends() const { return extends_; } 3068 Type* extends() const { return extends_; }
3041 3069
3042 protected: 3070 protected:
3043 TypeParameter(Zone* zone, const AstRawString* name, Type* extends, int pos) 3071 TypeParameter(Zone* zone, const AstRawString* name, Type* extends, int pos)
3044 : AstNode(pos), name_(name), extends_(extends) {} 3072 : AstNode(pos), name_(name), extends_(extends) {}
3045 3073
3046 private: 3074 private:
3047 const AstRawString* name_; 3075 const AstRawString* name_;
3048 Type* extends_; 3076 Type* extends_;
3049 }; 3077 };
3050 3078
3051 class FormalParameter : public AstNode { 3079 class FormalParameter : public AstNode {
3052 public: 3080 public:
3053 DECLARE_NODE_TYPE(FormalParameter) 3081 DECLARE_NODE_TYPE(FormalParameter)
3054 3082
3055 bool IsValidType() const { return name_ == nullptr; } 3083 bool IsValidType() const {
3084 return binder_ == nullptr && type_->IsValidType();
3085 }
3056 3086
3057 void MakeValidParameter(bool* ok) { 3087 void MakeValidParameter(bool* ok) {
3058 if (name_ != nullptr) return; 3088 if (binder_ != nullptr) return;
3059 if (optional_ || spread_ || !type_->IsSimpleIdentifier()) { 3089 if (optional_ || spread_ || !type_->IsValidBindingIdentifierOrPattern()) {
3060 *ok = false; 3090 *ok = false;
3061 return; 3091 return;
3062 } 3092 }
3063 name_ = type_->AsSimpleIdentifier(); 3093 binder_ = type_;
3064 type_ = nullptr; 3094 type_ = nullptr;
3065 } 3095 }
3066 3096
3067 const AstRawString* name() const { return name_; } 3097 Type* binder() const { return binder_; }
3068 bool optional() const { return optional_; } 3098 bool optional() const { return optional_; }
3069 bool spread() const { return spread_; } 3099 bool spread() const { return spread_; }
3070 Type* type() const { return type_; } 3100 Type* type() const { return type_; }
3071 3101
3072 protected: 3102 protected:
3073 FormalParameter(Zone* zone, const AstRawString* name, bool optional, 3103 FormalParameter(Zone* zone, Type* binder, bool optional, bool spread,
3074 bool spread, Type* type, int pos) 3104 Type* type, int pos)
3075 : AstNode(pos), 3105 : AstNode(pos),
3076 name_(name), 3106 binder_(binder),
3077 optional_(optional), 3107 optional_(optional),
3078 spread_(spread), 3108 spread_(spread),
3079 type_(type) {} 3109 type_(type) {}
3080 FormalParameter(Zone* zone, Type* type, int pos) 3110 FormalParameter(Zone* zone, Type* type, int pos)
3081 : AstNode(pos), 3111 : AstNode(pos),
3082 name_(nullptr), 3112 binder_(nullptr),
3083 optional_(false), 3113 optional_(false),
3084 spread_(false), 3114 spread_(false),
3085 type_(type) {} 3115 type_(type) {}
3086 3116
3087 friend class Type; 3117 friend class Type;
3088 3118
3089 private: 3119 private:
3090 const AstRawString* name_; 3120 Type* binder_;
3091 bool optional_; 3121 bool optional_;
3092 bool spread_; 3122 bool spread_;
3093 Type* type_; 3123 Type* type_;
3094 }; 3124 };
3095 3125
3096 3126
3097 class FunctionType : public Type { 3127 class FunctionType : public Type {
3098 public: 3128 public:
3099 DECLARE_NODE_TYPE(FunctionType) 3129 DECLARE_NODE_TYPE(FunctionType)
3100 3130
(...skipping 20 matching lines...) Expand all
3121 }; 3151 };
3122 3152
3123 3153
3124 class TypeReference : public Type { 3154 class TypeReference : public Type {
3125 public: 3155 public:
3126 DECLARE_NODE_TYPE(TypeReference) 3156 DECLARE_NODE_TYPE(TypeReference)
3127 3157
3128 const AstRawString* name() const { return name_; } 3158 const AstRawString* name() const { return name_; }
3129 ZoneList<Type*>* type_arguments() const { return type_arguments_; } 3159 ZoneList<Type*>* type_arguments() const { return type_arguments_; }
3130 3160
3161 bool IsValidBindingIdentifier() const {
3162 // TODO(nikolaos): This should probably exclude restricted identifiers.
3163 return type_arguments() == nullptr;
3164 }
3165
3131 protected: 3166 protected:
3132 TypeReference(Zone* zone, const AstRawString* name, 3167 TypeReference(Zone* zone, const AstRawString* name,
3133 ZoneList<Type*>* type_arguments, int pos) 3168 ZoneList<Type*>* type_arguments, int pos)
3134 : Type(zone, pos), name_(name), type_arguments_(type_arguments) {} 3169 : Type(zone, pos), name_(name), type_arguments_(type_arguments) {}
3135 3170
3136 private: 3171 private:
3137 const AstRawString* name_; 3172 const AstRawString* name_;
3138 ZoneList<Type*>* type_arguments_; 3173 ZoneList<Type*>* type_arguments_;
3139 }; 3174 };
3140 3175
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
3181 ZoneList<FormalParameter*>* parameters() const { return parameters_; } 3216 ZoneList<FormalParameter*>* parameters() const { return parameters_; }
3182 3217
3183 protected: 3218 protected:
3184 TypeOrParameters(Zone* zone, ZoneList<FormalParameter*>* parameters, int pos) 3219 TypeOrParameters(Zone* zone, ZoneList<FormalParameter*>* parameters, int pos)
3185 : Type(zone, pos), parameters_(parameters) {} 3220 : Type(zone, pos), parameters_(parameters) {}
3186 3221
3187 private: 3222 private:
3188 ZoneList<FormalParameter*>* parameters_; 3223 ZoneList<FormalParameter*>* parameters_;
3189 }; 3224 };
3190 3225
3191 V8_INLINE bool Type::IsSimpleIdentifier() const { 3226 V8_INLINE bool Type::IsValidType() const {
3192 const TypeReference* ref = AsTypeReference(); 3227 if (IsTypeOrParameters()) {
3193 if (ref == nullptr) return false; 3228 ZoneList<FormalParameter*>* parameters = AsTypeOrParameters()->parameters();
3194 return ref->type_arguments() == nullptr; 3229 return parameters->length() == 1 && parameters->at(0)->IsValidType();
3230 }
3231 if (IsTupleType()) return AsTupleType()->IsValidType();
3232 return true;
3195 } 3233 }
3196 3234
3197 V8_INLINE const AstRawString* Type::AsSimpleIdentifier() const { 3235 V8_INLINE bool Type::IsValidBindingIdentifierOrPattern() const {
3198 const TypeReference* ref = AsTypeReference(); 3236 if (IsTypeReference()) return AsTypeReference()->IsValidBindingIdentifier();
3199 DCHECK_NOT_NULL(ref); 3237 if (IsTupleType()) return AsTupleType()->IsValidBindingPattern();
3200 DCHECK_NULL(ref->type_arguments()); 3238 if (IsPredefinedType()) return AsPredefinedType()->IsValidBindingIdentifier();
3201 return ref->name(); 3239 return false;
3202 } 3240 }
3203 3241
3204 V8_INLINE Type* Type::Uncover(bool* ok) { 3242 V8_INLINE Type* Type::Uncover(bool* ok) {
3205 if (!IsTypeOrParameters()) return this; 3243 if (IsTypeOrParameters()) {
3206 ZoneList<FormalParameter*>* parameters = AsTypeOrParameters()->parameters(); 3244 ZoneList<FormalParameter*>* parameters = AsTypeOrParameters()->parameters();
3207 if (parameters->length() == 1 && parameters->at(0)->IsValidType()) 3245 if (parameters->length() == 1 && parameters->at(0)->IsValidType())
3208 return parameters->at(0)->type(); 3246 return parameters->at(0)->type();
3247 } else if (IsTupleType()) {
3248 if (AsTupleType()->IsValidType()) return this;
3249 } else {
3250 return this;
3251 }
3209 *ok = false; 3252 *ok = false;
3210 return nullptr; 3253 return nullptr;
3211 } 3254 }
3212 3255
3213 V8_INLINE ZoneList<FormalParameter*>* Type::AsValidParameterList( 3256 V8_INLINE ZoneList<FormalParameter*>* Type::AsValidParameterList(
3214 Zone* zone, bool* ok) const { 3257 Zone* zone, bool* ok) const {
3215 if (!IsTypeOrParameters()) { 3258 if (!IsTypeOrParameters()) {
3216 *ok = false; 3259 *ok = false;
3217 return nullptr; 3260 return nullptr;
3218 } 3261 }
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
3790 typesystem::Type* right, 3833 typesystem::Type* right,
3791 int pos) { 3834 int pos) {
3792 return new (local_zone_) 3835 return new (local_zone_)
3793 typesystem::IntersectionType(local_zone_, left, right, pos); 3836 typesystem::IntersectionType(local_zone_, left, right, pos);
3794 } 3837 }
3795 3838
3796 typesystem::ArrayType* NewArrayType(typesystem::Type* base, int pos) { 3839 typesystem::ArrayType* NewArrayType(typesystem::Type* base, int pos) {
3797 return new (local_zone_) typesystem::ArrayType(local_zone_, base, pos); 3840 return new (local_zone_) typesystem::ArrayType(local_zone_, base, pos);
3798 } 3841 }
3799 3842
3843 typesystem::TupleType* NewTupleType(ZoneList<typesystem::Type*>* elements,
3844 bool valid_type, bool valid_binder,
3845 bool spread, int pos) {
3846 return new (local_zone_) typesystem::TupleType(
3847 local_zone_, elements, valid_type, valid_binder, spread, pos);
3848 }
3849
3800 typesystem::FunctionType* NewFunctionType( 3850 typesystem::FunctionType* NewFunctionType(
3801 ZoneList<typesystem::TypeParameter*>* type_parameters, 3851 ZoneList<typesystem::TypeParameter*>* type_parameters,
3802 ZoneList<typesystem::FormalParameter*>* parameters, 3852 ZoneList<typesystem::FormalParameter*>* parameters,
3803 typesystem::Type* result_type, int pos, bool constructor = false) { 3853 typesystem::Type* result_type, int pos, bool constructor = false) {
3804 return new (local_zone_) 3854 return new (local_zone_)
3805 typesystem::FunctionType(local_zone_, type_parameters, parameters, 3855 typesystem::FunctionType(local_zone_, type_parameters, parameters,
3806 result_type, pos, constructor); 3856 result_type, pos, constructor);
3807 } 3857 }
3808 3858
3809 typesystem::TypeReference* NewTypeReference( 3859 typesystem::TypeReference* NewTypeReference(
3810 const AstRawString* name, ZoneList<typesystem::Type*>* type_arguments, 3860 const AstRawString* name, ZoneList<typesystem::Type*>* type_arguments,
3811 int pos) { 3861 int pos) {
3812 return new (local_zone_) 3862 return new (local_zone_)
3813 typesystem::TypeReference(local_zone_, name, type_arguments, pos); 3863 typesystem::TypeReference(local_zone_, name, type_arguments, pos);
3814 } 3864 }
3815 3865
3816 typesystem::StringLiteralType* NewStringLiteralType( 3866 typesystem::StringLiteralType* NewStringLiteralType(
3817 const AstRawString* string, int pos) { 3867 const AstRawString* string, int pos) {
3818 return new (local_zone_) 3868 return new (local_zone_)
3819 typesystem::StringLiteralType(local_zone_, string, pos); 3869 typesystem::StringLiteralType(local_zone_, string, pos);
3820 } 3870 }
3821 3871
3822 typesystem::QueryType* NewQueryType( 3872 typesystem::QueryType* NewQueryType(
3823 const AstRawString* name, ZoneList<const AstRawString*>* property_names, 3873 const AstRawString* name, ZoneList<const AstRawString*>* property_names,
3824 int pos) { 3874 int pos) {
3825 return new (local_zone_) 3875 return new (local_zone_)
3826 typesystem::QueryType(local_zone_, name, property_names, pos); 3876 typesystem::QueryType(local_zone_, name, property_names, pos);
3827 } 3877 }
3828 3878
3829 typesystem::FormalParameter* NewFormalParameter(const AstRawString* name, 3879 typesystem::FormalParameter* NewFormalParameter(typesystem::Type* binder,
3830 bool optional, bool spread, 3880 bool optional, bool spread,
3831 typesystem::Type* type, 3881 typesystem::Type* type,
3832 int pos) { 3882 int pos) {
3833 return new (local_zone_) typesystem::FormalParameter( 3883 return new (local_zone_) typesystem::FormalParameter(
3834 local_zone_, name, optional, spread, type, pos); 3884 local_zone_, binder, optional, spread, type, pos);
3835 } 3885 }
3836 3886
3837 typesystem::FormalParameter* NewFormalParameter(typesystem::Type* type, 3887 typesystem::FormalParameter* NewFormalParameter(typesystem::Type* type,
3838 int pos) { 3888 int pos) {
3839 return new (local_zone_) 3889 return new (local_zone_)
3840 typesystem::FormalParameter(local_zone_, type, pos); 3890 typesystem::FormalParameter(local_zone_, type, pos);
3841 } 3891 }
3842 3892
3843 typesystem::TypeOrParameters* NewTypeOrParameters( 3893 typesystem::TypeOrParameters* NewTypeOrParameters(
3844 ZoneList<typesystem::FormalParameter*>* parameters, int pos) { 3894 ZoneList<typesystem::FormalParameter*>* parameters, int pos) {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
3938 : NULL; \ 3988 : NULL; \
3939 } 3989 }
3940 TYPESYSTEM_NODE_LIST(DECLARE_NODE_FUNCTIONS) 3990 TYPESYSTEM_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3941 #undef DECLARE_NODE_FUNCTIONS 3991 #undef DECLARE_NODE_FUNCTIONS
3942 3992
3943 3993
3944 } // namespace internal 3994 } // namespace internal
3945 } // namespace v8 3995 } // namespace v8
3946 3996
3947 #endif // V8_AST_AST_H_ 3997 #endif // V8_AST_AST_H_
OLDNEW
« no previous file with comments | « no previous file | src/parsing/parser.h » ('j') | src/parsing/parser-base.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698