• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.squareup.kotlinpoet
17 
18 import com.squareup.kotlinpoet.KModifier.INTERNAL
19 import com.squareup.kotlinpoet.KModifier.PRIVATE
20 import com.squareup.kotlinpoet.KModifier.PROTECTED
21 import com.squareup.kotlinpoet.KModifier.PUBLIC
22 import java.util.EnumSet
23 
24 public enum class KModifier(
25   internal val keyword: String,
26   private vararg val targets: Target,
27 ) {
28   // Modifier order defined here:
29   // https://kotlinlang.org/docs/reference/coding-conventions.html#modifiers
30 
31   // Access.
32   PUBLIC("public", Target.PROPERTY),
33   PROTECTED("protected", Target.PROPERTY),
34   PRIVATE("private", Target.PROPERTY),
35   INTERNAL("internal", Target.PROPERTY),
36 
37   // Multiplatform modules.
38   EXPECT("expect", Target.CLASS, Target.FUNCTION, Target.PROPERTY),
39   ACTUAL("actual", Target.CLASS, Target.FUNCTION, Target.PROPERTY),
40 
41   FINAL("final", Target.CLASS, Target.FUNCTION, Target.PROPERTY),
42   OPEN("open", Target.CLASS, Target.FUNCTION, Target.PROPERTY),
43   ABSTRACT("abstract", Target.CLASS, Target.FUNCTION, Target.PROPERTY),
44   SEALED("sealed", Target.CLASS),
45   CONST("const", Target.PROPERTY),
46 
47   EXTERNAL("external", Target.CLASS, Target.FUNCTION, Target.PROPERTY),
48   OVERRIDE("override", Target.FUNCTION, Target.PROPERTY),
49   LATEINIT("lateinit", Target.PROPERTY),
50   TAILREC("tailrec", Target.FUNCTION),
51   VARARG("vararg", Target.PARAMETER),
52   SUSPEND("suspend", Target.FUNCTION),
53   INNER("inner", Target.CLASS),
54 
55   ENUM("enum", Target.CLASS),
56   ANNOTATION("annotation", Target.CLASS),
57   VALUE("value", Target.CLASS),
58   FUN("fun", Target.INTERFACE),
59 
60   COMPANION("companion", Target.CLASS),
61 
62   // Call-site compiler tips.
63   INLINE("inline", Target.FUNCTION),
64   NOINLINE("noinline", Target.PARAMETER),
65   CROSSINLINE("crossinline", Target.PARAMETER),
66   REIFIED("reified", Target.TYPE_PARAMETER),
67 
68   INFIX("infix", Target.FUNCTION),
69   OPERATOR("operator", Target.FUNCTION),
70 
71   DATA("data", Target.CLASS),
72 
73   IN("in", Target.VARIANCE_ANNOTATION),
74   OUT("out", Target.VARIANCE_ANNOTATION),
75   ;
76 
77   internal enum class Target {
78     CLASS,
79     VARIANCE_ANNOTATION,
80     PARAMETER,
81     TYPE_PARAMETER,
82     FUNCTION,
83     PROPERTY,
84     INTERFACE,
85   }
86 
checkTargetnull87   internal fun checkTarget(target: Target) {
88     require(target in targets) { "unexpected modifier $this for $target" }
89   }
90 }
91 
92 internal val VISIBILITY_MODIFIERS: Set<KModifier> = EnumSet.of(PUBLIC, INTERNAL, PROTECTED, PRIVATE)
93