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 235102
Collapse All | Expand All

(-)a/cnd.api.model/src/org/netbeans/modules/cnd/api/model/services/CsmTypes.java (-6 / +31 lines)
Lines 69-75 Link Here
69
    }
69
    }
70
70
71
    public static CsmType createSimpleType(CsmClassifier cls, OffsetDescriptor offs) {
71
    public static CsmType createSimpleType(CsmClassifier cls, OffsetDescriptor offs) {
72
        return getProvider().createType(cls, new TypeDescriptor(false, false, 0, 0), offs);
72
        return getProvider().createType(cls, new TypeDescriptor(false, 0, 0, 0), offs);
73
    }
73
    }
74
74
75
    /**
75
    /**
Lines 78-84 Link Here
78
     * @return new type
78
     * @return new type
79
     */
79
     */
80
    public static CsmType createConstType(CsmType orig) {
80
    public static CsmType createConstType(CsmType orig) {
81
        return getProvider().createType(orig, new TypeDescriptor(true, orig.isReference(), orig.getPointerDepth(), orig.getArrayDepth()));
81
        return getProvider().createType(orig, new TypeDescriptor(true, TypeDescriptor.getReferenceType(orig), orig.getPointerDepth(), orig.getArrayDepth()));
82
    }
82
    }
83
83
84
    /**
84
    /**
Lines 95-101 Link Here
95
        } else {
95
        } else {
96
            arrDepth = Math.max(arrDepth - 1, 0);
96
            arrDepth = Math.max(arrDepth - 1, 0);
97
        }
97
        }
98
        return getProvider().createType(type, new TypeDescriptor(type.isConst(), type.isReference(), ptrDepth, arrDepth));
98
        return getProvider().createType(type, new TypeDescriptor(type.isConst(), TypeDescriptor.getReferenceType(type), ptrDepth, arrDepth));
99
    }
99
    }
100
100
101
    //@Immutable
101
    //@Immutable
Lines 125-136 Link Here
125
125
126
    //@Immutable
126
    //@Immutable
127
    public static final class TypeDescriptor {
127
    public static final class TypeDescriptor {
128
        
129
        public static final int NON_REFERENCE = 0;
130
        
131
        public static final int REFERENCE = 1;
132
        
133
        public static final int RVALUE_REFERENCE = 2;
134
        
135
        public static int getReferenceType(CsmType type) {
136
            if (type.isRValueReference()) {
137
                return RVALUE_REFERENCE;
138
            } else if (type.isReference()) {
139
                return REFERENCE;
140
            }
141
            return NON_REFERENCE;
142
        }
143
        
144
        public static int getReferenceType(TypeDescriptor td) {
145
            return td._reference;
146
        }
147
148
        
128
        private final boolean _const;
149
        private final boolean _const;
129
        private final boolean _reference;
150
        private final int _reference;
130
        private final int _ptrDepth;
151
        private final int _ptrDepth;
131
        private final int _arrDepth;
152
        private final int _arrDepth;
132
153
133
        public TypeDescriptor(boolean _const, boolean _reference, int _ptrDepth, int _arrDepth) {
154
        public TypeDescriptor(boolean _const, int _reference, int _ptrDepth, int _arrDepth) {
134
            this._const = _const;
155
            this._const = _const;
135
            this._reference = _reference;
156
            this._reference = _reference;
136
            this._ptrDepth = _ptrDepth;
157
            this._ptrDepth = _ptrDepth;
Lines 150-156 Link Here
150
        }
171
        }
151
172
152
        public boolean isReference() {
173
        public boolean isReference() {
153
            return _reference;
174
            return _reference > 0;
175
        }
176
        
177
        public boolean isRValueReference() {
178
            return _reference > 1;
154
        }
179
        }
155
    }
180
    }
156
181
(-)a/cnd.completion/src/org/netbeans/modules/cnd/completion/cplusplus/ext/CompletionSupport.java (-7 / +393 lines)
Lines 65-79 Link Here
65
import org.netbeans.cnd.api.lexer.CppTokenId;
65
import org.netbeans.cnd.api.lexer.CppTokenId;
66
import org.netbeans.modules.cnd.api.model.CsmClass;
66
import org.netbeans.modules.cnd.api.model.CsmClass;
67
import org.netbeans.modules.cnd.api.model.CsmClassifier;
67
import org.netbeans.modules.cnd.api.model.CsmClassifier;
68
import org.netbeans.modules.cnd.api.model.CsmConstructor;
68
import org.netbeans.modules.cnd.api.model.CsmDeclaration;
69
import org.netbeans.modules.cnd.api.model.CsmDeclaration;
69
import org.netbeans.modules.cnd.api.model.CsmField;
70
import org.netbeans.modules.cnd.api.model.CsmField;
70
import org.netbeans.modules.cnd.api.model.CsmFile;
71
import org.netbeans.modules.cnd.api.model.CsmFile;
71
import org.netbeans.modules.cnd.api.model.CsmFunction;
72
import org.netbeans.modules.cnd.api.model.CsmFunction;
72
import org.netbeans.modules.cnd.api.model.CsmFunctional;
73
import org.netbeans.modules.cnd.api.model.CsmFunctional;
73
import org.netbeans.modules.cnd.api.model.CsmMember;
74
import org.netbeans.modules.cnd.api.model.CsmMember;
75
import org.netbeans.modules.cnd.api.model.CsmObject;
74
import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
76
import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
75
import org.netbeans.modules.cnd.api.model.CsmParameter;
77
import org.netbeans.modules.cnd.api.model.CsmParameter;
78
import org.netbeans.modules.cnd.api.model.CsmSpecializationParameter;
76
import org.netbeans.modules.cnd.api.model.CsmTemplate;
79
import org.netbeans.modules.cnd.api.model.CsmTemplate;
80
import org.netbeans.modules.cnd.api.model.CsmTemplateParameter;
77
import org.netbeans.modules.cnd.api.model.CsmType;
81
import org.netbeans.modules.cnd.api.model.CsmType;
78
import org.netbeans.modules.cnd.api.model.CsmVariable;
82
import org.netbeans.modules.cnd.api.model.CsmVariable;
79
import org.netbeans.modules.cnd.api.model.deep.CsmReturnStatement;
83
import org.netbeans.modules.cnd.api.model.deep.CsmReturnStatement;
Lines 81-100 Link Here
81
import org.netbeans.modules.cnd.api.model.deep.CsmStatement.Kind;
85
import org.netbeans.modules.cnd.api.model.deep.CsmStatement.Kind;
82
import org.netbeans.modules.cnd.api.model.services.CsmClassifierResolver;
86
import org.netbeans.modules.cnd.api.model.services.CsmClassifierResolver;
83
import org.netbeans.modules.cnd.api.model.services.CsmInheritanceUtilities;
87
import org.netbeans.modules.cnd.api.model.services.CsmInheritanceUtilities;
88
import org.netbeans.modules.cnd.api.model.services.CsmInstantiationProvider;
84
import org.netbeans.modules.cnd.api.model.services.CsmMacroExpansion;
89
import org.netbeans.modules.cnd.api.model.services.CsmMacroExpansion;
90
import org.netbeans.modules.cnd.api.model.services.CsmTypes;
91
import org.netbeans.modules.cnd.api.model.util.CsmBaseUtilities;
85
import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
92
import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
86
import org.netbeans.modules.cnd.api.model.util.UIDs;
93
import org.netbeans.modules.cnd.api.model.util.UIDs;
87
import org.netbeans.modules.cnd.completion.cplusplus.CsmFinderFactory;
94
import org.netbeans.modules.cnd.completion.cplusplus.CsmFinderFactory;
95
import org.netbeans.modules.cnd.completion.cplusplus.ext.CsmCompletionQuery.Context;
88
import org.netbeans.modules.cnd.completion.csm.CompletionUtilities;
96
import org.netbeans.modules.cnd.completion.csm.CompletionUtilities;
89
import org.netbeans.modules.cnd.completion.csm.CsmContext;
97
import org.netbeans.modules.cnd.completion.csm.CsmContext;
90
import org.netbeans.modules.cnd.completion.csm.CsmContextUtilities;
98
import org.netbeans.modules.cnd.completion.csm.CsmContextUtilities;
91
import org.netbeans.modules.cnd.completion.csm.CsmOffsetResolver;
99
import org.netbeans.modules.cnd.completion.csm.CsmOffsetResolver;
92
import org.netbeans.modules.cnd.completion.csm.CsmOffsetUtilities;
100
import org.netbeans.modules.cnd.completion.csm.CsmOffsetUtilities;
93
import org.netbeans.modules.cnd.completion.impl.xref.FileReferencesContext;
101
import org.netbeans.modules.cnd.completion.impl.xref.FileReferencesContext;
102
import org.netbeans.modules.cnd.modelutil.CsmUtilities;
94
import org.netbeans.modules.editor.NbEditorUtilities;
103
import org.netbeans.modules.editor.NbEditorUtilities;
104
import org.netbeans.spi.editor.completion.CompletionItem;
95
import org.netbeans.spi.editor.completion.CompletionProvider;
105
import org.netbeans.spi.editor.completion.CompletionProvider;
96
import org.openide.filesystems.FileObject;
106
import org.openide.filesystems.FileObject;
97
import org.openide.loaders.DataObject;
107
import org.openide.loaders.DataObject;
108
import org.openide.util.Pair;
98
109
99
/**
110
/**
100
 *
111
 *
Lines 378-392 Link Here
378
    /** Filter the list of the methods (usually returned from
389
    /** Filter the list of the methods (usually returned from
379
     * Finder.findMethods()) or the list of the constructors
390
     * Finder.findMethods()) or the list of the constructors
380
     * by the given parameter specification.
391
     * by the given parameter specification.
392
     * @param ctx - completion context
381
     * @param methodList list of the methods. They should have the same
393
     * @param methodList list of the methods. They should have the same
382
     *   name but in fact they don't have to.
394
     *   name but in fact they don't have to.
395
     * @param exp - instantiation params
383
     * @param parmTypes parameter types specification. If set to null, no filtering
396
     * @param parmTypes parameter types specification. If set to null, no filtering
384
     *   is performed and the same list is returned. If a particular
397
     *   is performed and the same list is returned. If a particular
385
     * @param acceptMoreParameters useful for code completion to get
398
     * @param acceptMoreParameters useful for code completion to get
386
     *   even the methods with more parameters.
399
     *   even the methods with more parameters.
387
     */
400
     */
388
    public static <T extends CsmFunctional> Collection<T> filterMethods(Collection<T> methodList, List parmTypeList,
401
    static <T extends CsmFunctional> Collection<T> filterMethods(Context ctx, 
389
            boolean acceptMoreParameters, boolean acceptIfSameNumberParams) {
402
                                                                 Collection<T> methodList, 
403
                                                                 CsmCompletionExpression exp,
404
                                                                 List parmTypeList,
405
                                                                 boolean acceptMoreParameters, 
406
                                                                 boolean acceptIfSameNumberParams
407
    ) {
390
        Collection<T> result = filterMethods(methodList, parmTypeList, acceptMoreParameters, acceptIfSameNumberParams, false);
408
        Collection<T> result = filterMethods(methodList, parmTypeList, acceptMoreParameters, acceptIfSameNumberParams, false);
391
        if (result.size() > 1) {
409
        if (result.size() > 1) {
392
            // it seems that this call couldn't filter anything
410
            // it seems that this call couldn't filter anything
Lines 394-400 Link Here
394
            
412
            
395
            // perform more accurate filtering if it is a strict request (for navigation probably)
413
            // perform more accurate filtering if it is a strict request (for navigation probably)
396
            if (!acceptMoreParameters && acceptIfSameNumberParams) {
414
            if (!acceptMoreParameters && acceptIfSameNumberParams) {
397
                result = accurateFilterMethods(result, parmTypeList);
415
                result = accurateFilterMethods(ctx, result, exp, parmTypeList);
398
            }
416
            }
399
        }
417
        }
400
        return result;
418
        return result;
Lines 490-501 Link Here
490
     * Please note: This method is designed to be called after preliminary filtering.
508
     * Please note: This method is designed to be called after preliminary filtering.
491
     *              But this is not a necessary requirement.
509
     *              But this is not a necessary requirement.
492
     * 
510
     * 
511
     * @param ctx - completion context
493
     * @param methods
512
     * @param methods
494
     * @param paramTypes
513
     * @param paramTypes
495
     * 
514
     * 
496
     * @return candidates
515
     * @return candidates
497
     */
516
     */
498
    private static <T extends CsmFunctional> Collection<T> accurateFilterMethods(Collection<T> methods, List paramTypes) {
517
    private static <T extends CsmFunctional> Collection<T> accurateFilterMethods(Context ctx, Collection<T> methods, CsmCompletionExpression exp, List paramTypes) {
499
        if (methods.size() <= 1) {
518
        if (methods.size() <= 1) {
500
            return methods;
519
            return methods;
501
        }
520
        }
Lines 551-558 Link Here
551
                    break;
570
                    break;
552
                }
571
                }
553
            }
572
            }
