Skip to content

Commit

Permalink
Add more C++ builtins
Browse files Browse the repository at this point in the history
Add __has_unique_object_representations
    __is_aggregate
    __is_assignable
    __is_nothrow_assignable
    __is_nothrow_constructible
  • Loading branch information
i-garrison authored and jonahgraham committed Feb 23, 2023
1 parent ab721ee commit 8f2342e
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static enum Operator {
* @since 7.1
*/
__is_same,
/** @since 8.1 */
__is_assignable,
/** @since 8.1 */
__is_nothrow_assignable,
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public interface IASTTypeIdExpression extends IASTExpression {
*/
public static final int op_is_trivially_copyable = 24;

/**
* Built-in type trait of g++.
* @since 8.1
*/
public static final int op_has_unique_object_representations = 25;

/**
* Built-in type trait of g++.
* @since 8.1
*/
public static final int op_is_aggregate = 26;

/**
* Returns the operator for the expression.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public interface ICPPASTNaryTypeIdExpression extends ICPPASTExpression {
public static enum Operator {
__is_trivially_constructible,
/** @since 6.6 */
__is_constructible
__is_constructible,
/** @since 8.1 */
__is_nothrow_constructible,
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static enum CompilerType {
private static final int VERSION_6_0 = version(6, 0);
private static final int VERSION_8_0 = version(8, 0);
private static final int VERSION_10_0 = version(10, 0);
private static final int VERSION_11_1 = version(11, 1);
private static GPPScannerExtensionConfiguration CONFIG = new GPPScannerExtensionConfiguration();
private static GPPScannerExtensionConfiguration CONFIG_4_2 = new GPPScannerExtensionConfiguration(VERSION_4_2);
private static GPPScannerExtensionConfiguration CONFIG_4_3 = new GPPScannerExtensionConfiguration(VERSION_4_3);
Expand All @@ -52,6 +53,7 @@ private static enum CompilerType {
private static GPPScannerExtensionConfiguration CONFIG_6_0 = new GPPScannerExtensionConfiguration(VERSION_6_0);
private static GPPScannerExtensionConfiguration CONFIG_8_0 = new GPPScannerExtensionConfiguration(VERSION_8_0);
private static GPPScannerExtensionConfiguration CONFIG_10_0 = new GPPScannerExtensionConfiguration(VERSION_10_0);
private static GPPScannerExtensionConfiguration CONFIG_11_1 = new GPPScannerExtensionConfiguration(VERSION_11_1);
private static GPPScannerExtensionConfiguration CONFIG_CLANG = new GPPScannerExtensionConfiguration(
CompilerType.Clang, 0 /* version is ignored for now */);
private static GPPScannerExtensionConfiguration CONFIG_CLANG_CL = new GPPScannerExtensionConfiguration(
Expand Down Expand Up @@ -89,6 +91,9 @@ public static GPPScannerExtensionConfiguration getInstance(IScannerInfo info) {
int major = Integer.valueOf(definedSymbols.get("__GNUC__")); //$NON-NLS-1$
int minor = Integer.valueOf(definedSymbols.get("__GNUC_MINOR__")); //$NON-NLS-1$
int version = version(major, minor);
if (version >= VERSION_11_1) {
return CONFIG_11_1;
}
if (version >= VERSION_10_0) {
return CONFIG_10_0;
}
Expand Down Expand Up @@ -192,12 +197,20 @@ public GPPScannerExtensionConfiguration(CompilerType compiler, int version) {
addKeyword(GCCKeywords.cp__is_same_as, IGCCToken.tTT_is_same);
}
if (version >= VERSION_8_0) {
addKeyword(GCCKeywords.cp__has_unique_object_representations,
IGCCToken.tTT_has_unique_object_representations);
addKeyword(GCCKeywords.cp__is_aggregate, IGCCToken.tTT_is_aggregate);
addKeyword(GCCKeywords.cp__is_assignable, IGCCToken.tTT_is_assignable);
addKeyword(GCCKeywords.cp__is_constructible, IGCCToken.tTT_is_constructible);
addKeyword(GCCKeywords.cp__integer_pack, IGCCToken.tTT_integer_pack);
}
if (version >= VERSION_10_0) {
addKeyword(GCCKeywords.cp__is_same, IGCCToken.tTT_is_same);
}
if (version >= VERSION_11_1) {
addKeyword(GCCKeywords.cp__is_nothrow_assignable, IGCCToken.tTT_is_nothrow_assignable);
addKeyword(GCCKeywords.cp__is_nothrow_constructible, IGCCToken.tTT_is_nothrow_constructible);
}
} else if (compiler == CompilerType.Clang || compiler == CompilerType.ClangCl) {
// As documented at
// http://clang.llvm.org/docs/LanguageExtensions.html#checks-for-type-trait-primitives.
Expand All @@ -212,13 +225,14 @@ public GPPScannerExtensionConfiguration(CompilerType compiler, int version) {
addKeyword(GCCKeywords.cp__has_trivial_copy, IGCCToken.tTT_has_trivial_copy);
addKeyword(GCCKeywords.cp__has_trivial_constructor, IGCCToken.tTT_has_trivial_constructor);
addKeyword(GCCKeywords.cp__has_trivial_destructor, IGCCToken.tTT_has_trivial_destructor);
// __has_unique_object_representations
addKeyword(GCCKeywords.cp__has_unique_object_representations,
IGCCToken.tTT_has_unique_object_representations);
addKeyword(GCCKeywords.cp__has_virtual_destructor, IGCCToken.tTT_has_virtual_destructor);
addKeyword(GCCKeywords.cp__is_abstract, IGCCToken.tTT_is_abstract);
// __is_aggregate
addKeyword(GCCKeywords.cp__is_aggregate, IGCCToken.tTT_is_aggregate);
// __is_arithmetic
// __is_array
// __is_assignable
addKeyword(GCCKeywords.cp__is_assignable, IGCCToken.tTT_is_assignable);
addKeyword(GCCKeywords.cp__is_base_of, IGCCToken.tTT_is_base_of);
addKeyword(GCCKeywords.cp__is_class, IGCCToken.tTT_is_class);
// __is_complete_type
Expand All @@ -242,8 +256,8 @@ public GPPScannerExtensionConfiguration(CompilerType compiler, int version) {
// __is_member_object_pointer
// __is_member_function_pointer
// __is_member_pointer
// __is_nothrow_assignable
// __is_nothrow_constructible
addKeyword(GCCKeywords.cp__is_nothrow_assignable, IGCCToken.tTT_is_nothrow_assignable);
addKeyword(GCCKeywords.cp__is_nothrow_constructible, IGCCToken.tTT_is_nothrow_constructible);
// __is_nothrow_destructible
// __is_object
addKeyword(GCCKeywords.cp__is_pod, IGCCToken.tTT_is_pod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,14 @@ public class GCCKeywords {
* @since 7.1
*/
public static final char[] cp__is_same = "__is_same".toCharArray(), cp__is_same_as = "__is_same_as".toCharArray();

/** @since 8.1 */
public static final char[] cp__has_unique_object_representations = "__has_unique_object_representations"
.toCharArray();

/** @since 8.1 */
public static final char[] cp__is_aggregate = "__is_aggregate".toCharArray(),
cp__is_assignable = "__is_assignable".toCharArray(),
cp__is_nothrow_assignable = "__is_nothrow_assignable".toCharArray(),
cp__is_nothrow_constructible = "__is_nothrow_constructible".toCharArray();
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,15 @@ public interface IGCCToken extends IToken {
* @since 7.1
*/
int tTT_is_same = FIRST_RESERVED_IGCCToken + 37;

/** @since 8.1 */
int tTT_is_aggregate = FIRST_RESERVED_IGCCToken + 38;
/** @since 8.1 */
int tTT_is_assignable = FIRST_RESERVED_IGCCToken + 39;
/** @since 8.1 */
int tTT_is_nothrow_assignable = FIRST_RESERVED_IGCCToken + 40;
/** @since 8.1 */
int tTT_is_nothrow_constructible = FIRST_RESERVED_IGCCToken + 41;
/** @since 8.1 */
int tTT_has_unique_object_representations = FIRST_RESERVED_IGCCToken + 42;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_constructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_copy;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_unique_object_representations;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_virtual_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_abstract;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_aggregate;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_class;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_empty;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_enum;
Expand Down Expand Up @@ -445,11 +447,16 @@ private static IValue applyUnaryTypeIdOperator(int operator, IType type) {
!(type instanceof ICPPClassType) || TypeTraits.hasTrivialCopyCtor((ICPPClassType) type) ? 1 : 0);
case op_has_trivial_destructor:
break; // TODO(sprigogin): Implement
case op_has_unique_object_representations:
break; // TODO: Implement
case op_has_virtual_destructor:
break; // TODO(sprigogin): Implement
case op_is_abstract:
return IntegralValue
.create(type instanceof ICPPClassType && TypeTraits.isAbstract((ICPPClassType) type) ? 1 : 0);
case op_is_aggregate:
return IntegralValue
.create(type instanceof ICPPClassType && TypeTraits.isAggregateClass((ICPPClassType) type) ? 1 : 0);
case op_is_class:
return IntegralValue.create(
type instanceof ICompositeType && ((ICompositeType) type).getKey() != ICompositeType.k_union ? 1
Expand Down Expand Up @@ -629,6 +636,10 @@ private static IValue applyBinaryTypeIdOperator(IASTBinaryTypeIdExpression.Opera
return IntegralValue.create(1);
}
return IntegralValue.create(0);
case __is_assignable:
return IntegralValue.UNKNOWN; // TODO: Implement.
case __is_nothrow_assignable:
return IntegralValue.UNKNOWN; // TODO: Implement.
case __is_same:
if (type1.isSameType(type2)) {
return IntegralValue.create(1);
Expand All @@ -654,6 +665,8 @@ private static IValue applyNaryTypeIdOperator(ICPPASTNaryTypeIdExpression.Operat
return IntegralValue.create(
TypeTraits.isConstructible(typeToConstruct, argumentTypes, pointOfDefinition, checkTrivial) ? 1
: 0);
case __is_nothrow_constructible:
return IntegralValue.UNKNOWN; // TODO: Implement
}
return IntegralValue.UNKNOWN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1789,8 +1789,10 @@ protected IASTExpression unaryExpression(CastExprCtx ctx, ITemplateIdStrategy st
case IGCCToken.tTT_has_trivial_constructor:
case IGCCToken.tTT_has_trivial_copy:
case IGCCToken.tTT_has_trivial_destructor:
case IGCCToken.tTT_has_unique_object_representations:
case IGCCToken.tTT_has_virtual_destructor:
case IGCCToken.tTT_is_abstract:
case IGCCToken.tTT_is_aggregate:
case IGCCToken.tTT_is_base_of:
case IGCCToken.tTT_is_class:
case IGCCToken.tTT_is_empty:
Expand All @@ -1807,6 +1809,9 @@ protected IASTExpression unaryExpression(CastExprCtx ctx, ITemplateIdStrategy st
case IGCCToken.tTT_is_trivially_assignable:
case IGCCToken.tTT_is_constructible:
case IGCCToken.tTT_is_same:
case IGCCToken.tTT_is_assignable:
case IGCCToken.tTT_is_nothrow_assignable:
case IGCCToken.tTT_is_nothrow_constructible:
return parseTypeTrait();

default:
Expand Down Expand Up @@ -1857,6 +1862,8 @@ private IASTExpression parseTypeTrait() throws EndOfFileException, BacktrackExce
private boolean isBinaryTrait(IToken first) {
switch (first.getType()) {
case IGCCToken.tTT_is_base_of:
case IGCCToken.tTT_is_assignable:
case IGCCToken.tTT_is_nothrow_assignable:
case IGCCToken.tTT_is_trivially_assignable:
case IGCCToken.tTT_is_same:
return true;
Expand All @@ -1868,6 +1875,7 @@ private boolean isNaryTrait(IToken operatorToken) {
switch (operatorToken.getType()) {
case IGCCToken.tTT_is_trivially_constructible:
case IGCCToken.tTT_is_constructible:
case IGCCToken.tTT_is_nothrow_constructible:
return true;
}
return false;
Expand All @@ -1877,6 +1885,10 @@ private IASTBinaryTypeIdExpression.Operator getBinaryTypeTraitOperator(IToken fi
switch (first.getType()) {
case IGCCToken.tTT_is_base_of:
return IASTBinaryTypeIdExpression.Operator.__is_base_of;
case IGCCToken.tTT_is_assignable:
return IASTBinaryTypeIdExpression.Operator.__is_assignable;
case IGCCToken.tTT_is_nothrow_assignable:
return IASTBinaryTypeIdExpression.Operator.__is_nothrow_assignable;
case IGCCToken.tTT_is_trivially_assignable:
return IASTBinaryTypeIdExpression.Operator.__is_trivially_assignable;
case IGCCToken.tTT_is_same:
Expand All @@ -1893,6 +1905,8 @@ private ICPPASTNaryTypeIdExpression.Operator getNaryTypeTraitOperator(IToken ope
return ICPPASTNaryTypeIdExpression.Operator.__is_trivially_constructible;
case IGCCToken.tTT_is_constructible:
return ICPPASTNaryTypeIdExpression.Operator.__is_constructible;
case IGCCToken.tTT_is_nothrow_constructible:
return ICPPASTNaryTypeIdExpression.Operator.__is_nothrow_constructible;
}

assert false;
Expand All @@ -1915,10 +1929,14 @@ private int getUnaryTypeTraitOperator(IToken first) {
return IASTTypeIdExpression.op_has_trivial_copy;
case IGCCToken.tTT_has_trivial_destructor:
return IASTTypeIdExpression.op_has_trivial_destructor;
case IGCCToken.tTT_has_unique_object_representations:
return IASTTypeIdExpression.op_has_unique_object_representations;
case IGCCToken.tTT_has_virtual_destructor:
return IASTTypeIdExpression.op_has_virtual_destructor;
case IGCCToken.tTT_is_abstract:
return IASTTypeIdExpression.op_is_abstract;
case IGCCToken.tTT_is_aggregate:
return IASTTypeIdExpression.op_is_aggregate;
case IGCCToken.tTT_is_class:
return IASTTypeIdExpression.op_is_class;
case IGCCToken.tTT_is_empty:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ public boolean isFunctionSet() {
public IType getType() {
switch (fOperator) {
case __is_base_of:
case __is_assignable:
case __is_nothrow_assignable:
case __is_trivially_assignable:
case __is_same:
return CPPBasicType.BOOLEAN;
default:
break;
}
return ProblemType.UNKNOWN_FOR_EXPRESSION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public IType getType() {
switch (fOperator) {
case __is_trivially_constructible:
case __is_constructible:
case __is_nothrow_constructible:
return CPPBasicType.BOOLEAN;
}
return ProblemType.UNKNOWN_FOR_EXPRESSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_constructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_copy;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_unique_object_representations;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_virtual_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_abstract;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_aggregate;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_class;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_empty;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_enum;
Expand Down Expand Up @@ -112,8 +114,10 @@ public boolean isValueDependent() {
case op_has_trivial_constructor:
case op_has_trivial_copy:
case op_has_trivial_destructor:
case op_has_unique_object_representations:
case op_has_virtual_destructor:
case op_is_abstract:
case op_is_aggregate:
case op_is_class:
case op_is_empty:
case op_is_enum:
Expand Down Expand Up @@ -173,8 +177,10 @@ private IType computeType() {
case op_has_trivial_constructor:
case op_has_trivial_copy:
case op_has_trivial_destructor:
case op_has_unique_object_representations:
case op_has_virtual_destructor:
case op_is_abstract:
case op_is_aggregate:
case op_is_class:
case op_is_empty:
case op_is_enum:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2352,7 +2352,7 @@ public Set<String> getSupportedFeatures() {
// missing: is_interface_class
// missing: is_literal
// missing: is_nothrow_assignable
// missing: is_nothrow_constructible
addTypeTraitPrimitive("is_nothrow_constructible", GCCKeywords.cp__is_nothrow_constructible);
// missing: is_nothrow_destructible
addTypeTraitPrimitive("is_pod", GCCKeywords.cp__is_pod);
addTypeTraitPrimitive("is_polymorphic", GCCKeywords.cp__is_polymorphic);
Expand Down

0 comments on commit 8f2342e

Please sign in to comment.