Skip to content

Commit

Permalink
refactored the decltype support by adding two places: either it repla…
Browse files Browse the repository at this point in the history
…ces an entire Type, or a part of a Name. Fixed the parser code accordingly. Still need an example of the Name kind.
  • Loading branch information
jurgenvinju committed Nov 13, 2023
1 parent daad508 commit 3cfc4e4
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 134 deletions.
11 changes: 4 additions & 7 deletions src/lang/cpp/AST.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ data Declarator(list[Attribute] attributes = [], loc src = |unknown:///|, loc de

data DeclSpecifier(list[Attribute] attributes = [], loc src = |unknown:///|, bool isMacroExpansion = false)
= \declSpecifier(list[Modifier] modifiers, Type \type)
| \declSpecifier(list[Modifier] modifiers, Type \type, Expression expression) //decltype and type_of
| \etsEnum(list[Modifier] modifiers, Name name, loc decl = |unknown:///|) //no attributes
| \etsStruct(list[Modifier] modifiers, Name name, loc decl = |unknown:///|) //ElaboratedTypeSpecifier //no attributes
| \etsUnion(list[Modifier] modifiers, Name name, loc decl = |unknown:///|) //no attributes
| \etsClass(list[Modifier] modifiers, Name name, loc decl = |unknown:///|) //no attributes
| \namedTypeSpecifier(list[Modifier] modifiers, Name name, loc decl = |unknown:///|) //no attributes
| \declType(Expression exp)

| \struct(list[Modifier] modifiers, Name name, list[Declaration] members, loc decl = |unknown:///|) //c //no attributes
| \union(list[Modifier] modifiers, Name name, list[Declaration] members, loc decl = |unknown:///|) //c //no attributes
Expand Down Expand Up @@ -303,13 +301,12 @@ data Expression(loc src = |unknown:///|, TypeSymbol typ = \unresolved(), bool is
data Name(loc src = |unknown:///|, bool isMacroExpansion = false) //no attributes
= \name(str \value)
| \qualifiedName(list[Name] qualifiers, Name lastName, loc decl = |unknown:///|)
| \qualifiedName(DeclSpecifier specifier, list[Name] qualifiers, Name lastName, loc decl = |unknown:///|)
| \qualifiedName(Type decltype, list[Name] qualifiers, Name lastName, loc decl = |unknown:///|)
| \operatorName(str \value)
| \conversionName(str \value, Expression typeId)

| \templateId(Name name, list[Expression] argumentTypes, loc decl = |unknown:///|)

| \abstractEmptyName()
| decltypeName(Expression expression)
;

data Statement(list[Attribute] attributes = [], loc src = |unknown:///|, bool isMacroExpansion = false)
Expand Down Expand Up @@ -355,8 +352,8 @@ data Type(loc src = |unknown:///|, bool isMacroExpansion = false) //no attribute
| \double()
| \bool()
| \wchar_t()
| \typeof()
| \decltype()
| \typeof(Expression expression)
| \decltype(Expression expression)
| \auto()
| \char16_t()
| \char32_t()
Expand Down
196 changes: 94 additions & 102 deletions src/lang/cpp/internal/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public AST (IValueFactory vf) {
= tf.constructor(typestore,_DeclSpecifier,"classFinal",tf.listType(_Modifier),"modifiers",_Name,"name",tf.listType(_Declaration),"baseSpecifiers",tf.listType(_Declaration),"members");
private static final Type _DeclSpecifier_struct_3
= tf.constructor(typestore,_DeclSpecifier,"struct",tf.listType(_Modifier),"modifiers",_Name,"name",tf.listType(_Declaration),"members");
private static final Type _DeclSpecifier_declSpecifier_3
= tf.constructor(typestore,_DeclSpecifier,"declSpecifier",tf.listType(_Modifier),"modifiers",_Type,"type",_Expression,"expression");
private static final Type _DeclSpecifier_msThrowEllipsis_0
= tf.constructor(typestore,_DeclSpecifier,"msThrowEllipsis");
private static final Type _DeclSpecifier_union_3
= tf.constructor(typestore,_DeclSpecifier,"union",tf.listType(_Modifier),"modifiers",_Name,"name",tf.listType(_Declaration),"members");
private static final Type _DeclSpecifier_union_4
= tf.constructor(typestore,_DeclSpecifier,"union",tf.listType(_Modifier),"modifiers",_Name,"name",tf.listType(_Declaration),"baseSpecifiers",tf.listType(_Declaration),"members");
private static final Type _DeclSpecifier_declSpecifier_2
= tf.constructor(typestore,_DeclSpecifier,"declSpecifier",tf.listType(_Modifier),"modifiers",_Type,"type");
private static final Type _DeclSpecifier_etsStruct_2
= tf.constructor(typestore,_DeclSpecifier,"etsStruct",tf.listType(_Modifier),"modifiers",_Name,"name");
private static final Type _DeclSpecifier_enumScoped_3
Expand All @@ -130,10 +130,6 @@ public AST (IValueFactory vf) {
= tf.constructor(typestore,_DeclSpecifier,"enum",tf.listType(_Modifier),"modifiers",_Name,"name",tf.listType(_Declaration),"enumerators");
private static final Type _DeclSpecifier_enumScopedOpaque_3
= tf.constructor(typestore,_DeclSpecifier,"enumScopedOpaque",tf.listType(_Modifier),"modifiers",_DeclSpecifier,"baseType",_Name,"name");
private static final Type _DeclSpecifier_declSpecifier_2
= tf.constructor(typestore,_DeclSpecifier,"declSpecifier",tf.listType(_Modifier),"modifiers",_Type,"type");
private static final Type _DeclSpecifier_declType_1
= tf.constructor(typestore,_DeclSpecifier,"declType",_Expression,"exp");


private static final Type _Declaration_sttClass_2
Expand Down Expand Up @@ -500,12 +496,12 @@ public AST (IValueFactory vf) {
= tf.constructor(typestore,_Type,"decimal128");
private static final Type _Type_nullptr_0
= tf.constructor(typestore,_Type,"nullptr");
private static final Type _Type_decltype_0
= tf.constructor(typestore,_Type,"decltype");
private static final Type _Type_wchar_t_0
= tf.constructor(typestore,_Type,"wchar_t");
private static final Type _Type_integer_0
= tf.constructor(typestore,_Type,"integer");
private static final Type _Type_decltype_1
= tf.constructor(typestore,_Type,"decltype",_Expression,"expression");
private static final Type _Type_float_0
= tf.constructor(typestore,_Type,"float");
private static final Type _Type_float128_0
Expand All @@ -522,6 +518,8 @@ public AST (IValueFactory vf) {
= tf.constructor(typestore,_Type,"decimal64");
private static final Type _Type_auto_0
= tf.constructor(typestore,_Type,"auto");
private static final Type _Type_typeof_1
= tf.constructor(typestore,_Type,"typeof",_Expression,"expression");
private static final Type _Type_char32_t_0
= tf.constructor(typestore,_Type,"char32_t");
private static final Type _Type_bool_0
Expand Down Expand Up @@ -832,12 +830,14 @@ public AST (IValueFactory vf) {
= tf.constructor(typestore,_M3,"m3",tf.sourceLocationType(),"id");


private static final Type _Name_qualifiedName_3
= tf.constructor(typestore,_Name,"qualifiedName",_DeclSpecifier,"specifier",tf.listType(_Name),"qualifiers",_Name,"lastName");
private static final Type _Name_templateId_2
= tf.constructor(typestore,_Name,"templateId",_Name,"name",tf.listType(_Expression),"argumentTypes");
private static final Type _Name_conversionName_2
= tf.constructor(typestore,_Name,"conversionName",tf.stringType(),"value",_Expression,"typeId");
private static final Type _Name_decltypeName_1
= tf.constructor(typestore,_Name,"decltypeName",_Expression,"expression");
private static final Type _Name_qualifiedName_3
= tf.constructor(typestore,_Name,"qualifiedName",_Type,"decltype",tf.listType(_Name),"qualifiers",_Name,"lastName");
private static final Type _Name_operatorName_1
= tf.constructor(typestore,_Name,"operatorName",tf.stringType(),"value");
private static final Type _Name_name_1
Expand Down Expand Up @@ -1524,31 +1524,6 @@ public IConstructor DeclSpecifier_struct(IList $modifiers, IConstructor $name, I
return vf.constructor(_DeclSpecifier_struct_3 , $modifiers, $name, $members).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor DeclSpecifier_declSpecifier(IList $modifiers, IConstructor $type, IConstructor $expression, IList $attributes, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!$modifiers.getType().isSubtypeOf(tf.listType(_Modifier))) {
throw new IllegalArgumentException("Expected " + tf.listType(_Modifier) + " but got " + $modifiers.getType() + " for $modifiers:" + $modifiers);
}

if (!$type.getType().isSubtypeOf(_Type)) {
throw new IllegalArgumentException("Expected " + _Type + " but got " + $type.getType() + " for $type:" + $type);
}

if (!$expression.getType().isSubtypeOf(_Expression)) {
throw new IllegalArgumentException("Expected " + _Expression + " but got " + $expression.getType() + " for $expression:" + $expression);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();
if (!$attributes.isEmpty()) {
kwParams.put("attributes", $attributes);
}
kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_DeclSpecifier_declSpecifier_3 , $modifiers, $type, $expression).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor DeclSpecifier_msThrowEllipsis(ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

Map<String, IValue> kwParams = new HashMap<String, IValue>();
Expand Down Expand Up @@ -1614,6 +1589,27 @@ public IConstructor DeclSpecifier_union(IList $modifiers, IConstructor $name, IL
return vf.constructor(_DeclSpecifier_union_4 , $modifiers, $name, $baseSpecifiers, $members).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor DeclSpecifier_declSpecifier(IList $modifiers, IConstructor $type, IList $attributes, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!$modifiers.getType().isSubtypeOf(tf.listType(_Modifier))) {
throw new IllegalArgumentException("Expected " + tf.listType(_Modifier) + " but got " + $modifiers.getType() + " for $modifiers:" + $modifiers);
}

if (!$type.getType().isSubtypeOf(_Type)) {
throw new IllegalArgumentException("Expected " + _Type + " but got " + $type.getType() + " for $type:" + $type);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();
if (!$attributes.isEmpty()) {
kwParams.put("attributes", $attributes);
}
kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_DeclSpecifier_declSpecifier_2 , $modifiers, $type).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor DeclSpecifier_etsStruct(IList $modifiers, IConstructor $name, ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

if (!$modifiers.getType().isSubtypeOf(tf.listType(_Modifier))) {
Expand Down Expand Up @@ -1927,44 +1923,6 @@ public IConstructor DeclSpecifier_enumScopedOpaque(IList $modifiers, IConstructo
return vf.constructor(_DeclSpecifier_enumScopedOpaque_3 , $modifiers, $baseType, $name).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor DeclSpecifier_declSpecifier(IList $modifiers, IConstructor $type, IList $attributes, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!$modifiers.getType().isSubtypeOf(tf.listType(_Modifier))) {
throw new IllegalArgumentException("Expected " + tf.listType(_Modifier) + " but got " + $modifiers.getType() + " for $modifiers:" + $modifiers);
}

if (!$type.getType().isSubtypeOf(_Type)) {
throw new IllegalArgumentException("Expected " + _Type + " but got " + $type.getType() + " for $type:" + $type);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();
if (!$attributes.isEmpty()) {
kwParams.put("attributes", $attributes);
}
kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_DeclSpecifier_declSpecifier_2 , $modifiers, $type).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor DeclSpecifier_declType(IConstructor $exp, IList $attributes, ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

if (!$exp.getType().isSubtypeOf(_Expression)) {
throw new IllegalArgumentException("Expected " + _Expression + " but got " + $exp.getType() + " for $exp:" + $exp);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();
if (!$attributes.isEmpty()) {
kwParams.put("attributes", $attributes);
}
kwParams.put("src", $loc);
if ($decl != null) { kwParams.put("decl", $decl); }

if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_DeclSpecifier_declType_1 , $exp).asWithKeywordParameters().setParameters(kwParams);
}


public IConstructor Declaration_sttClass(IConstructor $name, IConstructor $defaultType, ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

Expand Down Expand Up @@ -5038,37 +4996,41 @@ public IConstructor Type_nullptr(ISourceLocation $loc, boolean $isMacroExpansion
return vf.constructor(_Type_nullptr_0 ).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Type_decltype(ISourceLocation $loc, boolean $isMacroExpansion) {
public IConstructor Type_wchar_t(ISourceLocation $loc, boolean $isMacroExpansion) {

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Type_decltype_0 ).asWithKeywordParameters().setParameters(kwParams);
return vf.constructor(_Type_wchar_t_0 ).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Type_wchar_t(ISourceLocation $loc, boolean $isMacroExpansion) {
public IConstructor Type_integer(ISourceLocation $loc, boolean $isMacroExpansion) {

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Type_wchar_t_0 ).asWithKeywordParameters().setParameters(kwParams);
return vf.constructor(_Type_integer_0 ).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Type_integer(ISourceLocation $loc, boolean $isMacroExpansion) {
public IConstructor Type_decltype(IConstructor $expression, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!$expression.getType().isSubtypeOf(_Expression)) {
throw new IllegalArgumentException("Expected " + _Expression + " but got " + $expression.getType() + " for $expression:" + $expression);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Type_integer_0 ).asWithKeywordParameters().setParameters(kwParams);
return vf.constructor(_Type_decltype_1 , $expression).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Type_float(ISourceLocation $loc, boolean $isMacroExpansion) {
Expand Down Expand Up @@ -5171,6 +5133,21 @@ public IConstructor Type_auto(ISourceLocation $loc, boolean $isMacroExpansion) {
return vf.constructor(_Type_auto_0 ).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Type_typeof(IConstructor $expression, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!$expression.getType().isSubtypeOf(_Expression)) {
throw new IllegalArgumentException("Expected " + _Expression + " but got " + $expression.getType() + " for $expression:" + $expression);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Type_typeof_1 , $expression).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Type_char32_t(ISourceLocation $loc, boolean $isMacroExpansion) {

Map<String, IValue> kwParams = new HashMap<String, IValue>();
Expand Down Expand Up @@ -7336,29 +7313,6 @@ public IConstructor M3_m3(ISourceLocation $id) {
}


public IConstructor Name_qualifiedName(IConstructor $specifier, IList $qualifiers, IConstructor $lastName, ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

if (!$specifier.getType().isSubtypeOf(_DeclSpecifier)) {
throw new IllegalArgumentException("Expected " + _DeclSpecifier + " but got " + $specifier.getType() + " for $specifier:" + $specifier);
}

if (!$qualifiers.getType().isSubtypeOf(tf.listType(_Name))) {
throw new IllegalArgumentException("Expected " + tf.listType(_Name) + " but got " + $qualifiers.getType() + " for $qualifiers:" + $qualifiers);
}

if (!$lastName.getType().isSubtypeOf(_Name)) {
throw new IllegalArgumentException("Expected " + _Name + " but got " + $lastName.getType() + " for $lastName:" + $lastName);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);
if ($decl != null) { kwParams.put("decl", $decl); }

if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Name_qualifiedName_3 , $specifier, $qualifiers, $lastName).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Name_templateId(IConstructor $name, IList $argumentTypes, ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

if (!$name.getType().isSubtypeOf(_Name)) {
Expand Down Expand Up @@ -7397,6 +7351,44 @@ public IConstructor Name_conversionName(String $value, IConstructor $typeId, ISo
return vf.constructor(_Name_conversionName_2 , vf.string($value), $typeId).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Name_decltypeName(IConstructor $expression, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!$expression.getType().isSubtypeOf(_Expression)) {
throw new IllegalArgumentException("Expected " + _Expression + " but got " + $expression.getType() + " for $expression:" + $expression);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);


if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Name_decltypeName_1 , $expression).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Name_qualifiedName(IConstructor $decltype, IList $qualifiers, IConstructor $lastName, ISourceLocation $loc, ISourceLocation $decl, boolean $isMacroExpansion) {

if (!$decltype.getType().isSubtypeOf(_Type)) {
throw new IllegalArgumentException("Expected " + _Type + " but got " + $decltype.getType() + " for $decltype:" + $decltype);
}

if (!$qualifiers.getType().isSubtypeOf(tf.listType(_Name))) {
throw new IllegalArgumentException("Expected " + tf.listType(_Name) + " but got " + $qualifiers.getType() + " for $qualifiers:" + $qualifiers);
}

if (!$lastName.getType().isSubtypeOf(_Name)) {
throw new IllegalArgumentException("Expected " + _Name + " but got " + $lastName.getType() + " for $lastName:" + $lastName);
}

Map<String, IValue> kwParams = new HashMap<String, IValue>();

kwParams.put("src", $loc);
if ($decl != null) { kwParams.put("decl", $decl); }

if ($isMacroExpansion) kwParams.put("isMacroExpansion", vf.bool(true));
return vf.constructor(_Name_qualifiedName_3 , $decltype, $qualifiers, $lastName).asWithKeywordParameters().setParameters(kwParams);
}

public IConstructor Name_operatorName(String $value, ISourceLocation $loc, boolean $isMacroExpansion) {

if (!vf.string($value).getType().isSubtypeOf(tf.stringType())) {
Expand Down
Loading

0 comments on commit 3cfc4e4

Please sign in to comment.