554
            prev = candidate;
573
            
555
            result.add(candidate.function);
574
            boolean validCandidate = true;
575
            
576
            if (CsmKindUtilities.isTemplate(candidate.function)) {
577
                // we should check if this function is viable
578
                CsmType retType = extractFunctionType(ctx, Arrays.asList(candidate.function), exp, paramTypes);
579
                CsmClassifier cls = retType != null ? CsmBaseUtilities.getClassifier(retType, ctx.getContextFile(), ctx.getEndOffset(), true) : null;
580
                validCandidate = (cls != null && cls.isValid());
581
            }
582
583
            if (validCandidate) {
584
                prev = candidate;
585
                result.add(candidate.function);
586
            }
556
        }
587
        }
557
        
588
        
558
        return result.isEmpty() ? methods : result;
589
        return result.isEmpty() ? methods : result;
Lines 906-911 Link Here
906
        }
937
        }
907
        return false;
938
        return false;
908
    }
939
    }
940
    
941
    static CsmType extractFunctionType(Context context, Collection<? extends CsmFunctional> mtdList, CsmCompletionExpression genericNameExp, List<CsmType> typeList) {
942
        CsmType out = null;
943
        if (mtdList.isEmpty()) {
944
            return null;
945
        }
946
        for (CsmFunctional fun : mtdList) {
947
            CsmObject entity = fun;
948
949
            if (CsmKindUtilities.isConstructor(entity)) {
950
                entity = ((CsmConstructor) entity).getContainingClass();
951
            }
952
953
            if (CsmKindUtilities.isTemplate(entity)) {
954
                CsmObject inst = createInstantiation(context, (CsmTemplate) entity, genericNameExp, typeList);                    
955
                if (CsmKindUtilities.isFunction(inst) || CsmKindUtilities.isClassifier(inst)) {
956
                    entity = inst;
957
                }
958
            }
959
            
960
            if (CsmKindUtilities.isFunctional(entity)) {
961
                out = ((CsmFunctional) entity).getReturnType();
962
            } else if (CsmKindUtilities.isClassifier(entity)) {
963
                out = CsmCompletion.createType((CsmClassifier) entity, 0, 0, 0, false);
964
            }
965
            if (out != null) {
966
                break;
967
            }
968
        }
969
        return out;
970
    }        
971
    
972
    static CsmObject createInstantiation(Context context, CsmTemplate template, CsmCompletionExpression exp, List<CsmType> typeList) {
973
        if (exp != null || !typeList.isEmpty()) {
974
            CsmInstantiationProvider ip = CsmInstantiationProvider.getDefault();
975
            List<CsmSpecializationParameter> params = new ArrayList<CsmSpecializationParameter>();        
976
            params.addAll(collectInstantiationParameters(context, ip, exp));
977
            if (CsmKindUtilities.isFunction(template)) {
978
                params.addAll(collectInstantiationParameters(ip, (CsmFunction)template, params.size(), typeList));
979
            }
980
            if (!params.isEmpty()) {
981
                return ip.instantiate(template, params);
982
            }
983
        }
984
        return null;
985
    }   
986
    
987
    static List<CsmSpecializationParameter> collectInstantiationParameters(Context context, CsmInstantiationProvider ip, CsmCompletionExpression exp) {
988
        if (exp != null) {
989
            List<CsmSpecializationParameter> params = new ArrayList<CsmSpecializationParameter>();
990
            if (exp.getExpID() == CsmCompletionExpression.GENERIC_TYPE) {
991
                int paramsNumber = exp.getParameterCount() - 1;
992
                for (int i = 0; i < paramsNumber; i++) {
993
                    CsmCompletionExpression paramInst = exp.getParameter(i + 1);
994
                    if (paramInst != null) {
995
                        switch (paramInst.getExpID()) {
996
                            case CsmCompletionExpression.CONSTANT:
997
                                params.add(ip.createExpressionBasedSpecializationParameter(paramInst.getTokenText(0),
998
                                        context.getContextFile(), paramInst.getTokenOffset(0), paramInst.getTokenOffset(0) + paramInst.getTokenLength(0)));
999
                                break;
1000
                            default:
1001
                                CsmType type = null;
1002
                                
1003
                                if (!isExpression(paramInst)) {
1004
                                    type = context.resolveType(paramInst);                                
1005
                                    if (type != null) {
1006
                                        List<? extends CompletionItem> candidates = context.resolveNextExp(paramInst);
1007
                                        if (candidates != null && (candidates.get(0) instanceof CsmResultItem.VariableResultItem)) {
1008
                                            type = null;
1009
                                        }
1010
                                    }
1011
                                }
1012
                                
1013
                                if (type != null) {
1014
                                    params.add(ip.createTypeBasedSpecializationParameter(type));
1015
                                } else {
1016
                                    RenderedExpression renderedExpression = renderExpression(paramInst);
1017
                                    params.add(ip.createExpressionBasedSpecializationParameter(
1018
                                            renderedExpression.text,
1019
                                            context.getContextFile(), 
1020
                                            renderedExpression.startOffset, 
1021
                                            renderedExpression.endOffset
1022
                                    ));
1023
                                }
1024
                            }
1025
                    } else {
1026
                        break;
1027
                    }
1028
                }
1029
            }
1030
            return params;
1031
        }
1032
        return Collections.emptyList();
1033
    }
1034
    
1035
    static List<CsmSpecializationParameter> collectInstantiationParameters(CsmInstantiationProvider ip, CsmFunction function, int explicitelyMappedSize, List<CsmType> typeList) {            
1036
        if (CsmKindUtilities.isTemplate(function)) {
1037
            List<CsmSpecializationParameter> result = new ArrayList<CsmSpecializationParameter>();
1038
            
1039
            CsmTemplate template = (CsmTemplate) function;                
1040
            List<CsmTemplateParameter> templateParams = template.getTemplateParameters();
1041
            
1042
            if (templateParams.size() > explicitelyMappedSize) {
1043
                Map<CsmTemplateParameter, CsmType> paramsMap = gatherTemplateParamsMap(function, typeList);
1044
                
1045
                for (int i = explicitelyMappedSize; i < templateParams.size(); i++) {
1046
                    CsmTemplateParameter param = templateParams.get(i);
1047
                    CsmType mappedType = paramsMap.get(param);
1048
                    if (mappedType != null) {
1049
                        result.add(ip.createTypeBasedSpecializationParameter(mappedType));
1050
                    } else {
1051
                        // error
1052
                        return result;
1053
                    }
1054
                }
1055
            }
1056
            
1057
            return result;
1058
        }
1059
        return Collections.emptyList();
1060
    }
1061
    
1062
    static Map<CsmTemplateParameter, CsmType> gatherTemplateParamsMap(CsmFunction function, List<CsmType> typeList) {
1063
        assert CsmKindUtilities.isTemplate(function) : "Attempt to gather template parameters map from non-template function"; // NOI18N
1064
        CsmTemplate template = (CsmTemplate) function;
1065
        
1066
        Map<CsmTemplateParameter, CsmType> map = new HashMap<CsmTemplateParameter, CsmType>();
1067
        
1068
        for (CsmTemplateParameter templateParam : template.getTemplateParameters()) {
1069
            int paramIndex = 0;                
1070
            for (CsmParameter param : function.getParameters()) {
1071
                if (paramIndex >= typeList.size()) {
1072
                    break;
1073
                }
1074
                CsmType paramType = param.getType();
1075
                CsmType calculatedType = calcTypeFromParameter(templateParam, paramType, typeList.get(paramIndex));
1076
                if (calculatedType != null) {
1077
                     map.put(templateParam, calculatedType);
1078
                }
1079
                paramIndex++;
1080
            }
1081
        }
1082
        
1083
        return map;
1084
    }
1085
    
1086
    static CsmType calcTypeFromParameter(CsmTemplateParameter templateParam, CsmType paramType, CsmType passedType) {
1087
        if (paramType != null) {
1088
            CsmClassifier cls = paramType.getClassifier();
1089
            if (cls != null && cls.getQualifiedName().toString().equals(templateParam.getQualifiedName().toString())) {
1090
                boolean hasChanges = false;
1091
                
1092
                boolean newConst = false;
1093
                int newReference = CsmTypes.TypeDescriptor.NON_REFERENCE;
1094
                int newPtrDepth = 0;
1095
                int newArrayDepth = 0;
1096
                
1097
                CsmType underlyingType = CsmUtilities.iterateTypeChain(passedType, new CsmUtilities.ConstantPredicate<CsmType>(false));
1098
                
1099
                if (paramType.isConst()) {
1100
                    if (underlyingType.isConst()) {
1101
                        hasChanges = true;
1102
                        newConst = false;
1103
                    } else {
1104
                        return null;
1105
                    }
1106
                }
1107
                
1108
                if (paramType.isPointer()) {
1109
                    if (paramType.getPointerDepth() <= underlyingType.getPointerDepth()) {
1110
                        hasChanges = true;
1111
                        newPtrDepth = underlyingType.getPointerDepth() - paramType.getPointerDepth();
1112
                    } else {
1113
                        return null;
1114
                    }
1115
                }
1116
                
1117
                if (paramType.isReference()) {
1118
                    if (underlyingType.isReference()) {
1119
                        hasChanges = true;
1120
                        newReference = CsmTypes.TypeDescriptor.NON_REFERENCE;
1121
                    } else {
1122
                        return null;
1123
                    }
1124
                }
1125
                
1126
                if (paramType.isRValueReference()) {
1127
                    if (underlyingType.isRValueReference()) {
1128
                        hasChanges = true;
1129
                        newReference = CsmTypes.TypeDescriptor.NON_REFERENCE;
1130
                    } else {
1131
                        return null;
1132
                    }
1133
                }
1134
                
1135
                if (hasChanges) {
1136
                    CsmTypes.TypeDescriptor td = new CsmTypes.TypeDescriptor(
1137
                            newConst, 
1138
                            newReference, 
1139
                            newPtrDepth, 
1140
                            newArrayDepth
1141
                    );
1142
                    
1143
                    return CsmTypes.createType(underlyingType, td);
1144
                }
1145
                
1146
                return passedType;
1147
            }                
1148
        }
1149
        return null;
1150
    }   
1151
    
