Kaynağa Gözat

Added Private/Protected support for Type members.

woollybah 8 yıl önce
ebeveyn
işleme
052e06b0cc
5 değiştirilmiş dosya ile 147 ekleme ve 9 silme
  1. 23 1
      ctranslator.bmx
  2. 50 3
      decl.bmx
  3. 57 0
      iparser.bmx
  4. 16 4
      parser.bmx
  5. 1 1
      toker.bmx

+ 23 - 1
ctranslator.bmx

@@ -3744,6 +3744,7 @@ End Rem
 
 	Method EmitClassDecl( classDecl:TClassDecl )
 
+		PushEnv classDecl
 		'If classDecl.IsTemplateInst()
 		'	Return
 		'EndIf
@@ -4063,6 +4064,8 @@ End Rem
 			End If
 			
 		End If
+		
+		PopEnv
 
 	End Method
 
@@ -4626,6 +4629,13 @@ End Rem
 		If funcDecl.attrs & FUNC_OPERATOR Then
 			func :+ "O"
 		End If
+		
+		If funcDecl.attrs & DECL_PRIVATE Then
+			func :+ "P"
+		Else If funcDecl.attrs & DECL_PROTECTED Then
+			func :+ "R"
+		End If
+
 
 		func :+ "="
 
@@ -4752,6 +4762,12 @@ End Rem
 		Local f:String = "." + fieldDecl.ident + TransIfcType(fieldDecl.ty, fieldDecl.ModuleScope().IsSuperStrict())
 
 		f :+ "&"
+		
+		If fieldDecl.IsPrivate() Then
+			f :+ "`"
+		Else If fieldDecl.IsProtected() Then
+			f :+ "``"
+		End If
 
 		Emit f
 	End Method
@@ -4841,7 +4857,7 @@ End Rem
 			Else If classDecl.IsStruct() Then
 				flags :+ "S"
 			End If
-
+			
 			Emit "}" + flags + "=" + Enquote(classDecl.munged), False
 		Else
 			For Local decl:TDecl=EachIn classDecl.Decls()
@@ -4884,6 +4900,12 @@ End Rem
 
 		g:+ "&"
 
+		If globalDecl.IsPrivate() Then
+			g :+ "`"
+		Else If globalDecl.IsProtected() Then
+			g :+ "``"
+		End If
+
 		g :+ "="
 
 		g :+ "mem:p("

+ 50 - 3
decl.bmx

@@ -36,6 +36,7 @@ Const DECL_ARG:Int=         $800000
 Const DECL_INITONLY:Int=   $1000000
 
 Const DECL_NODEBUG:Int=    $2000000
+Const DECL_PROTECTED:Int=  $4000000
 
 Const DECL_API_WIN32:Int= $10000000
 Const DECL_API_OS:Int=DECL_API_WIN32
@@ -142,6 +143,10 @@ Type TDecl
 	Method IsPrivate:Int()
 		Return (attrs & DECL_PRIVATE)<>0
 	End Method
+
+	Method IsProtected:Int()
+		Return (attrs & DECL_PROTECTED)<>0
+	End Method
 	
 	Method IsAbstract:Int()
 		Return (attrs & DECL_ABSTRACT)<>0
@@ -188,7 +193,11 @@ Type TDecl
 	
 	Method AssertAccess()
 		If Not CheckAccess()
-			Err ToString() +" is private."
+			If IsPrivate() Then
+				Err ToString() +" is private."
+			Else
+				Err ToString() +" is protected."
+			End If
 		EndIf
 	End Method
 
@@ -576,6 +585,20 @@ Type TGlobalDecl Extends TVarDecl
 		Return inst
 	End Method
 
+	Method CheckAccess:Int()
+		Local cd:TClassDecl = ClassScope()
+		If cd Then
+			If IsPrivate() And cd<>_env.ClassScope() Return False
+			If IsProtected() Then
+				Local ec:TClassDecl = _env.ClassScope()
+				If Not ec Return False
+				If Not ec.ExtendsClass(cd) Return False
+			End If
+			Return True
+		End If
+		Return Super.CheckAccess()
+	End Method
+
 End Type
 
 Type TFieldDecl Extends TVarDecl
