1 // Filename: Parameter.cpp
2 #include "util/HashGenerator.h"
3 #include "value/default.h"
4 #include "dc/Struct.h"
5 #include "dc/Method.h"
6 
7 #include "Parameter.h"
8 namespace dclass   // open namespace dclass
9 {
10 
11 
12 // constructor
Parameter(DistributedType * type,const std::string & name)13 Parameter::Parameter(DistributedType* type, const std::string& name) :
14     m_name(name), m_type(type), m_method(nullptr), m_has_default_value(false)
15 {
16     bool implicit_value;
17     m_default_value = create_default_value(type, implicit_value);
18     m_has_default_value = !implicit_value;
19 
20     if(m_type == nullptr) {
21         m_type = DistributedType::invalid;
22     }
23 }
24 
25 // set_name sets the name of this parameter.  Returns false if a parameter with
26 //     the same name already exists in the containing method.
set_name(const std::string & name)27 bool Parameter::set_name(const std::string& name)
28 {
29     // Check to make sure no other fields in our struct have this name
30     if(m_method != nullptr && m_method->get_parameter_by_name(name) != nullptr) {
31         return false;
32     }
33 
34     m_name = name;
35     return true;
36 }
37 
38 // set_type sets the distributed type of the parameter and clear's the default value.
set_type(DistributedType * type)39 bool Parameter::set_type(DistributedType* type)
40 {
41     if(type == nullptr) {
42         return false;
43     }
44 
45     // Parameters can't have method types for now
46     if(type->get_type() == T_METHOD) {
47         return false;
48     }
49 
50     // Parameters can't have class types for now
51     if(type->get_type() == T_STRUCT && type->as_struct()->as_class()) {
52         return false;
53     }
54 
55     m_type = type;
56     m_has_default_value = false;
57     m_default_value = create_default_value(type);
58 
59     return true;
60 }
61 
62 // set_default_value defines a default value for this parameter.
63 //     Returns false if the value is invalid for the parameter's type.
set_default_value(const std::string & default_value)64 bool Parameter::set_default_value(const std::string& default_value)
65 {
66     // TODO: Validate default value
67     m_has_default_value = true;
68     m_default_value = default_value;
69     return true;
70 }
71 
72 // set_method sets a pointer to the method containing the parameter.
set_method(Method * method)73 void Parameter::set_method(Method* method)
74 {
75     m_method = method;
76 }
77 
78 // generate_hash accumulates the properties of this type into the hash.
generate_hash(HashGenerator & hashgen) const79 void Parameter::generate_hash(HashGenerator& hashgen) const
80 {
81     m_type->generate_hash(hashgen);
82 }
83 
84 
85 } // close namespace dclass
86