1152
    static RenderedExpression renderExpression(CsmCompletionExpression expr) {
1153
        if (expr == null) {
1154
            return null;
1155
        }            
1156
        switch (expr.getExpID()) {
1157
            case CsmCompletionExpression.GENERIC_TYPE: {
1158
                StringBuilder sb = new StringBuilder();
1159
                int startExpOffset = expr.getTokenOffset(0);
1160
                int endExpOffset = startExpOffset;
1161
                
1162
                for (int paramIndex = 0; paramIndex < expr.getParameterCount(); paramIndex++) {
1163
                    RenderedExpression current = renderExpression(expr.getParameter(paramIndex));
1164
                    
1165
                    sb.append(current.text);
1166
                    
1167
                    if (paramIndex > 0) {
1168
                        if (paramIndex < expr.getParameterCount() - 1) {
1169
                            sb.append(","); // NOI18N
1170
                        } else {
1171
                            sb.append(">"); // NOI18N
1172
                        }
1173
                        endExpOffset++;
1174
                    } else {
1175
                        sb.append(expr.getTokenText(0));
1176
                    }
1177
                    
1178
                    endExpOffset = current.endOffset;
1179
                }
1180
                
1181
                return new RenderedExpression(sb.toString(), startExpOffset, endExpOffset);
1182
            }
1183
            
1184
            case CsmCompletionExpression.UNARY_OPERATOR:
1185
            case CsmCompletionExpression.OPERATOR:
1186
            case CsmCompletionExpression.SCOPE: {
1187
                StringBuilder sb = new StringBuilder();
1188
                int startExpOffset = -1;
1189
                int endExprOffset = -1;
1190
1191
                int paramCount = expr.getParameterCount();
1192
                int tokenCount = expr.getTokenCount();
1193
                int paramIndex = 0;
1194
                int tokenIndex = 0;
1195
                
1196
                RenderedExpression renderedParam = null;
1197
                RenderedExpression renderedToken = null;                    
1198
                boolean lastWasParam = false;
1199
                boolean lastWasToken = false;
1200
                
1201
                boolean entityChanged = true;
1202
                
1203
                while (entityChanged) {
1204
                    entityChanged = false;
1205
                                            
1206
                    if (renderedParam == null && paramIndex < paramCount) {
1207
                        renderedParam = renderExpression(expr.getParameter(paramIndex));
1208
                        paramIndex++;
1209
                    }
1210
                    
1211
                    if (renderedToken == null && tokenIndex < tokenCount) {
1212
                        renderedToken = new RenderedExpression(
1213
                            expr.getTokenText(tokenIndex).toString(), 
1214
                            expr.getTokenOffset(tokenIndex), 
1215
                            expr.getTokenOffset(tokenIndex) + expr.getTokenLength(tokenIndex)
1216
                        );
1217
                        tokenIndex++;
1218
                    }
1219
                    
1220
                    RenderedExpression chosenExpression;
1221
                    
1222
                    if (renderedParam != null && renderedToken != null) {
1223
                        if (renderedParam.startOffset < renderedToken.startOffset) {
1224
                            chosenExpression = renderedParam;
1225
                        } else {
1226
                            chosenExpression = renderedToken;
1227
                        }
1228
                    } else if (renderedToken == null) {
1229
                        chosenExpression = renderedParam;
1230
                    } else {
1231
                        chosenExpression = renderedToken;
1232
                    }
1233
                    
1234
                    if (chosenExpression != null) {
1235
                        if (chosenExpression == renderedParam) {
1236
                            renderedParam = null;
1237
                            entityChanged = !lastWasParam;
1238
                            lastWasParam = true;
1239
                            lastWasToken = false;
1240
                        } else {
1241
                            renderedToken = null;
1242
                            entityChanged = !lastWasToken;
1243
                            lastWasToken = true;
1244
                            lastWasParam = false;
1245
                        }
1246
                        
1247
                        if (entityChanged) {
1248
                            sb.append(chosenExpression.text);
1249
                            if (startExpOffset == -1) {
1250
                                startExpOffset = chosenExpression.startOffset;
1251
                            }
1252
                            endExprOffset = chosenExpression.endOffset;
1253
                        }
1254
                    }
1255
                }
1256
                
1257
                return new RenderedExpression(sb.toString(), startExpOffset, endExprOffset);
1258
            }
1259
            
1260
            default: {
1261
                return new RenderedExpression(
1262
                        expr.getTokenText(0), 
1263
                        expr.getTokenOffset(0), 
1264
                        expr.getTokenOffset(0) + expr.getTokenLength(0)
1265
                );
1266
            }
1267
        }
1268
    }    
909
1269
910
    CsmType findExactVarType(CsmFile file, String var, int docPos, FileReferencesContext refContext) {
1270
    CsmType findExactVarType(CsmFile file, String var, int docPos, FileReferencesContext refContext) {
911
        if (file == null) {
1271
        if (file == null) {
Lines 1001-1007 Link Here
1001
            }
1361
            }
1002
        }
1362
        }
1003
        return null;
1363
        return null;
1004
    }
1364
    }    
1365
    
1366
    private static boolean isExpression(CsmCompletionExpression expression) {
1367
        return expression.getExpID() == CsmCompletionExpression.OPERATOR ||
1368
               expression.getExpID() == CsmCompletionExpression.UNARY_OPERATOR;
1369
    }    
1005
    
1370
    
1006
    private int findLCurlsNumberBeforPosition(String s, int pos) {
1371
    private int findLCurlsNumberBeforPosition(String s, int pos) {
1007
        int cursor = -1;
1372
        int cursor = -1;
Lines 1075-1078 Link Here
1075
    public void changedUpdate(DocumentEvent e) {
1440
    public void changedUpdate(DocumentEvent e) {
1076
        this.lastSeparatorOffset = -1;
1441
        this.lastSeparatorOffset = -1;
1077
    }
1442
    }
1443
    
1444
    
1445
    static class RenderedExpression {
1446
        
1447
        public final String text;
1448
        
1449
        public final int startOffset;
1450
        
1451
        public final int endOffset;
1452
1453
        public RenderedExpression(String text, int startOffset, int endOffset) {
1454
            this.text = text;
1455
            this.startOffset = startOffset;
1456
            this.endOffset = endOffset;
1457
        }
1458
1459
        @Override
1460
        public String toString() {
1461
            return text + "[" + startOffset + "," + endOffset + "]"; // NOI18N
1462
        }
1463
    }    
1078
}
1464
}
(-)a/cnd.completion/src/org/netbeans/modules/cnd/completion/cplusplus/ext/CsmCompletionQuery.java (-256 / +37 lines)
Lines 84-91 Link Here
84
import org.netbeans.modules.cnd.api.model.CsmField;
84
import org.netbeans.modules.cnd.api.model.CsmField;
85
import org.netbeans.modules.cnd.api.model.CsmFile;
85
import org.netbeans.modules.cnd.api.model.CsmFile;
86
import org.netbeans.modules.cnd.api.model.CsmFunction;
86
import org.netbeans.modules.cnd.api.model.CsmFunction;
87
import org.netbeans.modules.cnd.api.model.CsmFunctionPointerClassifier;
87
import org.netbeans.modules.cnd.api.model.CsmFunctionPointerType;
88
import org.netbeans.modules.cnd.api.model.CsmFunctionPointerType;
88
import org.netbeans.modules.cnd.api.model.CsmFunctionPointerClassifier;
89
import org.netbeans.modules.cnd.api.model.CsmFunctional;
89
import org.netbeans.modules.cnd.api.model.CsmFunctional;
90
import org.netbeans.modules.cnd.api.model.CsmInheritance;
90
import org.netbeans.modules.cnd.api.model.CsmInheritance;
91
import org.netbeans.modules.cnd.api.model.CsmInstantiation;
91
import org.netbeans.modules.cnd.api.model.CsmInstantiation;
Lines 122-127 Link Here
122
import org.netbeans.modules.cnd.api.model.services.CsmSelect;
122
import org.netbeans.modules.cnd.api.model.services.CsmSelect;
123
import org.netbeans.modules.cnd.api.model.services.CsmSelect.CsmFilter;
123
import org.netbeans.modules.cnd.api.model.services.CsmSelect.CsmFilter;
124
import org.netbeans.modules.cnd.api.model.services.CsmSelect.CsmFilterBuilder;
124
import org.netbeans.modules.cnd.api.model.services.CsmSelect.CsmFilterBuilder;
125
import org.netbeans.modules.cnd.api.model.services.CsmTypes;
125
import org.netbeans.modules.cnd.api.model.util.CsmBaseUtilities;
126
import org.netbeans.modules.cnd.api.model.util.CsmBaseUtilities;
126
import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
127
import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
127
import org.netbeans.modules.cnd.api.model.util.CsmSortUtilities;
128
import org.netbeans.modules.cnd.api.model.util.CsmSortUtilities;
Lines 141-146 Link Here
141
import org.netbeans.spi.editor.completion.CompletionItem;
142
import org.netbeans.spi.editor.completion.CompletionItem;
142
import org.openide.text.CloneableEditorSupport;
143
import org.openide.text.CloneableEditorSupport;
143
import org.openide.util.NbBundle;
144
import org.openide.util.NbBundle;
145
import org.openide.util.Pair;
144
146
145
/**
147
/**
146
 *
148
 *
Lines 1145-1150 Link Here
1145
            this.instantiateTypes = instantiateTypes;
1147
            this.instantiateTypes = instantiateTypes;
1146
            this.instantiations = instantiations;
1148
            this.instantiations = instantiations;
1147
        }
1149
        }
1150
        
1151
        CsmFile getContextFile() {
1152
            return contextFile;
1153
        }
1154
        
1155
        int getEndOffset() {
1156
            return endOffset;
1157
        }
1148
1158
1149
        private int convertOffset(int pos) {
1159
        private int convertOffset(int pos) {
1150
            return sup.doc2context(pos);
1160
            return sup.doc2context(pos);
Lines 1398-1434 Link Here
1398
            return out;
1408
            return out;
1399
        }
1409
        }
1400
1410
1401
        private CsmType extractFunctionType(Collection<? extends CsmFunctional> mtdList, CsmCompletionExpression genericNameExp, List<CsmType> typeList) {
1402
            CsmType out = null;
1403
            if (mtdList.isEmpty()) {
1404
                return null;
1405
            }
1406
            for (CsmFunctional fun : mtdList) {
1407
                CsmObject entity = fun;
1408
1409
                if (CsmKindUtilities.isConstructor(entity)) {
1410
                    entity = ((CsmConstructor) entity).getContainingClass();
1411
                }
1412
1413
                if (CsmKindUtilities.isTemplate(entity)) {
1414
                    CsmObject inst = createInstantiation((CsmTemplate) entity, genericNameExp, typeList);                    
1415
                    if (CsmKindUtilities.isFunction(inst) || CsmKindUtilities.isClassifier(inst)) {
1416
                        entity = inst;
1417
                    }
1418
                }
1419
                
1420
                if (CsmKindUtilities.isFunctional(entity)) {
1421
                    out = ((CsmFunctional) entity).getReturnType();
1422
                } else if (CsmKindUtilities.isClassifier(entity)) {
1423
                    out = CsmCompletion.createType((CsmClassifier) entity, 0, 0, 0, false);
1424
                }
1425
                if (out != null) {
1426
                    break;
1427
                }
1428
            }
1429
            return out;
1430
        }
1431
1432
        /*private CsmClassifier resolveTemplateParameter(CsmClassifier cls, CsmType type) {
1411
        /*private CsmClassifier resolveTemplateParameter(CsmClassifier cls, CsmType type) {
1433
        if (cls instanceof CsmClassifierBasedTemplateParameter) {
1412
        if (cls instanceof CsmClassifierBasedTemplateParameter) {
1434
        CsmClassifierBasedTemplateParameter tp = (CsmClassifierBasedTemplateParameter) cls;
1413
        CsmClassifierBasedTemplateParameter tp = (CsmClassifierBasedTemplateParameter) cls;
Lines 1450-1456 Link Here
1450
        }
1429
        }
1451
        return cls;
1430
        return cls;
1452
        }*/
1431
        }*/
1453
        private CsmType resolveType(CsmCompletionExpression exp) {
1432
        CsmType resolveType(CsmCompletionExpression exp) {
1454
            CsmType typ = exp.getCachedType();
1433
            CsmType typ = exp.getCachedType();
1455
            if (typ == null) {
1434
            if (typ == null) {
1456
                Context ctx = (Context) clone();
1435
                Context ctx = (Context) clone();
Lines 1465-1475 Link Here
1465
                    exp.cacheType(typ);
1444
                    exp.cacheType(typ);
1466
                    // restore old
1445
                    // restore old
1467
                    ctx.compResolver.setResolveScope(old);
1446
                    ctx.compResolver.setResolveScope(old);
1468
                }
1447
                }        
1469
            }
1448
            }
1470
            return typ;
1449
            return typ;
1471
        }
1450
        }
