Skip to content

Commit

Permalink
more graph work
Browse files Browse the repository at this point in the history
  • Loading branch information
cdacamar committed Sep 29, 2023
1 parent 0d311ca commit 3554516
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 48 deletions.
1 change: 1 addition & 0 deletions samples/sgraph-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ <h3>Decl Preview</h3>

<!-- Core -->
<script type='text/javascript' src='sgraph/decls.js'></script>
<script type='text/javascript' src='sgraph/dirs.js'></script>
<script type='text/javascript' src='sgraph/charts.js'></script>
<script type='text/javascript' src='sgraph/exprs.js'></script>
<script type='text/javascript' src='sgraph/io.js'></script>
Expand Down
26 changes: 25 additions & 1 deletion samples/sgraph-js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ function json_for_unsorted(resolver, index, root) {
const symbolic = symbolic_for_decl_sort(index.sort);
var symbolic_decl = resolver.read(symbolic, index.index);
if (symbolic_decl.identity == null) {
// Barren decls have no formal name, but we can still display them in a way that is useful (by using the 'sort' field).
if (index.sort == DeclIndex.Sort.Barren)
{
const sort = sort_to_string(DirIndex, symbolic_decl.directive.sort);
const idx = symbolic_decl.directive.index;
const entry_name = `Barren{${sort},${idx}}`;
const name = append_name_meta(entry_name, index);
root[name] = 1;
return;
}
root[`unknown_${sort_to_string(DeclIndex, index.sort)}_${index.index}`] = 1;
return;
}
Expand Down Expand Up @@ -134,6 +144,18 @@ function format_basic_spec(decl) {
return bitset_to_string(BasicSpecifiers, decl.basic_spec.value)
}

function locus_of_symbolic_decl(symbolic_decl, index) {
if (index.sort != DeclIndex.Sort.Barren) {
return sgraph.resolver.resolve_identity(symbolic_decl.identity).locus;
}
// Barren decls are a bit more complicated. Their locations are nested within the symbolic directive
// structure.
const symbolic = symbolic_for_dir_sort(symbolic_decl.directive.sort);
const symbolic_dir = sgraph.resolver.read(symbolic, symbolic_decl.directive.index);
// Each symbolic directive has a location as its first member.
return new ResolvedLocus(symbolic_dir.locus, sgraph.resolver);
}

function on_decl_selected(decl) {
if (!is_meta_name(decl)) {
set_decl_preview_content(decl);
Expand All @@ -146,7 +168,8 @@ function on_decl_selected(decl) {
const symbolic = symbolic_for_decl_sort(resolved_name.index.sort);
const symbolic_decl = sgraph.resolver.read(symbolic, resolved_name.index.index);
const decl_index_str = `DeclIndex{${sort_to_string(DeclIndex, resolved_name.index.sort)}, ${resolved_name.index.index}}`;
const locus = sgraph.resolver.resolve_identity(symbolic_decl.identity).locus;
//const locus = sgraph.resolver.resolve_identity(symbolic_decl.identity).locus;
const locus = locus_of_symbolic_decl(symbolic_decl, resolved_name.index);
const locus_str = `"${locus.file}"(${locus.line},${locus.column})`;
const basic_spec = format_basic_spec(symbolic_decl);
const json_str = JSON.stringify(symbolic_decl, null, 2);
Expand Down Expand Up @@ -207,6 +230,7 @@ function validate_sorts() {
&& validate_sort(ExprIndex)
&& validate_sort(NameIndex)
&& validate_sort(TypeIndex)
&& validate_sort(DirIndex)
&& validate_sort(UnitIndex);
}

Expand Down
16 changes: 5 additions & 11 deletions samples/sgraph-js/sgraph/decls.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,6 @@ class UsingDecl {
}
}

class UsingDirectiveDecl {
static partition_name = "decl.using-directive";

constructor(reader) {
this.qualified_name = new ExprIndex(reader);
}
}

class FriendDecl {
static partition_name = "decl.friend";

Expand Down Expand Up @@ -418,6 +410,9 @@ class BarrenDecl {
static partition_name = "decl.barren";

constructor(reader) {
this.directive = new DirIndex(reader);
this.basic_spec = new BasicSpecifiers(reader);
this.access = new Access(reader);
}
}

Expand Down Expand Up @@ -510,10 +505,8 @@ function symbolic_for_decl_sort(sort) {
return DestructorDecl;
case DeclIndex.Sort.Reference:
return ReferenceDecl;
case DeclIndex.Sort.UsingDeclaration:
case DeclIndex.Sort.Using:
return UsingDecl;
case DeclIndex.Sort.UsingDirective:
return UsingDirectiveDecl;
case DeclIndex.Sort.Friend:
return FriendDecl;
case DeclIndex.Sort.Expansion:
Expand All @@ -532,6 +525,7 @@ function symbolic_for_decl_sort(sort) {
return PropertyDecl;
case DeclIndex.Sort.OutputSegment:
return SegmentDecl;
case DeclIndex.Sort.UnusedSort0:
case DeclIndex.Sort.VendorExtension:
default:
console.error(`Bad sort: ${sort}`);
Expand Down
158 changes: 158 additions & 0 deletions samples/sgraph-js/sgraph/dirs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

class Phases {
static Values = {
Unknown: 0,
Reading: 1 << 0,
Lexing: 1 << 1,
Preprocessing: 1 << 2,
Parsing: 1 << 3,
Importing: 1 << 4,
NameResolution: 1 << 5,
Typing: 1 << 6,
Evaluation: 1 << 7,
Instantiation: 1 << 8,
Analysis: 1 << 9,
CodeGeneration: 1 << 10,
Linking: 1 << 11,
Loading: 1 << 12,
Execution: 1 << 13,
};

constructor(reader) {
this.value = reader.read_uint32();
}
}

class EmptyDir {
static partition_name = "dir.empty";

constructor(reader) {
this.locus = new SourceLocation(reader);
}
}

class AttributeDir {
static partition_name = "dir.attribute";

constructor(reader) {
this.locus = new SourceLocation(reader);
// TODO: AttrIndex
this.attr = reader.read_uint32();
}
}

class PragmaDir {
static partition_name = "dir.pragma";

constructor(reader) {
this.locus = new SourceLocation(reader);
this.words = new SentenceIndex(reader);
}
}

class UsingDir {
static partition_name = "dir.using";

constructor(reader) {
this.locus = new SourceLocation(reader);
this.nominated = new ExprIndex(reader);
this.resolution = new DeclIndex(reader);
}
}

class UsingDeclarationDir {
static partition_name = "dir.decl-use";

constructor(reader) {
this.locus = new SourceLocation(reader);
this.path = new ExprIndex(reader);
this.result = new DeclIndex(reader);
}
}

class ExprDir {
static partition_name = "dir.expr";

constructor(reader) {
this.locus = new SourceLocation(reader);
this.expr = new ExprIndex(reader);
this.phases = new Phases(reader);
}
}

class StructuredBindingDir {
static partition_name = "dir.struct-binding";

constructor(reader) {
this.locus = new SourceLocation(reader);
this.bindings = new Sequence(DeclIndex, reader);
this.names = new Sequence(TextOffset, reader);
}
}

class SpecifiersSpreadDir {
static partition_name = "dir.specifiers-spread";

constructor(reader) {
this.locus = new SourceLocation(reader);
}
}

class TupleDir {
static partition_name = "dir.tuple";

constructor(reader) {
this.seq = new HeapSequence(HeapSort.Values.Dir);
}
}

function symbolic_for_dir_sort(sort) {
switch (sort) {
case DirIndex.Sort.Empty:
return EmptyDir;
case DirIndex.Sort.Attribute:
return AttributeDir;
case DirIndex.Sort.Pragma:
return PragmaDir;
case DirIndex.Sort.Using:
return UsingDir;
case DirIndex.Sort.DeclUse:
return UsingDeclarationDir;
case DirIndex.Sort.Expr:
return ExprDir;
case DirIndex.Sort.StructuredBinding:
return StructuredBindingDir;
case DirIndex.Sort.SpecifiersSpread:
return SpecifiersSpreadDir;
case DirIndex.Sort.Tuple:
return TupleDir;
case DirIndex.Sort.Unused0:
case DirIndex.Sort.Unused1:
case DirIndex.Sort.Unused2:
case DirIndex.Sort.Unused3:
case DirIndex.Sort.Unused4:
case DirIndex.Sort.Unused5:
case DirIndex.Sort.Unused6:
case DirIndex.Sort.Unused7:
case DirIndex.Sort.Unused8:
case DirIndex.Sort.Unused9:
case DirIndex.Sort.Unused10:
case DirIndex.Sort.Unused11:
case DirIndex.Sort.Unused12:
case DirIndex.Sort.Unused13:
case DirIndex.Sort.Unused14:
case DirIndex.Sort.Unused15:
case DirIndex.Sort.Unused16:
case DirIndex.Sort.Unused17:
case DirIndex.Sort.Unused18:
case DirIndex.Sort.Unused19:
case DirIndex.Sort.Unused20:
case DirIndex.Sort.Unused21:
case DirIndex.Sort.VendorExtension:
case DirIndex.Sort.Count:
console.error(`Bad sort: ${sort}`);
return null;
}
}
31 changes: 18 additions & 13 deletions samples/sgraph-js/sgraph/exprs.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,6 @@ class HierarchyConversionExpr {
}
}

class SubobjectValueExpr {
static partition_name = "expr.class-subobject-value";

constructor(reader) {
this.value = new ExprIndex(reader);
}
}

class ProductTypeValueExpr {
static partition_name = "expr.product-type-value";

Expand All @@ -522,7 +514,7 @@ class SumTypeValueExpr {
this.type = new TypeIndex(reader);
this.variant = new TypeIndex(reader);
this.active_member = reader.read_uint32();
this.value = new SubobjectValueExpr(reader);
this.value = new ExprIndex(reader);
}
}

Expand Down Expand Up @@ -687,6 +679,17 @@ class LabelExpr {
}
}

class StatementExpr {
static partition_name = "expr.stmt";

constructor(reader) {
this.locus = new SourceLocation(reader);
this.type = new TypeIndex(reader);
// TODO StmtIndex.
this.stmt = reader.read_uint32();
}
}

function symbolic_for_expr_sort(sort) {
switch (sort) {
case ExprIndex.Sort.Empty:
Expand Down Expand Up @@ -761,18 +764,16 @@ function symbolic_for_expr_sort(sort) {
return InitializerExpr;
case ExprIndex.Sort.Requires:
return RequiresExpr;
case ExprIndex.Sort.UnaryFoldExpression:
case ExprIndex.Sort.UnaryFold:
return UnaryFoldExpr;
case ExprIndex.Sort.BinaryFoldExpression:
case ExprIndex.Sort.BinaryFold:
return BinaryFoldExpr;
case ExprIndex.Sort.HierarchyConversion:
return HierarchyConversionExpr;
case ExprIndex.Sort.ProductTypeValue:
return ProductTypeValueExpr;
case ExprIndex.Sort.SumTypeValue:
return SumTypeValueExpr;
case ExprIndex.Sort.SubobjectValue:
return SubobjectValueExpr;
case ExprIndex.Sort.ArrayValue:
return ArrayValueExpr;
case ExprIndex.Sort.DynamicDispatch:
Expand All @@ -793,6 +794,8 @@ function symbolic_for_expr_sort(sort) {
return ThisExpr;
case ExprIndex.Sort.TemplateReference:
return TemplateReferenceExpr;
case ExprIndex.Statement:
return StatementExpr;
case ExprIndex.Sort.TypeTraitIntrinsic:
return TypeTraitIntrinsicExpr;
case ExprIndex.Sort.DesignatedInitializer:
Expand All @@ -805,6 +808,8 @@ function symbolic_for_expr_sort(sort) {
return AssignInitializerExpr;
case ExprIndex.Sort.Label:
return LabelExpr;
case ExprIndex.Sort.UnusedSort0:
case ExprIndex.Sort.UnusedSort1:
case ExprIndex.Sort.VendorExtension:
default:
console.error(`Bad sort: ${sort}`);
Expand Down
8 changes: 8 additions & 0 deletions samples/sgraph-js/sgraph/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class MonadicOperator {
Materialize: 32, // temporary materialization -- abstract machine
PseudoDtorCall: 33, // p->~T(), with T a scalar type
LookupGlobally: 34, // ::x
Artificial: 35, // Compiler-generated expression wrapper
MetaDescriptor: 36, // a runtime reification of a type

Msvc: 0x0400,
MsvcAssume: 0x0401, // __assume(x)
Expand Down Expand Up @@ -107,6 +109,11 @@ class MonadicOperator {
MsvcConfusedPopState: 0x0FE3, // An EH state represented directly in a unary expression for invoking destructors after invoking a ctor.
MsvcConfusedDtorAction: 0x0FE4, // An EH state represented directly in a unary expression for invoking destructors directly.
MsvcConfusedVtorDisplacement: 0x0FE5, // The compiler generated expression representing an offset amount to a virtual base pointer address during initialization.
MsvcConfusedDependentExpression: 0x0FE6, // At times the old YACC parser will 'give up' parsing a dependent expression and simply return a constant with a dummy bit set.
// This operator attempts to catch these offending dependent expression values.
MsvcConfusedSubstitution: 0x0FE7, // Represents a template parameter substitution 'P -> expr' where 'expr' could be a type or a non-type argument replacement.
MsvcConfusedAggregateReturn: 0x0FE8, // Decorates a return statement which returns an aggregate class type with a user-defined destructor.
MsvcConfusedVftblPointerInit: 0x0FE9, // An initialization of a derived class's vftbl pointer.

Count: 0x0FE5
};
Expand Down Expand Up @@ -207,6 +214,7 @@ class DyadicOperator {
MsvcBuiltinIsCorrespondingMember: 0x0414, // __builtin_is_corresponding_member(x, y)
MsvcIntrinsic: 0x0415, // -- abstract machine, misc intrinsic with no regular function declaration
MsvcSaturatedArithmetic: 0x0416, // An MSVC intrinsic for an abstract machine saturated arithemtic operation.
MsvcBuiltinAllocationAnnotation: 0x0417, // An MSVC intrinsic used to propagate debugging information to the runtime. __allocation_annotation(x, T)

Count: 0x0417
};
Expand Down
Loading

0 comments on commit 3554516

Please sign in to comment.