Introduce JetParameter#hasValOrVarNode() and use it instead of getValOrVarNode()

This commit is contained in:
Pavel V. Talanov
2014-04-07 17:48:49 +04:00
parent 26452c845c
commit 9aa5681d80
22 changed files with 29 additions and 21 deletions
@@ -123,7 +123,7 @@ public abstract class DataClassMethodGenerator {
private List<PropertyDescriptor> getDataProperties() { private List<PropertyDescriptor> getDataProperties() {
List<PropertyDescriptor> result = Lists.newArrayList(); List<PropertyDescriptor> result = Lists.newArrayList();
for (JetParameter parameter : getPrimaryConstructorParameters()) { for (JetParameter parameter : getPrimaryConstructorParameters()) {
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
result.add(bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter)); result.add(bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter));
} }
} }
@@ -110,7 +110,7 @@ public abstract class ClassBodyCodegen extends MemberCodegen<JetClassOrObject> {
private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) { private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) {
boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation(); boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation();
for (JetParameter p : getPrimaryConstructorParameters()) { for (JetParameter p : getPrimaryConstructorParameters()) {
if (p.getValOrVarNode() != null) { if (p.hasValOrVarNode()) {
PropertyDescriptor propertyDescriptor = state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p); PropertyDescriptor propertyDescriptor = state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p);
if (propertyDescriptor != null) { if (propertyDescriptor != null) {
if (!isAnnotation) { if (!isAnnotation) {
@@ -1193,7 +1193,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
int curParam = 0; int curParam = 0;
List<JetParameter> constructorParameters = getPrimaryConstructorParameters(); List<JetParameter> constructorParameters = getPrimaryConstructorParameters();
for (JetParameter parameter : constructorParameters) { for (JetParameter parameter : constructorParameters) {
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
VariableDescriptor descriptor = paramDescrs.get(curParam); VariableDescriptor descriptor = paramDescrs.get(curParam);
Type type = typeMapper.mapType(descriptor); Type type = typeMapper.mapType(descriptor);
iv.load(0, classAsmType); iv.load(0, classAsmType);
@@ -83,6 +83,14 @@ public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> {
return modifierList != null && modifierList.getModifierNode(JetTokens.VARARG_KEYWORD) != null; return modifierList != null && modifierList.getModifierNode(JetTokens.VARARG_KEYWORD) != null;
} }
public boolean hasValOrVarNode() {
PsiJetParameterStub stub = getStub();
if (stub != null) {
return stub.hasValOrValNode();
}
return getValOrVarNode() != null;
}
@Nullable @Nullable
public ASTNode getValOrVarNode() { public ASTNode getValOrVarNode() {
PsiJetParameterStub stub = getStub(); PsiJetParameterStub stub = getStub();
@@ -518,7 +518,7 @@ public class JetPsiUtil {
@Nullable @Nullable
public static JetClass getClassIfParameterIsProperty(@NotNull JetParameter jetParameter) { public static JetClass getClassIfParameterIsProperty(@NotNull JetParameter jetParameter) {
if (jetParameter.getValOrVarNode() != null) { if (jetParameter.hasValOrVarNode()) {
PsiElement parent = jetParameter.getParent(); PsiElement parent = jetParameter.getParent();
if (parent instanceof JetParameterList && parent.getParent() instanceof JetClass) { if (parent instanceof JetParameterList && parent.getParent() instanceof JetClass) {
return (JetClass) parent.getParent(); return (JetClass) parent.getParent();
@@ -85,7 +85,7 @@ public fun PsiElement.getParentByTypeAndBranch<T: PsiElement>(
public fun JetClassOrObject.effectiveDeclarations(): List<JetDeclaration> = public fun JetClassOrObject.effectiveDeclarations(): List<JetDeclaration> =
when(this) { when(this) {
is JetClass -> is JetClass ->
getDeclarations() + getPrimaryConstructorParameters().filter { p -> p.getValOrVarNode() != null } getDeclarations() + getPrimaryConstructorParameters().filter { p -> p.hasValOrVarNode() }
else -> else ->
getDeclarations() getDeclarations()
} }
@@ -39,7 +39,7 @@ public class JetParameterElementType extends JetStubElementType<PsiJetParameterS
@Override @Override
public PsiJetParameterStub createStub(@NotNull JetParameter psi, StubElement parentStub) { public PsiJetParameterStub createStub(@NotNull JetParameter psi, StubElement parentStub) {
return new PsiJetParameterStubImpl(parentStub, psi.getFqName(), StringRef.fromString(psi.getName()), return new PsiJetParameterStubImpl(parentStub, psi.getFqName(), StringRef.fromString(psi.getName()),
psi.isMutable(), psi.isVarArg(), psi.getValOrVarNode() != null, psi.isMutable(), psi.isVarArg(), psi.hasValOrVarNode(),
psi.getDefaultValue() != null); psi.getDefaultValue() != null);
} }
@@ -162,7 +162,7 @@ public interface BindingContext {
PsiElement declarationPsiElement = map.get(BindingContextUtils.DESCRIPTOR_TO_DECLARATION, propertyDescriptor); PsiElement declarationPsiElement = map.get(BindingContextUtils.DESCRIPTOR_TO_DECLARATION, propertyDescriptor);
if (declarationPsiElement instanceof JetParameter) { if (declarationPsiElement instanceof JetParameter) {
JetParameter jetParameter = (JetParameter) declarationPsiElement; JetParameter jetParameter = (JetParameter) declarationPsiElement;
return jetParameter.getValOrVarNode() != null || return jetParameter.hasValOrVarNode() ||
backingFieldRequired; // this part is unused because we do not allow access to constructor parameters in member bodies backingFieldRequired; // this part is unused because we do not allow access to constructor parameters in member bodies
} }
if (propertyDescriptor.getModality() == Modality.ABSTRACT) return false; if (propertyDescriptor.getModality() == Modality.ABSTRACT) return false;
@@ -257,7 +257,7 @@ public class DeclarationResolver {
List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>(); List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>();
for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) { for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) {
JetParameter parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex()); JetParameter parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex());
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePrimaryConstructorParameterToAProperty( PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePrimaryConstructorParameterToAProperty(
classDescriptor, classDescriptor,
valueParameterDescriptor, valueParameterDescriptor,
@@ -318,7 +318,7 @@ public class DeclarationsChecker {
private void checkValOnAnnotationParameter(JetClass aClass) { private void checkValOnAnnotationParameter(JetClass aClass) {
for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) { for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) {
if (parameter.getValOrVarNode() == null) { if (!parameter.hasValOrVarNode()) {
trace.report(MISSING_VAL_ON_ANNOTATION_PARAMETER.on(parameter)); trace.report(MISSING_VAL_ON_ANNOTATION_PARAMETER.on(parameter));
} }
} }
@@ -1438,7 +1438,7 @@ public class DescriptorResolver {
@NotNull JetParameter parameter @NotNull JetParameter parameter
) { ) {
// If is not a property, then it must have no modifier // If is not a property, then it must have no modifier
if (parameter.getValOrVarNode() == null) { if (!parameter.hasValOrVarNode()) {
checkParameterHasNoModifier(trace, parameter); checkParameterHasNoModifier(trace, parameter);
} }
} }
@@ -180,7 +180,7 @@ public class LazyTopDownAnalyzer {
private void registerPrimaryConstructorParameters(@NotNull JetClass klass) { private void registerPrimaryConstructorParameters(@NotNull JetClass klass) {
for (JetParameter jetParameter : klass.getPrimaryConstructorParameters()) { for (JetParameter jetParameter : klass.getPrimaryConstructorParameters()) {
if (jetParameter.getValOrVarNode() != null) { if (jetParameter.hasValOrVarNode()) {
c.getPrimaryConstructorParameterProperties().put( c.getPrimaryConstructorParameterProperties().put(
jetParameter, jetParameter,
(PropertyDescriptor) resolveSession.resolveToDescriptor(jetParameter) (PropertyDescriptor) resolveSession.resolveToDescriptor(jetParameter)
@@ -407,7 +407,7 @@ public class ResolveSession implements KotlinCodeAnalyzer {
JetClass jetClass = (JetClass) grandFather; JetClass jetClass = (JetClass) grandFather;
// This is a primary constructor parameter // This is a primary constructor parameter
ClassDescriptor classDescriptor = getClassDescriptor(jetClass); ClassDescriptor classDescriptor = getClassDescriptor(jetClass);
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
classDescriptor.getDefaultType().getMemberScope().getProperties(safeNameForLazyResolve(parameter)); classDescriptor.getDefaultType().getMemberScope().getProperties(safeNameForLazyResolve(parameter));
return getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter); return getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
} }
@@ -48,7 +48,7 @@ public class PsiBasedClassMemberDeclarationProvider extends AbstractPsiBasedDecl
} }
for (JetParameter parameter : classInfo.getPrimaryConstructorParameters()) { for (JetParameter parameter : classInfo.getPrimaryConstructorParameters()) {
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
index.putToIndex(parameter); index.putToIndex(parameter);
} }
} }
@@ -282,7 +282,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, primaryConstructor, RedeclarationHandler.DO_NOTHING, "Scope with constructor parameters in " + getName()); WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, primaryConstructor, RedeclarationHandler.DO_NOTHING, "Scope with constructor parameters in " + getName());
for (int i = 0; i < originalClassInfo.getPrimaryConstructorParameters().size(); i++) { for (int i = 0; i < originalClassInfo.getPrimaryConstructorParameters().size(); i++) {
JetParameter jetParameter = originalClassInfo.getPrimaryConstructorParameters().get(i); JetParameter jetParameter = originalClassInfo.getPrimaryConstructorParameters().get(i);
if (jetParameter.getValOrVarNode() == null) { if (!jetParameter.hasValOrVarNode()) {
scope.addVariableDescriptor(primaryConstructor.getValueParameters().get(i)); scope.addVariableDescriptor(primaryConstructor.getValueParameters().get(i));
} }
} }
@@ -248,7 +248,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
if (!name.equals(valueParameterDescriptor.getName())) continue; if (!name.equals(valueParameterDescriptor.getName())) continue;
JetParameter parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex()); JetParameter parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex());
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
PropertyDescriptor propertyDescriptor = PropertyDescriptor propertyDescriptor =
resolveSession.getDescriptorResolver().resolvePrimaryConstructorParameterToAProperty( resolveSession.getDescriptorResolver().resolvePrimaryConstructorParameterToAProperty(
thisDescriptor, thisDescriptor,
@@ -150,7 +150,7 @@ public class JetSourceNavigationHelper {
// property constructor parameters // property constructor parameters
List<JetParameter> constructorParameters = containingClass.getPrimaryConstructorParameters(); List<JetParameter> constructorParameters = containingClass.getPrimaryConstructorParameters();
for (JetParameter constructorParameter : constructorParameters) { for (JetParameter constructorParameter : constructorParameters) {
if (memberName.equals(constructorParameter.getNameAsName()) && constructorParameter.getValOrVarNode() != null) { if (memberName.equals(constructorParameter.getNameAsName()) && constructorParameter.hasValOrVarNode()) {
return constructorParameter; return constructorParameter;
} }
} }
@@ -89,7 +89,7 @@ public class KotlinReferencesSearcher extends QueryExecutorBase<PsiReference, Re
else if (element instanceof KotlinLightMethod) { else if (element instanceof KotlinLightMethod) {
JetDeclaration declaration = ((KotlinLightMethod) element).getOrigin(); JetDeclaration declaration = ((KotlinLightMethod) element).getOrigin();
if (declaration instanceof JetProperty if (declaration instanceof JetProperty
|| (declaration instanceof JetParameter && ((JetParameter) declaration).getValOrVarNode() != null)) { || (declaration instanceof JetParameter && ((JetParameter) declaration).hasValOrVarNode())) {
searchNamedElement(queryParameters, (PsiNamedElement) declaration); searchNamedElement(queryParameters, (PsiNamedElement) declaration);
} }
else if (declaration instanceof JetPropertyAccessor) { else if (declaration instanceof JetPropertyAccessor) {
@@ -61,7 +61,7 @@ fun JetNamedDeclaration.namesWithAccessors(readable: Boolean = true, writable: B
is JetProperty -> is JetProperty ->
return LightClassUtil.getLightClassPropertyMethods(this).toNameList() return LightClassUtil.getLightClassPropertyMethods(this).toNameList()
is JetParameter -> is JetParameter ->
if (getValOrVarNode() != null) { if (hasValOrVarNode()) {
return LightClassUtil.getLightClassPropertyMethods(this).toNameList() return LightClassUtil.getLightClassPropertyMethods(this).toNameList()
} }
} }
@@ -139,7 +139,7 @@ fun PsiReference.isUsageInContainingDeclaration(declaration: JetNamedDeclaration
fun PsiReference.isCallableOverrideUsage(declaration: JetNamedDeclaration): Boolean { fun PsiReference.isCallableOverrideUsage(declaration: JetNamedDeclaration): Boolean {
val decl2Desc = {(declaration: JetDeclaration) -> val decl2Desc = {(declaration: JetDeclaration) ->
if (declaration is JetParameter && declaration.getValOrVarNode() != null) declaration.propertyDescriptor else declaration.descriptor if (declaration is JetParameter && declaration.hasValOrVarNode()) declaration.propertyDescriptor else declaration.descriptor
} }
return checkUsageVsOriginalDescriptor(declaration, decl2Desc) { (usageDescriptor, targetDescriptor) -> return checkUsageVsOriginalDescriptor(declaration, decl2Desc) { (usageDescriptor, targetDescriptor) ->
@@ -126,7 +126,7 @@ public class JetStructureViewElement implements StructureViewTreeElement {
JetClass jetClass = (JetClass) myElement; JetClass jetClass = (JetClass) myElement;
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>(); List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
for (JetParameter parameter : jetClass.getPrimaryConstructorParameters()) { for (JetParameter parameter : jetClass.getPrimaryConstructorParameters()) {
if (parameter.getValOrVarNode() != null) { if (parameter.hasValOrVarNode()) {
declarations.add(parameter); declarations.add(parameter);
} }
} }
@@ -63,7 +63,7 @@ class JsDataClassGenerator extends DataClassMethodGenerator {
JsExpression argumentValue; JsExpression argumentValue;
JsExpression parameterValue = new JsNameRef(paramName); JsExpression parameterValue = new JsNameRef(paramName);
if (constructorParam.getValOrVarNode() == null) { if (!constructorParam.hasValOrVarNode()) {
assert !function.getValueParameters().get(i).hasDefaultValue(); assert !function.getValueParameters().get(i).hasDefaultValue();
// Caller cannot rely on default value and pass undefined here. // Caller cannot rely on default value and pass undefined here.
argumentValue = parameterValue; argumentValue = parameterValue;