1472
1451
1452
        List<? extends CompletionItem> resolveNextExp(CsmCompletionExpression exp) {
1453
            Context ctx = (Context) clone();
1454
            ctx.setFindType(false);
1455
            // when resolve type use full scope of search
1456
            QueryScope old = ctx.compResolver.setResolveScope(QueryScope.GLOBAL_QUERY);
1457
            try {
1458
                if (ctx.resolveExp(exp, true)) {
1459
                    if (ctx.result != null && !ctx.result.getItems().isEmpty()) {
1460
                        return ctx.result.getItems();
1461
                    }
1462
                }
1463
            } finally {
1464
                // restore old
1465
                ctx.compResolver.setResolveScope(old);
1466
            }
1467
            return null;
1468
        }        
1469
1473
        private boolean isProjectBeeingParsed() {
1470
        private boolean isProjectBeeingParsed() {
1474
            return CsmCompletionQuery.this.isProjectBeeingParsed(openingSource);
1471
            return CsmCompletionQuery.this.isProjectBeeingParsed(openingSource);
1475
        }
1472
        }
Lines 2066-2072 Link Here
2066
                        lastType = typ;
2063
                        lastType = typ;
2067
                        CsmClassifier cls = getClassifier(lastType, contextFile, endOffset);
2064
                        CsmClassifier cls = getClassifier(lastType, contextFile, endOffset);
2068
                        if (cls != null && CsmKindUtilities.isTemplate(cls)) {
2065
                        if (cls != null && CsmKindUtilities.isTemplate(cls)) {
2069
                            CsmObject obj = createInstantiation((CsmTemplate)cls, item, Collections.<CsmType>emptyList());
2066
                            CsmObject obj = CompletionSupport.createInstantiation(this, (CsmTemplate)cls, item, Collections.<CsmType>emptyList());
2070
                            if (obj != null && CsmKindUtilities.isClass(obj)) {
2067
                            if (obj != null && CsmKindUtilities.isClass(obj)) {
2071
                                lastType = CsmCompletion.createType((CsmClass)obj, 0, 0, 0, false);
2068
                                lastType = CsmCompletion.createType((CsmClass)obj, 0, 0, 0, false);
2072
                            }
2069
                            }
Lines 2158-2167 Link Here
2158
                                if (!mtdList.isEmpty()) {
2155
                                if (!mtdList.isEmpty()) {
2159
                                    List<CsmType> typeList = getTypeList(item, 0);
2156
                                    List<CsmType> typeList = getTypeList(item, 0);
2160
                                    // check exact overloaded operator
2157
                                    // check exact overloaded operator
2161
                                    Collection<CsmFunction> filtered = CompletionSupport.filterMethods(mtdList, typeList, false, false);
2158
                                    Collection<CsmFunction> filtered = CompletionSupport.filterMethods(this, mtdList, null, typeList, false, false);
2162
                                    if (filtered.size() > 0) {
2159
                                    if (filtered.size() > 0) {
2163
                                        mtdList = filtered;
2160
                                        mtdList = filtered;
2164
                                        lastType = extractFunctionType(mtdList, null, typeList);
2161
                                        lastType = CompletionSupport.extractFunctionType(this, mtdList, null, typeList);
2165
                                    } else if (item.getParameterCount() > 1) {
2162
                                    } else if (item.getParameterCount() > 1) {
2166
                                        CsmType type0 = resolveType(item.getParameter(0));
2163
                                        CsmType type0 = resolveType(item.getParameter(0));
2167
                                        CsmType type1 = resolveType(item.getParameter(1));
2164
                                        CsmType type1 = resolveType(item.getParameter(1));
Lines 2672-2678 Link Here
2672
                                        }
2669
                                        }
2673
                                        if (cls != null) {
2670
                                        if (cls != null) {
2674
                                            if (CsmKindUtilities.isTemplate(cls) && genericNameExp != null) {
2671
                                            if (CsmKindUtilities.isTemplate(cls) && genericNameExp != null) {
2675
                                                CsmObject inst = createInstantiation((CsmTemplate)cls, genericNameExp, Collections.<CsmType>emptyList());
2672
                                                CsmObject inst = CompletionSupport.createInstantiation(this, (CsmTemplate)cls, genericNameExp, Collections.<CsmType>emptyList());
2676
                                                if (CsmKindUtilities.isClassifier(inst)) {
2673
                                                if (CsmKindUtilities.isClassifier(inst)) {
2677
                                                    cls = (CsmClassifier) inst;
2674
                                                    cls = (CsmClassifier) inst;
2678
                                                }
2675
                                                }
Lines 2690-2696 Link Here
2690
                            }
2687
                            }
2691
                            String parmStr = "*"; // NOI18N
2688
                            String parmStr = "*"; // NOI18N
2692
                            List<CsmType> typeList = getTypeList(item, 1);
2689
                            List<CsmType> typeList = getTypeList(item, 1);
2693
                            Collection<CsmFunctional> filtered = CompletionSupport.filterMethods(mtdList, typeList, methodOpen, true);
2690
                            Collection<CsmFunctional> filtered = CompletionSupport.filterMethods(this, mtdList, genericNameExp, typeList, methodOpen, true);
2694
                            if (filtered.size() > 0) {
2691
                            if (filtered.size() > 0) {
2695
                                mtdList = filtered;
2692
                                mtdList = filtered;
2696
                                parmStr = formatTypeList(typeList, methodOpen);
2693
                                parmStr = formatTypeList(typeList, methodOpen);
Lines 2701-2707 Link Here
2701
                                            formatType(lastType, true, true, false) + mtdName + '(' + parmStr + ')',
2698
                                            formatType(lastType, true, true, false) + mtdName + '(' + parmStr + ')',
2702
                                            item, endOffset, 0, 0, isProjectBeeingParsed(), contextElement, instantiateTypes);
2699
                                            item, endOffset, 0, 0, isProjectBeeingParsed(), contextElement, instantiateTypes);
2703
                                } else {
2700
                                } else {
2704
                                    lastType = extractFunctionType(mtdList, genericNameExp, typeList);
2701
                                    lastType = CompletionSupport.extractFunctionType(this, mtdList, genericNameExp, typeList);
2705
                                    staticOnly = false;
2702
                                    staticOnly = false;
2706
                                }
2703
                                }
2707
                            } else {
2704
                            } else {
Lines 2833-3054 Link Here
2833
            }
2830
            }
2834
            return false;
2831
            return false;
2835
        }        
2832
        }        
2836
        
2837
        private CsmObject createInstantiation(CsmTemplate template, CsmCompletionExpression exp, List<CsmType> typeList) {
2838
            if (exp != null || !typeList.isEmpty()) {
2839
                CsmInstantiationProvider ip = CsmInstantiationProvider.getDefault();
2840
                List<CsmSpecializationParameter> params = new ArrayList<CsmSpecializationParameter>();        
2841
                params.addAll(collectInstantiationParameters(ip, exp));
2842
                if (CsmKindUtilities.isFunction(template)) {
2843
                    params.addAll(collectInstantiationParameters(ip, (CsmFunction)template, params.size(), typeList));
2844
                }
2845
                if (!params.isEmpty()) {
2846
                    return ip.instantiate(template, params);
2847
                }
2848
            }
2849
            return null;
2850
        }   
2851
        
2852
        private List<CsmSpecializationParameter> collectInstantiationParameters(CsmInstantiationProvider ip, CsmCompletionExpression exp) {
2853
            if (exp != null) {
2854
                List<CsmSpecializationParameter> params = new ArrayList<CsmSpecializationParameter>();
2855
                if (exp.getExpID() == CsmCompletionExpression.GENERIC_TYPE) {
2856
                    int paramsNumber = exp.getParameterCount() - 1;
2857
                    for (int i = 0; i < paramsNumber; i++) {
2858
                        CsmCompletionExpression paramInst = exp.getParameter(i + 1);
2859
                        if (paramInst != null) {
2860
                            switch (paramInst.getExpID()) {
2861
                                case CsmCompletionExpression.CONSTANT:
2862
                                    params.add(ip.createExpressionBasedSpecializationParameter(paramInst.getTokenText(0),
2863
                                            contextFile, paramInst.getTokenOffset(0), paramInst.getTokenOffset(0) + paramInst.getTokenLength(0)));
2864
                                    break;
2865
                                default:
2866
                                    CsmType type = resolveType(paramInst);
2867
                                    if (type != null) {
2868
                                        params.add(ip.createTypeBasedSpecializationParameter(type));
2869
                                    } else {
2870
                                        RenderedExpression renderedExpression = renderExpression(paramInst);
2871
                                        params.add(ip.createExpressionBasedSpecializationParameter(
2872
                                                renderedExpression.text,
2873
                                                contextFile, 
2874
                                                renderedExpression.startOffset, 
2875
                                                renderedExpression.endOffset
2876
                                        ));
2877
                                    }
2878
                                }
2879
                        } else {
2880
                            break;
2881
                        }
2882
                    }
2883
                }
2884
                return params;
2885
            }
2886
            return Collections.emptyList();
2887
        }
2888
        
2889
        private List<CsmSpecializationParameter> collectInstantiationParameters(CsmInstantiationProvider ip, CsmFunction function, int explicitelyMappedSize, List<CsmType> typeList) {            
2890
            if (CsmKindUtilities.isTemplate(function)) {
2891
                List<CsmSpecializationParameter> result = new ArrayList<CsmSpecializationParameter>();
2892
                
2893
                CsmTemplate template = (CsmTemplate) function;                
2894
                List<CsmTemplateParameter> templateParams = template.getTemplateParameters();
2895
                
2896
                if (templateParams.size() > explicitelyMappedSize) {
2897
                    Map<CsmTemplateParameter, CsmType> paramsMap = gatherTemplateParamsMap(function, typeList);
2898
                    
2899
                    for (int i = explicitelyMappedSize; i < templateParams.size(); i++) {
2900
                        CsmTemplateParameter param = templateParams.get(i);
2901
                        CsmType mappedType = paramsMap.get(param);
2902
                        if (mappedType != null) {
2903
                            result.add(ip.createTypeBasedSpecializationParameter(mappedType));
2904
                        } else {
2905
                            // error
2906
                            return result;
2907
                        }
2908
                    }
2909
                }
2910
                
2911
                return result;
2912
            }
2913
            return Collections.emptyList();
2914
        }
2915
        
2916
        private Map<CsmTemplateParameter, CsmType> gatherTemplateParamsMap(CsmFunction function, List<CsmType> typeList) {
2917
            assert CsmKindUtilities.isTemplate(function) : "Attempt to gather template parameters map from non-template function"; // NOI18N
2918
            CsmTemplate template = (CsmTemplate) function;
2919
            
2920
            Map<CsmTemplateParameter, CsmType> map = new HashMap<CsmTemplateParameter, CsmType>();
2921
            
2922
            for (CsmTemplateParameter templateParam : template.getTemplateParameters()) {
2923
                int paramIndex = 0;                
2924
                for (CsmParameter param : function.getParameters()) {
2925
                    if (paramIndex >= typeList.size()) {
2926
                        break;
2927
                    }
2928
                    CsmType paramType = param.getType();
2929
                    if (paramType != null) {
2930
                        CsmClassifier cls = paramType.getClassifier();
2931
                        if (cls != null) {
2932
                            if (cls.getQualifiedName().toString().equals(templateParam.getQualifiedName().toString())) {
2933
                                map.put(templateParam, typeList.get(paramIndex));
2934
                            }
2935
                        }
2936
                    }
2937
                    paramIndex++;
2938
                }
2939
            }
2940
            
2941
            return map;
2942
        }
2943
        
