This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 243083
Collapse All | Expand All

(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/csm/Instantiation.java (-92 / +160 lines)
Lines 1327-1335 Link Here
1327
        public boolean isVarArgs() {
1327
        public boolean isVarArgs() {
1328
            return declaration.isVarArgs();
1328
            return declaration.isVarArgs();
1329
        }
1329
        }
1330
    }   
1331
    
1332
    public static CsmType createType(CsmType type, CsmInstantiation instantiation) {
1333
        return createType(type, instantiation, new TemplateParameterResolver());
1330
    }
1334
    }
1331
    
1335
    
1332
    public static CsmType createType(CsmType type, CsmInstantiation instantiation) {
1336
    private static CsmType createType(CsmType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
1333
        if (type == null) {
1337
        if (type == null) {
1334
            throw new NullPointerException("no type for " + instantiation); // NOI18N
1338
            throw new NullPointerException("no type for " + instantiation); // NOI18N
1335
        }
1339
        }
Lines 1341-1438 Link Here
1341
        }
1345
        }
1342
//        System.err.println("Instantiation.createType for " + type + " with instantiation " + instantiation);
1346
//        System.err.println("Instantiation.createType for " + type + " with instantiation " + instantiation);
1343
        if (CsmKindUtilities.isTemplateParameterType(type)) {
1347
        if (CsmKindUtilities.isTemplateParameterType(type)) {
1344
            CsmType instantiatedType = resolveTemplateParameterType(type, instantiation);
1348
            CsmType instantiatedType = templateParamResolver.clone().resolveTemplateParameterType(type, instantiation);
1345
            if (instantiatedType == null || CsmKindUtilities.isTemplateParameterType(instantiatedType)) {
1349
            if (instantiatedType == null || CsmKindUtilities.isTemplateParameterType(instantiatedType)) {
1346
                return new TemplateParameterType(type, instantiation);
1350
                return new TemplateParameterType(type, instantiation, templateParamResolver);
1351
            } else if (instantiatedType instanceof org.netbeans.modules.cnd.modelimpl.csm.NestedType) {
1352
                return new NestedTemplateParameterType(type, instantiation, templateParamResolver);
1347
            }
1353
            }
1348
        }
1354
        }
1349
        if (type instanceof org.netbeans.modules.cnd.modelimpl.csm.NestedType ||
1355
        if (type instanceof NestedTemplateParameterType) {
1350
                type instanceof NestedType) {
1356
            return new NestedTemplateParameterType(type, instantiation, templateParamResolver);
1351
            return new NestedType(type, instantiation);
1357
        }
1358
        if (isNestedType(type)) {
1359
            return new NestedType(type, instantiation, templateParamResolver);
1352
        }
1360
        }
1353
        if (type instanceof DeclTypeImpl || type instanceof Decltype) {
1361
        if (type instanceof DeclTypeImpl || type instanceof Decltype) {
1354
            return new Decltype(type, instantiation);
1362
            return new Decltype(type, instantiation, templateParamResolver);
1355
        }
1363
        }
1356
        if (CsmKindUtilities.isFunctionPointerType(type)) {
1364
        if (CsmKindUtilities.isFunctionPointerType(type)) {
1357
            return new TypeFunPtr((CsmFunctionPointerType) type, instantiation);
1365
            return new TypeFunPtr((CsmFunctionPointerType) type, instantiation, templateParamResolver);
1358
        }
1366
        }
1359
        return new Type(type, instantiation);       
1367
        return new Type(type, instantiation, templateParamResolver);       
1360
    }
1368
    }   
1361
    
1369
    
1362
    public static CsmType createType(CsmType type, List<CsmInstantiation> instantiations) {
1370
    public static CsmType createType(CsmType type, List<CsmInstantiation> instantiations) {
1363
        for (CsmInstantiation instantiation : instantiations) {
1371
        for (CsmInstantiation instantiation : instantiations) {
1364
            type = createType(type, instantiation);
1372
            type = createType(type, instantiation);
1365
        }
1373
        }
1366
        return type;
1374
        return type;
1367
    }
1375
    }            
1368
    
1369
//    public static CsmType resolveTemplateParameterType(CsmType type, Map<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1370
//        if (CsmKindUtilities.isTemplateParameterType(type)) {
1371
//            LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{type.getText(), instantiation.getTemplateDeclaration().getName()});
1372
//            CsmType resolvedType = resolveTemplateParameterType(((CsmTemplateParameterType) type).getParameter(), new MapHierarchy<>(mapping));
1373
//            if (resolvedType != null) {
1374
//                return resolvedType;
1375
//            }            
1376
//        }
1377
//        return type;
1378
//    }    
1379
    
