Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHannes Vogt2019-09-13 17:44:17 -0400
committerNathan Ridge2019-09-26 00:12:11 -0400
commit944ec0e06edcf5869b5af2322da7dd863f3b2dc9 (patch)
treea9e6d9577006fbd96afb5fb80028f2f4f0ccb8a8
parentaee38fb062025cf9d0d74c0d4f69bd05f064805c (diff)
downloadorg.eclipse.cdt-944ec0e06edcf5869b5af2322da7dd863f3b2dc9.tar.gz
org.eclipse.cdt-944ec0e06edcf5869b5af2322da7dd863f3b2dc9.tar.xz
org.eclipse.cdt-944ec0e06edcf5869b5af2322da7dd863f3b2dc9.zip
Bug 549367 - [C++17] Aggregate init of base
Implements http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0017r1.html: Types with non-private, non-protected, non-virtual base classes can be aggregate initialized. Change-Id: Idad341d45d6aaf1d8c36691cf8d7bc7cd049e28b Signed-off-by: Hannes Vogt <hannes@havogt.de>
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx17/CXX17ExtensionsTests.java34
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java10
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/TypeTraits.java11
3 files changed, 53 insertions, 2 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx17/CXX17ExtensionsTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx17/CXX17ExtensionsTests.java
new file mode 100644
index 0000000000..b91cb51f6b
--- /dev/null
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx17/CXX17ExtensionsTests.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+package org.eclipse.cdt.core.parser.tests.ast2.cxx17;
+
+import org.eclipse.cdt.core.parser.tests.ast2.AST2CPPTestBase;
+
+import junit.framework.TestSuite;
+
+public class CXX17ExtensionsTests extends AST2CPPTestBase {
+
+ public static TestSuite suite() {
+ return suite(CXX17ExtensionsTests.class);
+ }
+
+ // struct Base {
+ // int foo;
+ // };
+ // struct MyStruct : public Base {
+ // int a;
+ // };
+ //
+ // int main() {
+ // MyStruct test = { {0}, 9 };
+ // }
+ public void testAggregateInitializationOfBaseClass_549367() throws Exception {
+ parseAndCheckImplicitNameBindings();
+ }
+}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java
index 71790051d2..49798f6f32 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java
@@ -14,6 +14,7 @@ import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
@@ -131,6 +132,15 @@ class AggregateInitialization {
*/
private Cost checkInitializationOfElements(IType type, Cost worstCost) throws DOMException {
if (type instanceof ICPPClassType && isAggregate(type)) {
+ ICPPBase[] bases = ((ICPPClassType) type).getBases();
+ for (ICPPBase base : bases) {
+ Cost cost = checkElement(base.getBaseClassType(), null, worstCost);
+ if (!cost.converts())
+ return cost;
+ if (cost.compareTo(worstCost) > 0) {
+ worstCost = cost;
+ }
+ }
ICPPField[] fields = getFieldsForAggregateInitialization((ICPPClassType) type);
for (ICPPField field : fields) {
Cost cost = checkElement(field.getType(), field.getInitialValue(), worstCost);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/TypeTraits.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/TypeTraits.java
index cbba16a0c7..07b7aee8e9 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/TypeTraits.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/TypeTraits.java
@@ -271,8 +271,15 @@ public class TypeTraits {
// 8.1.5.1 p.2 (N4659): The closure type is not an aggregate type.
if (classType instanceof CPPClosureType)
return false;
- if (classType.getBases().length > 0)
- return false;
+ if (classType.getBases().length > 0) {
+ // c++17 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0017r1.html
+ for (ICPPBase base : classType.getBases()) {
+ if (base.isVirtual())
+ return false;
+ if (base.getVisibility() == ICPPBase.v_private || base.getVisibility() == ICPPBase.v_protected)
+ return false;
+ }
+ }
ICPPMethod[] methods = classType.getDeclaredMethods();
for (ICPPMethod m : methods) {
if (m instanceof ICPPConstructor)

Back to the top