1 #include <utils/Assert.h>
7 #include "utils/BumpAllocator.h"
8 #include "utils/Utils.h"
17 LinkingUnit::LinkingUnit(BumpAllocator& alloc,
18 array_ref<CompilationUnit*> compilationUnits)
noexcept
19 : compilationUnits_{alloc} {
20 utils::move_vector<CompilationUnit*>(compilationUnits, compilationUnits_);
23 ostream& LinkingUnit::print(ostream& os,
int indentation)
const {
27 os << i1 <<
"LinkingUnit {\n" << i2 <<
"asts: ";
29 for(
auto& cu : compilationUnits_) {
30 cu->print(os, indentation + 1) <<
"\n";
37 int LinkingUnit::printDotNode(DotPrinter& dp)
const {
43 for(
auto& cu : compilationUnits_) {
44 dp.printConnection(id, cu->printDotNode(dp));
51 CompilationUnit::CompilationUnit(BumpAllocator& alloc,
ReferenceType* package,
52 array_ref<ImportDeclaration> imports,
54 : package_{package}, imports_{alloc}, body_{body}, location_{location} {
55 if(
auto decl = dyn_cast_or_null<Decl*>(body)) {
56 decl->setParent(
this);
57 }
else if(decl !=
nullptr) {
58 assert(
false &&
"Body must be a Decl.");
60 utils::move_vector<ImportDeclaration>(imports, imports_);
63 ostream& CompilationUnit::print(ostream& os,
int indentation)
const {
67 os << i1 <<
"CompilationUnit {\n"
68 << i2 <<
"package: " << (package_ ? package_
->toString() :
"null") <<
"\n"
69 << i2 <<
"imports: [";
70 for(
auto& import : imports_) {
71 os <<
"\"" << import.type->toString() << (import.isOnDemand ?
".*" :
"")
76 body_->print(os, indentation + 1);
82 int CompilationUnit::printDotNode(DotPrinter& dp)
const {
88 for(
auto& import : imports_) {
89 imports += import.type->toString();
90 imports += import.isOnDemand ?
".*" :
"";
101 ClassDecl::ClassDecl(BumpAllocator& alloc,
Modifiers modifiers,
104 array_ref<Decl*> classBodyDecls)
throw()
106 modifiers_{modifiers},
107 superClasses_{super1, super2},
111 constructors_{alloc},
112 location_{location} {
113 utils::move_vector<ReferenceType*>(interfaces, interfaces_);
115 for(
auto bodyDecl : classBodyDecls) {
116 if(
auto fieldDecl = dyn_cast<FieldDecl*>(bodyDecl)) {
117 fields_.push_back(fieldDecl);
118 }
else if(
auto methodDecl = dyn_cast<MethodDecl*>(bodyDecl)) {
119 if(methodDecl->isConstructor())
120 constructors_.push_back(methodDecl);
122 methods_.push_back(methodDecl);
124 assert(
false &&
"Unexpected class body declaration type");
129 ostream& ClassDecl::print(ostream& os,
int indentation)
const {
133 os << i1 <<
"ClassDecl {\n"
134 << i2 <<
"modifiers: " << modifiers_.toString() <<
"\n"
135 << i2 <<
"name: " <<
this->name()
141 << i2 <<
"interfaces: []\n"
142 << i2 <<
"fields:\n";
143 for(
auto& field : fields_) field->print(os, indentation + 2);
144 os << i2 <<
"methods:\n";
145 for(
auto& method : methods_) method->print(os, indentation + 2);
146 os << i2 <<
"constructors:\n";
147 for(
auto& constructor : constructors_) constructor->print(os, indentation + 2);
152 int ClassDecl::printDotNode(DotPrinter& dp)
const {
162 for(
auto& i : interfaces()) intf += string{i->toString()} +
"\n";
168 {
"port",
"constructors"}
,
169 {
"port",
"methods"}
);
172 for(
auto& f : fields())
173 dp.printConnection(id,
":fields:w", f->printDotNode(dp));
174 for(
auto& f : constructors())
175 dp.printConnection(id,
":constructors:c", f->printDotNode(dp));
176 for(
auto& f : methods())
177 dp.printConnection(id,
":methods:e", f->printDotNode(dp));
182 auto cu = cast<CompilationUnit*>(parent);
186 if(!cu->isDefaultPackage()) {
187 canonicalName_ = cu->getPackageName();
188 canonicalName_ +=
".";
190 canonicalName_ +=
name();
193 for(
auto& field : fields_) field->setParent(
this);
194 for(
auto& method : methods_) method->setParent(
this);
195 for(
auto& constructor : constructors_) constructor->setParent(
this);
198 bool ClassDecl::hasDefaultCtor()
const {
199 for(
auto& ctor : constructors_) {
200 if(ctor->parameters().empty())
return true;
207 InterfaceDecl::InterfaceDecl(BumpAllocator& alloc,
Modifiers modifiers,
209 array_ref<ReferenceType*> extends,
211 array_ref<Decl*> interfaceBodyDecls)
throw()
213 modifiers_{modifiers},
217 objectSuperclass_{objectSuperclass} {
218 utils::move_vector<ReferenceType*>(extends, extends_);
219 for(
auto bodyDecl : interfaceBodyDecls) {
220 methods_.push_back(cast<MethodDecl*>(bodyDecl));
224 ostream& InterfaceDecl::print(ostream& os,
int indentation)
const {
228 os << i1 <<
"InterfaceDecl {\n"
229 << i2 <<
"modifiers: " << modifiers_.toString() <<
"\n"
230 << i2 <<
"name: " <<
this->name() <<
"\n"
231 << i2 <<
"extends: []\n"
232 << i2 <<
"methods:\n";
233 for(
auto& method : methods_) {
234 method->print(os, indentation + 2);
240 int InterfaceDecl::printDotNode(DotPrinter& dp)
const {
247 for(
auto& e : extends()) ext += string{e->toString()} +
"\n";
252 for(
auto& f : methods())
253 dp.printConnection(id,
":methods", f->printDotNode(dp));
258 auto cu = cast<CompilationUnit*>(parent);
262 if(!cu->isDefaultPackage()) {
263 canonicalName_ = cu->getPackageName();
264 canonicalName_ +=
".";
266 canonicalName_ +=
name();
268 for(
auto& method : methods_) method->setParent(
this);
273 ostream& MethodDecl::print(ostream& os,
int indentation)
const {
277 os << i1 <<
"MethodDecl {\n"
278 << i2 <<
"modifiers: " << modifiers_.toString() <<
"\n"
279 << i2 <<
"name: " <<
this->name() <<
"\n"
280 << i2 <<
"returnType: " << (returnType_ ? returnType_->toString() :
"null")
282 << i2 <<
"parameters:\n";
283 for(
auto& parameter : parameters_) {
284 parameter->print(os, indentation + 2);
287 body_->print(os, indentation + 1);
293 std::ostream& MethodDecl::printSignature(std::ostream& os)
const {
294 os << returnType_->toString() <<
" " <<
name() <<
"(";
295 for(
auto& param : parameters_) {
296 os << param->type()->toString() <<
" " << param->name();
297 if(param != parameters_.back()) os <<
", ";
303 void MethodDecl::dumpSignature()
const {
304 printSignature(std::cerr) <<
"\n";
307 int MethodDecl::printDotNode(DotPrinter& dp)
const {
309 int paramSubgraphId = dp
.id();
318 returnType_ ? returnType_->toString() :
"null");
320 "Params", "Body", {
"port",
"parameters"}
, {
"port",
"body"}
);
325 int firstParamId = -1;
327 if(!parameters().empty()) {
330 firstParamId = printDotNodeList(dp, parameters());
333 if(firstParamId != -1)
337 if(!body_)
return id;
340 auto ids = printStmtSubgraph(dp, body_);
342 dp.printConnection(id,
":body:c", ids.first, ids.second);
344 dp.printConnection(id,
":body:c", ids.first);
347 for(
auto& p : locals_) {
348 if(
int d = dp.getId(p)) {
349 if(d != -1) dp.printBackedge(d, id);
357 auto decl = cast<
Decl*>(parent);
358 assert(dyn_cast<ClassDecl*>(parent) || dyn_cast<InterfaceDecl*>(parent));
362 canonicalName_ = decl->getCanonicalName();
363 canonicalName_ +=
".";
364 canonicalName_ +=
name();
366 for(
auto& parameter : locals_) parameter->setParent(
this);