2944
        private RenderedExpression renderExpression(CsmCompletionExpression expr) {
2945
            if (expr == null) {
2946
                return null;
2947
            }            
2948
            switch (expr.getExpID()) {
2949
                case CsmCompletionExpression.SCOPE: {
2950
                    StringBuilder sb = new StringBuilder();
2951
                    int startExpOffset = -1;
2952
                    int endExprOffset = -1;
2953
2954
                    int paramCount = expr.getParameterCount();
2955
                    int tokenCount = expr.getTokenCount();
2956
                    int paramIndex = 0;
2957
                    int tokenIndex = 0;
2958
                    
2959
                    RenderedExpression renderedParam = null;
2960
                    RenderedExpression renderedToken = null;                    
2961
                    boolean lastWasParam = false;
2962
                    boolean lastWasToken = false;
2963
                    
2964
                    boolean entityChanged = true;
2965
                    
2966
                    while (entityChanged) {
2967
                        entityChanged = false;
2968
                                                
2969
                        if (renderedParam == null && paramIndex < paramCount) {
2970
                            renderedParam = renderExpression(expr.getParameter(paramIndex));
2971
                            paramIndex++;
2972
                        }
2973
                        
2974
                        if (renderedToken == null && tokenIndex < tokenCount) {
2975
                            renderedToken = new RenderedExpression(
2976
                                expr.getTokenText(tokenIndex).toString(), 
2977
                                expr.getTokenOffset(tokenIndex), 
2978
                                expr.getTokenOffset(tokenIndex) + expr.getTokenLength(tokenIndex)
2979
                            );
2980
                            tokenIndex++;
2981
                        }
2982
                        
2983
                        RenderedExpression chosenExpression;
2984
                        
2985
                        if (renderedParam != null && renderedToken != null) {
2986
                            if (renderedParam.startOffset < renderedToken.startOffset) {
2987
                                chosenExpression = renderedParam;
2988
                            } else {
2989
                                chosenExpression = renderedToken;
2990
                            }
2991
                        } else if (renderedToken == null) {
2992
                            chosenExpression = renderedParam;
2993
                        } else {
2994
                            chosenExpression = renderedToken;
2995
                        }
2996
                        
2997
                        if (chosenExpression != null) {
2998
                            if (chosenExpression == renderedParam) {
2999
                                renderedParam = null;
3000
                                entityChanged = !lastWasParam;
3001
                                lastWasParam = true;
3002
                                lastWasToken = false;
3003
                            } else {
3004
                                renderedToken = null;
3005
                                entityChanged = !lastWasToken;
3006
                                lastWasToken = true;
3007
                                lastWasParam = false;
3008
                            }
3009
                            
3010
                            if (entityChanged) {
3011
                                sb.append(chosenExpression.text);
3012
                                if (startExpOffset == -1) {
3013
                                    startExpOffset = chosenExpression.startOffset;
3014
                                }
3015
                                endExprOffset = chosenExpression.endOffset;
3016
                            }
3017
                        }
3018
                    }
3019
                    
3020
                    return new RenderedExpression(sb.toString(), startExpOffset, endExprOffset);
3021
                }
3022
                
3023
                default: {
3024
                    return new RenderedExpression(
3025
                            expr.getTokenText(0), 
3026
                            expr.getTokenOffset(0), 
3027
                            expr.getTokenOffset(0) + expr.getTokenLength(0)
3028
                    );
3029
                }
3030
            }
3031
        }
3032
        
3033
        private class RenderedExpression {
3034
            
3035
            public final String text;
3036
            
3037
            public final int startOffset;
3038
            
3039
            public final int endOffset;
3040
3041
            public RenderedExpression(String text, int startOffset, int endOffset) {
3042
                this.text = text;
3043
                this.startOffset = startOffset;
3044
                this.endOffset = endOffset;
3045
            }
3046
3047
            @Override
3048
            public String toString() {
3049
                return text + "[" + startOffset + "," + endOffset + "]"; // NOI18N
3050
            }
3051
        }
3052
2833
3053
        private CsmType findBuiltInFunctionReturnType(String mtdName, int tokenOffset) {
2834
        private CsmType findBuiltInFunctionReturnType(String mtdName, int tokenOffset) {
3054
            CsmType out = null;
2835
            CsmType out = null;
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/csm/Instantiation.java (-3 / +21 lines)
Lines 1227-1244 Link Here
1227
        }
1227
        }
1228
        return new Type(type, instantiation);       
1228
        return new Type(type, instantiation);       
1229
    }
1229
    }
1230
1230
    
1231
    public static CsmType createType(CsmType type, List<CsmInstantiation> instantiations) {
1232
        for (CsmInstantiation instantiation : instantiations) {
1233
            type = createType(type, instantiation);
1234
        }
1235
        return type;
1236
    }
1237
    
1238
//    public static CsmType resolveTemplateParameterType(CsmType type, Map<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1239
//        if (CsmKindUtilities.isTemplateParameterType(type)) {
1240
//            LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{type.getText(), instantiation.getTemplateDeclaration().getName()});
1241
//            CsmType resolvedType = resolveTemplateParameterType(((CsmTemplateParameterType) type).getParameter(), new MapHierarchy<>(mapping));
1242
//            if (resolvedType != null) {
1243
//                return resolvedType;
1244
//            }            
1245
//        }
1246
//        return type;
1247
//    }    
1248
    
1231
    public static CsmType resolveTemplateParameterType(CsmType type, CsmInstantiation instantiation) {
1249
    public static CsmType resolveTemplateParameterType(CsmType type, CsmInstantiation instantiation) {
1232
        if (CsmKindUtilities.isTemplateParameterType(type)) {
1250
        if (CsmKindUtilities.isTemplateParameterType(type)) {
1233
            LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{type.getText(), instantiation.getTemplateDeclaration().getName()});
1251
            LOG.log(Level.FINE, "Instantiation.resolveTemplateParameter {0}; mapping={1}\n", new Object[]{type.getText(), instantiation.getTemplateDeclaration().getName()});
1234
            MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping = TemplateUtils.gatherMapping(instantiation);
1252
            MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> mapping = new MapHierarchy<>(instantiation.getMapping());
1235
            CsmType resolvedType = resolveTemplateParameterType(((CsmTemplateParameterType) type).getParameter(), mapping);
1253
            CsmType resolvedType = resolveTemplateParameterType(((CsmTemplateParameterType) type).getParameter(), mapping);
1236
            if (resolvedType != null) {
1254
            if (resolvedType != null) {
1237
                return resolvedType;
1255
                return resolvedType;
1238
            }
1256
            }
1239
        }
1257
        }
1240
        return type;
1258
        return type;
1241
    }
1259
    }    
1242
    
1260
    
1243
    public static CsmType resolveTemplateParameterType(CsmTemplateParameter templateParameter, Map<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1261
    public static CsmType resolveTemplateParameterType(CsmTemplateParameter templateParameter, Map<CsmTemplateParameter, CsmSpecializationParameter> mapping) {
1244
        return resolveTemplateParameterType(templateParameter, new MapHierarchy<>(mapping));
1262
        return resolveTemplateParameterType(templateParameter, new MapHierarchy<>(mapping));
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/csm/NestedType.java (-5 / +5 lines)
Lines 132-142 Link Here
132
                    classifier = getNestedClassifier(memberResolver, parentClassifier, getOwnText());
132
                    classifier = getNestedClassifier(memberResolver, parentClassifier, getOwnText());
133
                }
133
                }
134
            }
134
            }
135
            if (!CsmBaseUtilities.isValid(classifier)) {
135
//            if (!CsmBaseUtilities.isValid(classifier)) {
136
                // try to resolve qualified name, not through the parent classifier
136
//                // try to resolve qualified name, not through the parent classifier
137
                List<CharSequence> fqn = getFullQName();
137
//                List<CharSequence> fqn = getFullQName();
138
                classifier = renderClassifier(fqn.toArray(new CharSequence[fqn.size()]));
138
//                classifier = renderClassifier(fqn.toArray(new CharSequence[fqn.size()]));
139
            }
139
//            }
140
            _setClassifier(classifier);
140
            _setClassifier(classifier);
141
        }
141
        }