@@ -608,7 +631,17 @@ Type TFieldDecl Extends TVarDecl
 		inst.declInit=declInit
 		Return inst
 	End Method
-	
+
+	Method CheckAccess:Int()
+		If IsPrivate() And ClassScope()<>_env.ClassScope() Return False
+		If IsProtected() And ClassScope() Then
+			Local ec:TClassDecl = _env.ClassScope()
+			If Not ec Return False
+			If Not ec.ExtendsClass(ClassScope()) Return False
+		End If
+		Return True
+	End Method
+
 End Type
 
 Type TAliasDecl Extends TDecl
@@ -1808,7 +1841,21 @@ Type TFuncDecl Extends TBlockDecl
 		
 		Super.OnSemant()
 	End Method
-	
+
+	Method CheckAccess:Int()
+		Local cd:TClassDecl = ClassScope()
+		If cd Then
+			If IsPrivate() And cd<>_env.ClassScope() Return False
+			If IsProtected() Then
+				Local ec:TClassDecl = _env.ClassScope()
+				If Not ec Return False
+				If Not ec.ExtendsClass(cd) Return False
+			End If
+			Return True
+		End If
+		Return Super.CheckAccess()
+	End Method
+
 End Type
 
 Type TNewDecl Extends TFuncDecl

+ 57 - 0
iparser.bmx

@@ -859,24 +859,72 @@ Type TIParser
 				attrs:|DECL_FINAL
 			Else If CParse( "FW" )
 				attrs:|DECL_FINAL | DECL_API_WIN32
+			Else If CParse( "FP" )
+				attrs:|DECL_FINAL|DECL_PRIVATE
+			Else If CParse( "FPW" )
+				attrs:|DECL_FINAL|DECL_PRIVATE| DECL_API_WIN32
+			Else If CParse( "FR" )
+				attrs:|DECL_FINAL|DECL_PROTECTED
+			Else If CParse( "FRW" )
+				attrs:|DECL_FINAL|DECL_PROTECTED| DECL_API_WIN32
 			Else If CParse( "A" )
 				attrs:|DECL_ABSTRACT
 			Else If CParse( "AW" )
 				attrs:|DECL_ABSTRACT | DECL_API_WIN32
+			Else If CParse( "AP" )
+				attrs:|DECL_ABSTRACT|DECL_PRIVATE
+			Else If CParse( "APW" )
+				attrs:|DECL_ABSTRACT|DECL_PRIVATE| DECL_API_WIN32
+			Else If CParse( "AR" )
+				attrs:|DECL_ABSTRACT|DECL_PROTECTED
+			Else If CParse( "ARW" )
+				attrs:|DECL_ABSTRACT|DECL_PROTECTED| DECL_API_WIN32
 			Else If CParse( "W" )
 				attrs:|DECL_API_WIN32
 			Else If CParse( "O" )
 				attrs:|FUNC_OPERATOR
 			Else If CParse( "OW" )
 				attrs:|FUNC_OPERATOR| DECL_API_WIN32
+			Else If CParse( "OP" )
+				attrs:|FUNC_OPERATOR|DECL_PRIVATE
+			Else If CParse( "OPW" )
+				attrs:|FUNC_OPERATOR|DECL_PRIVATE| DECL_API_WIN32
+			Else If CParse( "OR" )
+				attrs:|FUNC_OPERATOR|DECL_PROTECTED
+			Else If CParse( "ORW" )
+				attrs:|FUNC_OPERATOR|DECL_PROTECTED| DECL_API_WIN32
+			Else If CParse( "P" )
+				attrs:|DECL_PRIVATE
+			Else If CParse( "PW" )
+				attrs:|DECL_PRIVATE| DECL_API_WIN32
+			Else If CParse( "R" )
+				attrs:|DECL_PROTECTED
+			Else If CParse( "RW" )
+				attrs:|DECL_PROTECTED| DECL_API_WIN32
 			Else If CParse( "FO" )
 				attrs:|DECL_FINAL|FUNC_OPERATOR
 			Else If CParse( "FOW" )
 				attrs:|DECL_FINAL|FUNC_OPERATOR| DECL_API_WIN32
