.NAME Unique identifier for class, union and enum

.DESCRIPTION
 A class, union or enum name (including qualification, if any) shall be a unique identifier. 

.COMMENT V7.0

.SEVERITY Required

.COMMENT
Please refer to the MISRA C++:2008 document for further details (www.misra-cpp.com)

.COMMAND tcl

.REQUIRED cpp_semantic

.CODE

::logiscope::include -once common utilities.tcl

proc compute {standardName tree} {
	if {[string equal [::logiscope::fileName] [::logiscope::lastFile]]} {
		# get list of types which are enum, struct, union of class
		set sco [::Types::getAllTypes { TypeEnum TypeClass } ]
		foreach s $sco {
			set name [::Semantic::getAttribute $s name]
			checkName $name $s $standardName 1
		}
		# checking all variables, functions 
		set allScopes [::Scopes::getAllScopes]
		foreach sco $allScopes {
			if { [lsearch { FUNCTION NAMESPACE } [::Semantic::getAttribute $sco scopeKind]] >= 0 } {
				set name [::Semantic::getAttribute $sco name]
				# no check for constructor
				set isConstructor 0
				if { "[::Semantic::getAttribute $sco scopeKind]" == "FUNCTION" && [::Semantic::exists $sco class]} {
					set class [::Semantic::getAttribute $sco class]
					if { "$name" == "[::Semantic::getAttribute $class name]" } {
						set isConstructor 1
					}
				}
				if { !$isConstructor } {
					checkName $name $sco $standardName 0
				}
			}
			foreach var [::Semantic::getAttribute $sco variables] {
				set name [::Semantic::getAttribute $var name]
				checkName $name $var $standardName 0
			}
			
		}
	}
	return
}

proc checkName { name s standardName init } {
	variable seen
	if { "$name" != "" } {
		if { [info exists seen($name)] } {
			# name already used => violation
			::Semantic::violation $s $standardName "name already used object $s ([::Semantic::getAttribute $s kind])"
			if { $seen($name) != -1 } {
				::Semantic::violation $seen($name) $standardName "name already used object $seen($name) ([::Semantic::getAttribute $seen($name) kind])"
				set seen($name) -1
			}
		} else {
			if { $init } {
				set seen($name) $s
			}
		}
	}
}