1380
    public static CsmType resolveTemplateParameterType(CsmType type, CsmInstantiation instantiation) {
1381
        if (CsmKindUtilities.isTemplateParameterType(type)) {
1382
            if (LOG.isLoggable(Level.FINE)) {
1383
                LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{type.getText(), instantiation.getTemplateDeclaration().getName()});
1384
            }
1385
            MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping = new MapHierarchy<>(instantiation.getMapping());
1386
            CsmTemplateParameter param = ((CsmTemplateParameterType) type).getParameter();
1387
            if (param != null) {
1388
                CsmType resolvedType = resolveTemplateParameterType(param, mapping);
1389
                if (resolvedType != null) {
1390
                    return resolvedType;
1391
                }
1392
            } else {
1393
                LOG.log(Level.INFO, "no param for " + type + " and \n" + instantiation, new IllegalStateException()); // NOI18N;
1394
            }
1395
        }
1396
        return type;
1397
    }    
1398
    
1399
    public static CsmType resolveTemplateParameterType(CsmTemplateParameter templateParameter, Map<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1400
        return resolveTemplateParameterType(templateParameter, new MapHierarchy<>(mapping));
1401
    }
1402
    
1403
    public static CsmType resolveTemplateParameterType(CsmTemplateParameter templateParameter, MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1404
        CsmSpecializationParameter resolvedParam = resolveTemplateParameter(templateParameter, mapping);
1405
        if (resolvedParam != null && resolvedParam instanceof CsmTypeBasedSpecializationParameter) {
1406
            return ((CsmTypeBasedSpecializationParameter) resolvedParam).getType();
1407
        }
1408
        return null;  
1409
    }
1410
    
1411
    public static CsmSpecializationParameter resolveTemplateParameter(CsmTemplateParameter templateParameter, MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1412
        if (LOG.isLoggable(Level.FINE)) {
1413
            LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{templateParameter.getName(), mapping.size()});
1414
        }
1415
        CsmSpecializationParameter instantiatedType = mapping.get(templateParameter);
1416
        int iteration = MAX_INHERITANCE_DEPTH;
1417
        while (CsmKindUtilities.isTypeBasedSpecalizationParameter(instantiatedType) &&
1418
                CsmKindUtilities.isTemplateParameterType(((CsmTypeBasedSpecializationParameter) instantiatedType).getType()) && iteration != 0) {
1419
            CsmSpecializationParameter nextInstantiatedType = mapping.get(((CsmTemplateParameterType) ((CsmTypeBasedSpecializationParameter) instantiatedType).getType()).getParameter());
1420
            if (nextInstantiatedType != null) {
1421
                instantiatedType = nextInstantiatedType;
1422
            } else {
1423
                break;
1424
            }
1425
            iteration--;
1426
        }
1427
        if (instantiatedType != null && instantiatedType instanceof CsmTypeBasedSpecializationParameter) {
1428
            return instantiatedType;
1429
        }
1430
        return null;  
1431
    }    
1432
   
1376
   
1433
    private static class TemplateParameterType extends Type implements CsmTemplateParameterType {
1377
    private static class TemplateParameterType extends Type implements CsmTemplateParameterType {
1434
        public TemplateParameterType(CsmType type, CsmInstantiation instantiation) {
1378
        public TemplateParameterType(CsmType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
1435
            super(type, instantiation);
1379
            super(type, instantiation, templateParamResolver);
1436
        }
1380
        }
1437
1381
1438
        @Override
1382
        @Override
Lines 1445-1460 Link Here
1445
            return ((CsmTemplateParameterType)instantiatedType).getTemplateType();
1389
            return ((CsmTemplateParameterType)instantiatedType).getTemplateType();
1446
        }
1390
        }
1447
    }
1391
    }
1392
    