+			Else If CParse( "FOP" )
+				attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PRIVATE
+			Else If CParse( "FOPW" )
+				attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PRIVATE| DECL_API_WIN32
+			Else If CParse( "FOR" )
+				attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PROTECTED
+			Else If CParse( "FORW" )
+				attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PROTECTED| DECL_API_WIN32
 			Else If CParse( "AO" )
 				attrs:|DECL_ABSTRACT|FUNC_OPERATOR
 			Else If CParse( "AOW" )
 				attrs:|DECL_ABSTRACT|FUNC_OPERATOR| DECL_API_WIN32
+			Else If CParse( "AOP" )
+				attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PRIVATE
+			Else If CParse( "AOPW" )
+				attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PRIVATE| DECL_API_WIN32
+			Else If CParse( "AOR" )
+				attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PROTECTED
+			Else If CParse( "AORW" )
+				attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PROTECTED| DECL_API_WIN32
 			'Else If CParse( "property" )
 			'	If attrs & FUNC_METHOD
 			'		attrs:|FUNC_PROPERTY
@@ -1071,6 +1119,15 @@ Type TIParser
 					Wend
 
 				End If
+				
+				If CParse("`") Then
+					If CParse("`") Then
+						attrs :| DECL_PROTECTED
+					Else
+						attrs :| DECL_PRIVATE
+					End If
+				End If
+				
 
 Rem
 			If CParse( "=" )

+ 16 - 4
parser.bmx

@@ -3128,10 +3128,10 @@ End Rem
 
 		Local decl_attrs:Int=(attrs & DECL_EXTERN) | (attrs & DECL_NODEBUG) | (attrs & DECL_API_WIN32)
 
-		Local method_attrs:Int=decl_attrs|FUNC_METHOD | (attrs & DECL_NODEBUG)
-		If attrs & CLASS_INTERFACE method_attrs:|DECL_ABSTRACT
-		
 		Repeat
+			Local method_attrs:Int=decl_attrs|FUNC_METHOD | (attrs & DECL_NODEBUG)
+			If attrs & CLASS_INTERFACE method_attrs:|DECL_ABSTRACT
+		
 			SkipEols
 			Select _toke
 			Case "end"
@@ -3168,11 +3168,23 @@ End Rem
 				NextToke
 				Exit
 			Case "private"
+				If attrs & CLASS_INTERFACE Then
+					Err "Private cannot be used with interfaces."
+				End If
 				NextToke
 				decl_attrs=decl_attrs | DECL_PRIVATE
+				decl_attrs:& ~DECL_PROTECTED
+			Case "protected"
+				If attrs & CLASS_INTERFACE Then
+					Err "Protected cannot be used with interfaces."
+				End If
+				NextToke
+				decl_attrs=decl_attrs | DECL_PROTECTED
+				decl_attrs:& ~DECL_PRIVATE
 			Case "public"
 				NextToke
-				decl_attrs=decl_attrs & ~DECL_PRIVATE
+				decl_attrs:& ~DECL_PRIVATE
+				decl_attrs:& ~DECL_PROTECTED
 			Case "const","global","field"
 				If attrs & DECL_EXTERN Then
 					If (attrs & CLASS_INTERFACE) Then

+ 1 - 1
toker.bmx

@@ -43,7 +43,7 @@ Const TOKE_NATIVE:Int=11
 '***** Tokenizer *****
 Type TToker
 
-	Const __keywords$="strict,superstrict,public,private,short,int,float,double,long,string,object,ptr,var,varptr," + ..
+	Const __keywords$="strict,superstrict,public,private,protected,short,int,float,double,long,string,object,ptr,var,varptr," + ..
 		"mod,continue,exit,include,import,module,extern,framework,new,self,super,eachin,true,false," + ..
 		"null,not,extends,abstract,select,case,default,const,local,global,field,method,function,type," + ..
 		"and,or,shl,shr,sar,end,if,then,else,elseif,endif,while,wend,repeat,until,forever,for,to,step," + ..