142
        if (isInstantiation() && CsmKindUtilities.isTemplate(classifier) && !((CsmTemplate)classifier).getTemplateParameters().isEmpty()) {
142
        if (isInstantiation() && CsmKindUtilities.isTemplate(classifier) && !((CsmTemplate)classifier).getTemplateParameters().isEmpty()) {
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/csm/TypeImpl.java (+4 lines)
Lines 981-986 Link Here
981
981
982
    protected CsmClassifier _getClassifier() {
982
    protected CsmClassifier _getClassifier() {
983
        CsmClassifier classifier = null;
983
        CsmClassifier classifier = null;
984
        boolean clearClassifier = false;
985
        if (clearClassifier) {
986
            classifierUID = null;
987
        }
984
        if (classifierUID != null) {
988
        if (classifierUID != null) {
985
            classifier = UIDCsmConverter.UIDtoDeclaration(classifierUID);
989
            classifier = UIDCsmConverter.UIDtoDeclaration(classifierUID);
986
        } else {
990
        } else {
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/impl/services/ExpressionEvaluator.java (-2 / +2 lines)
Lines 212-223 Link Here
212
                for (CsmTemplateParameter param : orderedParamsList) {
212
                for (CsmTemplateParameter param : orderedParamsList) {
213
                    Map<CsmTemplateParameter, CsmSpecializationParameter> newMapping = new HashMap<CsmTemplateParameter, CsmSpecializationParameter>();
213
                    Map<CsmTemplateParameter, CsmSpecializationParameter> newMapping = new HashMap<CsmTemplateParameter, CsmSpecializationParameter>();
214
                    CsmSpecializationParameter spec = inst.getMapping().get(param);
214
                    CsmSpecializationParameter spec = inst.getMapping().get(param);
215
                    if(CsmKindUtilities.isExpressionBasedSpecalizationParameter(spec)) {
215
                    /*if(CsmKindUtilities.isExpressionBasedSpecalizationParameter(spec)) {
216
                        Object o = eval(((CsmExpressionBasedSpecializationParameter) spec).getText().toString(), inst.getTemplateDeclaration(), mapHierarchy);
216
                        Object o = eval(((CsmExpressionBasedSpecializationParameter) spec).getText().toString(), inst.getTemplateDeclaration(), mapHierarchy);
217
                        CsmSpecializationParameter newSpec = ExpressionBasedSpecializationParameterImpl.create(o.toString(),
217
                        CsmSpecializationParameter newSpec = ExpressionBasedSpecializationParameterImpl.create(o.toString(),
218
                                spec.getContainingFile(), spec.getStartOffset(), spec.getEndOffset());
218
                                spec.getContainingFile(), spec.getStartOffset(), spec.getEndOffset());
219
                        newMapping.put(param, newSpec);
219
                        newMapping.put(param, newSpec);
220
                    } else {
220
                    } else*/ {
221
                        newMapping.put(param, spec);
221
                        newMapping.put(param, spec);
222
                    }
222
                    }
223
                    mapping.putAll(newMapping);
223
                    mapping.putAll(newMapping);
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/impl/services/InstantiationProviderImpl.java (-66 / +95 lines)
Lines 120-125 Link Here
120
import org.netbeans.modules.cnd.modelutil.CsmUtilities;
120
import org.netbeans.modules.cnd.modelutil.CsmUtilities;
121
import org.netbeans.modules.cnd.spi.model.services.CsmExpressionEvaluatorProvider;
121
import org.netbeans.modules.cnd.spi.model.services.CsmExpressionEvaluatorProvider;
122
import org.netbeans.modules.cnd.utils.CndCollectionUtils;
122
import org.netbeans.modules.cnd.utils.CndCollectionUtils;
123
import org.openide.util.Pair;
123
124
124
/**
125
/**
125
 * Service that provides template instantiations
126
 * Service that provides template instantiations
Lines 211-217 Link Here
211
    public CsmObject instantiate(CsmTemplate template, CsmInstantiation instantiation) {
212
    public CsmObject instantiate(CsmTemplate template, CsmInstantiation instantiation) {
212
        return instantiate(template, instantiation, true);        
213
        return instantiate(template, instantiation, true);        
213
    }
214
    }
214
215
     
215
    public CsmObject instantiate(CsmTemplate template, CsmInstantiation instantiation, boolean specialize) {
216
    public CsmObject instantiate(CsmTemplate template, CsmInstantiation instantiation, boolean specialize) {
216
        return instantiate(template, null, 0, instantiation, specialize);
217
        return instantiate(template, null, 0, instantiation, specialize);
217
    }
218
    }
Lines 483-489 Link Here
483
    }
484
    }
484
    
485
    
485
    private CsmClassifier specialize(CsmClassifier classifier, CsmFile contextFile, int contextOffset) {
486
    private CsmClassifier specialize(CsmClassifier classifier, CsmFile contextFile, int contextOffset) {
486
        List<CsmSpecializationParameter> params = getInstantiationParams(classifier);
487
        List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> params = getInstantiationParams(classifier);
487
        CsmClassifier specialization = null;
488
        CsmClassifier specialization = null;
488
        if (CsmKindUtilities.isTemplate(classifier)) {
489
        if (CsmKindUtilities.isTemplate(classifier)) {
489
            List<CsmTemplateParameter> templateParams = ((CsmTemplate) classifier).getTemplateParameters();
490
            List<CsmTemplateParameter> templateParams = ((CsmTemplate) classifier).getTemplateParameters();
Lines 493-499 Link Here
493
                    // try to find full specialization of class
494
                    // try to find full specialization of class
494
                    CsmClass cls = (CsmClass) classifier;
495
                    CsmClass cls = (CsmClass) classifier;
495
                    StringBuilder fqn = new StringBuilder(cls.getUniqueName());
496
                    StringBuilder fqn = new StringBuilder(cls.getUniqueName());
496
                    fqn.append(Instantiation.getInstantiationCanonicalText(params));
497
                    fqn.append(Instantiation.getInstantiationCanonicalText(getPlainParams(params)));
497
                    
498
                    
498
                    for (CsmProject proj : projects) {
499
                    for (CsmProject proj : projects) {
499
                        CsmDeclaration decl = proj.findDeclaration(fqn.toString());
500
                        CsmDeclaration decl = proj.findDeclaration(fqn.toString());
Lines 651-670 Link Here
651
        return projects;
652
        return projects;
652
    }
653
    }
653
654
654
    private static CsmClassifier findBestSpecialization(Collection<CsmOffsetableDeclaration> specializations, List<CsmSpecializationParameter> params, final CsmClassifier cls) {
655
    private static CsmClassifier findBestSpecialization(Collection<CsmOffsetableDeclaration> specializations, List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> params, final CsmClassifier cls) {
655
        // TODO : update
656
        // TODO : update
656
657
657
        CsmClassifier bestSpecialization = null;
658
        CsmClassifier bestSpecialization = null;
658
659
659
        boolean variadic = false;
660
        boolean variadic = false;
660
        
661
        
661
        List<CsmSpecializationParameter> params2 = new ArrayList<CsmSpecializationParameter>();
662
        List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> params2 = new ArrayList<Pair<CsmSpecializationParameter, List<CsmInstantiation>>>();
662
        for (CsmSpecializationParameter param : params) {
663
        for (Pair<CsmSpecializationParameter, List<CsmInstantiation>> pair : params) {
664
            CsmSpecializationParameter param = pair.first();
663
            if(CsmKindUtilities.isVariadicSpecalizationParameter(param)) {
665
            if(CsmKindUtilities.isVariadicSpecalizationParameter(param)) {
664
                params2.addAll(((CsmVariadicSpecializationParameter)param).getArgs());
666
                for (CsmSpecializationParameter arg : ((CsmVariadicSpecializationParameter)param).getArgs()) {
667
                    params2.add(Pair.of(arg, pair.second()));
668
                }
665
                variadic = true;
669
                variadic = true;
666
            } else {
670
            } else {
667
                params2.add(param);
671
                params2.add(pair);
668
            }
672
            }
669
        }
673
        }
670
        params = params2;
674
        params = params2;
Lines 672-678 Link Here
672
        if (!specializations.isEmpty()) {
676
        if (!specializations.isEmpty()) {
673
            int bestMatch = 0;
677
            int bestMatch = 0;
674
            int paramsSize = 0;
678
            int paramsSize = 0;
675
            for (CsmSpecializationParameter param : params) {
679
            for (Pair<CsmSpecializationParameter, List<CsmInstantiation>> pair : params) {
680
                CsmSpecializationParameter param = pair.first();
676
                if(CsmKindUtilities.isVariadicSpecalizationParameter(param)) {
681
                if(CsmKindUtilities.isVariadicSpecalizationParameter(param)) {
677
                    paramsSize += ((CsmVariadicSpecializationParameter)param).getArgs().size();
682
                    paramsSize += ((CsmVariadicSpecializationParameter)param).getArgs().size();
678
                } else {
683
                } else {
Lines 682-729 Link Here
682
687
683
            List<CharSequence> paramsText = new ArrayList<CharSequence>();
688
            List<CharSequence> paramsText = new ArrayList<CharSequence>();
684
            List<CsmType> paramsType = new ArrayList<CsmType>();
689
            List<CsmType> paramsType = new ArrayList<CsmType>();
685
            for (CsmSpecializationParameter param : params) {
690
            for (Pair<CsmSpecializationParameter, List<CsmInstantiation>> pair : params) {
691
                CsmSpecializationParameter param = pair.first();
686
                if (CsmKindUtilities.isTypeBasedSpecalizationParameter(param)) {
692
                if (CsmKindUtilities.isTypeBasedSpecalizationParameter(param)) {
687
                    CsmType paramType = ((CsmTypeBasedSpecializationParameter) param).getType();
693
                    CsmType paramType = ((CsmTypeBasedSpecializationParameter) param).getType();
688
                    
694
                    
689
                    if (CsmKindUtilities.isInstantiation(cls)) {
695
                    if (CsmKindUtilities.isInstantiation(cls)) {
690
                        // Try to instantiate type with appropriate instantiations
696
                        paramType = Instantiation.createType(paramType, pair.second());
691
                        Iterator<CsmInstantiation> instIters = new Iterator() {
697
//                        // Try to instantiate type with appropriate instantiations
692
698
//                        Iterator<CsmInstantiation> instIters = new Iterator() {
693
                            private CsmInstantiation instantiation = (CsmInstantiation) cls;
699
//
694
700
//                            private CsmInstantiation instantiation = (CsmInstantiation) cls;
695
                            @Override
701
//
696
                            public boolean hasNext() {
702
//                            @Override
697
                                return instantiation != null;
703
//                            public boolean hasNext() {
698
                            }
704
//                                return instantiation != null;
699
705
//                            }
700
                            @Override
706
//
701
                            public Object next() {
707
//                            @Override
702
                                CsmInstantiation res = instantiation;
708
//                            public Object next() {
703
                                if (CsmKindUtilities.isInstantiation(instantiation.getTemplateDeclaration())) {
709
//                                CsmInstantiation res = instantiation;
704
                                    instantiation = (CsmInstantiation) instantiation.getTemplateDeclaration();
710
//                                if (CsmKindUtilities.isInstantiation(instantiation.getTemplateDeclaration())) {
705
                                } else {
711
//                                    instantiation = (CsmInstantiation) instantiation.getTemplateDeclaration();
706
                                    instantiation = null;
712
//                                } else {
707
                                }
713
//                                    instantiation = null;
708
                                return res;
714
//                                }
709
                            }
715
//                                return res;
710
716
//                            }
711
                            @Override
717
//
712
                            public void remove() {
718
//                            @Override
713
                                throw new UnsupportedOperationException("Not supported.");  // NOI18N
719
//                            public void remove() {
714
                            }
720
//                                throw new UnsupportedOperationException("Not supported.");  // NOI18N
715
                        };
721
//                            }
716
                        
722
//                        };
717
                        CsmType instantiatedType = createTypeInstantiationForTypeParameter((CsmTypeBasedSpecializationParameter) param, instIters, 0);
723
//                        
718
//                        CsmType instantiatedType = null;
724
//                        CsmType instantiatedType = createTypeInstantiationForTypeParameter((CsmTypeBasedSpecializationParameter) param, instIters, 0);
719
                        
725
////                        CsmType instantiatedType = null;
720
                        if (instantiatedType != null) {
726
//                        
721
                            // If success then it will be parameter type
727
//                        if (instantiatedType != null) {
722
                            paramType = instantiatedType;
728
//                            // If success then it will be parameter type
723
                        } else {
729
//                            paramType = instantiatedType;
724
                            // If instantiation failed then create instantiation with the the full class isntantiation
730
//                        } else {
725
                            paramType = Instantiation.createType(paramType, (CsmInstantiation) cls);
731
//                            // If instantiation failed then create instantiation with the the full class isntantiation
726
                        }
732
//                            paramType = Instantiation.createType(paramType, (CsmInstantiation) cls);
733
//                        }
727
                    }
734
                    }
728
                    
735
                    
729
                    paramsType.add(paramType);
736
                    paramsType.add(paramType);
Lines 746-756 Link Here
746
                    if (specParams.size() == paramsSize) {
753
                    if (specParams.size() == paramsSize) {
747
                        for (int i = 0; i < paramsSize - 1; i++) {
754
                        for (int i = 0; i < paramsSize - 1; i++) {
748
                            CsmSpecializationParameter specParam1 = specParams.get(i);
755
                            CsmSpecializationParameter specParam1 = specParams.get(i);
749
                            CsmSpecializationParameter param1 = params.get(i);
756
                            CsmSpecializationParameter param1 = params.get(i).first();
750
//                            for (int j = i + 1; j < paramsSize; j++) {
757
//                            for (int j = i + 1; j < paramsSize; j++) {
751
                            int j = i + 1;
758
                            int j = i + 1;
752
                                CsmSpecializationParameter specParam2 = specParams.get(j);
759
                                CsmSpecializationParameter specParam2 = specParams.get(j);
753
                                CsmSpecializationParameter param2 = params.get(j);
760
                                CsmSpecializationParameter param2 = params.get(j).first();
754
                                if (specParam1.getText().toString().equals(specParam2.getText().toString())
761
                                if (specParam1.getText().toString().equals(specParam2.getText().toString())
755
                                        && param1.getText().toString().equals(param2.getText().toString())) {
762
                                        && param1.getText().toString().equals(param2.getText().toString())) {
756
                                    match += 1;
763
                                    match += 1;
Lines 761-781 Link Here
761
                                            CsmKindUtilities.isTypeBasedSpecalizationParameter(param1) &&
768
                                            CsmKindUtilities.isTypeBasedSpecalizationParameter(param1) &&
762
                                            CsmKindUtilities.isTypeBasedSpecalizationParameter(param2)) {
769
                                            CsmKindUtilities.isTypeBasedSpecalizationParameter(param2)) {
763
                                        CsmTypeBasedSpecializationParameter tbp1 = (CsmTypeBasedSpecializationParameter) param1;
770
                                        CsmTypeBasedSpecializationParameter tbp1 = (CsmTypeBasedSpecializationParameter) param1;
764
                                        CsmType type1 = tbp1.getType();
771
                                        CsmType type1 = paramsType.get(i);
765
                                        if(CsmKindUtilities.isInstantiation(cls)) {
772
//                                        CsmType type1 = tbp1.getType();
766
                                            type1 = Instantiation.createType(tbp1.getType(), (Instantiation)cls);
773
//                                        if(CsmKindUtilities.isInstantiation(cls)) {
767
                                        }
774
//                                            type1 = Instantiation.createType(tbp1.getType(), (Instantiation)cls);
775
//                                        }
768
                                        CsmClassifier tbsp1Cls = getClassifier(type1);
776
                                        CsmClassifier tbsp1Cls = getClassifier(type1);
769
                                        if (tbsp1Cls != null) {
777
                                        if (tbsp1Cls != null) {
770
                                            CsmTypeBasedSpecializationParameter tbp2 = (CsmTypeBasedSpecializationParameter) param2;
778
                                            CsmTypeBasedSpecializationParameter tbp2 = (CsmTypeBasedSpecializationParameter) param2;
771
                                            CsmType type2 = tbp2.getType();
779
                                            CsmType type2 = paramsType.get(j);
772
                                            if(CsmKindUtilities.isInstantiation(cls)) {
780
//                                            CsmType type2 = tbp2.getType();
773
                                                type2 = Instantiation.createType(tbp2.getType(), (Instantiation)cls);
781
//                                            if(CsmKindUtilities.isInstantiation(cls)) {
774
                                            }
782
//                                                type2 = Instantiation.createType(tbp2.getType(), (Instantiation)cls);
783
//                                            }
775
                                            CsmClassifier tbsp2Cls = getClassifier(type2);
784
                                            CsmClassifier tbsp2Cls = getClassifier(type2);
776
                                            if(tbsp2Cls != null) {
785
                                            if(tbsp2Cls != null) {
777
                                                if (tbsp1Cls.getQualifiedName().toString().equals(tbsp2Cls.getQualifiedName().toString())) {
786
                                                if (tbsp1Cls.getQualifiedName().toString().equals(tbsp2Cls.getQualifiedName().toString())) {
778
                                                    match += 1;
787
                                                    match += 1;
788
                                                } else {
789
                                                    tbsp1Cls = CsmBaseUtilities.getOriginalClassifier(tbsp1Cls, param1.getContainingFile());
790
                                                    tbsp2Cls = CsmBaseUtilities.getOriginalClassifier(tbsp2Cls, param2.getContainingFile());
791
                                                    if (tbsp1Cls.getQualifiedName().toString().equals(tbsp2Cls.getQualifiedName().toString())) {
792
                                                        match += 1;
793
                                                    }
779
                                                }
794
                                                }
780
                                            }
795
                                            }
781
                                        }
796
                                        }
Lines 785-791 Link Here
785
                        }
800
                        }
786
                        for (int i = 0; i < paramsSize; i++) {
801
                        for (int i = 0; i < paramsSize; i++) {
787
                            CsmSpecializationParameter specParam = specParams.get(i);
802
                            CsmSpecializationParameter specParam = specParams.get(i);
788
                            CsmSpecializationParameter param = params.get(i);
803
                            CsmSpecializationParameter param = params.get(i).first();
789
                            if (CsmKindUtilities.isTypeBasedSpecalizationParameter(specParam) &&
804
                            if (CsmKindUtilities.isTypeBasedSpecalizationParameter(specParam) &&
790
                                    CsmKindUtilities.isTypeBasedSpecalizationParameter(param)) {
805
                                    CsmKindUtilities.isTypeBasedSpecalizationParameter(param)) {
791
                                CsmTypeBasedSpecializationParameter instSpecParam = (CsmTypeBasedSpecializationParameter)param;                                
806
                                CsmTypeBasedSpecializationParameter instSpecParam = (CsmTypeBasedSpecializationParameter)param;                                
Lines 1017-1028 Link Here
1017
        return ExpressionBasedSpecializationParameterImpl.create(expression, file, start, end);
1032
        return ExpressionBasedSpecializationParameterImpl.create(expression, file, start, end);
1018
    }
1033
    }
1019
    
1034
    
1020
    public List<CsmSpecializationParameter> getInstantiationParams(CsmObject o) {
1035
    public List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> getInstantiationParams(CsmObject o) {
1021
        if (!CsmKindUtilities.isInstantiation(o)) {
1036
        if (!CsmKindUtilities.isInstantiation(o)) {
1022
            return Collections.emptyList();
1037
            return Collections.emptyList();
1023
        }
1038
        }
1024
        long time = System.currentTimeMillis();
1039
        long time = System.currentTimeMillis();
1025
        List<CsmSpecializationParameter> res = new ArrayList<CsmSpecializationParameter>();
1040
        List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> res = new ArrayList<Pair<CsmSpecializationParameter, List<CsmInstantiation>>>();
1026
        CsmInstantiation i = (CsmInstantiation) o;
1041
        CsmInstantiation i = (CsmInstantiation) o;
1027
        Map<CsmTemplateParameter, CsmSpecializationParameter> m = i.getMapping();
1042
        Map<CsmTemplateParameter, CsmSpecializationParameter> m = i.getMapping();
1028
        CsmOffsetableDeclaration decl = i.getTemplateDeclaration();
1043
        CsmOffsetableDeclaration decl = i.getTemplateDeclaration();
Lines 1032-1057 Link Here
1032
                for (CsmTemplateParameter tp : ((CsmTemplate)decl).getTemplateParameters()) {
1047
                for (CsmTemplateParameter tp : ((CsmTemplate)decl).getTemplateParameters()) {
1033
                    CsmSpecializationParameter sp = m.get(tp);
1048
                    CsmSpecializationParameter sp = m.get(tp);
1034
                    if(sp != null) {
1049
                    if(sp != null) {
1035
                        res.add(sp);
1050
                        List<CsmInstantiation> insts = new ArrayList<>();
1051
                        insts.add(i);
1052
                        res.add(Pair.of(sp, insts));
1036
                    }
1053
                    }
1037
                }
1054
                }
1038
            }
1055
            }
1039
        } else {
1056
        } else {
1040
            // non first inst
1057
            // non first inst
1041
            List<CsmSpecializationParameter> sps = getInstantiationParams(decl);
1058
            List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> sps = getInstantiationParams(decl);
1042
            for (CsmSpecializationParameter instParam : sps) {
1059
            for (Pair<CsmSpecializationParameter, List<CsmInstantiation>> pair : sps) {
1060
                CsmSpecializationParameter instParam = pair.first();
1061
                List<CsmInstantiation> instantiations = pair.second();
1043
                if (CsmKindUtilities.isTypeBasedSpecalizationParameter(instParam) &&
1062
                if (CsmKindUtilities.isTypeBasedSpecalizationParameter(instParam) &&
1044
                        CsmKindUtilities.isTemplateParameterType(((CsmTypeBasedSpecializationParameter) instParam).getType())) {
1063
                        CsmKindUtilities.isTemplateParameterType(((CsmTypeBasedSpecializationParameter) instParam).getType())) {
1045
                    CsmTemplateParameterType paramType = (CsmTemplateParameterType) ((CsmTypeBasedSpecializationParameter) instParam).getType();
1064
                    CsmTemplateParameterType paramType = (CsmTemplateParameterType) ((CsmTypeBasedSpecializationParameter) instParam).getType();
1046
                    CsmSpecializationParameter newTp = m.get(paramType.getParameter());
1065
                    CsmSpecializationParameter newTp = m.get(paramType.getParameter());
1047
                    if (newTp != null && newTp != instParam) {
1066
                    if (newTp != null && newTp != instParam) {
1048
                        res.add(newTp);
1067
                        instantiations.clear();
1068
                        res.add(Pair.of(newTp, instantiations));
1049
                    } else {
1069
                    } else {
1050
                        res.add(instParam);
1070
                        res.add(pair);
1051
                    }
1071
                    }
1052
                } else {
1072
                } else {
1053
                    res.add(instParam);
1073
                    res.add(pair);
1054
                }
1074
                }
1075
                instantiations.add(i);
1055
            }
1076
            }
1056
        }
1077
        }
1057
        time = System.currentTimeMillis() - time;
1078
        time = System.currentTimeMillis() - time;
Lines 1059-1064 Link Here
1059
        return res;
1080
        return res;
1060
    }    
1081
    }    
1061
    
1082
    
1083
    private List<CsmSpecializationParameter> getPlainParams(List<Pair<CsmSpecializationParameter, List<CsmInstantiation>>> instParams) {
1084
        List<CsmSpecializationParameter> params = new ArrayList<>(instParams.size());
1085
        for (Pair<CsmSpecializationParameter, List<CsmInstantiation>> pair : instParams) {
1086
            params.add(pair.first());
1087
        }
1088
        return params;
1089
    }
1090
    
1062
    private CsmClassForwardDeclaration findCsmClassForwardDeclaration(CsmScope scope, CsmClass cls) {
1091
    private CsmClassForwardDeclaration findCsmClassForwardDeclaration(CsmScope scope, CsmClass cls) {
1063
        if (scope != null) {
1092
        if (scope != null) {
1064
            if (CsmKindUtilities.isFile(scope)) {
1093
            if (CsmKindUtilities.isFile(scope)) {
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/impl/services/evaluator/VariableProvider.java (-7 / +28 lines)
Lines 67-72 Link Here
67
import org.netbeans.modules.cnd.api.model.CsmTemplateParameterType;
67
import org.netbeans.modules.cnd.api.model.CsmTemplateParameterType;
68
import org.netbeans.modules.cnd.api.model.CsmType;
68
import org.netbeans.modules.cnd.api.model.CsmType;
69
import org.netbeans.modules.cnd.api.model.CsmTypeBasedSpecializationParameter;
69
import org.netbeans.modules.cnd.api.model.CsmTypeBasedSpecializationParameter;
70
import org.netbeans.modules.cnd.api.model.deep.CsmExpression;
70
import org.netbeans.modules.cnd.api.model.services.CsmClassifierResolver;
71
import org.netbeans.modules.cnd.api.model.services.CsmClassifierResolver;
71
import org.netbeans.modules.cnd.api.model.services.CsmInstantiationProvider;
72
import org.netbeans.modules.cnd.api.model.services.CsmInstantiationProvider;
72
import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
73
import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
Lines 91-97 Link Here
91
 */
92
 */
92
public class VariableProvider {
93
public class VariableProvider {
93
    private static final Logger LOG = Logger.getLogger(VariableProvider.class.getSimpleName());
94
    private static final Logger LOG = Logger.getLogger(VariableProvider.class.getSimpleName());
94
    public static final int INFINITE_RECURSION = 10;
95
    public static final int INFINITE_RECURSION = 16;
95
96
96
    private final int level;
97
    private final int level;
97
    private CsmOffsetableDeclaration decl;
98
    private CsmOffsetableDeclaration decl;
Lines 142-147 Link Here
142
                            }
143
                            }
143
                        } else if (CsmKindUtilities.isTypeBasedSpecalizationParameter(spec)) {
144
                        } else if (CsmKindUtilities.isTypeBasedSpecalizationParameter(spec)) {
144
                            CsmTypeBasedSpecializationParameter specParameter = (CsmTypeBasedSpecializationParameter) spec;
145
                            CsmTypeBasedSpecializationParameter specParameter = (CsmTypeBasedSpecializationParameter) spec;
146
                            
147
// TODO: think about param decl and appropriate mapping (could be necessary if parameter is instantiation itself)
148
//                            CsmOffsetableDeclaration paramContextDecl = decl;
149
//                            MapHierarchy<CsmTemplateParameter, CsmSpecializationParameter> paramMapping = mapping;
150
//                            
151
//                            CsmType specParamType = specParameter.getType();
152
//                            CsmClassifier specParamCls = specParamType.getClassifier();
153
//                            if (CsmKindUtilities.isInstantiation(specParamCls) && CsmKindUtilities.isOffsetableDeclaration(specParamCls)) {
154
//                                paramContextDecl = (CsmOffsetableDeclaration) specParamCls;
155
//                                paramMapping = TemplateUtils.gatherMapping((CsmInstantiation) paramContextDecl);
156
//                            }
157
                            
145
                            Object eval = new ExpressionEvaluator(level+1).eval(specParameter.getText().toString(), decl, specParameter.getContainingFile(), specParameter.getStartOffset(), specParameter.getEndOffset(), mapping);
158
                            Object eval = new ExpressionEvaluator(level+1).eval(specParameter.getText().toString(), decl, specParameter.getContainingFile(), specParameter.getStartOffset(), specParameter.getEndOffset(), mapping);
146
                            if (eval instanceof Integer) {
159
                            if (eval instanceof Integer) {
147
                                return (Integer) eval;
160
                                return (Integer) eval;
Lines 156-167 Link Here
156
                    if (classMembers.hasNext()) {
169
                    if (classMembers.hasNext()) {
157
                        CsmMember member = classMembers.next();
170
                        CsmMember member = classMembers.next();
158
                        if(member.isStatic() && CsmKindUtilities.isField(member) && member.getName().toString().equals(variableName)) {
171
                        if(member.isStatic() && CsmKindUtilities.isField(member) && member.getName().toString().equals(variableName)) {
172
                            CsmExpression expr = ((CsmField)member).getInitialValue();
159
                            if(CsmKindUtilities.isInstantiation(member)) {
173
                            if(CsmKindUtilities.isInstantiation(member)) {
160
                                Object eval = new ExpressionEvaluator(level+1).eval(((CsmField)member).getInitialValue().getText().toString(), member.getContainingClass(), getMapping((CsmInstantiation) member));
174
                                Object eval = new ExpressionEvaluator(level+1).eval(
175
                                        expr.getText().toString(), 
176
                                        member.getContainingClass(), 
177
                                        expr.getContainingFile(),
178
                                        expr.getStartOffset(),
179
                                        expr.getEndOffset(),
180
                                        getMapping((CsmInstantiation) member)
181
                                );
161
                                if (eval instanceof Integer) {
182
                                if (eval instanceof Integer) {
162
                                    return (Integer) eval;
183
                                    return (Integer) eval;
163
                                }
184
                                }
164
                            } else if (((CsmField)member).getInitialValue() != null) {
185
                            } else if (expr != null) {
165
                                Object eval = new ExpressionEvaluator(level+1).eval(((CsmField)member).getInitialValue().getText().toString(), member.getContainingClass(), Collections.<CsmTemplateParameter, CsmSpecializationParameter>emptyMap());
186
                                Object eval = new ExpressionEvaluator(level+1).eval(((CsmField)member).getInitialValue().getText().toString(), member.getContainingClass(), Collections.<CsmTemplateParameter, CsmSpecializationParameter>emptyMap());
166
                                if (eval instanceof Integer) {
187
                                if (eval instanceof Integer) {
167
                                    return (Integer) eval;
188
                                    return (Integer) eval;
Lines 171-177 Link Here
171
                    }
192
                    }
172
                }
193
                }
173
194
174
                boolean executeSimpleResolution = !(TraceFlags.EXPRESSION_EVALUATOR_DEEP_VARIABLE_PROVIDER && variableName.contains("<")); // NOI18N
195
                boolean executeSimpleResolution = false;// !(TraceFlags.EXPRESSION_EVALUATOR_DEEP_VARIABLE_PROVIDER && variableName.contains("<")); // NOI18N
175
196
176
                if (executeSimpleResolution) {
197
                if (executeSimpleResolution) {
177
                    CsmObject o = null;
198
                    CsmObject o = null;
Lines 242-250 Link Here
242
                                }
263
                                }
243
                            }
264
                            }
244
265
245
                            if(CsmKindUtilities.isInstantiation(decl)) {
266
//                            if(CsmKindUtilities.isInstantiation(decl)) {
246
                                type = Instantiation.createType(type, (Instantiation)decl);
267
//                                type = Instantiation.createType(type, (Instantiation)decl);
247
                            }
268
//                            }
248
269
249
                            CsmClassifier originalClassifier = CsmClassifierResolver.getDefault().getOriginalClassifier(type.getClassifier(), decl.getContainingFile());
270
                            CsmClassifier originalClassifier = CsmClassifierResolver.getDefault().getOriginalClassifier(type.getClassifier(), decl.getContainingFile());
250
                            if (CsmKindUtilities.isTemplate(originalClassifier)) {
271
                            if (CsmKindUtilities.isTemplate(originalClassifier)) {
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/impl/services/evaluator/parser/Evaluator.g (-15 / +29 lines)
Lines 37-48 Link Here
37
    ;
37
    ;
38
38
39
multExpr returns [int value]
39
multExpr returns [int value]
40
    :   e=atom {$value = $e.value;} (STAR e=atom {$value *= $e.value;})*
40
    :   e=unaryExpr {$value = $e.value;} (STAR e=unaryExpr {$value *= $e.value;})*
41
    ;
42
43
unaryExpr returns [int value]
44
    :   e=atom {$value = $e.value;}
45
    |   NOT e=unaryExpr {$value = ($e.value == 0 ? 1 : 0);}
41
    ;
46
    ;
42
47
43
atom returns [int value]
48
atom returns [int value]
44
    :   DECIMALINT {$value = (($DECIMALINT.text) == null) ? 0 : Integer.parseInt(($DECIMALINT.text).replaceAll("[a-z,A-Z,_].*", "")) ;}
49
    :   
45
    |   id = qualified_id
50
        DECIMALINT {$value = (($DECIMALINT.text) == null) ? 0 : Integer.parseInt(($DECIMALINT.text).replaceAll("[a-z,A-Z,_].*", "")) ;}
51
    |   
52
        (LITERAL_const)*
53
        id = qualified_id
46
        {
54
        {
47
            $value = vp==null?0:vp.getValue($id.q);
55
            $value = vp==null?0:vp.getValue($id.q);
48
            //Integer v = (Integer)memory.get($IDENT.text);
56
            //Integer v = (Integer)memory.get($IDENT.text);
Lines 69-79 Link Here
69
            IDENT
77
            IDENT
70
            {q += $IDENT.text;}
78
            {q += $IDENT.text;}
71
            (
79
            (
72
                LESSTHAN
80
                inner = balance_less_greater {q += $inner.s;}
73
                {q += "<";}
74
                (x=~GREATERTHAN {q += $x.text;})*
75
                GREATERTHAN
76
                {q += ">";}
77
            )?
81
            )?
78
        )
82
        )
79
    ;
83
    ;
Lines 84-90 Link Here
84
            SCOPE { s += "::";}
88
            SCOPE { s += "::";}
85
        )?
89
        )?
86
        (
90
        (
87
            sp = scope_override_part
91
            (IDENT (balance_less_greater)? SCOPE)=> sp = scope_override_part
88
            {
92
            {
89
                    s += ($sp.s != null) ? $sp.s : "";
93
                    s += ($sp.s != null) ? $sp.s : "";
90
            }
94
            }
Lines 98-120 Link Here
98
            s += $IDENT.text;
102
            s += $IDENT.text;
99
        }
103
        }
100
        (
104
        (
101
            LESSTHAN
105
            inner = balance_less_greater {s += $inner.s;}
102
            {s += "<";}
103
            (x=~GREATERTHAN {s += $x.text;})*
104
            GREATERTHAN
105
            {s += ">";}
106
        )?
106
        )?
107
        SCOPE
107
        SCOPE
108
        {
108
        {
109
            s += "::";
109
            s += "::";
110
        }
110
        }
111
111
112
        ((IDENT SCOPE) => sp = scope_override_part)?
112
        ((IDENT (balance_less_greater)? SCOPE)=> sp = scope_override_part)?
113
        {
113
        {
114
            s += ($sp.s != null) ? $sp.s : "";
114
            s += ($sp.s != null) ? $sp.s : "";
115
        }
115
        }
116
    ;
116
    ;
117
117
118
balance_less_greater returns [String s = ""]
119
    :
120
        LESSTHAN {s += "<";}
121
        (
122
            (LESSTHAN)=> inner = balance_less_greater {s += $inner.s;}
123
        |
124
            other = (~GREATERTHAN) {s += $other.text;}
125
        )*
126
        GREATERTHAN {s += ">";}
127
    ;
128
118
// Suppressing warnings "no lexer rule corresponding to token"
129
// Suppressing warnings "no lexer rule corresponding to token"
119
fragment IDENT: ' ';
130
fragment IDENT: ' ';
120
fragment DECIMALINT: ' ';
131
fragment DECIMALINT: ' ';
Lines 124-129 Link Here
124
fragment STAR: ' ';
135
fragment STAR: ' ';
125
fragment LESSTHAN: ' ';
136
fragment LESSTHAN: ' ';
126
fragment GREATERTHAN: ' ';
137
fragment GREATERTHAN: ' ';
138
fragment NOT: ' ';
127
139
128
fragment SCOPE: ' ';
140
fragment SCOPE: ' ';
129
141
Lines 134-136 Link Here
134
fragment LITERAL_true: ' ';
146
fragment LITERAL_true: ' ';
135
fragment LITERAL_false: ' ';
147
fragment LITERAL_false: ' ';
136
148
149
fragment LITERAL_const: ' ';
150
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/util/MapHierarchy.java (+6 lines)
Lines 66-71 Link Here
66
        push(map);
66
        push(map);
67
    }    
67
    }    
68
    
68
    
69
    public MapHierarchy(MapHierarchy<K, V> mapHierarchy) {
70
        for (Map<K, V> map : mapHierarchy.maps) {
71
            push(map);
72
        }
73
    }        
74
    
69
    public void push(Map<K, V> map) {
75
    public void push(Map<K, V> map) {
70
        maps.push(map);
76
        maps.push(map);
71
    }
77
    }
(-)a/cnd.modelutil/src/org/netbeans/modules/cnd/modelutil/CsmUtilities.java (-2 / +50 lines)
Lines 83-88 Link Here
83
import org.netbeans.modules.cnd.utils.CndPathUtilities;
83
import org.netbeans.modules.cnd.utils.CndPathUtilities;
84
import org.netbeans.modules.cnd.utils.CndUtils;
84
import org.netbeans.modules.cnd.utils.CndUtils;
85
import org.netbeans.modules.cnd.utils.FSPath;
85
import org.netbeans.modules.cnd.utils.FSPath;
86
import org.netbeans.modules.cnd.utils.MutableObject;
86
import org.netbeans.modules.editor.NbEditorUtilities;
87
import org.netbeans.modules.editor.NbEditorUtilities;
87
import org.openide.awt.StatusDisplayer;
88
import org.openide.awt.StatusDisplayer;
88
import org.openide.cookies.EditorCookie;
89
import org.openide.cookies.EditorCookie;
Lines 1152-1162 Link Here
1152
     * @return last type
1153
     * @return last type
1153
     */
1154
     */
1154
    public static CsmType iterateTypeChain(CsmType type, Predicate<CsmType> stopFilter) {
1155
    public static CsmType iterateTypeChain(CsmType type, Predicate<CsmType> stopFilter) {
1156
        return iterateTypeChain(type, stopFilter, 50);
1157
    }
1158
    
1159
    /**
1160
     * Iterates type chain until end is reached or stopFilter returned true
1161
     * @param type from iterate should start
1162
     * @param stopFilter
1163
     * @param maxDepth - max number of iterations
1164
     * @return last type
1165
     */    
1166
    public static CsmType iterateTypeChain(CsmType type, Predicate<CsmType> stopFilter, int maxDepth) {
1155
        CsmType lastNestedType = type;
1167
        CsmType lastNestedType = type;
1156
        
1168
        
1157
        Set<CsmType> antiLoop = new HashSet<CsmType>();
1169
        Set<CsmType> antiLoop = new HashSet<CsmType>();
1158
        
1170
        
1159
        while (type != null && !antiLoop.contains(type) && antiLoop.size() < 50) {
1171
        while (type != null && !antiLoop.contains(type) && antiLoop.size() < maxDepth) {
1160
            lastNestedType = type;
1172
            lastNestedType = type;
1161
            
1173
            
1162
            if (stopFilter.check(type)) {
1174
            if (stopFilter.check(type)) {
Lines 1175-1181 Link Here
1175
        }
1187
        }
1176
        
1188
        
1177
        return lastNestedType;
1189
        return lastNestedType;
1178
    }                 
1190
    }    
1191
    
1192
//    public static CsmType checkPointerDepth(CsmType type, int depth, int maxIterations) {
1193
//        final MutableObject<Integer> result = new MutableObject<>(0);
1194
//        iterateTypeChain(type, new Predicate<CsmType>() {
1195
//
1196
//            @Override
1197
//            public boolean check(CsmType value) {
1198
//                if (value.isPointer()) {
1199
//                    result.value += value.getPointerDepth();
1200
//                }
1201
//                return false;
1202
//            }
1203
//            
1204
//        }, maxDepth);
1205
//        return result.value;
1206
//    }    
1207
//
1208
//    public static int checkReference(CsmType type, int maxDepth) {
1209
//        final MutableObject<Integer> result = new MutableObject<>(0);
1210
//        iterateTypeChain(type, new Predicate<CsmType>() {
1211
//
1212
//            @Override
1213
//            public boolean check(CsmType value) {
1214
//                if (value.isReference()) {
1215
//                    if (value.isRValueReference()) {
1216
//                        result.value = 2;
1217
//                    }
1218
//                    result.value = 1;
1219
//                    return true;
1220
//                }
1221
//                return false;
1222
//            }
1223
//            
1224
//        }, maxDepth);
1225
//        return result.value;
1226
//    }    
1179
    
1227
    
1180
    //-----------------------------------------------------------------
1228
    //-----------------------------------------------------------------
1181
1229

Return to bug 235102