1393
    private static class NestedTemplateParameterType extends Type {
1394
        
1395
        public NestedTemplateParameterType(CsmType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
1396
            super(type, instantiation, templateParamResolver);
1397
            
1398
            if (CsmKindUtilities.isTemplateParameterType(type)) {
1399
                if (instantiationHappened() && isNestedType(instantiatedType)) { // paranoia
1400
                    instantiatedType = createType(instantiatedType, instantiation, templateParamResolver);
1401
                }                
1402
            } else if (type instanceof NestedTemplateParameterType) {
1403
                NestedTemplateParameterType prev = (NestedTemplateParameterType) type;
1404
                if (!instantiationHappened() && isNestedType(prev.instantiatedType)) { // paranoia
1405
                    instantiatedType = createType(prev.instantiatedType, instantiation, templateParamResolver);
1406
                }
1407
            }
1408
        }        
1409
    }
1448
1410
1449
    private static class Type implements CsmType, Resolver.SafeTemplateBasedProvider {
1411
    private static class Type implements CsmType, Resolver.SafeTemplateBasedProvider {
1450
        protected final CsmType originalType;
1412
        protected final CsmType originalType;
1451
        protected final CsmInstantiation instantiation;
1413
        protected final CsmInstantiation instantiation;
1452
        protected final CsmType instantiatedType;
1453
        protected final boolean inst;
1414
        protected final boolean inst;
1415
        protected CsmType instantiatedType;
1454
        protected CsmClassifier resolved;
1416
        protected CsmClassifier resolved;
1455
        protected CsmTemplateParameter parameter;
1417
        protected CsmTemplateParameter parameter;
1456
1418
1457
        private Type(CsmType type, CsmInstantiation instantiation) {
1419
        private Type(CsmType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
1458
            this.instantiation = instantiation;
1420
            this.instantiation = instantiation;
1459
            inst = type.isInstantiation();
1421
            inst = type.isInstantiation();
1460
            CsmType origType = type;
1422
            CsmType origType = type;
Lines 1464-1470 Link Here
1464
            if (CsmKindUtilities.isTemplateParameterType(type)) {
1426
            if (CsmKindUtilities.isTemplateParameterType(type)) {
1465
                CsmTemplateParameterType paramType = (CsmTemplateParameterType)type;
1427
                CsmTemplateParameterType paramType = (CsmTemplateParameterType)type;
1466
                parameter = paramType.getParameter();
1428
                parameter = paramType.getParameter();
1467
                newType = Instantiation.resolveTemplateParameterType(type, instantiation);
1429
                newType = templateParamResolver.resolveTemplateParameterType(type, instantiation);
1468
                if (newType != null) {
1430
                if (newType != null) {
1469
                    int pointerDepth = (newType != origType ? newType.getPointerDepth() + origType.getPointerDepth() : origType.getPointerDepth());
1431
                    int pointerDepth = (newType != origType ? newType.getPointerDepth() + origType.getPointerDepth() : origType.getPointerDepth());
1470
                    int arrayDepth = (newType != origType ? newType.getArrayDepth() + origType.getArrayDepth() : origType.getArrayDepth());
1432
                    int arrayDepth = (newType != origType ? newType.getArrayDepth() + origType.getArrayDepth() : origType.getArrayDepth());
Lines 1498-1507 Link Here
1498
                        }
1460
                        }
1499
                    }
1461
                    }
1500
                    
1462
                    
1501
                    if (newType instanceof org.netbeans.modules.cnd.modelimpl.csm.NestedType) {
1502
                        newType = createType(newType, instantiation);
1503
                    }
1504
                    
1505
                    origType = paramType.getTemplateType();
1463
                    origType = paramType.getTemplateType();
1506
                } else {
1464
                } else {
1507
                    newType = type;
1465
                    newType = type;
Lines 1886-1893 Link Here
1886
    
1844
    
1887
    private static class TypeFunPtr extends Type implements CsmFunctionPointerType {
1845
    private static class TypeFunPtr extends Type implements CsmFunctionPointerType {
1888
1846
1889
        public TypeFunPtr(CsmFunctionPointerType type, CsmInstantiation instantiation) {
1847
        public TypeFunPtr(CsmFunctionPointerType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
1890
            super((CsmType)type, instantiation);
1848
            super((CsmType)type, instantiation, templateParamResolver);
1891
        }
1849
        }
1892
1850
1893
        @Override
1851
        @Override
Lines 1913-1920 Link Here
1913
    
1871
    
1914
    private static class Decltype extends Type {
1872
    private static class Decltype extends Type {
1915
1873
1916
        public Decltype(CsmType type, CsmInstantiation instantiation) {
1874
        public Decltype(CsmType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
1917
            super(type, instantiation);
1875
            super(type, instantiation, templateParamResolver);
1918
        }
1876
        }
1919
1877
1920
        @Override
1878
        @Override
Lines 2002-2015 Link Here
2002
1960
2003
        private final CsmType parentType;
1961
        private final CsmType parentType;
2004
1962
2005
        private NestedType(CsmType type, CsmInstantiation instantiation) {
1963
        private NestedType(CsmType type, CsmInstantiation instantiation, TemplateParameterResolver templateParamResolver) {
2006
            super(type, instantiation);
1964
            super(type, instantiation, templateParamResolver);
2007
1965
2008
            if (type instanceof org.netbeans.modules.cnd.modelimpl.csm.NestedType) {
1966
            if (type instanceof org.netbeans.modules.cnd.modelimpl.csm.NestedType) {
2009
                org.netbeans.modules.cnd.modelimpl.csm.NestedType t = (org.netbeans.modules.cnd.modelimpl.csm.NestedType) type;
1967
                org.netbeans.modules.cnd.modelimpl.csm.NestedType t = (org.netbeans.modules.cnd.modelimpl.csm.NestedType) type;
2010
                CsmType parent = t.getParent();
1968
                CsmType parent = t.getParent();
2011
                if (parent != null) {
1969
                if (parent != null) {
2012
                    parentType = createType(parent, instantiation);
1970
                    parentType = createType(parent, instantiation, templateParamResolver);
2013
                } else {
1971
                } else {
2014
                    parentType = null;
1972
                    parentType = null;
2015
                }
1973
                }
Lines 2017-2023 Link Here
2017
                NestedType t = (NestedType) type;
1975
                NestedType t = (NestedType) type;
2018
                CsmType parent = t.parentType;
1976
                CsmType parent = t.parentType;
2019
                if (parent != null) {
1977
                if (parent != null) {
2020
                    parentType = createType(parent, instantiation);
1978
                    parentType = createType(parent, instantiation, templateParamResolver);
2021
                } else {
1979
                } else {
2022
                    parentType = null;
1980
                    parentType = null;
2023
                }
1981
                }
Lines 2141-2146 Link Here
2141
    private static CsmClassifier getNestedClassifier(MemberResolverImpl memberResolver, CsmClassifier parentClassifier, CharSequence ownText) {
2099
    private static CsmClassifier getNestedClassifier(MemberResolverImpl memberResolver, CsmClassifier parentClassifier, CharSequence ownText) {
2142
        return org.netbeans.modules.cnd.modelimpl.csm.NestedType.getNestedClassifier(memberResolver, parentClassifier, ownText);
2100
        return org.netbeans.modules.cnd.modelimpl.csm.NestedType.getNestedClassifier(memberResolver, parentClassifier, ownText);
2143
    }
2101
    }
2102
    
2103
    private static boolean isNestedType(CsmType type) {
2104
        return type instanceof org.netbeans.modules.cnd.modelimpl.csm.NestedType ||
2105
               type instanceof NestedType; 
2106
    }
2144
2107
2145
    public final static class InstantiationSelfUID implements CsmUID<CsmInstantiation>, SelfPersistent {
2108
    public final static class InstantiationSelfUID implements CsmUID<CsmInstantiation>, SelfPersistent {
2146
        private final Instantiation ref;
2109
        private final Instantiation ref;
Lines 2204-2207 Link Here
2204
        TemplateUtils.addGREATERTHAN(sb);
2167
        TemplateUtils.addGREATERTHAN(sb);
2205
        return sb;
2168
        return sb;
2206
    }
2169
    }
2170
    
2171
    public static CsmSpecializationParameter resolveTemplateParameter(CsmTemplateParameter templateParameter, MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
2172
        return new TemplateParameterResolver().resolveTemplateParameter(templateParameter, mapping);
2173
    }
2174
    
2175
    private static class TemplateParameterResolver implements Cloneable {
2176
        
2177
        private static final int RESOLVED_LIMIT = 32;
2178
        
2179
        // Technically it is enough to store one last parameter, 
2180
        // but to be extra sure that there is no infinite recursion here,
2181
        // we store all of them and also set restriction on number of resolved 
2182
        // parameters per instantiation of one type
2183
        private final Set<CsmTemplateParameter> lastResolvedParameters = new HashSet<>();
2184
2185
        public TemplateParameterResolver() {
2186
            this(Collections.<CsmTemplateParameter>emptySet());
2187
        }
2188
2189
        public TemplateParameterResolver(Set<CsmTemplateParameter> lastResolvedParameters) {
2190
            this.lastResolvedParameters.addAll(lastResolvedParameters);
2191
        }
2192
        
2193
        public CsmType resolveTemplateParameterType(CsmType type, CsmInstantiation instantiation) {
2194
            if (CsmKindUtilities.isTemplateParameterType(type)) {
2195
                if (LOG.isLoggable(Level.FINE)) {
2196
                    LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{type.getText(), instantiation.getTemplateDeclaration().getName()});
2197
                }
2198
                MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping = new MapHierarchy<>(instantiation.getMapping());
2199
                CsmTemplateParameter param = ((CsmTemplateParameterType) type).getParameter();
2200
                if (param != null) {
2201
                    CsmType resolvedType = resolveTemplateParameterType(param, mapping);
2202
                    if (resolvedType != null) {
2203
                        return resolvedType;
2204
                    }
2205
                } else {
2206
                    LOG.log(Level.INFO, "no param for " + type + " and \n" + instantiation, new IllegalStateException()); // NOI18N;
2207
                }
2208
            }
2209
            return type;
2210
        }    
2211
2212
        public CsmType resolveTemplateParameterType(CsmTemplateParameter templateParameter, MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
2213
            CsmSpecializationParameter resolvedParam = resolveTemplateParameter(templateParameter, mapping);
2214
            if (resolvedParam != null && resolvedParam instanceof CsmTypeBasedSpecializationParameter) {
2215
                return ((CsmTypeBasedSpecializationParameter) resolvedParam).getType();
2216
            }
2217
            return null;  
2218
        }    
2219
2220
        public CsmSpecializationParameter resolveTemplateParameter(CsmTemplateParameter templateParameter, MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
2221
            if (LOG.isLoggable(Level.FINE)) {
2222
                LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{templateParameter.getName(), mapping.size()});
2223
            }
2224
            if (lastResolvedParameters.size() > RESOLVED_LIMIT) {
2225
                return null; // probably infinite recursion
2226
            }
2227
            CsmSpecializationParameter instantiatedType = mapping.get(templateParameter);
2228
            int iteration = MAX_INHERITANCE_DEPTH;
2229
            while (CsmKindUtilities.isTypeBasedSpecalizationParameter(instantiatedType) &&
2230
                    CsmKindUtilities.isTemplateParameterType(((CsmTypeBasedSpecializationParameter) instantiatedType).getType()) && iteration != 0) {
2231
                CsmTemplateParameter nextTemplateParameter = ((CsmTemplateParameterType) ((CsmTypeBasedSpecializationParameter) instantiatedType).getType()).getParameter();
2232
                CsmSpecializationParameter nextInstantiatedType = mapping.get(templateParameter);
2233
                if (nextInstantiatedType != null) {
2234
                    instantiatedType = nextInstantiatedType;
2235
                    templateParameter = nextTemplateParameter;
2236
                } else {
2237
                    break;
2238
                }
2239
                iteration--;
2240
            }
2241
            if (instantiatedType != null && instantiatedType instanceof CsmTypeBasedSpecializationParameter) {
2242
                for (CsmTemplateParameter alreadyResolvedParam : lastResolvedParameters) {
2243
                    if (alreadyResolvedParam.getScope() == templateParameter.getScope()) {
2244
                        if (alreadyResolvedParam.getStartOffset() <= templateParameter.getStartOffset()) {
2245
                            // This prevent us from infinite instantiation of type
2246
                            //
2247
                            // Example: resolving 'next::type' never finishes because 
2248
                            // we instantiate type T with instantiation of AAA 
2249
                            // where T = T::next
2250
                            //
2251
                            // template <typename T>
2252
                            // struct AAA {
2253
                            //   typedef AAA<T::next> next;
2254
                            //   typedef T type;
2255
                            // }
2256
                            //
2257
                            // TODO: it is better to use not lastResolvedParameters, 
2258
                            // but just iterator instead of mapping to make sure that we
2259
                            // never go backward.                            
2260
                            return null;
2261
                        }
2262
                    }
2263
                }
2264
                lastResolvedParameters.add(templateParameter);
2265
                return instantiatedType;                
2266
            }
2267
            return null;  
2268
        }       
2269
2270
        @Override
2271
        protected TemplateParameterResolver clone() {
2272
            return new TemplateParameterResolver(lastResolvedParameters);
2273
        }
2274
    }        
2207
}
2275
}

Return to bug 243083