Browse Source

Add `//+private file` to complement `//+private` (`//+private package`)

gingerBill 3 years ago
parent
commit
78815778ee
5 changed files with 32 additions and 11 deletions
  1. 6 3
      src/checker.cpp
  2. 1 1
      src/entity.cpp
  3. 10 2
      src/parser.cpp
  4. 5 3
      src/parser.hpp
  5. 10 2
      src/string.cpp

+ 6 - 3
src/checker.cpp

@@ -3467,9 +3467,12 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) {
 
 	if (entity_visibility_kind == EntityVisiblity_Public &&
 	    (c->scope->flags&ScopeFlag_File) &&
-	    c->scope->file &&
-	    (c->scope->file->flags & AstFile_IsPrivate)) {
-		entity_visibility_kind = EntityVisiblity_PrivateToPackage;
+	    c->scope->file) {
+	    	if (c->scope->file->flags & AstFile_IsPrivatePkg) {
+			entity_visibility_kind = EntityVisiblity_PrivateToPackage;
+	    	} else if (c->scope->file->flags & AstFile_IsPrivateFile) {
+			entity_visibility_kind = EntityVisiblity_PrivateToFile;
+		}
 	}
 
 	if (entity_visibility_kind != EntityVisiblity_Public && !(c->scope->flags&ScopeFlag_File)) {

+ 1 - 1
src/entity.cpp

@@ -245,7 +245,7 @@ bool is_entity_exported(Entity *e, bool allow_builtin = false) {
 	if (e->flags & EntityFlag_NotExported) {
 		return false;
 	}
-	if (e->file != nullptr && (e->file->flags & AstFile_IsPrivate) != 0) {
+	if (e->file != nullptr && (e->file->flags & (AstFile_IsPrivatePkg|AstFile_IsPrivateFile)) != 0) {
 		return false;
 	}
 

+ 10 - 2
src/parser.cpp

@@ -5535,8 +5535,16 @@ bool parse_file(Parser *p, AstFile *f) {
 						if (!parse_build_tag(tok, lc)) {
 							return false;
 						}
-					} else if (lc == "+private") {
-						f->flags |= AstFile_IsPrivate;
+					} else if (string_starts_with(lc, str_lit("+private"))) {
+						f->flags |= AstFile_IsPrivatePkg;
+						String command = string_trim_starts_with(lc, str_lit("+private "));
+						if (lc == "+private") {
+							f->flags |= AstFile_IsPrivatePkg;
+						} else if (command == "package") {
+							f->flags |= AstFile_IsPrivatePkg;
+						} else if (command == "file") {
+							f->flags |= AstFile_IsPrivateFile;
+						}
 					} else if (lc == "+lazy") {
 						if (build_context.ignore_lazy) {
 							// Ignore

+ 5 - 3
src/parser.hpp

@@ -78,9 +78,11 @@ struct ImportedFile {
 };
 
 enum AstFileFlag : u32 {
-	AstFile_IsPrivate = 1<<0,
-	AstFile_IsTest    = 1<<1,
-	AstFile_IsLazy    = 1<<2,
+	AstFile_IsPrivatePkg = 1<<0,
+	AstFile_IsPrivateFile = 1<<1,
+
+	AstFile_IsTest    = 1<<3,
+	AstFile_IsLazy    = 1<<4,
 };
 
 enum AstDelayQueueKind {

+ 10 - 2
src/string.cpp

@@ -195,8 +195,6 @@ template <isize N> bool operator >  (String const &a, char const (&b)[N]) { retu
 template <isize N> bool operator <= (String const &a, char const (&b)[N]) { return str_le(a, make_string(cast(u8 *)b, N-1)); }
 template <isize N> bool operator >= (String const &a, char const (&b)[N]) { return str_ge(a, make_string(cast(u8 *)b, N-1)); }
 
-
-
 gb_inline bool string_starts_with(String const &s, String const &prefix) {
 	if (prefix.len > s.len) {
 		return false;
@@ -230,6 +228,16 @@ gb_inline bool string_ends_with(String const &s, u8 suffix) {
 	return s[s.len-1] == suffix;
 }
 
+
+
+gb_inline String string_trim_starts_with(String const &s, String const &prefix) {
+	if (string_starts_with(s, prefix)) {
+		return substring(s, prefix.len, s.len);
+	}
+	return s;
+}
+
+
 gb_inline isize string_extension_position(String const &str) {
 	isize dot_pos = -1;
 	isize i = str.len;